iPXE
time_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Date and time self-tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <time.h>
00036 #include <ipxe/test.h>
00037 
00038 /** A mktime() test */
00039 struct mktime_test {
00040         /** Broken-down time */
00041         struct tm tm;
00042         /** Day of the week */
00043         int wday;
00044         /** Day of the year */
00045         int yday;
00046         /** Seconds since the Epoch */
00047         time_t time;
00048 };
00049 
00050 /**
00051  * Define a mktime() test
00052  *
00053  * @v name              Test name
00054  * @v SEC               Seconds [0,60]
00055  * @v MIN               Minutes [0,59]
00056  * @v HOUR              Hour [0,23]
00057  * @v MDAY              Day of month [1,31]
00058  * @v MON               Month of year [0,11]
00059  * @v YEAR              Years since 1900
00060  * @v WDAY              Day of week [0,6] (Sunday=0)
00061  * @v YDAY              Day of year [0,365]
00062  * @v ISDST             Daylight savings flag (ignored)
00063  * @v TIME              Seconds since the Epoch
00064  * @ret test            mktime() test
00065  *
00066  * This macro is designed to make it easy to generate test vectors in
00067  * Perl using
00068  *
00069  *    print join ", ", gmtime ( $time ), $time."ULL";
00070  *
00071  */
00072 #define MKTIME_TEST( name, SEC, MIN, HOUR, MDAY, MON, YEAR, WDAY,       \
00073                      YDAY, ISDST, TIME )                                \
00074         static struct mktime_test name = {                              \
00075                 .tm = {                                                 \
00076                         .tm_sec = SEC,                                  \
00077                         .tm_min = MIN,                                  \
00078                         .tm_hour = HOUR,                                \
00079                         .tm_mday = MDAY,                                \
00080                         .tm_mon = MON,                                  \
00081                         .tm_year = YEAR,                                \
00082                         .tm_isdst = ISDST,                              \
00083                 },                                                      \
00084                 .wday = WDAY,                                           \
00085                 .yday = YDAY,                                           \
00086                 .time = TIME,                                           \
00087         }
00088 
00089 /**
00090  * Report mktime() test result
00091  *
00092  * @v test              mktime() test
00093  */
00094 #define mktime_ok( test ) do {                                          \
00095         time_t time = mktime ( &(test)->tm );                           \
00096         ok ( time == (test)->time );                                    \
00097         ok ( (test)->tm.tm_wday == (test)->wday );                      \
00098         ok ( (test)->tm.tm_yday == (test)->yday );                      \
00099         } while ( 0 )
00100 
00101 /* Start of the Epoch */
00102 MKTIME_TEST ( mktime_epoch, 00, 00, 00, 01, 00, 70, 4, 0, 0, 0 );
00103 
00104 /* Birth of iPXE as a new project */
00105 MKTIME_TEST ( mktime_ipxe, 01, 15, 20, 19, 03, 110, 1, 108, 0, 1271708101ULL );
00106 
00107 /* Random test vectors generated using Perl's gmtime() */
00108 MKTIME_TEST ( mktime_0, 4, 17, 20, 1, 0, 150, 6, 0, 0, 2524681024ULL );
00109 MKTIME_TEST ( mktime_1, 22, 47, 21, 27, 11, 77, 2, 360, 0, 252107242ULL );
00110 MKTIME_TEST ( mktime_2, 26, 10, 0, 7, 2, 196, 3, 66, 0, 3981917426ULL );
00111 MKTIME_TEST ( mktime_3, 44, 44, 23, 15, 9, 261, 4, 287, 0, 6052319084ULL );
00112 MKTIME_TEST ( mktime_4, 3, 22, 18, 8, 9, 296, 6, 281, 0, 7156232523ULL );
00113 MKTIME_TEST ( mktime_5, 27, 26, 16, 18, 11, 338, 2, 351, 0, 8487649587ULL );
00114 MKTIME_TEST ( mktime_6, 31, 36, 22, 3, 3, 293, 3, 92, 0, 7045310191ULL );
00115 MKTIME_TEST ( mktime_7, 2, 0, 6, 25, 5, 289, 4, 175, 0, 6926191202ULL );
00116 MKTIME_TEST ( mktime_8, 43, 50, 1, 8, 0, 210, 3, 7, 0, 4418589043ULL );
00117 MKTIME_TEST ( mktime_9, 48, 14, 20, 23, 3, 86, 3, 112, 0, 514671288ULL );
00118 MKTIME_TEST ( mktime_10, 4, 43, 5, 29, 11, 173, 5, 362, 0, 3281751784ULL );
00119 MKTIME_TEST ( mktime_11, 47, 26, 21, 12, 7, 177, 4, 223, 0, 3396029207ULL );
00120 MKTIME_TEST ( mktime_12, 18, 55, 20, 26, 11, 88, 1, 360, 0, 599172918ULL );
00121 MKTIME_TEST ( mktime_13, 8, 32, 13, 15, 7, 314, 1, 226, 0, 7719456728ULL );
00122 MKTIME_TEST ( mktime_14, 0, 16, 11, 20, 6, 138, 2, 200, 0, 2163237360ULL );
00123 MKTIME_TEST ( mktime_15, 48, 0, 9, 31, 2, 202, 5, 89, 0, 4173238848ULL );
00124 MKTIME_TEST ( mktime_16, 51, 55, 0, 15, 1, 323, 6, 45, 0, 7987769751ULL );
00125 MKTIME_TEST ( mktime_17, 36, 10, 7, 11, 5, 301, 4, 161, 0, 7303590636ULL );
00126 MKTIME_TEST ( mktime_18, 22, 39, 11, 21, 9, 233, 3, 293, 0, 5169181162ULL );
00127 MKTIME_TEST ( mktime_19, 48, 29, 8, 31, 7, 207, 3, 242, 0, 4344222588ULL );
00128 MKTIME_TEST ( mktime_20, 4, 53, 22, 8, 8, 165, 2, 250, 0, 3019675984ULL );
00129 MKTIME_TEST ( mktime_21, 14, 16, 8, 10, 5, 298, 0, 160, 0, 7208900174ULL );
00130 MKTIME_TEST ( mktime_22, 10, 35, 3, 12, 3, 188, 1, 102, 0, 3732579310ULL );
00131 MKTIME_TEST ( mktime_23, 47, 12, 18, 22, 2, 103, 6, 80, 0, 1048356767ULL );
00132 MKTIME_TEST ( mktime_24, 23, 29, 17, 23, 10, 201, 3, 326, 0, 4162210163ULL );
00133 MKTIME_TEST ( mktime_25, 58, 35, 23, 24, 3, 111, 0, 113, 0, 1303688158ULL );
00134 MKTIME_TEST ( mktime_26, 34, 56, 15, 24, 11, 154, 4, 357, 0, 2681740594ULL );
00135 MKTIME_TEST ( mktime_27, 7, 11, 22, 28, 1, 243, 4, 58, 0, 5464447867ULL );
00136 MKTIME_TEST ( mktime_28, 25, 45, 23, 29, 11, 90, 6, 362, 0, 662514325ULL );
00137 MKTIME_TEST ( mktime_29, 31, 20, 12, 24, 1, 146, 6, 54, 0, 2403087631ULL );
00138 MKTIME_TEST ( mktime_30, 49, 7, 18, 16, 10, 271, 6, 319, 0, 6370596469ULL );
00139 MKTIME_TEST ( mktime_31, 31, 55, 2, 25, 5, 141, 2, 175, 0, 2255741731ULL );
00140 
00141 /**
00142  * Perform date and time self-tests
00143  *
00144  */
00145 static void time_test_exec ( void ) {
00146 
00147         mktime_ok ( &mktime_epoch );
00148         mktime_ok ( &mktime_ipxe );
00149         mktime_ok ( &mktime_0 );
00150         mktime_ok ( &mktime_1 );
00151         mktime_ok ( &mktime_2 );
00152         mktime_ok ( &mktime_3 );
00153         mktime_ok ( &mktime_4 );
00154         mktime_ok ( &mktime_5 );
00155         mktime_ok ( &mktime_6 );
00156         mktime_ok ( &mktime_7 );
00157         mktime_ok ( &mktime_8 );
00158         mktime_ok ( &mktime_9 );
00159         mktime_ok ( &mktime_10 );
00160         mktime_ok ( &mktime_11 );
00161         mktime_ok ( &mktime_12 );
00162         mktime_ok ( &mktime_13 );
00163         mktime_ok ( &mktime_14 );
00164         mktime_ok ( &mktime_15 );
00165         mktime_ok ( &mktime_16 );
00166         mktime_ok ( &mktime_17 );
00167         mktime_ok ( &mktime_18 );
00168         mktime_ok ( &mktime_19 );
00169         mktime_ok ( &mktime_20 );
00170         mktime_ok ( &mktime_21 );
00171         mktime_ok ( &mktime_22 );
00172         mktime_ok ( &mktime_23 );
00173         mktime_ok ( &mktime_24 );
00174         mktime_ok ( &mktime_25 );
00175         mktime_ok ( &mktime_26 );
00176         mktime_ok ( &mktime_27 );
00177         mktime_ok ( &mktime_28 );
00178         mktime_ok ( &mktime_29 );
00179         mktime_ok ( &mktime_30 );
00180         mktime_ok ( &mktime_31 );
00181 }
00182 
00183 /** Date and time self-test */
00184 struct self_test time_test __self_test = {
00185         .name = "time",
00186         .exec = time_test_exec,
00187 };