1 /* Test of ordered map data type implementation.
2 Copyright (C) 2006-2020 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_rbtree_omap.h"
25 #include "gl_array_omap.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))]
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
;
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
);
66 check_all (gl_omap_t map1
, gl_omap_t map2
)
68 gl_rbtree_omap_check_invariants (map2
);
69 check_equals (map1
, map2
);
73 main (int argc
, char *argv
[])
77 /* Allow the user to provide a non-default random seed on the command line. */
79 srand (atoi (argv
[1]));
82 size_t initial_size
= RANDOM (20);
87 map1
= gl_omap_nx_create_empty (GL_ARRAY_OMAP
, (gl_mapkey_compar_fn
) strcmp
, NULL
, NULL
);
88 ASSERT (map1
!= NULL
);
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);
112 const char *key
= RANDOM_OBJECT ();
113 ASSERT (gl_omap_get (map1
, key
) == gl_omap_get (map2
, key
));
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
));
125 const char *key
= RANDOM_OBJECT ();
126 ASSERT (gl_omap_remove (map1
, key
) == gl_omap_remove (map2
, key
));
130 check_all (map1
, map2
);