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