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/>. */
20 #include "gl_array_omap.h"
26 #include "gl_array_list.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))]
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
;
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
);
71 check_all (gl_omap_t map1
, gl_list_t keys
, gl_list_t values
)
73 check_equals (map1
, keys
, values
);
77 main (int argc
, char *argv
[])
83 /* Allow the user to provide a non-default random seed on the command line. */
85 srand (atoi (argv
[1]));
88 size_t initial_size
= RANDOM (20);
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)));
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
);
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);
128 const char *key
= RANDOM_OBJECT ();
129 const void *ret
= gl_omap_get (map1
, key
);
131 gl_sortedlist_indexof (keys
, (gl_listelement_compar_fn
)strcmp
, key
);
133 == (index
!= (size_t)(-1) ? gl_list_get_at (values
, index
) : NULL
));
138 const char *key
= RANDOM_OBJECT ();
139 const char *value
= RANDOM_OBJECT ();
140 bool added
= gl_omap_nx_put (map1
, key
, value
);
142 gl_sortedlist_indexof (keys
, (gl_listelement_compar_fn
)strcmp
, key
);
143 ASSERT (added
== (index
== (size_t)(-1)));
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
);
151 gl_list_set_at (values
, index
, value
);
156 const char *key
= RANDOM_OBJECT ();
157 bool removed
= gl_omap_remove (map1
, key
);
159 gl_sortedlist_indexof (keys
, (gl_listelement_compar_fn
)strcmp
, key
);
160 ASSERT (removed
== (index
!= (size_t)(-1)));
163 gl_list_remove_at (keys
, index
);
164 gl_list_remove_at (values
, index
);
169 check_all (map1
, keys
, values
);
174 gl_list_free (values
);
177 return test_exit_status
;