This source file includes following definitions.
- 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_avltree_omap.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "gl_array_omap.h"
26 #include "macros.h"
27
28 extern void gl_avltree_omap_check_invariants (gl_omap_t set);
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 void
40 check_equals (gl_omap_t map1, gl_omap_t map2)
41 {
42 size_t n = gl_omap_size (map1);
43 gl_omap_iterator_t iter1, iter2;
44 const void *key1;
45 const void *value1;
46 const void *key2;
47 const void *value2;
48 size_t i;
49
50 iter1 = gl_omap_iterator (map1);
51 iter2 = gl_omap_iterator (map2);
52 for (i = 0; i < n; i++)
53 {
54 ASSERT (gl_omap_iterator_next (&iter1, &key1, &value1));
55 ASSERT (gl_omap_iterator_next (&iter2, &key2, &value2));
56 ASSERT (key1 == key2);
57 ASSERT (value1 == value2);
58 }
59 ASSERT (!gl_omap_iterator_next (&iter1, &key1, &value1));
60 ASSERT (!gl_omap_iterator_next (&iter2, &key2, &value2));
61 gl_omap_iterator_free (&iter1);
62 gl_omap_iterator_free (&iter2);
63 }
64
65 static void
66 check_all (gl_omap_t map1, gl_omap_t map2)
67 {
68 gl_avltree_omap_check_invariants (map2);
69 check_equals (map1, map2);
70 }
71
72 int
73 main (int argc, char *argv[])
74 {
75 gl_omap_t map1, map2;
76
77
78 if (argc > 1)
79 srand (atoi (argv[1]));
80
81 {
82 size_t initial_size = RANDOM (20);
83 size_t i;
84 unsigned int repeat;
85
86
87 map1 = gl_omap_nx_create_empty (GL_ARRAY_OMAP, (gl_mapkey_compar_fn) strcmp, NULL, NULL);
88 ASSERT (map1 != NULL);
89
90
91 map2 = gl_omap_nx_create_empty (GL_AVLTREE_OMAP, (gl_mapkey_compar_fn) strcmp, NULL, NULL);
92 ASSERT (map2 != NULL);
93
94 check_all (map1, map2);
95
96
97 for (i = 0; i < initial_size; i++)
98 {
99 const char *key = RANDOM_OBJECT ();
100 const char *value = RANDOM_OBJECT ();
101 ASSERT (gl_omap_nx_put (map1, key, value) == gl_omap_nx_put (map2, key, value));
102 check_all (map1, map2);
103 }
104
105 for (repeat = 0; repeat < 100000; repeat++)
106 {
107 unsigned int operation = RANDOM (3);
108 switch (operation)
109 {
110 case 0:
111 {
112 const char *key = RANDOM_OBJECT ();
113 ASSERT (gl_omap_get (map1, key) == gl_omap_get (map2, key));
114 }
115 break;
116 case 1:
117 {
118 const char *key = RANDOM_OBJECT ();
119 const char *value = RANDOM_OBJECT ();
120 ASSERT (gl_omap_nx_put (map1, key, value) == gl_omap_nx_put (map2, key, value));
121 }
122 break;
123 case 2:
124 {
125 const char *key = RANDOM_OBJECT ();
126 ASSERT (gl_omap_remove (map1, key) == gl_omap_remove (map2, key));
127 }
128 break;
129 }
130 check_all (map1, map2);
131 }
132
133 gl_omap_free (map1);
134 gl_omap_free (map2);
135 }
136
137 return 0;
138 }