Add methods for converting between Collections (asBag, asSet, etc)
[panda.git] / libs / libmpa / tester.c
blob082b64ab2b4662e3a89f15f9037c38d5796dbaa3
1 /* **************************************************************************
2 * Copyright (c) 2007, David Waite
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice
12 * this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * The name(s) of contributors may not be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 ************************************************************************* */
30 #include <stdint.h>
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include "ptr_array.h"
36 int main(int argc, char ** argv);
38 void test_new()
40 ptr_array p1 = ptr_array_new(10);
41 assert(p1 != NULL);
42 assert(p1->size >= 10);
43 assert(p1->array != NULL);
44 assert(p1->length == 0);
45 ptr_array_free(p1);
48 void test_new_oom()
50 fprintf(stderr, "Your malloc implementation may report an (ignorable) "
51 "error for this test\n");
52 ptr_array p1 = ptr_array_new(1000000000);
53 assert (p1 == NULL);
56 void test_free()
58 ptr_array p1 = ptr_array_new(1);
59 ptr_array_free(p1);
60 ptr_array p2 = NULL;
61 ptr_array_free(p2);
64 void test_new_zero()
66 ptr_array p1 = ptr_array_new(0);
67 assert(p1 != NULL);
68 assert(p1->size == 0);
69 assert(p1->array == NULL);
70 assert(p1->length == 0);
71 ptr_array_free(p1);
74 static ptr_array setup_data()
76 ptr_array p1 = ptr_array_new(4);
77 ptr_array_append(p1, &malloc);
78 ptr_array_append(p1, &realloc);
79 ptr_array_append(p1, &free);
80 ptr_array_append(p1, &main);
81 return p1;
84 void test_remove_ordered()
86 ptr_array p1 = setup_data();
87 void * value =
88 ptr_array_remove_ordered(p1, &realloc);
89 assert(value == &realloc);
90 assert(ptr_array_get_index(p1, 0) == &malloc);
91 assert(ptr_array_get_index(p1, 1) == &free);
92 assert(ptr_array_get_index(p1, 2) == &main);
93 assert(ptr_array_length(p1) == 3);
95 value =
96 ptr_array_remove_ordered(p1, &malloc);
97 assert(value == &malloc);
98 assert(ptr_array_get_index(p1, 0) == &free);
99 assert(ptr_array_get_index(p1, 1) == &main);
100 assert(ptr_array_length(p1) == 2);
102 value =
103 ptr_array_remove_ordered(p1, &main);
104 assert(value == &main);
105 assert(ptr_array_get_index(p1, 0) == &free);
106 assert(ptr_array_length(p1) == 1);
108 value =
109 ptr_array_remove_ordered(p1, &free);
110 assert (value == &free);
111 assert(ptr_array_length(p1) == 0);
114 void test_remove_ordered_nonexistant()
116 ptr_array p1 = setup_data();
117 void * value = ptr_array_remove_ordered(p1, "some random data");
118 assert(value == NULL);
121 void test_remove_index_ordered()
123 ptr_array p1 = setup_data();
124 void * value =
125 ptr_array_remove_index_ordered(p1, 1);
126 assert(value == &realloc);
127 assert(ptr_array_get_index(p1, 0) == &malloc);
128 assert(ptr_array_get_index(p1, 1) == &free);
129 assert(ptr_array_get_index(p1, 2) == &main);
130 assert(ptr_array_length(p1) == 3);
132 value =
133 ptr_array_remove_index_ordered(p1, 0);
134 assert(value == &malloc);
135 assert(ptr_array_get_index(p1, 0) == &free);
136 assert(ptr_array_get_index(p1, 1) == &main);
137 assert(ptr_array_length(p1) == 2);
139 value =
140 ptr_array_remove_index_ordered(p1, 1);
141 assert(value == &main);
142 assert(ptr_array_get_index(p1, 0) == &free);
143 assert(ptr_array_length(p1) == 1);
145 value =
146 ptr_array_remove_index_ordered(p1, 0);
147 assert (value == &free);
148 assert(ptr_array_length(p1) == 0);
150 void test_remove_fast()
152 ptr_array p1 = setup_data();
153 void * value =
154 ptr_array_remove_fast(p1, &realloc);
155 assert(value == &realloc);
156 assert(ptr_array_contains(p1, &malloc));
157 assert(ptr_array_contains(p1, &free));
158 assert(ptr_array_contains(p1, &main));
159 assert(ptr_array_length(p1) == 3);
161 value =
162 ptr_array_remove_fast(p1, &malloc);
163 assert(value == &malloc);
164 assert(ptr_array_contains(p1, &free));
165 assert(ptr_array_contains(p1, &main));
166 assert(ptr_array_length(p1) == 2);
168 value =
169 ptr_array_remove_fast(p1, &main);
170 assert(value == &main);
171 assert(ptr_array_contains(p1, &free));
172 assert(ptr_array_length(p1) == 1);
174 value =
175 ptr_array_remove_fast(p1, &free);
176 assert (value == &free);
177 assert(ptr_array_length(p1) == 0);
180 void test_remove_fast_nonexistant()
182 ptr_array p1 = setup_data();
183 void * value = ptr_array_remove_fast(p1, "some random data");
184 assert(value == NULL);
187 void test_remove_index_fast()
189 ptr_array p1 = setup_data();
190 void * value =
191 ptr_array_remove_index_fast(p1, 1);
192 assert(value == &realloc);
193 assert(ptr_array_contains(p1, &malloc));
194 assert(ptr_array_contains(p1, &free));
195 assert(ptr_array_contains(p1, &main));
196 assert(ptr_array_length(p1) == 3);
198 value =
199 ptr_array_remove_index_fast(p1, 0);
200 assert(value == &malloc);
201 assert(ptr_array_contains(p1, &free));
202 assert(ptr_array_contains(p1, &main));
203 assert(ptr_array_length(p1) == 2);
205 value =
206 ptr_array_remove_index_fast(p1, 1);
207 assert(value == &main);
208 assert(ptr_array_contains(p1, &free));
209 assert(ptr_array_length(p1) == 1);
211 value =
212 ptr_array_remove_index_fast(p1, 0);
213 assert (value == &free);
214 assert(ptr_array_length(p1) == 0);
217 void test_get_set_index()
219 ptr_array p1 = setup_data();
220 assert(ptr_array_get_index(p1, 0) == &malloc);
221 ptr_array_set_index(p1, 0, &realloc);
222 assert(ptr_array_get_index(p1, 0) == &realloc);
225 void test_array_contains()
227 ptr_array p1 = setup_data();
228 assert(ptr_array_contains(p1, &malloc));
229 assert(!ptr_array_contains(p1, &test_array_contains));
232 void test_array_length()
234 ptr_array p1 = setup_data();
235 assert(ptr_array_length(p1) == 4);
236 ptr_array_remove_index_fast(p1, 0);
237 assert(ptr_array_length(p1) == 3);
240 void test_array_append()
242 int i;
243 ptr_array p1 = ptr_array_new(0);
244 for (i = 0; i < 1000; i++)
245 ptr_array_append(p1, &test_array_append);
246 assert(ptr_array_length(p1) == 1000);
248 for (i = 0; i < 1000; i++)
249 assert(p1->array[i] == &test_array_append);
252 void test_array_clear()
254 ptr_array p1 = setup_data();
255 ptr_array_clear(p1);
256 assert(ptr_array_length(p1) == 0);
257 ptr_array_append(p1, 0);
258 assert(ptr_array_length(p1) == 1);
260 int main (int argc, char **argv)
262 test_new();
263 test_new_oom();
264 test_new_zero();
266 test_remove_ordered();
267 test_remove_ordered_nonexistant();
268 test_remove_index_ordered();
270 test_remove_fast();
271 test_remove_fast_nonexistant();
272 test_remove_index_fast();
274 test_get_set_index();
275 test_array_contains();
276 test_array_length();
278 test_array_append();
279 test_array_clear();
280 return 0;