Browse Source

[test] Add self-tests for mktime()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
c130001bdf
2 changed files with 183 additions and 0 deletions
  1. 1
    0
      src/tests/tests.c
  2. 182
    0
      src/tests/time_test.c

+ 1
- 0
src/tests/tests.c View File

@@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
27 27
 /* Drag in all applicable self-tests */
28 28
 REQUIRE_OBJECT ( list_test );
29 29
 REQUIRE_OBJECT ( byteswap_test );
30
+REQUIRE_OBJECT ( time_test );
30 31
 REQUIRE_OBJECT ( md5_test );
31 32
 REQUIRE_OBJECT ( sha1_test );
32 33
 REQUIRE_OBJECT ( sha256_test );

+ 182
- 0
src/tests/time_test.c View File

@@ -0,0 +1,182 @@
1
+/*
2
+ * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+FILE_LICENCE ( GPL2_OR_LATER );
20
+
21
+/** @file
22
+ *
23
+ * Date and time self-tests
24
+ *
25
+ */
26
+
27
+/* Forcibly enable assertions */
28
+#undef NDEBUG
29
+
30
+#include <time.h>
31
+#include <ipxe/test.h>
32
+
33
+/** A mktime() test */
34
+struct mktime_test {
35
+	/** Broken-down time */
36
+	struct tm tm;
37
+	/** Day of the week */
38
+	int wday;
39
+	/** Day of the year */
40
+	int yday;
41
+	/** Seconds since the Epoch */
42
+	time_t time;
43
+};
44
+
45
+/**
46
+ * Define a mktime() test
47
+ *
48
+ * @v name		Test name
49
+ * @v SEC		Seconds [0,60]
50
+ * @v MIN		Minutes [0,59]
51
+ * @v HOUR		Hour [0,23]
52
+ * @v MDAY		Day of month [1,31]
53
+ * @v MON		Month of year [0,11]
54
+ * @v YEAR		Years since 1900
55
+ * @v WDAY		Day of week [0,6] (Sunday=0)
56
+ * @v YDAY		Day of year [0,365]
57
+ * @v ISDST		Daylight savings flag (ignored)
58
+ * @v TIME		Seconds since the Epoch
59
+ * @ret test		mktime() test
60
+ *
61
+ * This macro is designed to make it easy to generate test vectors in
62
+ * Perl using
63
+ *
64
+ *    print join ", ", gmtime ( $time ), $time."ULL";
65
+ *
66
+ */
67
+#define MKTIME_TEST( name, SEC, MIN, HOUR, MDAY, MON, YEAR, WDAY,	\
68
+		     YDAY, ISDST, TIME )				\
69
+	static struct mktime_test name = {				\
70
+		.tm = {							\
71
+			.tm_sec = SEC,					\
72
+			.tm_min = MIN,					\
73
+			.tm_hour = HOUR,				\
74
+			.tm_mday = MDAY,				\
75
+			.tm_mon = MON,					\
76
+			.tm_year = YEAR,				\
77
+			.tm_isdst = ISDST,				\
78
+		},							\
79
+		.wday = WDAY,						\
80
+		.yday = YDAY,						\
81
+		.time = TIME,						\
82
+	}
83
+
84
+/**
85
+ * Report mktime() test result
86
+ *
87
+ * @v test		mktime() test
88
+ */
89
+#define mktime_ok( test ) do {						\
90
+	time_t time = mktime ( &(test)->tm );				\
91
+	ok ( time == (test)->time );					\
92
+	ok ( (test)->tm.tm_wday == (test)->wday );			\
93
+	ok ( (test)->tm.tm_yday == (test)->yday );			\
94
+	} while ( 0 )
95
+
96
+/* Start of the Epoch */
97
+MKTIME_TEST ( mktime_epoch, 00, 00, 00, 01, 00, 70, 4, 0, 0, 0 );
98
+
99
+/* Birth of iPXE as a new project */
100
+MKTIME_TEST ( mktime_ipxe, 01, 15, 20, 19, 03, 110, 1, 108, 0, 1271708101ULL );
101
+
102
+/* Random test vectors generated using Perl's gmtime() */
103
+MKTIME_TEST ( mktime_0, 4, 17, 20, 1, 0, 150, 6, 0, 0, 2524681024ULL );
104
+MKTIME_TEST ( mktime_1, 22, 47, 21, 27, 11, 77, 2, 360, 0, 252107242ULL );
105
+MKTIME_TEST ( mktime_2, 26, 10, 0, 7, 2, 196, 3, 66, 0, 3981917426ULL );
106
+MKTIME_TEST ( mktime_3, 44, 44, 23, 15, 9, 261, 4, 287, 0, 6052319084ULL );
107
+MKTIME_TEST ( mktime_4, 3, 22, 18, 8, 9, 296, 6, 281, 0, 7156232523ULL );
108
+MKTIME_TEST ( mktime_5, 27, 26, 16, 18, 11, 338, 2, 351, 0, 8487649587ULL );
109
+MKTIME_TEST ( mktime_6, 31, 36, 22, 3, 3, 293, 3, 92, 0, 7045310191ULL );
110
+MKTIME_TEST ( mktime_7, 2, 0, 6, 25, 5, 289, 4, 175, 0, 6926191202ULL );
111
+MKTIME_TEST ( mktime_8, 43, 50, 1, 8, 0, 210, 3, 7, 0, 4418589043ULL );
112
+MKTIME_TEST ( mktime_9, 48, 14, 20, 23, 3, 86, 3, 112, 0, 514671288ULL );
113
+MKTIME_TEST ( mktime_10, 4, 43, 5, 29, 11, 173, 5, 362, 0, 3281751784ULL );
114
+MKTIME_TEST ( mktime_11, 47, 26, 21, 12, 7, 177, 4, 223, 0, 3396029207ULL );
115
+MKTIME_TEST ( mktime_12, 18, 55, 20, 26, 11, 88, 1, 360, 0, 599172918ULL );
116
+MKTIME_TEST ( mktime_13, 8, 32, 13, 15, 7, 314, 1, 226, 0, 7719456728ULL );
117
+MKTIME_TEST ( mktime_14, 0, 16, 11, 20, 6, 138, 2, 200, 0, 2163237360ULL );
118
+MKTIME_TEST ( mktime_15, 48, 0, 9, 31, 2, 202, 5, 89, 0, 4173238848ULL );
119
+MKTIME_TEST ( mktime_16, 51, 55, 0, 15, 1, 323, 6, 45, 0, 7987769751ULL );
120
+MKTIME_TEST ( mktime_17, 36, 10, 7, 11, 5, 301, 4, 161, 0, 7303590636ULL );
121
+MKTIME_TEST ( mktime_18, 22, 39, 11, 21, 9, 233, 3, 293, 0, 5169181162ULL );
122
+MKTIME_TEST ( mktime_19, 48, 29, 8, 31, 7, 207, 3, 242, 0, 4344222588ULL );
123
+MKTIME_TEST ( mktime_20, 4, 53, 22, 8, 8, 165, 2, 250, 0, 3019675984ULL );
124
+MKTIME_TEST ( mktime_21, 14, 16, 8, 10, 5, 298, 0, 160, 0, 7208900174ULL );
125
+MKTIME_TEST ( mktime_22, 10, 35, 3, 12, 3, 188, 1, 102, 0, 3732579310ULL );
126
+MKTIME_TEST ( mktime_23, 47, 12, 18, 22, 2, 103, 6, 80, 0, 1048356767ULL );
127
+MKTIME_TEST ( mktime_24, 23, 29, 17, 23, 10, 201, 3, 326, 0, 4162210163ULL );
128
+MKTIME_TEST ( mktime_25, 58, 35, 23, 24, 3, 111, 0, 113, 0, 1303688158ULL );
129
+MKTIME_TEST ( mktime_26, 34, 56, 15, 24, 11, 154, 4, 357, 0, 2681740594ULL );
130
+MKTIME_TEST ( mktime_27, 7, 11, 22, 28, 1, 243, 4, 58, 0, 5464447867ULL );
131
+MKTIME_TEST ( mktime_28, 25, 45, 23, 29, 11, 90, 6, 362, 0, 662514325ULL );
132
+MKTIME_TEST ( mktime_29, 31, 20, 12, 24, 1, 146, 6, 54, 0, 2403087631ULL );
133
+MKTIME_TEST ( mktime_30, 49, 7, 18, 16, 10, 271, 6, 319, 0, 6370596469ULL );
134
+MKTIME_TEST ( mktime_31, 31, 55, 2, 25, 5, 141, 2, 175, 0, 2255741731ULL );
135
+
136
+/**
137
+ * Perform date and time self-tests
138
+ *
139
+ */
140
+static void time_test_exec ( void ) {
141
+
142
+	mktime_ok ( &mktime_epoch );
143
+	mktime_ok ( &mktime_ipxe );
144
+	mktime_ok ( &mktime_0 );
145
+	mktime_ok ( &mktime_1 );
146
+	mktime_ok ( &mktime_2 );
147
+	mktime_ok ( &mktime_3 );
148
+	mktime_ok ( &mktime_4 );
149
+	mktime_ok ( &mktime_5 );
150
+	mktime_ok ( &mktime_6 );
151
+	mktime_ok ( &mktime_7 );
152
+	mktime_ok ( &mktime_8 );
153
+	mktime_ok ( &mktime_9 );
154
+	mktime_ok ( &mktime_10 );
155
+	mktime_ok ( &mktime_11 );
156
+	mktime_ok ( &mktime_12 );
157
+	mktime_ok ( &mktime_13 );
158
+	mktime_ok ( &mktime_14 );
159
+	mktime_ok ( &mktime_15 );
160
+	mktime_ok ( &mktime_16 );
161
+	mktime_ok ( &mktime_17 );
162
+	mktime_ok ( &mktime_18 );
163
+	mktime_ok ( &mktime_19 );
164
+	mktime_ok ( &mktime_20 );
165
+	mktime_ok ( &mktime_21 );
166
+	mktime_ok ( &mktime_22 );
167
+	mktime_ok ( &mktime_23 );
168
+	mktime_ok ( &mktime_24 );
169
+	mktime_ok ( &mktime_25 );
170
+	mktime_ok ( &mktime_26 );
171
+	mktime_ok ( &mktime_27 );
172
+	mktime_ok ( &mktime_28 );
173
+	mktime_ok ( &mktime_29 );
174
+	mktime_ok ( &mktime_30 );
175
+	mktime_ok ( &mktime_31 );
176
+}
177
+
178
+/** Date and time self-test */
179
+struct self_test time_test __self_test = {
180
+	.name = "time",
181
+	.exec = time_test_exec,
182
+};

Loading…
Cancel
Save