AmendHub

Download

ftech

/

SList

/

slist-test.c

 

(View History)

Francois Techene   Changed license to MIT + refactored to feature camel case syntax. Latest amendment: 4 on 2026-04-01

1 /* slist_test.c
2 *
3 * Copyright 2026 Francois Techene
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Except as contained in this notice, the name(s) of the above copyright
25 * holders shall not be used in advertising or otherwise to promote the sale,
26 * use or other dealings in this Software without prior written
27 * authorization.
28 */
29
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include "slist.h"
35
36 #ifndef bool
37 typedef Boolean bool;
38 #endif
39
40
41 typedef struct _MyObject {
42
43 char* text;
44 int num;
45
46 } MyObject;
47
48 MyObject* NewMyObject(char* text, int num)
49 {
50 MyObject* self = (MyObject*)malloc(sizeof(MyObject));
51
52 self->text = text;
53 self->num = num;
54
55 return self;
56 }
57
58
59 MyObject* obj1 = NULL;
60 MyObject* obj2 = NULL;
61 MyObject* obj3 = NULL;
62
63 SList* list = NULL;
64
65 bool foundError = false;
66
67
68 //////////////////////////////////////////////////////////
69 // Assert Functions
70 //
71 bool
72 UtAsserPtrEqual(void* value1, void* value2, char* msg)
73 {
74 if (value1 != value2) {
75 printf(" FAILED: %s - Pointers do not match\n", msg);
76 foundError = true;
77 return 0;
78 }
79 return 1;
80 }
81
82 bool
83 UtAsserNull(void* value, char* msg)
84 {
85 if (value) {
86 printf(" FAILED: %s - Should be NULL\n", msg);
87 foundError = true;
88 return 0;
89 }
90 return 1;
91 }
92
93 bool
94 UtAsserNotNull(void* value, char* msg)
95 {
96 if (!value) {
97 printf(" FAILED: %s - Should not be NULL\n", msg);
98 foundError = true;
99 return 0;
100 }
101 return 1;
102 }
103
104 bool
105 UtAsserTrue(bool value, char* msg)
106 {
107 if (!value) {
108 printf(" FAILED: %s - Should be true\n", msg);
109 foundError = true;
110 return 0;
111 }
112 return 1;
113 }
114
115 bool
116 UtAsserFalse(bool value, char* msg)
117 {
118 if (value) {
119 printf(" FAILED: %s - Should be false\n", msg);
120 foundError = true;
121 return 0;
122 }
123 return 1;
124 }
125
126 bool
127 UtAsserGreaterInt(int value1, int value2, char* msg)
128 {
129 if (value1 > value2) {
130 printf(" FAILED: %s - Should be greater than %d and is %d\n",
131 msg,
132 value2,
133 value1);
134 foundError = true;
135 return 0;
136 }
137 return 1;
138 }
139
140 bool
141 UtAsserNotEqualInt(int value1, int value2, char* msg)
142 {
143 if (value1 == value2) {
144 printf(" FAILED: %s - Should not equal %d\n", msg, value2);
145 foundError = true;
146 return 0;
147 }
148 return 1;
149 }
150
151 bool
152 UtAsserEqualInt(int value1, int value2, char* msg)
153 {
154 if (value1 != value2) {
155 printf(" FAILED: %s - Value is %d, should be %d\n",
156 msg,
157 value1,
158 value2);
159 foundError = true;
160 return 0;
161 }
162 return 1;
163 }
164
165 //////////////////////////////////////////////////////////
166 // Unit Test
167 //
168 void
169 InitTests()
170 {
171 printf("Initializing Objects... \n");
172
173 obj1 = NewMyObject("Obj1", 1);
174 obj2 = NewMyObject("Obj2", 2);
175 obj3 = NewMyObject("Obj3", 3);
176
177 list = NewSList();
178
179 UtAsserEqualInt(obj1->num, 1,
180 "InitTests() - obj1->num");
181 UtAsserEqualInt(obj2->num, 2,
182 "InitTests() - obj2->num");
183 UtAsserEqualInt(obj3->num, 3,
184 "InitTests() - obj3->num");
185
186 UtAsserNull(list->head,
187 "InitTests() - list->head");
188
189 UtAsserEqualInt(list->count, 0,
190 "InitTests() - list->count");
191 }
192
193 void
194 TestAppend()
195 {
196 MyObject* obj = NULL;
197 SListItem* item = NULL;
198 int count = 0;
199
200 printf("Testing append... \n");
201
202 SListAppend(list, obj1);
203 SListAppend(list, obj2);
204 SListAppend(list, obj3);
205
206 SL_FOREACH(item, list) {
207 count++;
208 obj = (MyObject*)item->data;
209
210 UtAsserEqualInt(obj->num, count,
211 "TestAppend() - obj->num");
212
213 }
214
215 UtAsserEqualInt(count, 3,
216 "TestAppend() - FOREACH count");
217 }
218
219 void
220 TestRemoveItem()
221 {
222 MyObject* obj = NULL;
223 SListItem* item = NULL;
224 int count = 0;
225
226 printf("Testing remove item...\n");
227
228 SListRemoveValue(list, obj2);
229
230 SL_FOREACH(item, list) {
231 count++;
232 obj = (MyObject*)item->data;
233
234 if (count == 1) {
235 UtAsserEqualInt(obj->num, 1,
236 "TestRemoveItem() - obj->num");
237 }
238
239 if (count == 2) {
240 UtAsserEqualInt(obj->num, 3,
241 "TestRemoveItem() - obj->num");
242 }
243
244 }
245
246 UtAsserEqualInt(list->count, 2,
247 "TestRemoveItem() - list->count");
248
249 UtAsserEqualInt(count, 2,
250 "TestRemoveItem() - FOREACH count");
251 }
252
253 void
254 TestInsertAt()
255 {
256 MyObject* obj = NULL;
257 SListItem* item = NULL;
258 int count = 0;
259
260 printf("Testing insert at... \n");
261
262 SListInsertAt(list, obj2, 1);
263
264 SL_FOREACH(item, list) {
265 count++;
266 obj = (MyObject*)item->data;
267
268 if (count == 1) {
269 UtAsserEqualInt(obj->num, 1,
270 "TestInsertAt() - obj->num");
271 }
272 if (count == 2) {
273 UtAsserEqualInt(obj->num, 2,
274 "TestInsertAt() - obj->num");
275 }
276 if (count == 3) {
277 UtAsserEqualInt(obj->num, 3,
278 "TestInsertAt() - obj->num");
279 }
280
281 }
282
283 UtAsserEqualInt(list->count, 3,
284 "TestInsertAt() - list->count");
285
286 UtAsserEqualInt(count, 3,
287 "TestInsertAt() - FOREACH count");
288 }
289
290 void
291 TestRemoveAt()
292 {
293 MyObject* obj = NULL;
294 SListItem* item = NULL;
295 int count = 0;
296
297 printf("Testing remove first...\n");
298
299 SListRemoveAt(list, 1);
300
301 SL_FOREACH(item, list) {
302 count++;
303 obj = (MyObject*)item->data;
304
305 if (count == 1) {
306 UtAsserEqualInt(obj->num, 1,
307 "TestRemoveAt() - obj->num");
308 }
309
310 if (count == 2) {
311 UtAsserEqualInt(obj->num, 3,
312 "TestRemoveAt() - obj->num");
313 }
314
315 }
316
317 UtAsserEqualInt(list->count, 2,
318 "TestRemoveAt() - list->count");
319
320 UtAsserEqualInt(count, 2,
321 "TestRemoveAt() - FOREACH count");
322 }
323
324 void
325 TestInsertAfter()
326 {
327 MyObject* obj = NULL;
328 SListItem* item = NULL;
329 int count = 0;
330
331 printf("Testing insert after... \n");
332
333 SListInsertAfter(list, obj2, obj1);
334
335 SL_FOREACH(item, list) {
336 count++;
337 obj = (MyObject*)item->data;
338
339 if (count == 1) {
340 UtAsserEqualInt(obj->num, 1,
341 "TestInsertAfter()");
342 }
343 if (count == 2) {
344 UtAsserEqualInt(obj->num, 2,
345 "TestInsertAfter()");
346 }
347 if (count == 3) {
348 UtAsserEqualInt(obj->num, 3,
349 "TestInsertAfter()");
350 }
351
352 }
353
354 UtAsserEqualInt(list->count, 3,
355 "TestInsertAfter() - list->count");
356
357 UtAsserEqualInt(count, 3,
358 "TestInsertAfter() - FOREACH count");
359 }
360
361 void
362 TestRemoveLast()
363 {
364 MyObject* obj = NULL;
365 SListItem* item = NULL;
366 int count = 0;
367
368 printf("Testing remove last...\n");
369
370 SListRemoveLast(list);
371
372 SL_FOREACH(item, list) {
373 count++;
374 obj = (MyObject*)item->data;
375
376 if (count == 1) {
377 UtAsserEqualInt(obj->num, 1,
378 "TestRemoveLast()");
379 }
380 if (count == 2) {
381 UtAsserEqualInt(obj->num, 2,
382 "TestRemoveLast()");
383 }
384 }
385
386 UtAsserEqualInt(list->count, 2,
387 "TestRemoveLast() - list->count");
388
389 UtAsserEqualInt(count, 2,
390 "TestRemoveLast() - FOREACH count");
391 }
392
393 void
394 TestEmptyList()
395 {
396 MyObject* obj = NULL;
397 SListItem* item = NULL;
398 int count = 0;
399
400 printf("Testing empty list...\n");
401
402 SListAppend(list, obj1);
403 SListAppend(list, obj2);
404
405 UtAsserNotEqualInt(list->count, 0,
406 "TestEmptyList() - list->count");
407
408 SListEmpty(list);
409
410 SL_FOREACH(item, list) {
411 count++;
412 obj = (MyObject*)item->data;
413 }
414
415 UtAsserEqualInt(count, 0,
416 "TestEmptyList() - FOREACH count");
417
418 UtAsserEqualInt(list->count, 0,
419 "TestEmptyList() - list->count");
420 }
421
422 void TestMemory()
423 {
424 long totalMem;
425 long mem1;
426 long mem2;
427 long mem3;
428 long mem4;
429 long mem5;
430 long memEmpty;
431
432 printf("\nTesting memory...\n\n");
433
434 totalMem = FreeMem();
435
436 SListAppend(list, obj1);
437 SListAppend(list, obj2);
438 SListAppend(list, obj3);
439 mem1 = totalMem - FreeMem();
440
441 SListRemoveAt(list, 0);
442 mem2 = totalMem - FreeMem();
443
444 SListRemoveLast(list);
445 mem3 = totalMem - FreeMem();
446
447 SListRemoveValue(list, obj2);
448 mem4 = totalMem - FreeMem();
449
450 SListAppend(list, obj1);
451 SListAppend(list, obj2);
452 SListAppend(list, obj3);
453 mem5 = totalMem - FreeMem();
454
455 SListEmpty(list);
456 memEmpty = totalMem - FreeMem();
457
458
459 printf("After appending 3 items: using %ld bytes\n", mem1);
460 printf("After removing first: using %ld bytes\n", mem2);
461 printf("After removing last: using %ld bytes\n", mem3);
462 printf("After removing item: using %ld bytes\n", mem4);
463 printf("After re-appending 3 items: using %ld bytes\n", mem5);
464 printf("After emptying: using %ld bytes\n", memEmpty);
465 }
466
467 int
468 main(void)
469 {
470
471
472 MaxApplZone();
473
474 printf("+-----------------------------------+\n");
475 printf("| SList Unit Test |\n");
476 printf("+-----------------------------------+\n\n");
477
478 InitTests();
479
480 TestAppend();
481 TestRemoveItem();
482 TestInsertAt();
483 TestRemoveAt();
484 TestInsertAfter();
485 TestRemoveLast();
486 TestEmptyList();
487
488 TestMemory();
489
490 if (!foundError) {
491 printf("\nSuccessful!\n");
492 }
493 else {
494 printf("\n** Errors were found **\n");
495 }
496
497 return 1;
498 }