gettext: Sync with gettext 0.23.
[gnulib.git] / tests / test-array_omap.c
blob2aabe7d53a491efea75ae5dd5a05da9d01c2850d
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_array_omap.h"
22 #include <stdlib.h>
23 #include <string.h>
25 #include "gl_xlist.h"
26 #include "gl_array_list.h"
27 #include "macros.h"
29 static const char *objects[30] =
31 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
32 "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
35 #define RANDOM(n) (rand () % (n))
36 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
38 static void
39 check_equals (gl_omap_t map1, gl_list_t keys, gl_list_t values)
41 size_t n = gl_omap_size (map1);
42 gl_omap_iterator_t iter1;
43 gl_list_iterator_t iterk, iterv;
44 const void *key1;
45 const void *value1;
46 const void *key2;
47 const void *value2;
48 gl_list_node_t node;
49 size_t i;
51 iter1 = gl_omap_iterator (map1);
52 iterk = gl_list_iterator (keys);
53 iterv = gl_list_iterator (values);
54 for (i = 0; i < n; i++)
56 ASSERT (gl_omap_iterator_next (&iter1, &key1, &value1));
57 ASSERT (gl_list_iterator_next (&iterk, &key2, &node));
58 ASSERT (gl_list_iterator_next (&iterv, &value2, &node));
59 ASSERT (key1 == key2);
60 ASSERT (value1 == value2);
62 ASSERT (!gl_omap_iterator_next (&iter1, &key1, &value1));
63 ASSERT (!gl_list_iterator_next (&iterk, &key2, &node));
64 ASSERT (!gl_list_iterator_next (&iterv, &value2, &node));
65 gl_omap_iterator_free (&iter1);
66 gl_list_iterator_free (&iterk);
67 gl_list_iterator_free (&iterv);
70 static void
71 check_all (gl_omap_t map1, gl_list_t keys, gl_list_t values)
73 check_equals (map1, keys, values);
76 int
77 main (int argc, char *argv[])
79 gl_omap_t map1;
80 gl_list_t keys;
81 gl_list_t values;
83 /* Allow the user to provide a non-default random seed on the command line. */
84 if (argc > 1)
85 srand (atoi (argv[1]));
88 size_t initial_size = RANDOM (20);
89 size_t i;
90 unsigned int repeat;
92 /* Create map1. */
93 map1 = gl_omap_nx_create_empty (GL_ARRAY_OMAP, (gl_mapkey_compar_fn) strcmp, NULL, NULL);
94 ASSERT (map1 != NULL);
96 /* Create keys and values. */
97 keys = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, false);
98 values = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, false);
100 check_all (map1, keys, values);
102 /* Initialize them. */
103 for (i = 0; i < initial_size; i++)
105 const char *key = RANDOM_OBJECT ();
106 const char *value = RANDOM_OBJECT ();
107 bool added = gl_omap_nx_put (map1, key, value);
108 size_t index = gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
109 ASSERT (added == (index == (size_t)(-1)));
110 if (added)
112 gl_sortedlist_add (keys, (gl_listelement_compar_fn)strcmp, key);
113 index = gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
114 gl_list_add_at (values, index, value);
116 else
117 gl_list_set_at (values, index, value);
118 check_all (map1, keys, values);
121 for (repeat = 0; repeat < 100000; repeat++)
123 unsigned int operation = RANDOM (3);
124 switch (operation)
126 case 0:
128 const char *key = RANDOM_OBJECT ();
129 const void *ret = gl_omap_get (map1, key);
130 size_t index =
131 gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
132 ASSERT (ret
133 == (index != (size_t)(-1) ? gl_list_get_at (values, index) : NULL));
135 break;
136 case 1:
138 const char *key = RANDOM_OBJECT ();
139 const char *value = RANDOM_OBJECT ();
140 bool added = gl_omap_nx_put (map1, key, value);
141 size_t index =
142 gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
143 ASSERT (added == (index == (size_t)(-1)));
144 if (added)
146 gl_sortedlist_add (keys, (gl_listelement_compar_fn)strcmp, key);
147 index = gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
148 gl_list_add_at (values, index, value);
150 else
151 gl_list_set_at (values, index, value);
153 break;
154 case 2:
156 const char *key = RANDOM_OBJECT ();
157 bool removed = gl_omap_remove (map1, key);
158 size_t index =
159 gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
160 ASSERT (removed == (index != (size_t)(-1)));
161 if (removed)
163 gl_list_remove_at (keys, index);
164 gl_list_remove_at (values, index);
167 break;
169 check_all (map1, keys, values);
172 gl_omap_free (map1);
173 gl_list_free (keys);
174 gl_list_free (values);
177 return test_exit_status;