changeset 21662:4f63ef17d9dc

lib: Place input/output of utc_mktime tests in one struct Define both input and output of a utc_mktime test case in a single struct so it is easier to follow and less error prone.
author Martti Rannanj?rvi <martti.rannanjarvi@dovecot.fi>
date Tue, 21 Feb 2017 17:00:14 +0200
parents c4584d7dc07e
children 2a7bffd568e7
files src/lib/test-utc-mktime.c
diffstat 1 files changed, 30 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/test-utc-mktime.c	Tue Feb 21 16:25:25 2017 +0200
+++ b/src/lib/test-utc-mktime.c	Tue Feb 21 17:00:14 2017 +0200
@@ -3,83 +3,58 @@
 #include "test-lib.h"
 #include "utc-mktime.h"
 
-struct test_utc_mktime_input {
+struct test_utc_mktime {
 	int year, month, day, hour, min, sec;
+	time_t out;
 };
 
 void test_utc_mktime(void)
 {
-	static struct test_utc_mktime_input input[] = {
+	static const struct test_utc_mktime tests[] = {
 #ifdef TIME_T_SIGNED
-		{ 1969, 12, 31, 23, 59, 59 },
-		{ 1901, 12, 13, 20, 45, 53 },
+		{ 1969, 12, 31, 23, 59, 59, -1 },
+		{ 1901, 12, 13, 20, 45, 53, -2147483647 },
 #endif
 #if (TIME_T_MAX_BITS > 32 || !defined(TIME_T_SIGNED))
-		{ 2106, 2, 7, 6, 28, 15 },
+		{ 2106, 2, 7, 6, 28, 15, 4294967295 },
 #endif
-		{ 2007, 11, 7, 1, 7, 20 },
-		{ 1970, 1, 1, 0, 0, 0 },
-		{ 2038, 1, 19, 3, 14, 7 },
-		{ INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX },
-		{ 2038, 1, 19, 3, 14, 8 },
-		{ 2106, 2, 7, 6, 28, 15 },
-		{ 2106, 2, 7, 6, 28, 16 },
+		{ 2007, 11, 7, 1, 7, 20, 1194397640 },
+		{ 1970, 1, 1, 0, 0, 0, 0 },
+		{ 2038, 1, 19, 3, 14, 7, 2147483647 },
+		{ INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, -1 },
+		{ 2038, 1, 19, 3, 14, 8, 2147483648 },
+		{ 2106, 2, 7, 6, 28, 15, 4294967295 },
+		{ 2106, 2, 7, 6, 28, 16, 4294967296 },
 		/* June leap second */
-		{ 2015, 6, 30, 23, 59, 59 },
-		{ 2015, 6, 30, 23, 59, 60 },
-		{ 2015, 7, 1, 0, 0, 0 },
+		{ 2015, 6, 30, 23, 59, 59, 1435708799 },
+		{ 2015, 6, 30, 23, 59, 60, 1435708799 },
+		{ 2015, 7, 1, 0, 0, 0, 1435708800 },
 		/* Invalid leap second */
-		{ 2017, 1, 24, 16, 40, 60 },
+		{ 2017, 1, 24, 16, 40, 60, 1485276059 },
 		/* Dec leap second */
-		{ 2016, 12, 31, 23, 59, 59 },
-		{ 2016, 12, 31, 23, 59, 60 },
-		{ 2017, 1, 1, 0, 0, 0 },
+		{ 2016, 12, 31, 23, 59, 59, 1483228799 },
+		{ 2016, 12, 31, 23, 59, 60, 1483228799 },
+		{ 2017, 1, 1, 0, 0, 0, 1483228800 },
 	};
-	static time_t output[] = {
-#ifdef TIME_T_SIGNED
-		-1,
-		-2147483647,
-#endif
-#if (TIME_T_MAX_BITS > 32 || !defined(TIME_T_SIGNED))
-		4294967295,
-#endif
-		1194397640,
-		0,
-		2147483647,
-		-1,
-		2147483648,
-		4294967295,
-		4294967296,
-		/* June leap second */
-		1435708799,
-		1435708799,
-		1435708800,
-		/* Invalid leap second - utc_mktime() doesn't mind */
-		1485276059,
-		/* Dec leap second */
-		1483228799,
-		1483228799,
-		1483228800,
-	};
-	i_assert(N_ELEMENTS(input) == N_ELEMENTS(output));
 	struct tm tm;
 	unsigned int i;
 	time_t t;
 	bool success;
 
-	for (i = 0; i < N_ELEMENTS(input); i++) {
+	for (i = 0; i < N_ELEMENTS(tests); i++) {
+		const struct test_utc_mktime *test = &tests[i];
 		i_zero(&tm);
-		tm.tm_year = input[i].year - 1900;
-		tm.tm_mon = input[i].month - 1;
-		tm.tm_mday = input[i].day;
-		tm.tm_hour = input[i].hour;
-		tm.tm_min = input[i].min;
-		tm.tm_sec = input[i].sec;
+		tm.tm_year = test->year - 1900;
+		tm.tm_mon = test->month - 1;
+		tm.tm_mday = test->day;
+		tm.tm_hour = test->hour;
+		tm.tm_min = test->min;
+		tm.tm_sec = test->sec;
 
 		t = utc_mktime(&tm);
-		success = t == output[i];
+		success = t == test->out;
 		test_out_reason(t_strdup_printf("utc_mktime(%d)", i), success,
 				success ? NULL : t_strdup_printf("%ld != %ld",
-						     (long)t, (long)output[i]));
+							(long)t, (long)test->out));
 	}
 }