This source file includes following definitions.
- check_equals
- check_all
- is_at_least
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <config.h>
19
20 #include "gl_avltree_oset.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "gl_array_oset.h"
26 #include "macros.h"
27
28 #include "test-oset-update.h"
29
30 extern void gl_avltree_oset_check_invariants (gl_oset_t set);
31
32 static const char *objects[30] =
33 {
34 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
35 "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
36 };
37
38 #define RANDOM(n) (rand () % (n))
39 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
40
41 static void
42 check_equals (gl_oset_t set1, gl_oset_t set2)
43 {
44 size_t n = gl_oset_size (set1);
45 gl_oset_iterator_t iter1, iter2;
46 const void *elt1;
47 const void *elt2;
48 size_t i;
49
50 iter1 = gl_oset_iterator (set1);
51 iter2 = gl_oset_iterator (set2);
52 for (i = 0; i < n; i++)
53 {
54 ASSERT (gl_oset_iterator_next (&iter1, &elt1));
55 ASSERT (gl_oset_iterator_next (&iter2, &elt2));
56 ASSERT (elt1 == elt2);
57 }
58 ASSERT (!gl_oset_iterator_next (&iter1, &elt1));
59 ASSERT (!gl_oset_iterator_next (&iter2, &elt2));
60 gl_oset_iterator_free (&iter1);
61 gl_oset_iterator_free (&iter2);
62 }
63
64 static void
65 check_all (gl_oset_t set1, gl_oset_t set2)
66 {
67 gl_avltree_oset_check_invariants (set2);
68 check_equals (set1, set2);
69 }
70
71 static bool
72 is_at_least (const void *elt, const void *threshold)
73 {
74 return strcmp ((const char *) elt, (const char *) threshold) >= 0;
75 }
76
77 int
78 main (int argc, char *argv[])
79 {
80 gl_oset_t set1, set2;
81
82
83 if (argc > 1)
84 srand (atoi (argv[1]));
85
86 {
87 size_t initial_size = RANDOM (20);
88 size_t i;
89 unsigned int repeat;
90
91
92 set1 = gl_oset_nx_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
93 ASSERT (set1 != NULL);
94
95
96 set2 = gl_oset_nx_create_empty (GL_AVLTREE_OSET, (gl_setelement_compar_fn) strcmp, NULL);
97 ASSERT (set2 != NULL);
98
99 check_all (set1, set2);
100
101
102 for (i = 0; i < initial_size; i++)
103 {
104 const char *obj = RANDOM_OBJECT ();
105 ASSERT (gl_oset_nx_add (set1, obj) == gl_oset_nx_add (set2, obj));
106 check_all (set1, set2);
107 }
108
109 for (repeat = 0; repeat < 100000; repeat++)
110 {
111 unsigned int operation = RANDOM (4);
112 switch (operation)
113 {
114 case 0:
115 {
116 const char *obj = RANDOM_OBJECT ();
117 ASSERT (gl_oset_search (set1, obj) == gl_oset_search (set2, obj));
118 }
119 break;
120 case 1:
121 {
122 const char *obj = RANDOM_OBJECT ();
123 ASSERT (gl_oset_nx_add (set1, obj) == gl_oset_nx_add (set2, obj));
124 }
125 break;
126 case 2:
127 {
128 const char *obj = RANDOM_OBJECT ();
129 ASSERT (gl_oset_remove (set1, obj) == gl_oset_remove (set2, obj));
130 }
131 break;
132 case 3:
133 {
134 const char *obj = RANDOM_OBJECT ();
135 gl_oset_iterator_t iter1 = gl_oset_iterator_atleast (set1, is_at_least, obj);
136 gl_oset_iterator_t iter2 = gl_oset_iterator_atleast (set2, is_at_least, obj);
137 const void *elt1;
138 const void *elt2;
139
140
141
142 bool havenext1 = gl_oset_iterator_next (&iter1, &elt1);
143 bool havenext2 = gl_oset_iterator_next (&iter2, &elt2);
144 ASSERT (havenext1 == havenext2);
145 if (havenext1)
146 {
147 ASSERT (elt1 == elt2);
148 havenext1 = gl_oset_iterator_next (&iter1, &elt1);
149 havenext2 = gl_oset_iterator_next (&iter2, &elt2);
150 ASSERT (havenext1 == havenext2);
151 if (havenext1)
152 ASSERT (elt1 == elt2);
153 }
154 gl_oset_iterator_free (&iter1);
155 gl_oset_iterator_free (&iter2);
156 }
157 break;
158 }
159 check_all (set1, set2);
160 }
161
162 gl_oset_free (set1);
163 gl_oset_free (set2);
164 }
165
166 test_update (GL_AVLTREE_OSET);
167
168 return 0;
169 }