1 /*
2 * Copyright (c) 2010, Texas Instruments Incorporated
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
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * */
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #define __DEBUG__
37 #define __DEBUG_ASSERT__
38 #define __DEBUG_ENTRY__
40 #include <tiler_utils/utils.h>
41 #include <tiler_utils/list_utils.h>
42 #include <tiler_utils/debug_utils.h>
43 #include "testlib.h"
45 #define TESTS\
46 T(test_new())\
47 T(test_list())\
48 T(test_ezlist())\
49 T(test_dzlist())\
50 T(test_plist())\
51 T(test_mlist())\
52 T(test_math())
54 #define F() in = head.next;
55 #define N(a) res |= NOT_P(in,!=,&head); \
56 res |= NOT_P(in->me,!=,NULL) || NOT_I(in->me->data,==,a); in = in->next;
57 #define Z(a) res |= NOT_P(in,!=,&head); \
58 res |= NOT_I(in->data,==,a); in = in->next;
59 #define L() res |= NOT_P(in,==,&head);
61 int all_zero(int *p, int len)
62 {
63 IN;
64 int ix = 0;
65 for (ix = 0; ix < len; ix ++)
66 {
67 if (p[ix])
68 {
69 P(DEBUG_TRACE_INFO, "[%d]=%d\n", ix, p[ix]);
70 return R_I(1);
71 }
72 }
73 return R_I(0);
74 }
76 int test_new() {
77 IN;
78 int *p;
79 p = NEW(int);
80 int res = NOT_I(all_zero(p, 1),==,0);
81 FREE(p);
82 res |= NOT_I(p,==,NULL);
83 p = NEWN(int, 8000);
84 res |= NOT_I(all_zero(p, 8000),==,0);
85 FREE(p);
86 p = NEWN(int, 1000000);
87 res |= NOT_I(all_zero(p, 1000000),==,0);
88 FREE(p);
89 return R_I(res);
90 }
92 int test_list() {
93 IN;
95 struct elem {
96 int data;
97 } *elA, *elB;
98 struct list {
99 struct elem *me;
100 struct list *last, *next;
101 } head, *inA, *inB, *in, *in_safe;
103 /* initialization */
104 DLIST_INIT(head);
105 int res = NOT_I(DLIST_IS_EMPTY(head),!=,0);
107 /* add element at beginning of list */
108 elA = NEW(struct elem);
109 elA->data = 1;
110 inA = NEW(struct list);
111 DLIST_ADD_AFTER(head, elA, *inA);
112 F()N(1)L();
114 /* add element after an element */
115 elB = NEW(struct elem);
116 elB->data = 2;
117 inB = NEW(struct list);
118 DLIST_ADD_AFTER(*inA, elB, *inB);
119 F()N(1)N(2)L();
121 /* add element at the end of the list */
122 elB = NEW(struct elem);
123 inB = NEW(struct list);
124 (DLIST_ADD_BEFORE(head, elB, *inB))->data = 3;
125 F()N(1)N(2)N(3)L();
127 /* move an element to another position or another list */
128 DLIST_MOVE_AFTER(head, *inB);
129 F()N(3)N(1)N(2)L();
131 DLIST_MOVE_BEFORE(head, *inB);
132 F()N(1)N(2)N(3)L();
134 /* works even if the position is the same */
135 DLIST_MOVE_BEFORE(head, *inB);
136 F()N(1)N(2)N(3)L();
138 res |= NOT_I(DLIST_FIRST(head)->data,==,1);
139 res |= NOT_I(DLIST_LAST(head)->data,==,3);
141 DLIST_LOOP(head, in) {
142 P(DEBUG_TRACE_INFO, "%d", in->me->data);
143 }
144 P(DEBUG_TRACE_INFO, ".");
146 /* remove elements */
147 DLIST_SAFE_LOOP(head, in, in_safe) {
148 if (in->me->data > 1)
149 {
150 DLIST_REMOVE(*in);
151 FREE(in->me);
152 FREE(in);
153 }
154 }
155 F()N(1)L();
157 /* delete list */
158 DLIST_SAFE_LOOP(head, in, in_safe) {
159 DLIST_REMOVE(*in);
160 FREE(in->me);
161 FREE(in);
162 }
163 F()L();
165 return R_I(res);
166 }
168 int test_ezlist() {
169 IN;
171 struct elem {
172 int data;
173 struct elem *me, *last, *next;
174 } *elA, *elB, head, *el, *el_safe, *in;
176 /* initialization */
177 DLIST_INIT(head);
178 int res = NOT_I(DLIST_IS_EMPTY(head),!=,0);
180 /* add element at beginning of list */
181 elA = NEW(struct elem);
182 elA->data = 1;
183 DLIST_ADD_AFTER(head, elA, *elA);
184 F()N(1)L();
186 /* add element after an element */
187 elB = NEW(struct elem);
188 elB->data = 2;
189 DLIST_ADD_AFTER(*elA, elB, *elB);
190 F()N(1)N(2)L();
192 /* add element at the end of the list */
193 elB = NEW(struct elem);
194 (DLIST_ADD_BEFORE(head, elB, *elB))->data = 3;
195 F()N(1)N(2)N(3)L();
197 /* move an element to another position or another list */
198 DLIST_MOVE_AFTER(head, *elB);
199 F()N(3)N(1)N(2)L();
201 DLIST_MOVE_BEFORE(head, *elB);
202 F()N(1)N(2)N(3)L();
204 /* works even if the position is the same */
205 DLIST_MOVE_BEFORE(head, *elB);
206 F()N(1)N(2)N(3)L();
208 res |= NOT_I(DLIST_FIRST(head)->data,==,1);
209 res |= NOT_I(DLIST_LAST(head)->data,==,3);
211 DLIST_LOOP(head, el) {
212 P(DEBUG_TRACE_INFO, "%d", el->data);
213 }
214 P(DEBUG_TRACE_INFO, ".");
216 /* remove elements */
217 DLIST_SAFE_RLOOP(head, el, el_safe) {
218 if (el->me->data == 1)
219 {
220 DLIST_REMOVE(*el);
221 FREE(el);
222 }
223 }
224 F()N(2)N(3)L();
226 /* delete list */
227 DLIST_SAFE_LOOP(head, el, el_safe) {
228 DLIST_REMOVE(*el);
229 FREE(el);
230 }
231 F()L();
233 return R_I(res);
234 }
236 int test_dzlist() {
237 IN;
239 struct elem {
240 int data;
241 struct elem *last, *next;
242 } *elA, *elB, head, *el, *el_safe, *in;
244 /* initialization */
245 DZLIST_INIT(head);
246 int res = NOT_I(DZLIST_IS_EMPTY(head),!=,0);
248 /* add element at beginning of list */
249 elA = NEW(struct elem);
250 elA->data = 1;
251 DZLIST_ADD_AFTER(head, *elA);
252 F()Z(1)L();
254 /* add element after an element */
255 elB = NEW(struct elem);
256 elB->data = 2;
257 DZLIST_ADD_AFTER(*elA, *elB);
258 F()Z(1)Z(2)L();
260 /* add element at the end of the list */
261 elB = NEW(struct elem);
262 (DZLIST_ADD_BEFORE(head, *elB))->data = 3;
263 F()Z(1)Z(2)Z(3)L();
265 /* move an element to another position or another list */
266 DZLIST_MOVE_AFTER(head, *elB);
267 F()Z(3)Z(1)Z(2)L();
269 DZLIST_MOVE_BEFORE(head, *elB);
270 F()Z(1)Z(2)Z(3)L();
272 /* works even if the position is the same */
273 DZLIST_MOVE_BEFORE(head, *elB);
274 F()Z(1)Z(2)Z(3)L();
276 res |= NOT_I(DZLIST_FIRST(head)->data,==,1);
277 res |= NOT_I(DZLIST_LAST(head)->data,==,3);
279 DZLIST_LOOP(head, el) {
280 P(DEBUG_TRACE_INFO, "%d", el->data);
281 }
282 P(DEBUG_TRACE_INFO, ".");
284 /* remove elements */
285 DZLIST_SAFE_RLOOP(head, el, el_safe) {
286 if (el->data == 1)
287 {
288 DZLIST_REMOVE(*el);
289 FREE(el);
290 }
291 }
292 F()Z(2)Z(3)L();
294 /* delete list */
295 DZLIST_SAFE_LOOP(head, el, el_safe) {
296 DZLIST_REMOVE(*el);
297 FREE(el);
298 }
299 F()L();
301 return R_I(res);
302 }
304 int test_plist() {
305 IN;
307 struct elem;
308 struct list {
309 struct elem *me;
310 struct list *last, *next;
311 } head, *inA, *inB, *in;
312 struct elem {
313 int data;
314 struct list *list_data;
315 } *elA, *elB, *el, *el_safe;
317 /* initialization */
318 DLIST_INIT(head);
319 int res = NOT_I(DLIST_IS_EMPTY(head),!=,0);
321 /* add element at beginning of list */
322 elA = NEW(struct elem);
323 elA->data = 1;
324 inA = NEW(struct list);
325 DLIST_PADD_AFTER(head, elA, inA, list_data);
326 F()N(1)L();
328 /* add element after an element */
329 elB = NEW(struct elem);
330 elB->data = 2;
331 inB = NEW(struct list);
332 DLIST_PADD_AFTER(*inA, elB, inB, list_data);
333 F()N(1)N(2)L();
335 /* add element at the end of the list */
336 elB = NEW(struct elem);
337 inB = NEW(struct list);
338 (DLIST_PADD_BEFORE(head, elB, inB, list_data))->data = 3;
339 F()N(1)N(2)N(3)L();
341 /* move an element to another position or another list */
342 DLIST_MOVE_AFTER(head, *inB);
343 F()N(3)N(1)N(2)L();
345 DLIST_MOVE_BEFORE(head, *inB);
346 F()N(1)N(2)N(3)L();
348 /* works even if the position is the same */
349 DLIST_MOVE_BEFORE(head, *inB);
350 F()N(1)N(2)N(3)L();
352 res |= NOT_I(DLIST_FIRST(head)->data,==,1);
353 res |= NOT_I(DLIST_LAST(head)->data,==,3);
355 DLIST_LOOP(head, in) {
356 P(DEBUG_TRACE_INFO, "%d", in->me->data);
357 }
358 P(DEBUG_TRACE_INFO, ".");
359 DLIST_PLOOP(head, el, list_data) {
360 P(DEBUG_TRACE_INFO, "%d", el->data);
361 }
362 P(DEBUG_TRACE_INFO, ".");
364 /* remove elements */
365 DLIST_SAFE_PLOOP(head, el, el_safe, list_data) {
366 if (el->data == 2)
367 {
368 DLIST_REMOVE(*el->list_data);
369 FREE(el->list_data);
370 FREE(el);
371 }
372 }
373 F()N(1)N(3)L();
375 /* delete list */
376 DLIST_SAFE_PLOOP(head, el, el_safe, list_data) {
377 DLIST_REMOVE(*el->list_data);
378 FREE(el->list_data);
379 FREE(el);
380 }
381 F()L();
383 return R_I(res);
384 }
386 int test_mlist() {
387 IN;
389 struct elem {
390 int data;
391 struct list {
392 struct list *last, *next;
393 struct elem *me;
394 } list_data;
395 } *elA, *elB, *el, *el_safe;
396 struct list head, *in;
398 /* initialization */
399 DLIST_INIT(head);
400 int res = NOT_I(DLIST_IS_EMPTY(head),!=,0);
402 /* add element at beginning of list */
403 elA = NEW(struct elem);
404 elA->data = 1;
405 DLIST_MADD_AFTER(head, elA, list_data);
406 F()N(1)L();
408 /* add element after an element */
409 elB = NEW(struct elem);
410 elB->data = 2;
411 DLIST_MADD_AFTER(elA->list_data, elB, list_data);
412 F()N(1)N(2)L();
414 /* add element at the end of the list */
415 elB = NEW(struct elem);
416 (DLIST_MADD_BEFORE(head, elB, list_data))->data = 3;
417 F()N(1)N(2)N(3)L();
419 /* move an element to another position or another list */
420 DLIST_MOVE_AFTER(head, elB->list_data);
421 F()N(3)N(1)N(2)L();
423 DLIST_MOVE_BEFORE(head, elB->list_data);
424 F()N(1)N(2)N(3)L();
426 /* works even if the position is the same */
427 DLIST_MOVE_BEFORE(head, elB->list_data);
428 F()N(1)N(2)N(3)L();
430 res |= NOT_I(DLIST_FIRST(head)->data,==,1);
431 res |= NOT_I(DLIST_LAST(head)->data,==,3);
433 DLIST_LOOP(head, in) {
434 P(DEBUG_TRACE_INFO, "%d", in->me->data);
435 }
436 P(DEBUG_TRACE_INFO, ".");
437 DLIST_MLOOP(head, el, list_data) {
438 P(DEBUG_TRACE_INFO, "%d", el->data);
439 }
440 P(DEBUG_TRACE_INFO, ".");
442 /* remove elements */
443 DLIST_SAFE_MLOOP(head, el, el_safe, list_data) {
444 if (el->data != 2)
445 {
446 DLIST_REMOVE(el->list_data);
447 FREE(el);
448 }
449 }
450 F()N(2)L();
452 /* delete list */
453 DLIST_SAFE_MLOOP(head, el, el_safe, list_data) {
454 DLIST_REMOVE(el->list_data);
455 FREE(el);
456 }
457 F()L();
459 return R_I(res);
460 }
462 int test_math()
463 {
464 IN;
465 int res = 0;
466 res |= NOT_I(ROUND_UP_TO2POW(0, 4096),==,0);
467 res |= NOT_I(ROUND_UP_TO2POW(1, 4096),==,4096);
468 res |= NOT_I(ROUND_UP_TO2POW(4095, 4096),==,4096);
469 res |= NOT_I(ROUND_UP_TO2POW(4096, 4096),==,4096);
470 res |= NOT_I(ROUND_UP_TO2POW(4097, 4096),==,8192);
471 res |= NOT_I(ROUND_DOWN_TO2POW(0, 4096),==,0);
472 res |= NOT_I(ROUND_DOWN_TO2POW(1, 4096),==,0);
473 res |= NOT_I(ROUND_DOWN_TO2POW(4095, 4096),==,0);
474 res |= NOT_I(ROUND_DOWN_TO2POW(4096, 4096),==,4096);
475 res |= NOT_I(ROUND_DOWN_TO2POW(4097, 4096),==,4096);
476 return R_I(res);
477 }
479 DEFINE_TESTS(TESTS)
481 int main(int argc, char **argv)
482 {
483 return TestLib_Run(argc, argv, nullfn, nullfn, NULL);
484 }