annotate tests/test-array_map.c @ 40174:b76c7bdde2bf

*-map tests: Fix compilation error. * tests/test-array_map.c: Include <limits.h>, for CHAR_BIT. * tests/test-hash_map.c: Likewise. * tests/test-linkedhash_map.c: Likewise.
author Colin Watson <cjwatson@debian.org>
date Sat, 02 Feb 2019 16:12:09 +0100
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40022
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Test of map data type implementation.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 40022
diff changeset
2 Copyright (C) 2006-2019 Free Software Foundation, Inc.
40022
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2018.
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #include <config.h>
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include "gl_array_map.h"
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
40174
b76c7bdde2bf *-map tests: Fix compilation error.
Colin Watson <cjwatson@debian.org>
parents: 40057
diff changeset
22 #include <limits.h>
40022
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <stdlib.h>
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <string.h>
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include "gl_xlist.h"
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include "gl_array_list.h"
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include "macros.h"
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 static const char *objects[30] =
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 };
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 #define SIZE_BITS (sizeof (size_t) * CHAR_BIT)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 static bool
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 string_equals (const void *x1, const void *x2)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 const char *s1 = x1;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 const char *s2 = x2;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 return strcmp (s1, s2) == 0;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 /* A hash function for NUL-terminated char* strings using
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 the method described by Bruno Haible.
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 See https://www.haible.de/bruno/hashfunc.html. */
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 static size_t
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 string_hash (const void *x)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 const char *s = x;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 size_t h = 0;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 for (; *s; s++)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 h = *s + ((h << 9) | (h >> (SIZE_BITS - 9)));
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 return h;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 #define RANDOM(n) (rand () % (n))
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 struct pair
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 const void *key;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 const void *value;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 };
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 static int
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 cmp_pairs_in_array (const void *pairptr1, const void *pairptr2)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 const void *key1 = ((struct pair const *)pairptr1)->key;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 const void *key2 = ((struct pair const *)pairptr2)->key;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 return strcmp ((const char *) key1, (const char *) key2);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 static void
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 check_equals (gl_map_t map1, gl_list_t keys, gl_list_t values)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 size_t n = gl_map_size (map1);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 struct pair *pairs_of_map1 = XNMALLOC (n, struct pair);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 gl_map_iterator_t iter1;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 const void *key1;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 const void *value1;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 size_t i;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 ASSERT (gl_list_size (keys) == n);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 ASSERT (gl_list_size (values) == n);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 iter1 = gl_map_iterator (map1);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 for (i = 0; i < n; i++)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 ASSERT (gl_map_iterator_next (&iter1, &key1, &value1));
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 pairs_of_map1[i].key = key1;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 pairs_of_map1[i].value = value1;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 ASSERT (!gl_map_iterator_next (&iter1, &key1, &value1));
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 gl_map_iterator_free (&iter1);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 if (n > 0)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 qsort (pairs_of_map1, n, sizeof (struct pair), cmp_pairs_in_array);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 for (i = 0; i < n; i++)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 ASSERT (pairs_of_map1[i].key == gl_list_get_at (keys, i));
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 ASSERT (pairs_of_map1[i].value == gl_list_get_at (values, i));
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 free (pairs_of_map1);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 static void
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 check_all (gl_map_t map1, gl_list_t keys, gl_list_t values)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 check_equals (map1, keys, values);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 int
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 main (int argc, char *argv[])
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 gl_map_t map1;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 gl_list_t keys;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 gl_list_t values;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 /* Allow the user to provide a non-default random seed on the command line. */
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 if (argc > 1)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 srand (atoi (argv[1]));
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 size_t initial_size = RANDOM (20);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 size_t i;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 unsigned int repeat;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 /* Create map1. */
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 map1 = gl_map_nx_create_empty (GL_ARRAY_MAP,
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 string_equals, string_hash, NULL, NULL);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 ASSERT (map1 != NULL);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 /* Create keys and values. */
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 keys = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, false);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 values = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, false);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 check_all (map1, keys, values);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 /* Initialize them. */
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 for (i = 0; i < initial_size; i++)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 const char *key = RANDOM_OBJECT ();
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 const char *value = RANDOM_OBJECT ();
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 bool added = gl_map_nx_put (map1, key, value);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 size_t index = gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 ASSERT (added == (index == (size_t)(-1)));
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 if (added)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 gl_sortedlist_add (keys, (gl_listelement_compar_fn)strcmp, key);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 index = gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 gl_list_add_at (values, index, value);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 else
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 gl_list_set_at (values, index, value);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 check_all (map1, keys, values);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 for (repeat = 0; repeat < 100000; repeat++)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 unsigned int operation = RANDOM (3);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 switch (operation)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 case 0:
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 const char *key = RANDOM_OBJECT ();
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 const void *ret = gl_map_get (map1, key);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 size_t index =
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 ASSERT (ret
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 == (index != (size_t)(-1) ? gl_list_get_at (values, index) : NULL));
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 break;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 case 1:
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 const char *key = RANDOM_OBJECT ();
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 const char *value = RANDOM_OBJECT ();
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 bool added = gl_map_nx_put (map1, key, value);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 size_t index =
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 ASSERT (added == (index == (size_t)(-1)));
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 if (added)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 gl_sortedlist_add (keys, (gl_listelement_compar_fn)strcmp, key);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 index = gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 gl_list_add_at (values, index, value);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 else
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 gl_list_set_at (values, index, value);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 break;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 case 2:
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 const char *key = RANDOM_OBJECT ();
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 bool removed = gl_map_remove (map1, key);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 size_t index =
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 ASSERT (removed == (index != (size_t)(-1)));
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 if (removed)
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 {
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 gl_list_remove_at (keys, index);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 gl_list_remove_at (values, index);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 break;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211 check_all (map1, keys, values);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 gl_map_free (map1);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 gl_list_free (keys);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 gl_list_free (values);
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 }
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 return 0;
df91ed80f476 array-map: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 }