]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
* tests/date_unit_test.c: New test.
authorVladimir Serbinenko <phcoder@gmail.com>
Sat, 26 Oct 2013 00:48:19 +0000 (02:48 +0200)
committerVladimir Serbinenko <phcoder@gmail.com>
Sat, 26 Oct 2013 00:48:19 +0000 (02:48 +0200)
Makefile.util.def
tests/date_unit_test.c [new file with mode: 0644]

index efb0d8786add426104d53364e37d98cf7675f9b1..cdeb733f4dbab85a082d6054540378af609f6eef 100644 (file)
@@ -899,6 +899,21 @@ program = {
   ldadd = '$(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
 };
 
+program = {
+  testcase;
+  name = date_test;
+  common = tests/date_unit_test.c;
+  common = tests/lib/unit_test.c;
+  common = grub-core/kern/list.c;
+  common = grub-core/kern/misc.c;
+  common = grub-core/tests/lib/test.c;
+  ldadd = libgrubmods.a;
+  ldadd = libgrubgcry.a;
+  ldadd = libgrubkern.a;
+  ldadd = grub-core/gnulib/libgnu.a;
+  ldadd = '$(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
+};
+
 program = {
   testcase;
   name = priority_queue_unit_test;
diff --git a/tests/date_unit_test.c b/tests/date_unit_test.c
new file mode 100644 (file)
index 0000000..99774f1
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2013 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <time.h>
+#include <stdlib.h>
+
+#include <grub/misc.h>
+#include <grub/datetime.h>
+#include <grub/test.h>
+
+static void
+date_test (grub_int32_t v)
+{
+  struct grub_datetime dt;
+  time_t t = v;
+  struct tm *g;
+  int w;
+
+  g = gmtime (&t);
+
+  grub_unixtime2datetime (v, &dt);
+
+  w = grub_get_weekday (&dt);
+
+  grub_test_assert (g->tm_sec == dt.second, "time %d bad second: %d vs %d", v,
+                   g->tm_sec, dt.second);
+  grub_test_assert (g->tm_min == dt.minute, "time %d bad minute: %d vs %d", v,
+                   g->tm_min, dt.minute);
+  grub_test_assert (g->tm_hour == dt.hour, "time %d bad hour: %d vs %d", v,
+                   g->tm_hour, dt.hour);
+  grub_test_assert (g->tm_mday == dt.day, "time %d bad day: %d vs %d", v,
+                   g->tm_mday, dt.day);
+  grub_test_assert (g->tm_mon + 1 == dt.month, "time %d bad month: %d vs %d", v,
+                   g->tm_mon + 1, dt.month);
+  grub_test_assert (g->tm_year + 1900 == dt.year,
+                   "time %d bad year: %d vs %d", v,
+                   g->tm_year + 1900, dt.year);
+  grub_test_assert (g->tm_wday == w, "time %d bad week day: %d vs %d", v,
+                   g->tm_wday, w);
+}
+
+static void
+date_test_iter (void)
+{
+  grub_int32_t tests[] = { -1, 0, +1, -2133156255, GRUB_INT32_MIN,
+                          GRUB_INT32_MAX };
+  unsigned i;
+
+  for (i = 0; i < ARRAY_SIZE (tests); i++)
+    date_test (tests[i]);
+  srand (42);
+  for (i = 0; i < 1000000; i++)
+    {
+      grub_int32_t x = rand ();
+      date_test (x);
+      date_test (-x);
+    }
+}
+
+GRUB_UNIT_TEST ("date_unit_test", date_test_iter);