| 1 |
/* slist_test.c |
| 2 |
* |
| 3 |
* Copyright 2025 Francois Techene |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <stdarg.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
|
| 25 |
#include "slist.h" |
| 26 |
|
| 27 |
#ifndef bool |
| 28 |
typedef Boolean bool; |
| 29 |
#endif |
| 30 |
|
| 31 |
|
| 32 |
typedef struct _MyObject { |
| 33 |
|
| 34 |
char* text; |
| 35 |
int num; |
| 36 |
|
| 37 |
} MyObject; |
| 38 |
|
| 39 |
MyObject* MyObject_new(char* text, int num) |
| 40 |
{ |
| 41 |
MyObject* self = (MyObject*)malloc(sizeof(MyObject)); |
| 42 |
|
| 43 |
self->text = text; |
| 44 |
self->num = num; |
| 45 |
|
| 46 |
return self; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
MyObject* obj1 = NULL; |
| 51 |
MyObject* obj2 = NULL; |
| 52 |
MyObject* obj3 = NULL; |
| 53 |
|
| 54 |
SList* list = NULL; |
| 55 |
|
| 56 |
bool found_error = false; |
| 57 |
|
| 58 |
|
| 59 |
////////////////////////////////////////////////////////// |
| 60 |
// Assert Functions |
| 61 |
// |
| 62 |
bool |
| 63 |
ut_asser_ptr_equal(void* value_1, void* value_2, char* msg) |
| 64 |
{ |
| 65 |
if (value_1 != value_2) { |
| 66 |
printf(" FAILED: %s - Pointers do not match\n", msg); |
| 67 |
found_error = true; |
| 68 |
return 0; |
| 69 |
} |
| 70 |
return 1; |
| 71 |
} |
| 72 |
|
| 73 |
bool |
| 74 |
ut_asser_null(void* value, char* msg) |
| 75 |
{ |
| 76 |
if (value) { |
| 77 |
printf(" FAILED: %s - Should be NULL\n", msg); |
| 78 |
found_error = true; |
| 79 |
return 0; |
| 80 |
} |
| 81 |
return 1; |
| 82 |
} |
| 83 |
|
| 84 |
bool |
| 85 |
ut_asser_not_null(void* value, char* msg) |
| 86 |
{ |
| 87 |
if (!value) { |
| 88 |
printf(" FAILED: %s - Should not be NULL\n", msg); |
| 89 |
found_error = true; |
| 90 |
return 0; |
| 91 |
} |
| 92 |
return 1; |
| 93 |
} |
| 94 |
|
| 95 |
bool |
| 96 |
ut_asser_true(bool value, char* msg) |
| 97 |
{ |
| 98 |
if (!value) { |
| 99 |
printf(" FAILED: %s - Should be true\n", msg); |
| 100 |
found_error = true; |
| 101 |
return 0; |
| 102 |
} |
| 103 |
return 1; |
| 104 |
} |
| 105 |
|
| 106 |
bool |
| 107 |
ut_asser_false(bool value, char* msg) |
| 108 |
{ |
| 109 |
if (value) { |
| 110 |
printf(" FAILED: %s - Should be false\n", msg); |
| 111 |
found_error = true; |
| 112 |
return 0; |
| 113 |
} |
| 114 |
return 1; |
| 115 |
} |
| 116 |
|
| 117 |
bool |
| 118 |
ut_asser_greater_int(int value_1, int value_2, char* msg) |
| 119 |
{ |
| 120 |
if (value_1 > value_2) { |
| 121 |
printf(" FAILED: %s - Should be greater than %d and is %d\n", |
| 122 |
msg, |
| 123 |
value_2, |
| 124 |
value_1); |
| 125 |
found_error = true; |
| 126 |
return 0; |
| 127 |
} |
| 128 |
return 1; |
| 129 |
} |
| 130 |
|
| 131 |
bool |
| 132 |
ut_asser_not_equal_int(int value_1, int value_2, char* msg) |
| 133 |
{ |
| 134 |
if (value_1 == value_2) { |
| 135 |
printf(" FAILED: %s - Should not equal %d\n", msg, value_2); |
| 136 |
found_error = true; |
| 137 |
return 0; |
| 138 |
} |
| 139 |
return 1; |
| 140 |
} |
| 141 |
|
| 142 |
bool |
| 143 |
ut_asser_equal_int(int value_1, int value_2, char* msg) |
| 144 |
{ |
| 145 |
if (value_1 != value_2) { |
| 146 |
printf(" FAILED: %s - Value is %d, should be %d\n", |
| 147 |
msg, |
| 148 |
value_1, |
| 149 |
value_2); |
| 150 |
found_error = true; |
| 151 |
return 0; |
| 152 |
} |
| 153 |
return 1; |
| 154 |
} |
| 155 |
|
| 156 |
////////////////////////////////////////////////////////// |
| 157 |
// Unit Test |
| 158 |
// |
| 159 |
void |
| 160 |
init_tests() |
| 161 |
{ |
| 162 |
printf("Initializing Objects... \n"); |
| 163 |
|
| 164 |
obj1 = MyObject_new("Obj1", 1); |
| 165 |
obj2 = MyObject_new("Obj2", 2); |
| 166 |
obj3 = MyObject_new("Obj3", 3); |
| 167 |
|
| 168 |
list = SList_new(); |
| 169 |
|
| 170 |
ut_asser_equal_int(obj1->num, 1, |
| 171 |
"init_tests() - obj1->num"); |
| 172 |
ut_asser_equal_int(obj2->num, 2, |
| 173 |
"init_tests() - obj2->num"); |
| 174 |
ut_asser_equal_int(obj3->num, 3, |
| 175 |
"init_tests() - obj3->num"); |
| 176 |
|
| 177 |
ut_asser_null(list->head, |
| 178 |
"init_tests() - list->head"); |
| 179 |
|
| 180 |
ut_asser_equal_int(list->count, 0, |
| 181 |
"init_tests() - list->count"); |
| 182 |
} |
| 183 |
|
| 184 |
void |
| 185 |
test_append() |
| 186 |
{ |
| 187 |
MyObject* obj = NULL; |
| 188 |
SListItem* item = NULL; |
| 189 |
int count = 0; |
| 190 |
|
| 191 |
printf("Testing append... \n"); |
| 192 |
|
| 193 |
slist_append(list, obj1); |
| 194 |
slist_append(list, obj2); |
| 195 |
slist_append(list, obj3); |
| 196 |
|
| 197 |
SL_FOREACH(item, list) { |
| 198 |
count++; |
| 199 |
obj = (MyObject*)item->data; |
| 200 |
|
| 201 |
ut_asser_equal_int(obj->num, count, |
| 202 |
"test_append() - obj->num"); |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
ut_asser_equal_int(count, 3, |
| 207 |
"test_append() - FOREACH count"); |
| 208 |
} |
| 209 |
|
| 210 |
void |
| 211 |
test_remove_item() |
| 212 |
{ |
| 213 |
MyObject* obj = NULL; |
| 214 |
SListItem* item = NULL; |
| 215 |
int count = 0; |
| 216 |
|
| 217 |
printf("Testing remove item...\n"); |
| 218 |
|
| 219 |
slist_remove_value(list, obj2); |
| 220 |
|
| 221 |
SL_FOREACH(item, list) { |
| 222 |
count++; |
| 223 |
obj = (MyObject*)item->data; |
| 224 |
|
| 225 |
if (count == 1) { |
| 226 |
ut_asser_equal_int(obj->num, 1, |
| 227 |
"test_remove_item() - obj->num"); |
| 228 |
} |
| 229 |
|
| 230 |
if (count == 2) { |
| 231 |
ut_asser_equal_int(obj->num, 3, |
| 232 |
"test_remove_item() - obj->num"); |
| 233 |
} |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
ut_asser_equal_int(list->count, 2, |
| 238 |
"test_remove_item() - list->count"); |
| 239 |
|
| 240 |
ut_asser_equal_int(count, 2, |
| 241 |
"test_remove_item() - FOREACH count"); |
| 242 |
} |
| 243 |
|
| 244 |
void |
| 245 |
test_insert_at() |
| 246 |
{ |
| 247 |
MyObject* obj = NULL; |
| 248 |
SListItem* item = NULL; |
| 249 |
int count = 0; |
| 250 |
|
| 251 |
printf("Testing insert at... \n"); |
| 252 |
|
| 253 |
slist_insert_at(list, obj2, 1); |
| 254 |
|
| 255 |
SL_FOREACH(item, list) { |
| 256 |
count++; |
| 257 |
obj = (MyObject*)item->data; |
| 258 |
|
| 259 |
if (count == 1) { |
| 260 |
ut_asser_equal_int(obj->num, 1, |
| 261 |
"test_insert_at() - obj->num"); |
| 262 |
} |
| 263 |
if (count == 2) { |
| 264 |
ut_asser_equal_int(obj->num, 2, |
| 265 |
"test_insert_at() - obj->num"); |
| 266 |
} |
| 267 |
if (count == 3) { |
| 268 |
ut_asser_equal_int(obj->num, 3, |
| 269 |
"test_insert_at() - obj->num"); |
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
ut_asser_equal_int(list->count, 3, |
| 275 |
"test_insert_at() - list->count"); |
| 276 |
|
| 277 |
ut_asser_equal_int(count, 3, |
| 278 |
"test_insert_at() - FOREACH count"); |
| 279 |
} |
| 280 |
|
| 281 |
void |
| 282 |
test_remove_at() |
| 283 |
{ |
| 284 |
MyObject* obj = NULL; |
| 285 |
SListItem* item = NULL; |
| 286 |
int count = 0; |
| 287 |
|
| 288 |
printf("Testing remove first...\n"); |
| 289 |
|
| 290 |
slist_remove_at(list, 1); |
| 291 |
|
| 292 |
SL_FOREACH(item, list) { |
| 293 |
count++; |
| 294 |
obj = (MyObject*)item->data; |
| 295 |
|
| 296 |
if (count == 1) { |
| 297 |
ut_asser_equal_int(obj->num, 1, |
| 298 |
"test_remove_at() - obj->num"); |
| 299 |
} |
| 300 |
|
| 301 |
if (count == 2) { |
| 302 |
ut_asser_equal_int(obj->num, 3, |
| 303 |
"test_remove_at() - obj->num"); |
| 304 |
} |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
ut_asser_equal_int(list->count, 2, |
| 309 |
"test_remove_at() - list->count"); |
| 310 |
|
| 311 |
ut_asser_equal_int(count, 2, |
| 312 |
"test_remove_at() - FOREACH count"); |
| 313 |
} |
| 314 |
|
| 315 |
void |
| 316 |
test_insert_after() |
| 317 |
{ |
| 318 |
MyObject* obj = NULL; |
| 319 |
SListItem* item = NULL; |
| 320 |
int count = 0; |
| 321 |
|
| 322 |
printf("Testing insert after... \n"); |
| 323 |
|
| 324 |
slist_insert_after(list, obj2, obj1); |
| 325 |
|
| 326 |
SL_FOREACH(item, list) { |
| 327 |
count++; |
| 328 |
obj = (MyObject*)item->data; |
| 329 |
|
| 330 |
if (count == 1) { |
| 331 |
ut_asser_equal_int(obj->num, 1, |
| 332 |
"test_insert_after()"); |
| 333 |
} |
| 334 |
if (count == 2) { |
| 335 |
ut_asser_equal_int(obj->num, 2, |
| 336 |
"test_insert_after()"); |
| 337 |
} |
| 338 |
if (count == 3) { |
| 339 |
ut_asser_equal_int(obj->num, 3, |
| 340 |
"test_insert_after()"); |
| 341 |
} |
| 342 |
|
| 343 |
} |
| 344 |
|
| 345 |
ut_asser_equal_int(list->count, 3, |
| 346 |
"test_insert_after() - list->count"); |
| 347 |
|
| 348 |
ut_asser_equal_int(count, 3, |
| 349 |
"test_insert_after() - FOREACH count"); |
| 350 |
} |
| 351 |
|
| 352 |
void |
| 353 |
test_remove_last() |
| 354 |
{ |
| 355 |
MyObject* obj = NULL; |
| 356 |
SListItem* item = NULL; |
| 357 |
int count = 0; |
| 358 |
|
| 359 |
printf("Testing remove last...\n"); |
| 360 |
|
| 361 |
slist_remove_last(list); |
| 362 |
|
| 363 |
SL_FOREACH(item, list) { |
| 364 |
count++; |
| 365 |
obj = (MyObject*)item->data; |
| 366 |
|
| 367 |
if (count == 1) { |
| 368 |
ut_asser_equal_int(obj->num, 1, |
| 369 |
"test_remove_last()"); |
| 370 |
} |
| 371 |
if (count == 2) { |
| 372 |
ut_asser_equal_int(obj->num, 2, |
| 373 |
"test_remove_last()"); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
ut_asser_equal_int(list->count, 2, |
| 378 |
"test_remove_last() - list->count"); |
| 379 |
|
| 380 |
ut_asser_equal_int(count, 2, |
| 381 |
"test_remove_last() - FOREACH count"); |
| 382 |
} |
| 383 |
|
| 384 |
void |
| 385 |
test_empty_list() |
| 386 |
{ |
| 387 |
MyObject* obj = NULL; |
| 388 |
SListItem* item = NULL; |
| 389 |
int count = 0; |
| 390 |
|
| 391 |
printf("Testing empty list...\n"); |
| 392 |
|
| 393 |
slist_append(list, obj1); |
| 394 |
slist_append(list, obj2); |
| 395 |
|
| 396 |
ut_asser_not_equal_int(list->count, 0, |
| 397 |
"test_empty_list() - list->count"); |
| 398 |
|
| 399 |
slist_empty(list); |
| 400 |
|
| 401 |
SL_FOREACH(item, list) { |
| 402 |
count++; |
| 403 |
obj = (MyObject*)item->data; |
| 404 |
} |
| 405 |
|
| 406 |
ut_asser_equal_int(count, 0, |
| 407 |
"test_empty_list() - FOREACH count"); |
| 408 |
|
| 409 |
ut_asser_equal_int(list->count, 0, |
| 410 |
"test_empty_list() - list->count"); |
| 411 |
} |
| 412 |
|
| 413 |
void test_memory() |
| 414 |
{ |
| 415 |
long total_mem; |
| 416 |
long mem_1; |
| 417 |
long mem_2; |
| 418 |
long mem_3; |
| 419 |
long mem_4; |
| 420 |
long mem_5; |
| 421 |
long mem_empty; |
| 422 |
|
| 423 |
printf("\nTesting memory...\n\n"); |
| 424 |
|
| 425 |
total_mem = FreeMem(); |
| 426 |
|
| 427 |
slist_append(list, obj1); |
| 428 |
slist_append(list, obj2); |
| 429 |
slist_append(list, obj3); |
| 430 |
mem_1 = total_mem - FreeMem(); |
| 431 |
|
| 432 |
slist_remove_at(list, 0); |
| 433 |
mem_2 = total_mem - FreeMem(); |
| 434 |
|
| 435 |
slist_remove_last(list); |
| 436 |
mem_3 = total_mem - FreeMem(); |
| 437 |
|
| 438 |
slist_remove_value(list, obj2); |
| 439 |
mem_4 = total_mem - FreeMem(); |
| 440 |
|
| 441 |
slist_append(list, obj1); |
| 442 |
slist_append(list, obj2); |
| 443 |
slist_append(list, obj3); |
| 444 |
mem_5 = total_mem - FreeMem(); |
| 445 |
|
| 446 |
slist_empty(list); |
| 447 |
mem_empty = total_mem - FreeMem(); |
| 448 |
|
| 449 |
|
| 450 |
printf("After appending 3 items: using %ld bytes\n", mem_1); |
| 451 |
printf("After removing first: using %ld bytes\n", mem_2); |
| 452 |
printf("After removing last: using %ld bytes\n", mem_3); |
| 453 |
printf("After removing item: using %ld bytes\n", mem_4); |
| 454 |
printf("After re-appending 3 items: using %ld bytes\n", mem_5); |
| 455 |
printf("After emptying: using %ld bytes\n", mem_empty); |
| 456 |
} |
| 457 |
|
| 458 |
int |
| 459 |
main(void) |
| 460 |
{ |
| 461 |
|
| 462 |
|
| 463 |
MaxApplZone(); |
| 464 |
|
| 465 |
printf("+-----------------------------------+\n"); |
| 466 |
printf("| SList Unit Test |\n"); |
| 467 |
printf("+-----------------------------------+\n\n"); |
| 468 |
|
| 469 |
init_tests(); |
| 470 |
|
| 471 |
test_append(); |
| 472 |
test_remove_item(); |
| 473 |
test_insert_at(); |
| 474 |
test_remove_at(); |
| 475 |
test_insert_after(); |
| 476 |
test_remove_last(); |
| 477 |
test_empty_list(); |
| 478 |
|
| 479 |
test_memory(); |
| 480 |
|
| 481 |
if (!found_error) { |
| 482 |
printf("\nSuccessful!\n"); |
| 483 |
} |
| 484 |
else { |
| 485 |
printf("\n** Errors were found **\n"); |
| 486 |
} |
| 487 |
|
| 488 |
return 1; |
| 489 |
} |