1 /* settest.c -- Test program for Khepera set routines
2  * Created: Wed Nov  9 15:04:25 1994 by faith@dict.org
3  * Copyright 1994-1996, 2002 Rickard E. Faith (faith@dict.org)
4  * Copyright 2002-2008 Aleksey Cheusov (vle@gmx.net)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26 
27 #include "maaP.h"
28 
29 extern void init_rand(void);
30 extern int get_rand(int ll, int ul);
31 
iterator(const void * key)32 static int iterator(const void *key)
33 {
34 	printf("%s\n", (char *) __UNCONST(key));
35 	return 0;
36 }
37 
freer(const void * key)38 static int freer(const void *key)
39 {
40 	xfree(__UNCONST(key));
41 	return 0;
42 }
43 
hsh_string_hash_32bit(const void * key)44 static unsigned long hsh_string_hash_32bit(const void *key)
45 {
46 	return hsh_string_hash(key) & 0xFFFFFFFFul;
47 }
48 
main(int argc,char ** argv)49 int main(int argc, char **argv)
50 {
51 	set_Set      t;
52 	set_Set      t1;
53 	set_Set      t2;
54 	int          i;
55 	int          j;
56 	int          count;
57 	set_Position p;
58 	const void   *k;
59 
60 	if (argc == 1) {
61 		count = 100;
62 	} else if (argc != 2) {
63 		fprintf(stderr, "usage: settest count\n");
64 		return 1;
65 	} else {
66 		count = atoi(argv[1]);
67 	}
68 
69 	printf("Running test for count of %d\n", count);
70 
71 	/* Test sequential keys */
72 	t = set_create(hsh_string_hash_32bit, NULL);
73 
74 	for (i = 0; i < count; i++) {
75 		char *key   = xmalloc(20);
76 
77 		sprintf(key, "key%d", i);
78 		set_insert(t, key);
79 	}
80 
81 	for (i = count; i >= 0; i--) {
82 		char        key[100];
83 
84 		sprintf(key, "key%d", i);
85 		if (!set_member(t, key))
86 			printf("\"%s\" is not a member of the set\n", key);
87 	}
88 
89 	if (count <= 200) set_iterate(t, iterator);
90 
91 	/*   set_print_stats(t, stdout);*/
92 
93 	set_iterate(t, freer);
94 	set_destroy(t);
95 
96 
97 
98 	/* Test random keys */
99 	t = set_create(hsh_string_hash_32bit, NULL);
100 
101 	init_rand();
102 	for (i = 0; i < count; i++) {
103 		int  len = get_rand(2, 32);
104 		char *key   = xmalloc(len + 1);
105 
106 		for (j = 0; j < len; j++) key[j] = get_rand(32, 128);
107 		key[ len ] = '\0';
108 		set_insert(t, key);
109 		printf("item%d = %s\n", i, key);
110 	}
111 
112 	init_rand();
113 	for (i = 0; i < count; i++) {
114 		int         len = get_rand(2, 32);
115 		char       *key = xmalloc(len + 1);
116 
117 		for (j = 0; j < len; j++) key[j] = get_rand(32, 128);
118 		key[ len ] = '\0';
119 		printf("lookup%d = %s\n", i, key);
120 		if (!set_member(t, key))
121 			printf("\"%s\" is not a member of the set", key);
122 
123 		xfree(key);
124 	}
125 
126 	/*   set_print_stats(t, stdout);*/
127 
128 	set_iterate(t, freer);
129 	set_destroy(t);
130 
131 
132 
133 	/* Test (random) integer keys */
134 	t = set_create(hsh_pointer_hash, hsh_pointer_compare);
135 
136 	init_rand();
137 	for (i = 0; i < count; i++) {
138 		long key    = get_rand(1, 16777216);
139 
140 		set_insert(t, (void *)key);
141 		printf("int%d = %ld\n", i, key);
142 	}
143 
144 	init_rand();
145 	for (i = 0; i < count; i++) {
146 		long        key = get_rand(1, 16777216);
147 
148 		printf("intlookup%d = %ld\n", i, key);
149 		if (!set_member(t, (void *)key))
150 			printf("%ld is not a member of the set", key);
151 	}
152 
153 	/*   set_print_stats(t, stdout);*/
154 
155 	set_destroy(t);
156 
157 	/* Test set operations */
158 
159 	t1 = set_create(hsh_string_hash_32bit, NULL);
160 	t2 = set_create(hsh_string_hash_32bit, NULL);
161 
162 	set_insert(t1, "foo");
163 	set_insert(t1, "bar");
164 
165 	set_insert(t2, "t2-foo");
166 	set_insert(t2, "bar");
167 
168 	printf("\nSet 1:\n");
169 	set_iterate(t1, iterator);
170 
171 
172 	printf("\nSet 1 (again):\n");
173 	SET_ITERATE(t1,p,k) printf("%s\n", (const char *)k);
174 
175 	printf("\nSet 2:\n");
176 	set_iterate(t2, iterator);
177 
178 	printf("Set 1 == Set 2 ? ===> %d\n", set_equal(t1, t2));
179 	printf("Set 1 == Set 1 ? ===> %d\n", set_equal(t1, t1));
180 
181 	printf("\nUnion:\n");
182 	t = set_union(t1, t2);
183 	set_iterate(t, iterator);
184 	set_destroy(t);
185 
186 	printf("\nIntersection:\n");
187 	t = set_inter(t1, t2);
188 	set_iterate(t, iterator);
189 	set_destroy(t);
190 
191 	printf("\nDifference:\n");
192 	t = set_diff(t1, t2);
193 	set_iterate(t, iterator);
194 	set_destroy(t);
195 
196 	set_destroy(t1);
197 	set_destroy(t2);
198 
199 	return 0;
200 }
201