annotate tests/test-avltree_omap.c @ 40196:e63f5d3edab5

relocatable-prog: Update documentation. * doc/relocatable-maint.texi (Supporting Relocation): Update to match the recent changes.
author Bruno Haible <bruno@clisp.org>
date Sun, 24 Feb 2019 01:49:15 +0100
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40010
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Test of ordered map data type implementation.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 40010
diff changeset
2 Copyright (C) 2006-2019 Free Software Foundation, Inc.
40010
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2018.
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #include <config.h>
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include "gl_avltree_omap.h"
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <stdlib.h>
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <string.h>
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include "gl_array_omap.h"
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include "macros.h"
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 extern void gl_avltree_omap_check_invariants (gl_omap_t set);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 static const char *objects[30] =
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 {
f7c78ac940ad avltree-omap: 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",
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 };
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 #define RANDOM(n) (rand () % (n))
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 static void
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 check_equals (gl_omap_t map1, gl_omap_t map2)
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 {
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 size_t n = gl_omap_size (map1);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 gl_omap_iterator_t iter1, iter2;
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 const void *key1;
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 const void *value1;
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 const void *key2;
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 const void *value2;
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 size_t i;
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 iter1 = gl_omap_iterator (map1);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 iter2 = gl_omap_iterator (map2);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 for (i = 0; i < n; i++)
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 {
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 ASSERT (gl_omap_iterator_next (&iter1, &key1, &value1));
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 ASSERT (gl_omap_iterator_next (&iter2, &key2, &value2));
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 ASSERT (key1 == key2);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 ASSERT (value1 == value2);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 }
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 ASSERT (!gl_omap_iterator_next (&iter1, &key1, &value1));
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 ASSERT (!gl_omap_iterator_next (&iter2, &key2, &value2));
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 gl_omap_iterator_free (&iter1);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 gl_omap_iterator_free (&iter2);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 }
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 static void
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 check_all (gl_omap_t map1, gl_omap_t map2)
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 {
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 gl_avltree_omap_check_invariants (map2);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 check_equals (map1, map2);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 }
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 int
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 main (int argc, char *argv[])
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 {
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 gl_omap_t map1, map2;
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 /* Allow the user to provide a non-default random seed on the command line. */
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 if (argc > 1)
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 srand (atoi (argv[1]));
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 {
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 size_t initial_size = RANDOM (20);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 size_t i;
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 unsigned int repeat;
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 /* Create map1. */
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 map1 = gl_omap_nx_create_empty (GL_ARRAY_OMAP, (gl_mapkey_compar_fn) strcmp, NULL, NULL);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 ASSERT (map1 != NULL);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 /* Create map2. */
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 map2 = gl_omap_nx_create_empty (GL_AVLTREE_OMAP, (gl_mapkey_compar_fn) strcmp, NULL, NULL);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 ASSERT (map2 != NULL);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 check_all (map1, map2);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 /* Initialize them. */
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 for (i = 0; i < initial_size; i++)
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 {
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 const char *key = RANDOM_OBJECT ();
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 const char *value = RANDOM_OBJECT ();
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 ASSERT (gl_omap_nx_put (map1, key, value) == gl_omap_nx_put (map2, key, value));
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 check_all (map1, map2);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 }
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 for (repeat = 0; repeat < 100000; repeat++)
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 {
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 unsigned int operation = RANDOM (3);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 switch (operation)
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 {
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 case 0:
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 {
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 const char *key = RANDOM_OBJECT ();
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 ASSERT (gl_omap_get (map1, key) == gl_omap_get (map2, key));
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 }
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 break;
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 case 1:
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 {
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 const char *key = RANDOM_OBJECT ();
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 const char *value = RANDOM_OBJECT ();
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 ASSERT (gl_omap_nx_put (map1, key, value) == gl_omap_nx_put (map2, key, value));
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 }
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 break;
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 case 2:
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 {
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 const char *key = RANDOM_OBJECT ();
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 ASSERT (gl_omap_remove (map1, key) == gl_omap_remove (map2, key));
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 }
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 break;
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 }
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 check_all (map1, map2);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 }
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 gl_omap_free (map1);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 gl_omap_free (map2);
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 }
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 return 0;
f7c78ac940ad avltree-omap: Add tests.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 }