]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
* tests/priority_queue_unit_test.cc: New test.
authorVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Tue, 7 May 2013 09:30:48 +0000 (11:30 +0200)
committerVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Tue, 7 May 2013 09:30:48 +0000 (11:30 +0200)
ChangeLog
Makefile.util.def
configure.ac
grub-core/lib/priority_queue.c
include/grub/misc.h
include/grub/priority_queue.h
include/grub/test.h
tests/priority_queue_unit_test.cc [new file with mode: 0644]

index 0fa3be0cd223bfb8870e870f29b68c41a975cd14..0fe18bb39aa7150f70509e827277bee546f9618c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2013-05-07  Vladimir Serbinenko  <phcoder@gmail.com>
+
+       * tests/priority_queue_unit_test.cc: New test.
+
 2013-05-07  Vladimir Serbinenko  <phcoder@gmail.com>
 
        * grub-core/font/font.c: Use grub_dprintf for debug statements rather
index d0ae67f722927573f777750550abd1bf3c63aa2f..086ca12857c93b466e77146f02ea79f5cd6c1db2 100644 (file)
@@ -857,6 +857,23 @@ program = {
   ldadd = '$(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
 };
 
+program = {
+  testcase;
+  name = priority_queue_unit_test;
+  common = tests/priority_queue_unit_test.cc;
+  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;
+  common = grub-core/lib/priority_queue.c;
+  ldadd = libgrubmods.a;
+  ldadd = libgrubgcry.a;
+  ldadd = libgrubkern.a;
+  ldadd = grub-core/gnulib/libgnu.a;
+  ldadd = '$(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
+  condition = COND_HAVE_CXX;
+};
+
 program = {
   testcase;
   name = cmp_test;
index 00540e6be1438eb3a2599f10a32c65833ca3d322..6c310f779f1844865dca75458e1ffbba3fa555c5 100644 (file)
@@ -320,12 +320,15 @@ AC_PATH_PROGS(MAKEINFO, makeinfo true)
 
 AC_PROG_CC
 gl_EARLY
+AC_PROG_CXX
 AM_PROG_CC_C_O
 AM_PROG_AS
 
 # Must be GCC.
 test "x$GCC" = xyes || AC_MSG_ERROR([GCC is required])
 
+AC_CHECK_PROG(HAVE_CXX, $CXX, yes, no)
+
 AC_GNU_SOURCE
 AM_GNU_GETTEXT([external])
 AC_SYS_LARGEFILE
@@ -1178,6 +1181,8 @@ AM_CONDITIONAL([COND_ENABLE_EFIEMU], [test x$enable_efiemu = xyes])
 AM_CONDITIONAL([COND_ENABLE_CACHE_STATS], [test x$DISK_CACHE_STATS = x1])
 AM_CONDITIONAL([COND_ENABLE_BOOT_TIME_STATS], [test x$BOOT_TIME_STATS = x1])
 
+AM_CONDITIONAL([COND_HAVE_CXX], [test x$HAVE_CXX = xyes])
+
 AM_CONDITIONAL([COND_HAVE_ASM_USCORE], [test x$HAVE_ASM_USCORE = x1])
 AM_CONDITIONAL([COND_CYGWIN], [test x$host_os = xcygwin])
 AM_CONDITIONAL([COND_STARFIELD], [test "x$starfield_excuse" = x])
index a790910a8091c49350b05328548dbe5ffcc69af5..659be0b7f409cd8601cb582318214a38b215ca99 100644 (file)
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef TEST
 #include <grub/priority_queue.h>
 #include <grub/mm.h>
 #include <grub/dl.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-#else
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-#include <queue>
-
-using namespace std;
-
-typedef size_t grub_size_t;
-typedef int (*grub_comparator_t) (const void *a, const void *b);
-typedef unsigned char grub_uint8_t;
-#define grub_malloc malloc
-#define grub_memcpy memcpy
-#define grub_realloc realloc
-#define grub_free free
-
-typedef enum
-  {
-    GRUB_ERR_NONE,
-    grub_errno
-  } grub_err_t;
-#endif
-
 struct grub_priority_queue
 {
   grub_size_t elsize;
@@ -56,10 +31,6 @@ struct grub_priority_queue
   void *els;
 };
 
-#ifdef TEST
-typedef struct grub_priority_queue *grub_priority_queue_t;
-#endif
-
 static inline void *
 element (struct grub_priority_queue *pq, grub_size_t k)
 {
@@ -189,69 +160,4 @@ grub_priority_queue_pop (grub_priority_queue_t pq)
     }
 }
 
-#ifdef TEST
-
-static int 
-compar (const void *a_, const void *b_)
-{
-  int a = *(int *) a_;
-  int b = *(int *) b_;
-  if (a < b)
-    return -1;
-  if (a > b)
-    return +1;
-  return 0;
-}
 
-int
-main (void)
-{
-  priority_queue <int> pq;
-  grub_priority_queue_t pq2;
-  int counter;
-  int s = 0;
-  pq2 = grub_priority_queue_new (sizeof (int), compar);
-  if (!pq2)
-    return 1;
-  srand (1);
-
-  for (counter = 0; counter < 1000000; counter++)
-    {
-      int op = rand () % 10;
-      if (s && *(int *) grub_priority_queue_top (pq2) != pq.top ())
-       {
-         printf ("Error at %d\n", counter);
-         return 2;
-       }
-      if (op < 3 && s)
-       {
-         grub_priority_queue_pop (pq2);
-         pq.pop ();
-         s--;
-       }
-      else
-       {
-         int v = rand ();
-         int e;
-         pq.push (v);
-         e = grub_priority_queue_push (pq2, &v);
-         if (e)
-           return 3;
-         s++;
-       }
-    }
-  while (s)
-    {
-      if (*(int *) grub_priority_queue_top (pq2) != pq.top ())
-       {
-         printf ("Error at the end. %d elements remaining.\n", s);
-         return 4;
-       }
-      grub_priority_queue_pop (pq2);
-      pq.pop ();
-      s--;
-    }
-  printf ("All tests passed successfully\n");
-  return 0;
-}
-#endif
index c191c1f5138645ba758eac379f20c197447c8f67..9d7f2121d158023d23cbf8875602556dea3a3777 100644 (file)
@@ -392,7 +392,7 @@ void EXPORT_FUNC (__deregister_frame_info) (void);
 static inline char *
 grub_memchr (const void *p, int c, grub_size_t len)
 {
-  const char *s = p;
+  const char *s = (const char *) p;
   const char *e = s + len;
 
   for (; s < e; s++)
index a5d98c8c93ca0e08039899532d228818a972a3b3..64cbc451254f998daa5634c4062f5b12caa3714e 100644 (file)
 #include <grub/misc.h>
 #include <grub/err.h>
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 struct grub_priority_queue;
 typedef struct grub_priority_queue *grub_priority_queue_t;
 typedef int (*grub_comparator_t) (const void *a, const void *b);
@@ -33,4 +37,8 @@ void *grub_priority_queue_top (grub_priority_queue_t pq);
 void grub_priority_queue_pop (grub_priority_queue_t pq);
 grub_err_t grub_priority_queue_push (grub_priority_queue_t pq, const void *el);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif
index cc4e4a82a0d60f698c6d0b7a5fee1793cc75fbb7..8df85d0105b5d6d2f9ad2a764c4877b1ff52605c 100644 (file)
 #include <grub/video.h>
 #include <grub/video_fb.h>
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 struct grub_test
 {
   /* The next test.  */
@@ -108,4 +112,8 @@ grub_video_checksum_get_modename (void);
 #define GRUB_TEST_VIDEO_SMALL_N_MODES 6
 extern struct grub_video_mode_info grub_test_video_modes[30];
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /* ! GRUB_TEST_HEADER */
diff --git a/tests/priority_queue_unit_test.cc b/tests/priority_queue_unit_test.cc
new file mode 100644 (file)
index 0000000..bb0060e
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ *  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 <string.h>
+#include <grub/test.h>
+#include <grub/misc.h>
+#include <grub/priority_queue.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <queue>
+
+using namespace std;
+
+static int 
+compar (const void *a_, const void *b_)
+{
+  int a = *(int *) a_;
+  int b = *(int *) b_;
+  if (a < b)
+    return -1;
+  if (a > b)
+    return +1;
+  return 0;
+}
+
+static void
+priority_queue_test (void)
+{
+  priority_queue <int> pq;
+  grub_priority_queue_t pq2;
+  int counter;
+  int s = 0;
+  pq2 = grub_priority_queue_new (sizeof (int), compar);
+  if (!pq2)
+    {
+      grub_test_assert (0,
+                       "priority queue: queue creating failed\n");
+      return;
+    }
+  srand (1);
+
+  for (counter = 0; counter < 1000000; counter++)
+    {
+      int op = rand () % 10;
+      if (s && *(int *) grub_priority_queue_top (pq2) != pq.top ())
+       {
+         printf ("Error at %d\n", counter);
+         grub_test_assert (0,
+                           "priority queue: error at %d\n", counter);
+         return;
+       }
+      if (op < 3 && s)
+       {
+         grub_priority_queue_pop (pq2);
+         pq.pop ();
+         s--;
+       }
+      else
+       {
+         int v = rand ();
+         pq.push (v);
+         if (grub_priority_queue_push (pq2, &v) != 0)
+           {
+             grub_test_assert (0,
+                               "priority queue: push failed");
+             return;
+           }
+         s++;
+       }
+    }
+  while (s)
+    {
+      if (*(int *) grub_priority_queue_top (pq2) != pq.top ())
+       {
+         grub_test_assert (0,
+                           "priority queue: Error at the end. %d elements remaining.\n", s);
+         return;
+       }
+      grub_priority_queue_pop (pq2);
+      pq.pop ();
+      s--;
+    }
+  printf ("priority_queue: passed successfully\n");
+}
+
+GRUB_UNIT_TEST ("priority_queue_unit_test", priority_queue_test);