Blame SOURCES/0430-tests-priority_queue_unit_test.cc-New-test.patch

f96e0b
From bcf0809ca0ce82d3f4608ddddfb6996dda99a4f1 Mon Sep 17 00:00:00 2001
f96e0b
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
f96e0b
Date: Tue, 7 May 2013 11:30:48 +0200
f96e0b
Subject: [PATCH 430/482] 	* tests/priority_queue_unit_test.cc: New test.
f96e0b
f96e0b
---
f96e0b
 ChangeLog                         |   4 ++
f96e0b
 Makefile.util.def                 |  17 ++++++
f96e0b
 configure.ac                      |   5 ++
f96e0b
 grub-core/lib/priority_queue.c    |  94 ----------------------------------
f96e0b
 include/grub/misc.h               |   2 +-
f96e0b
 include/grub/priority_queue.h     |   8 +++
f96e0b
 include/grub/test.h               |   8 +++
f96e0b
 tests/priority_queue_unit_test.cc | 105 ++++++++++++++++++++++++++++++++++++++
f96e0b
 8 files changed, 148 insertions(+), 95 deletions(-)
f96e0b
 create mode 100644 tests/priority_queue_unit_test.cc
f96e0b
f96e0b
diff --git a/ChangeLog b/ChangeLog
f96e0b
index 0fa3be0..0fe18bb 100644
f96e0b
--- a/ChangeLog
f96e0b
+++ b/ChangeLog
f96e0b
@@ -1,5 +1,9 @@
f96e0b
 2013-05-07  Vladimir Serbinenko  <phcoder@gmail.com>
f96e0b
 
f96e0b
+	* tests/priority_queue_unit_test.cc: New test.
f96e0b
+
f96e0b
+2013-05-07  Vladimir Serbinenko  <phcoder@gmail.com>
f96e0b
+
f96e0b
 	* grub-core/font/font.c: Use grub_dprintf for debug statements rather
f96e0b
 	than printf.
f96e0b
 
f96e0b
diff --git a/Makefile.util.def b/Makefile.util.def
f96e0b
index d0ae67f..086ca12 100644
f96e0b
--- a/Makefile.util.def
f96e0b
+++ b/Makefile.util.def
f96e0b
@@ -859,6 +859,23 @@ program = {
f96e0b
 
f96e0b
 program = {
f96e0b
   testcase;
f96e0b
+  name = priority_queue_unit_test;
f96e0b
+  common = tests/priority_queue_unit_test.cc;
f96e0b
+  common = tests/lib/unit_test.c;
f96e0b
+  common = grub-core/kern/list.c;
f96e0b
+  common = grub-core/kern/misc.c;
f96e0b
+  common = grub-core/tests/lib/test.c;
f96e0b
+  common = grub-core/lib/priority_queue.c;
f96e0b
+  ldadd = libgrubmods.a;
f96e0b
+  ldadd = libgrubgcry.a;
f96e0b
+  ldadd = libgrubkern.a;
f96e0b
+  ldadd = grub-core/gnulib/libgnu.a;
f96e0b
+  ldadd = '$(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
f96e0b
+  condition = COND_HAVE_CXX;
f96e0b
+};
f96e0b
+
f96e0b
+program = {
f96e0b
+  testcase;
f96e0b
   name = cmp_test;
f96e0b
   common = tests/cmp_unit_test.c;
f96e0b
   common = tests/lib/unit_test.c;
f96e0b
diff --git a/configure.ac b/configure.ac
f96e0b
index 00540e6..6c310f7 100644
f96e0b
--- a/configure.ac
f96e0b
+++ b/configure.ac
f96e0b
@@ -320,12 +320,15 @@ AC_PATH_PROGS(MAKEINFO, makeinfo true)
f96e0b
 
f96e0b
 AC_PROG_CC
f96e0b
 gl_EARLY
f96e0b
+AC_PROG_CXX
f96e0b
 AM_PROG_CC_C_O
f96e0b
 AM_PROG_AS
f96e0b
 
f96e0b
 # Must be GCC.
f96e0b
 test "x$GCC" = xyes || AC_MSG_ERROR([GCC is required])
f96e0b
 
f96e0b
+AC_CHECK_PROG(HAVE_CXX, $CXX, yes, no)
f96e0b
+
f96e0b
 AC_GNU_SOURCE
f96e0b
 AM_GNU_GETTEXT([external])
f96e0b
 AC_SYS_LARGEFILE
f96e0b
@@ -1178,6 +1181,8 @@ AM_CONDITIONAL([COND_ENABLE_EFIEMU], [test x$enable_efiemu = xyes])
f96e0b
 AM_CONDITIONAL([COND_ENABLE_CACHE_STATS], [test x$DISK_CACHE_STATS = x1])
f96e0b
 AM_CONDITIONAL([COND_ENABLE_BOOT_TIME_STATS], [test x$BOOT_TIME_STATS = x1])
f96e0b
 
f96e0b
+AM_CONDITIONAL([COND_HAVE_CXX], [test x$HAVE_CXX = xyes])
f96e0b
+
f96e0b
 AM_CONDITIONAL([COND_HAVE_ASM_USCORE], [test x$HAVE_ASM_USCORE = x1])
f96e0b
 AM_CONDITIONAL([COND_CYGWIN], [test x$host_os = xcygwin])
f96e0b
 AM_CONDITIONAL([COND_STARFIELD], [test "x$starfield_excuse" = x])
f96e0b
diff --git a/grub-core/lib/priority_queue.c b/grub-core/lib/priority_queue.c
f96e0b
index a790910..659be0b 100644
f96e0b
--- a/grub-core/lib/priority_queue.c
f96e0b
+++ b/grub-core/lib/priority_queue.c
f96e0b
@@ -16,37 +16,12 @@
f96e0b
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
f96e0b
  */
f96e0b
 
f96e0b
-#ifndef TEST
f96e0b
 #include <grub/priority_queue.h>
f96e0b
 #include <grub/mm.h>
f96e0b
 #include <grub/dl.h>
f96e0b
 
f96e0b
 GRUB_MOD_LICENSE ("GPLv3+");
f96e0b
 
f96e0b
-#else
f96e0b
-#include <stdlib.h>
f96e0b
-#include <stdio.h>
f96e0b
-#include <string.h>
f96e0b
-
f96e0b
-#include <queue>
f96e0b
-
f96e0b
-using namespace std;
f96e0b
-
f96e0b
-typedef size_t grub_size_t;
f96e0b
-typedef int (*grub_comparator_t) (const void *a, const void *b);
f96e0b
-typedef unsigned char grub_uint8_t;
f96e0b
-#define grub_malloc malloc
f96e0b
-#define grub_memcpy memcpy
f96e0b
-#define grub_realloc realloc
f96e0b
-#define grub_free free
f96e0b
-
f96e0b
-typedef enum
f96e0b
-  {
f96e0b
-    GRUB_ERR_NONE,
f96e0b
-    grub_errno
f96e0b
-  } grub_err_t;
f96e0b
-#endif
f96e0b
-
f96e0b
 struct grub_priority_queue
f96e0b
 {
f96e0b
   grub_size_t elsize;
f96e0b
@@ -56,10 +31,6 @@ struct grub_priority_queue
f96e0b
   void *els;
f96e0b
 };
f96e0b
 
f96e0b
-#ifdef TEST
f96e0b
-typedef struct grub_priority_queue *grub_priority_queue_t;
f96e0b
-#endif
f96e0b
-
f96e0b
 static inline void *
f96e0b
 element (struct grub_priority_queue *pq, grub_size_t k)
f96e0b
 {
f96e0b
@@ -189,69 +160,4 @@ grub_priority_queue_pop (grub_priority_queue_t pq)
f96e0b
     }
f96e0b
 }
f96e0b
 
f96e0b
-#ifdef TEST
f96e0b
-
f96e0b
-static int 
f96e0b
-compar (const void *a_, const void *b_)
f96e0b
-{
f96e0b
-  int a = *(int *) a_;
f96e0b
-  int b = *(int *) b_;
f96e0b
-  if (a < b)
f96e0b
-    return -1;
f96e0b
-  if (a > b)
f96e0b
-    return +1;
f96e0b
-  return 0;
f96e0b
-}
f96e0b
 
f96e0b
-int
f96e0b
-main (void)
f96e0b
-{
f96e0b
-  priority_queue <int> pq;
f96e0b
-  grub_priority_queue_t pq2;
f96e0b
-  int counter;
f96e0b
-  int s = 0;
f96e0b
-  pq2 = grub_priority_queue_new (sizeof (int), compar);
f96e0b
-  if (!pq2)
f96e0b
-    return 1;
f96e0b
-  srand (1);
f96e0b
-
f96e0b
-  for (counter = 0; counter < 1000000; counter++)
f96e0b
-    {
f96e0b
-      int op = rand () % 10;
f96e0b
-      if (s && *(int *) grub_priority_queue_top (pq2) != pq.top ())
f96e0b
-	{
f96e0b
-	  printf ("Error at %d\n", counter);
f96e0b
-	  return 2;
f96e0b
-	}
f96e0b
-      if (op < 3 && s)
f96e0b
-	{
f96e0b
-	  grub_priority_queue_pop (pq2);
f96e0b
-	  pq.pop ();
f96e0b
-	  s--;
f96e0b
-	}
f96e0b
-      else
f96e0b
-	{
f96e0b
-	  int v = rand ();
f96e0b
-	  int e;
f96e0b
-	  pq.push (v);
f96e0b
-	  e = grub_priority_queue_push (pq2, &v);
f96e0b
-	  if (e)
f96e0b
-	    return 3;
f96e0b
-	  s++;
f96e0b
-	}
f96e0b
-    }
f96e0b
-  while (s)
f96e0b
-    {
f96e0b
-      if (*(int *) grub_priority_queue_top (pq2) != pq.top ())
f96e0b
-	{
f96e0b
-	  printf ("Error at the end. %d elements remaining.\n", s);
f96e0b
-	  return 4;
f96e0b
-	}
f96e0b
-      grub_priority_queue_pop (pq2);
f96e0b
-      pq.pop ();
f96e0b
-      s--;
f96e0b
-    }
f96e0b
-  printf ("All tests passed successfully\n");
f96e0b
-  return 0;
f96e0b
-}
f96e0b
-#endif
f96e0b
diff --git a/include/grub/misc.h b/include/grub/misc.h
f96e0b
index c191c1f..9d7f212 100644
f96e0b
--- a/include/grub/misc.h
f96e0b
+++ b/include/grub/misc.h
f96e0b
@@ -392,7 +392,7 @@ void EXPORT_FUNC (__deregister_frame_info) (void);
f96e0b
 static inline char *
f96e0b
 grub_memchr (const void *p, int c, grub_size_t len)
f96e0b
 {
f96e0b
-  const char *s = p;
f96e0b
+  const char *s = (const char *) p;
f96e0b
   const char *e = s + len;
f96e0b
 
f96e0b
   for (; s < e; s++)
f96e0b
diff --git a/include/grub/priority_queue.h b/include/grub/priority_queue.h
f96e0b
index a5d98c8..64cbc45 100644
f96e0b
--- a/include/grub/priority_queue.h
f96e0b
+++ b/include/grub/priority_queue.h
f96e0b
@@ -22,6 +22,10 @@
f96e0b
 #include <grub/misc.h>
f96e0b
 #include <grub/err.h>
f96e0b
 
f96e0b
+#ifdef __cplusplus
f96e0b
+extern "C" {
f96e0b
+#endif
f96e0b
+
f96e0b
 struct grub_priority_queue;
f96e0b
 typedef struct grub_priority_queue *grub_priority_queue_t;
f96e0b
 typedef int (*grub_comparator_t) (const void *a, const void *b);
f96e0b
@@ -33,4 +37,8 @@ void *grub_priority_queue_top (grub_priority_queue_t pq);
f96e0b
 void grub_priority_queue_pop (grub_priority_queue_t pq);
f96e0b
 grub_err_t grub_priority_queue_push (grub_priority_queue_t pq, const void *el);
f96e0b
 
f96e0b
+#ifdef __cplusplus
f96e0b
+}
f96e0b
+#endif
f96e0b
+
f96e0b
 #endif
f96e0b
diff --git a/include/grub/test.h b/include/grub/test.h
f96e0b
index cc4e4a8..8df85d0 100644
f96e0b
--- a/include/grub/test.h
f96e0b
+++ b/include/grub/test.h
f96e0b
@@ -28,6 +28,10 @@
f96e0b
 #include <grub/video.h>
f96e0b
 #include <grub/video_fb.h>
f96e0b
 
f96e0b
+#ifdef __cplusplus
f96e0b
+extern "C" {
f96e0b
+#endif
f96e0b
+
f96e0b
 struct grub_test
f96e0b
 {
f96e0b
   /* The next test.  */
f96e0b
@@ -108,4 +112,8 @@ grub_video_checksum_get_modename (void);
f96e0b
 #define GRUB_TEST_VIDEO_SMALL_N_MODES 6
f96e0b
 extern struct grub_video_mode_info grub_test_video_modes[30];
f96e0b
 
f96e0b
+#ifdef __cplusplus
f96e0b
+}
f96e0b
+#endif
f96e0b
+
f96e0b
 #endif /* ! GRUB_TEST_HEADER */
f96e0b
diff --git a/tests/priority_queue_unit_test.cc b/tests/priority_queue_unit_test.cc
f96e0b
new file mode 100644
f96e0b
index 0000000..bb0060e
f96e0b
--- /dev/null
f96e0b
+++ b/tests/priority_queue_unit_test.cc
f96e0b
@@ -0,0 +1,105 @@
f96e0b
+/*
f96e0b
+ *  GRUB  --  GRand Unified Bootloader
f96e0b
+ *  Copyright (C) 2013 Free Software Foundation, Inc.
f96e0b
+ *
f96e0b
+ *  GRUB is free software: you can redistribute it and/or modify
f96e0b
+ *  it under the terms of the GNU General Public License as published by
f96e0b
+ *  the Free Software Foundation, either version 3 of the License, or
f96e0b
+ *  (at your option) any later version.
f96e0b
+ *
f96e0b
+ *  GRUB is distributed in the hope that it will be useful,
f96e0b
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
f96e0b
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f96e0b
+ *  GNU General Public License for more details.
f96e0b
+ *
f96e0b
+ *  You should have received a copy of the GNU General Public License
f96e0b
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
f96e0b
+ */
f96e0b
+
f96e0b
+#include <stdio.h>
f96e0b
+#include <string.h>
f96e0b
+#include <grub/test.h>
f96e0b
+#include <grub/misc.h>
f96e0b
+#include <grub/priority_queue.h>
f96e0b
+
f96e0b
+#include <stdlib.h>
f96e0b
+#include <stdio.h>
f96e0b
+#include <string.h>
f96e0b
+
f96e0b
+#include <queue>
f96e0b
+
f96e0b
+using namespace std;
f96e0b
+
f96e0b
+static int 
f96e0b
+compar (const void *a_, const void *b_)
f96e0b
+{
f96e0b
+  int a = *(int *) a_;
f96e0b
+  int b = *(int *) b_;
f96e0b
+  if (a < b)
f96e0b
+    return -1;
f96e0b
+  if (a > b)
f96e0b
+    return +1;
f96e0b
+  return 0;
f96e0b
+}
f96e0b
+
f96e0b
+static void
f96e0b
+priority_queue_test (void)
f96e0b
+{
f96e0b
+  priority_queue <int> pq;
f96e0b
+  grub_priority_queue_t pq2;
f96e0b
+  int counter;
f96e0b
+  int s = 0;
f96e0b
+  pq2 = grub_priority_queue_new (sizeof (int), compar);
f96e0b
+  if (!pq2)
f96e0b
+    {
f96e0b
+      grub_test_assert (0,
f96e0b
+			"priority queue: queue creating failed\n");
f96e0b
+      return;
f96e0b
+    }
f96e0b
+  srand (1);
f96e0b
+
f96e0b
+  for (counter = 0; counter < 1000000; counter++)
f96e0b
+    {
f96e0b
+      int op = rand () % 10;
f96e0b
+      if (s && *(int *) grub_priority_queue_top (pq2) != pq.top ())
f96e0b
+	{
f96e0b
+	  printf ("Error at %d\n", counter);
f96e0b
+	  grub_test_assert (0,
f96e0b
+			    "priority queue: error at %d\n", counter);
f96e0b
+	  return;
f96e0b
+	}
f96e0b
+      if (op < 3 && s)
f96e0b
+	{
f96e0b
+	  grub_priority_queue_pop (pq2);
f96e0b
+	  pq.pop ();
f96e0b
+	  s--;
f96e0b
+	}
f96e0b
+      else
f96e0b
+	{
f96e0b
+	  int v = rand ();
f96e0b
+	  pq.push (v);
f96e0b
+	  if (grub_priority_queue_push (pq2, &v) != 0)
f96e0b
+	    {
f96e0b
+	      grub_test_assert (0,
f96e0b
+				"priority queue: push failed");
f96e0b
+	      return;
f96e0b
+	    }
f96e0b
+	  s++;
f96e0b
+	}
f96e0b
+    }
f96e0b
+  while (s)
f96e0b
+    {
f96e0b
+      if (*(int *) grub_priority_queue_top (pq2) != pq.top ())
f96e0b
+	{
f96e0b
+	  grub_test_assert (0,
f96e0b
+			    "priority queue: Error at the end. %d elements remaining.\n", s);
f96e0b
+	  return;
f96e0b
+	}
f96e0b
+      grub_priority_queue_pop (pq2);
f96e0b
+      pq.pop ();
f96e0b
+      s--;
f96e0b
+    }
f96e0b
+  printf ("priority_queue: passed successfully\n");
f96e0b
+}
f96e0b
+
f96e0b
+GRUB_UNIT_TEST ("priority_queue_unit_test", priority_queue_test);
f96e0b
-- 
f96e0b
1.8.2.1
f96e0b