priority_queue_unit_test.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <string.h>
  20. #include <grub/test.h>
  21. #include <grub/misc.h>
  22. #include <grub/priority_queue.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <queue>
  27. using namespace std;
  28. static int
  29. compar (const void *a_, const void *b_)
  30. {
  31. int a = *(int *) a_;
  32. int b = *(int *) b_;
  33. if (a < b)
  34. return -1;
  35. if (a > b)
  36. return +1;
  37. return 0;
  38. }
  39. static void
  40. priority_queue_test (void)
  41. {
  42. priority_queue <int> pq;
  43. grub_priority_queue_t pq2;
  44. int counter;
  45. int s = 0;
  46. pq2 = grub_priority_queue_new (sizeof (int), compar);
  47. if (!pq2)
  48. {
  49. grub_test_assert (0,
  50. "priority queue: queue creating failed\n");
  51. return;
  52. }
  53. srand (1);
  54. for (counter = 0; counter < 1000000; counter++)
  55. {
  56. int op = rand () % 10;
  57. if (s && *(int *) grub_priority_queue_top (pq2) != pq.top ())
  58. {
  59. printf ("Error at %d\n", counter);
  60. grub_test_assert (0,
  61. "priority queue: error at %d\n", counter);
  62. return;
  63. }
  64. if (op < 3 && s)
  65. {
  66. grub_priority_queue_pop (pq2);
  67. pq.pop ();
  68. s--;
  69. }
  70. else
  71. {
  72. int v = rand ();
  73. pq.push (v);
  74. if (grub_priority_queue_push (pq2, &v) != 0)
  75. {
  76. grub_test_assert (0,
  77. "priority queue: push failed");
  78. return;
  79. }
  80. s++;
  81. }
  82. }
  83. while (s)
  84. {
  85. if (*(int *) grub_priority_queue_top (pq2) != pq.top ())
  86. {
  87. grub_test_assert (0,
  88. "priority queue: Error at the end. %d elements remaining.\n", s);
  89. return;
  90. }
  91. grub_priority_queue_pop (pq2);
  92. pq.pop ();
  93. s--;
  94. }
  95. printf ("priority_queue: passed successfully\n");
  96. }
  97. GRUB_UNIT_TEST ("priority_queue_unit_test", priority_queue_test);