1 /* **************************************************************************
2 * Copyright (c) 2007, David Waite
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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 ************************************************************************* */
34 #include "ptr_array.h"
36 int main(int argc
, char ** argv
);
40 ptr_array p1
= ptr_array_new(10);
42 assert(p1
->size
>= 10);
43 assert(p1
->array
!= NULL
);
44 assert(p1
->length
== 0);
50 fprintf(stderr
, "Your malloc implementation may report an (ignorable) "
51 "error for this test\n");
52 ptr_array p1
= ptr_array_new(1000000000);
58 ptr_array p1
= ptr_array_new(1);
66 ptr_array p1
= ptr_array_new(0);
68 assert(p1
->size
== 0);
69 assert(p1
->array
== NULL
);
70 assert(p1
->length
== 0);
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
);
84 void test_remove_ordered()
86 ptr_array p1
= setup_data();
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);
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);
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);
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();
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);
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);
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);
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();
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);
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);
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);
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();
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);
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);
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);
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()
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();
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
)
266 test_remove_ordered();
267 test_remove_ordered_nonexistant();
268 test_remove_index_ordered();
271 test_remove_fast_nonexistant();
272 test_remove_index_fast();
274 test_get_set_index();
275 test_array_contains();