date_unit_test.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include <stdio.h>
  19. #include <time.h>
  20. #include <stdlib.h>
  21. #include <grub/misc.h>
  22. #include <grub/datetime.h>
  23. #include <grub/test.h>
  24. static void
  25. date_test (grub_int32_t v)
  26. {
  27. struct grub_datetime dt;
  28. time_t t = v;
  29. struct tm *g;
  30. int w;
  31. g = gmtime (&t);
  32. grub_unixtime2datetime (v, &dt);
  33. w = grub_get_weekday (&dt);
  34. grub_test_assert (g->tm_sec == dt.second, "time %d bad second: %d vs %d", v,
  35. g->tm_sec, dt.second);
  36. grub_test_assert (g->tm_min == dt.minute, "time %d bad minute: %d vs %d", v,
  37. g->tm_min, dt.minute);
  38. grub_test_assert (g->tm_hour == dt.hour, "time %d bad hour: %d vs %d", v,
  39. g->tm_hour, dt.hour);
  40. grub_test_assert (g->tm_mday == dt.day, "time %d bad day: %d vs %d", v,
  41. g->tm_mday, dt.day);
  42. grub_test_assert (g->tm_mon + 1 == dt.month, "time %d bad month: %d vs %d", v,
  43. g->tm_mon + 1, dt.month);
  44. grub_test_assert (g->tm_year + 1900 == dt.year,
  45. "time %d bad year: %d vs %d", v,
  46. g->tm_year + 1900, dt.year);
  47. grub_test_assert (g->tm_wday == w, "time %d bad week day: %d vs %d", v,
  48. g->tm_wday, w);
  49. }
  50. static void
  51. date_test_iter (void)
  52. {
  53. grub_int32_t tests[] = { -1, 0, +1, -2133156255, GRUB_INT32_MIN,
  54. GRUB_INT32_MAX };
  55. unsigned i;
  56. for (i = 0; i < ARRAY_SIZE (tests); i++)
  57. date_test (tests[i]);
  58. srand (42);
  59. for (i = 0; i < 1000000; i++)
  60. {
  61. grub_int32_t x = rand ();
  62. date_test (x);
  63. date_test (-x);
  64. }
  65. }
  66. GRUB_UNIT_TEST ("date_unit_test", date_test_iter);