ieee1275: split up grub_machine_get_bootlocation
[grub.git] / tests / date_unit_test.c
1 /*
2  *  GRUB  --  GRand Unified Bootloader
3  *  Copyright (C) 2013 Free Software Foundation, Inc.
4  *
5  *  GRUB is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  GRUB is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20 #include <time.h>
21 #include <stdlib.h>
22
23 #include <grub/misc.h>
24 #include <grub/datetime.h>
25 #include <grub/test.h>
26
27 static void
28 date_test (grub_int32_t v)
29 {
30   struct grub_datetime dt;
31   time_t t = v;
32   struct tm *g;
33   int w;
34
35   g = gmtime (&t);
36
37   grub_unixtime2datetime (v, &dt);
38
39   w = grub_get_weekday (&dt);
40
41   grub_test_assert (g->tm_sec == dt.second, "time %d bad second: %d vs %d", v,
42                     g->tm_sec, dt.second);
43   grub_test_assert (g->tm_min == dt.minute, "time %d bad minute: %d vs %d", v,
44                     g->tm_min, dt.minute);
45   grub_test_assert (g->tm_hour == dt.hour, "time %d bad hour: %d vs %d", v,
46                     g->tm_hour, dt.hour);
47   grub_test_assert (g->tm_mday == dt.day, "time %d bad day: %d vs %d", v,
48                     g->tm_mday, dt.day);
49   grub_test_assert (g->tm_mon + 1 == dt.month, "time %d bad month: %d vs %d", v,
50                     g->tm_mon + 1, dt.month);
51   grub_test_assert (g->tm_year + 1900 == dt.year,
52                     "time %d bad year: %d vs %d", v,
53                     g->tm_year + 1900, dt.year);
54   grub_test_assert (g->tm_wday == w, "time %d bad week day: %d vs %d", v,
55                     g->tm_wday, w);
56 }
57
58 static void
59 date_test_iter (void)
60 {
61   grub_int32_t tests[] = { -1, 0, +1, -2133156255, GRUB_INT32_MIN,
62                            GRUB_INT32_MAX };
63   unsigned i;
64
65   for (i = 0; i < ARRAY_SIZE (tests); i++)
66     date_test (tests[i]);
67   srand (42);
68   for (i = 0; i < 1000000; i++)
69     {
70       grub_int32_t x = rand ();
71       date_test (x);
72       date_test (-x);
73     }
74 }
75
76 GRUB_UNIT_TEST ("date_unit_test", date_test_iter);