maint.mk: Update system header list for #include syntax checks.
[gnulib.git] / tests / test-rbtree_omap.c
blobde346f0de63960dffdc056e79479e309265d2570
1 /* Test of ordered map data type implementation.
2 Copyright (C) 2006-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2018.
5 This program 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 This program 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 this program. If not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include "gl_rbtree_omap.h"
22 #include <stdlib.h>
23 #include <string.h>
25 #include "gl_array_omap.h"
26 #include "macros.h"
28 extern void gl_rbtree_omap_check_invariants (gl_omap_t set);
30 static const char *objects[30] =
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", "<", ">", "[", "]"
36 #define RANDOM(n) (rand () % (n))
37 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
39 static void
40 check_equals (gl_omap_t map1, gl_omap_t map2)
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;
50 iter1 = gl_omap_iterator (map1);
51 iter2 = gl_omap_iterator (map2);
52 for (i = 0; i < n; i++)
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);
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);
65 static void
66 check_all (gl_omap_t map1, gl_omap_t map2)
68 gl_rbtree_omap_check_invariants (map2);
69 check_equals (map1, map2);
72 int
73 main (int argc, char *argv[])
75 gl_omap_t map1, map2;
77 /* Allow the user to provide a non-default random seed on the command line. */
78 if (argc > 1)
79 srand (atoi (argv[1]));
82 size_t initial_size = RANDOM (20);
83 size_t i;
84 unsigned int repeat;
86 /* Create map1. */
87 map1 = gl_omap_nx_create_empty (GL_ARRAY_OMAP, (gl_mapkey_compar_fn) strcmp, NULL, NULL);
88 ASSERT (map1 != NULL);
90 /* Create map2. */
91 map2 = gl_omap_nx_create_empty (GL_RBTREE_OMAP, (gl_mapkey_compar_fn) strcmp, NULL, NULL);
92 ASSERT (map2 != NULL);
94 check_all (map1, map2);
96 /* Initialize them. */
97 for (i = 0; i < initial_size; i++)
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);
105 for (repeat = 0; repeat < 100000; repeat++)
107 unsigned int operation = RANDOM (3);
108 switch (operation)
110 case 0:
112 const char *key = RANDOM_OBJECT ();
113 ASSERT (gl_omap_get (map1, key) == gl_omap_get (map2, key));
115 break;
116 case 1:
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));
122 break;
123 case 2:
125 const char *key = RANDOM_OBJECT ();
126 ASSERT (gl_omap_remove (map1, key) == gl_omap_remove (map2, key));
128 break;
130 check_all (map1, map2);
133 gl_omap_free (map1);
134 gl_omap_free (map2);
137 return test_exit_status;