This source file includes following definitions.
- cmp_objects_in_array
- check_equals
- check_all
- 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_array_set.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "gl_xoset.h"
26 #include "gl_array_oset.h"
27 #include "xalloc.h"
28 #include "macros.h"
29
30 static const char *objects[30] =
31 {
32 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
33 "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
34 };
35
36 #define RANDOM(n) (rand () % (n))
37 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
38
39 static int
40 cmp_objects_in_array (const void *objptr1, const void *objptr2)
41 {
42 const void *obj1 = *(const void * const *)objptr1;
43 const void *obj2 = *(const void * const *)objptr2;
44 return strcmp ((const char *) obj1, (const char *) obj2);
45 }
46
47 static void
48 check_equals (gl_set_t set1, gl_oset_t set2)
49 {
50 size_t n = gl_set_size (set1);
51 const void **elements_of_set1 = XNMALLOC (n, const void *);
52 const void **elements_of_set2 = XNMALLOC (n, const void *);
53
54 gl_set_iterator_t iter1;
55 gl_oset_iterator_t iter2;
56 const void *elt1;
57 const void *elt2;
58 size_t i;
59
60 iter1 = gl_set_iterator (set1);
61 iter2 = gl_oset_iterator (set2);
62 for (i = 0; i < n; i++)
63 {
64 ASSERT (gl_set_iterator_next (&iter1, &elt1));
65 ASSERT (gl_oset_iterator_next (&iter2, &elt2));
66 elements_of_set1[i] = elt1;
67 elements_of_set2[i] = elt2;
68 }
69 ASSERT (!gl_set_iterator_next (&iter1, &elt1));
70 ASSERT (!gl_oset_iterator_next (&iter2, &elt2));
71 gl_set_iterator_free (&iter1);
72 gl_oset_iterator_free (&iter2);
73
74 if (n > 0)
75 {
76 qsort (elements_of_set1, n, sizeof (const void *), cmp_objects_in_array);
77 qsort (elements_of_set2, n, sizeof (const void *), cmp_objects_in_array);
78 }
79 for (i = 0; i < n; i++)
80 ASSERT (elements_of_set1[i] == elements_of_set2[i]);
81 free (elements_of_set2);
82 free (elements_of_set1);
83 }
84
85 static void
86 check_all (gl_set_t set1, gl_oset_t set2)
87 {
88 check_equals (set1, set2);
89 }
90
91 int
92 main (int argc, char *argv[])
93 {
94 gl_set_t set1;
95 gl_oset_t set2;
96
97
98 if (argc > 1)
99 srand (atoi (argv[1]));
100
101 {
102 size_t initial_size = RANDOM (20);
103 size_t i;
104 unsigned int repeat;
105
106
107 set1 = gl_set_nx_create_empty (GL_ARRAY_SET, NULL, NULL, NULL);
108 ASSERT (set1 != NULL);
109
110
111 set2 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
112
113 check_all (set1, set2);
114
115
116 for (i = 0; i < initial_size; i++)
117 {
118 const char *obj = RANDOM_OBJECT ();
119 ASSERT (gl_set_nx_add (set1, obj) == gl_oset_add (set2, obj));
120 check_all (set1, set2);
121 }
122
123 for (repeat = 0; repeat < 100000; repeat++)
124 {
125 unsigned int operation = RANDOM (3);
126 switch (operation)
127 {
128 case 0:
129 {
130 const char *obj = RANDOM_OBJECT ();
131 ASSERT (gl_set_search (set1, obj) == gl_oset_search (set2, obj));
132 }
133 break;
134 case 1:
135 {
136 const char *obj = RANDOM_OBJECT ();
137 ASSERT (gl_set_nx_add (set1, obj) == gl_oset_add (set2, obj));
138 }
139 break;
140 case 2:
141 {
142 const char *obj = RANDOM_OBJECT ();
143 ASSERT (gl_set_remove (set1, obj) == gl_oset_remove (set2, obj));
144 }
145 break;
146 }
147 check_all (set1, set2);
148 }
149
150 gl_set_free (set1);
151 gl_oset_free (set2);
152 }
153
154 return 0;
155 }