tpm2_key_protector: Enable build for powerpc_ieee1275
[grub.git] / tests / priority_queue_unit_test.cc
blobbb0060e5e425bdd027618376839582be261999ab
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2013 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <stdio.h>
20 #include <string.h>
21 #include <grub/test.h>
22 #include <grub/misc.h>
23 #include <grub/priority_queue.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include <queue>
31 using namespace std;
33 static int
34 compar (const void *a_, const void *b_)
36 int a = *(int *) a_;
37 int b = *(int *) b_;
38 if (a < b)
39 return -1;
40 if (a > b)
41 return +1;
42 return 0;
45 static void
46 priority_queue_test (void)
48 priority_queue <int> pq;
49 grub_priority_queue_t pq2;
50 int counter;
51 int s = 0;
52 pq2 = grub_priority_queue_new (sizeof (int), compar);
53 if (!pq2)
55 grub_test_assert (0,
56 "priority queue: queue creating failed\n");
57 return;
59 srand (1);
61 for (counter = 0; counter < 1000000; counter++)
63 int op = rand () % 10;
64 if (s && *(int *) grub_priority_queue_top (pq2) != pq.top ())
66 printf ("Error at %d\n", counter);
67 grub_test_assert (0,
68 "priority queue: error at %d\n", counter);
69 return;
71 if (op < 3 && s)
73 grub_priority_queue_pop (pq2);
74 pq.pop ();
75 s--;
77 else
79 int v = rand ();
80 pq.push (v);
81 if (grub_priority_queue_push (pq2, &v) != 0)
83 grub_test_assert (0,
84 "priority queue: push failed");
85 return;
87 s++;
90 while (s)
92 if (*(int *) grub_priority_queue_top (pq2) != pq.top ())
94 grub_test_assert (0,
95 "priority queue: Error at the end. %d elements remaining.\n", s);
96 return;
98 grub_priority_queue_pop (pq2);
99 pq.pop ();
100 s--;
102 printf ("priority_queue: passed successfully\n");
105 GRUB_UNIT_TEST ("priority_queue_unit_test", priority_queue_test);