/* slist_test.c * * Copyright 2026 Francois Techene * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name(s) of the above copyright * holders shall not be used in advertising or otherwise to promote the sale, * use or other dealings in this Software without prior written * authorization. */ #include #include #include #include "slist.h" #ifndef bool typedef Boolean bool; #endif typedef struct _MyObject { char* text; int num; } MyObject; MyObject* NewMyObject(char* text, int num) { MyObject* self = (MyObject*)malloc(sizeof(MyObject)); self->text = text; self->num = num; return self; } MyObject* obj1 = NULL; MyObject* obj2 = NULL; MyObject* obj3 = NULL; SList* list = NULL; bool foundError = false; ////////////////////////////////////////////////////////// // Assert Functions // bool UtAsserPtrEqual(void* value1, void* value2, char* msg) { if (value1 != value2) { printf(" FAILED: %s - Pointers do not match\n", msg); foundError = true; return 0; } return 1; } bool UtAsserNull(void* value, char* msg) { if (value) { printf(" FAILED: %s - Should be NULL\n", msg); foundError = true; return 0; } return 1; } bool UtAsserNotNull(void* value, char* msg) { if (!value) { printf(" FAILED: %s - Should not be NULL\n", msg); foundError = true; return 0; } return 1; } bool UtAsserTrue(bool value, char* msg) { if (!value) { printf(" FAILED: %s - Should be true\n", msg); foundError = true; return 0; } return 1; } bool UtAsserFalse(bool value, char* msg) { if (value) { printf(" FAILED: %s - Should be false\n", msg); foundError = true; return 0; } return 1; } bool UtAsserGreaterInt(int value1, int value2, char* msg) { if (value1 > value2) { printf(" FAILED: %s - Should be greater than %d and is %d\n", msg, value2, value1); foundError = true; return 0; } return 1; } bool UtAsserNotEqualInt(int value1, int value2, char* msg) { if (value1 == value2) { printf(" FAILED: %s - Should not equal %d\n", msg, value2); foundError = true; return 0; } return 1; } bool UtAsserEqualInt(int value1, int value2, char* msg) { if (value1 != value2) { printf(" FAILED: %s - Value is %d, should be %d\n", msg, value1, value2); foundError = true; return 0; } return 1; } ////////////////////////////////////////////////////////// // Unit Test // void InitTests() { printf("Initializing Objects... \n"); obj1 = NewMyObject("Obj1", 1); obj2 = NewMyObject("Obj2", 2); obj3 = NewMyObject("Obj3", 3); list = NewSList(); UtAsserEqualInt(obj1->num, 1, "InitTests() - obj1->num"); UtAsserEqualInt(obj2->num, 2, "InitTests() - obj2->num"); UtAsserEqualInt(obj3->num, 3, "InitTests() - obj3->num"); UtAsserNull(list->head, "InitTests() - list->head"); UtAsserEqualInt(list->count, 0, "InitTests() - list->count"); } void TestAppend() { MyObject* obj = NULL; SListItem* item = NULL; int count = 0; printf("Testing append... \n"); SListAppend(list, obj1); SListAppend(list, obj2); SListAppend(list, obj3); SL_FOREACH(item, list) { count++; obj = (MyObject*)item->data; UtAsserEqualInt(obj->num, count, "TestAppend() - obj->num"); } UtAsserEqualInt(count, 3, "TestAppend() - FOREACH count"); } void TestRemoveItem() { MyObject* obj = NULL; SListItem* item = NULL; int count = 0; printf("Testing remove item...\n"); SListRemoveValue(list, obj2); SL_FOREACH(item, list) { count++; obj = (MyObject*)item->data; if (count == 1) { UtAsserEqualInt(obj->num, 1, "TestRemoveItem() - obj->num"); } if (count == 2) { UtAsserEqualInt(obj->num, 3, "TestRemoveItem() - obj->num"); } } UtAsserEqualInt(list->count, 2, "TestRemoveItem() - list->count"); UtAsserEqualInt(count, 2, "TestRemoveItem() - FOREACH count"); } void TestInsertAt() { MyObject* obj = NULL; SListItem* item = NULL; int count = 0; printf("Testing insert at... \n"); SListInsertAt(list, obj2, 1); SL_FOREACH(item, list) { count++; obj = (MyObject*)item->data; if (count == 1) { UtAsserEqualInt(obj->num, 1, "TestInsertAt() - obj->num"); } if (count == 2) { UtAsserEqualInt(obj->num, 2, "TestInsertAt() - obj->num"); } if (count == 3) { UtAsserEqualInt(obj->num, 3, "TestInsertAt() - obj->num"); } } UtAsserEqualInt(list->count, 3, "TestInsertAt() - list->count"); UtAsserEqualInt(count, 3, "TestInsertAt() - FOREACH count"); } void TestRemoveAt() { MyObject* obj = NULL; SListItem* item = NULL; int count = 0; printf("Testing remove first...\n"); SListRemoveAt(list, 1); SL_FOREACH(item, list) { count++; obj = (MyObject*)item->data; if (count == 1) { UtAsserEqualInt(obj->num, 1, "TestRemoveAt() - obj->num"); } if (count == 2) { UtAsserEqualInt(obj->num, 3, "TestRemoveAt() - obj->num"); } } UtAsserEqualInt(list->count, 2, "TestRemoveAt() - list->count"); UtAsserEqualInt(count, 2, "TestRemoveAt() - FOREACH count"); } void TestInsertAfter() { MyObject* obj = NULL; SListItem* item = NULL; int count = 0; printf("Testing insert after... \n"); SListInsertAfter(list, obj2, obj1); SL_FOREACH(item, list) { count++; obj = (MyObject*)item->data; if (count == 1) { UtAsserEqualInt(obj->num, 1, "TestInsertAfter()"); } if (count == 2) { UtAsserEqualInt(obj->num, 2, "TestInsertAfter()"); } if (count == 3) { UtAsserEqualInt(obj->num, 3, "TestInsertAfter()"); } } UtAsserEqualInt(list->count, 3, "TestInsertAfter() - list->count"); UtAsserEqualInt(count, 3, "TestInsertAfter() - FOREACH count"); } void TestRemoveLast() { MyObject* obj = NULL; SListItem* item = NULL; int count = 0; printf("Testing remove last...\n"); SListRemoveLast(list); SL_FOREACH(item, list) { count++; obj = (MyObject*)item->data; if (count == 1) { UtAsserEqualInt(obj->num, 1, "TestRemoveLast()"); } if (count == 2) { UtAsserEqualInt(obj->num, 2, "TestRemoveLast()"); } } UtAsserEqualInt(list->count, 2, "TestRemoveLast() - list->count"); UtAsserEqualInt(count, 2, "TestRemoveLast() - FOREACH count"); } void TestEmptyList() { MyObject* obj = NULL; SListItem* item = NULL; int count = 0; printf("Testing empty list...\n"); SListAppend(list, obj1); SListAppend(list, obj2); UtAsserNotEqualInt(list->count, 0, "TestEmptyList() - list->count"); SListEmpty(list); SL_FOREACH(item, list) { count++; obj = (MyObject*)item->data; } UtAsserEqualInt(count, 0, "TestEmptyList() - FOREACH count"); UtAsserEqualInt(list->count, 0, "TestEmptyList() - list->count"); } void TestMemory() { long totalMem; long mem1; long mem2; long mem3; long mem4; long mem5; long memEmpty; printf("\nTesting memory...\n\n"); totalMem = FreeMem(); SListAppend(list, obj1); SListAppend(list, obj2); SListAppend(list, obj3); mem1 = totalMem - FreeMem(); SListRemoveAt(list, 0); mem2 = totalMem - FreeMem(); SListRemoveLast(list); mem3 = totalMem - FreeMem(); SListRemoveValue(list, obj2); mem4 = totalMem - FreeMem(); SListAppend(list, obj1); SListAppend(list, obj2); SListAppend(list, obj3); mem5 = totalMem - FreeMem(); SListEmpty(list); memEmpty = totalMem - FreeMem(); printf("After appending 3 items: using %ld bytes\n", mem1); printf("After removing first: using %ld bytes\n", mem2); printf("After removing last: using %ld bytes\n", mem3); printf("After removing item: using %ld bytes\n", mem4); printf("After re-appending 3 items: using %ld bytes\n", mem5); printf("After emptying: using %ld bytes\n", memEmpty); } int main(void) { MaxApplZone(); printf("+-----------------------------------+\n"); printf("| SList Unit Test |\n"); printf("+-----------------------------------+\n\n"); InitTests(); TestAppend(); TestRemoveItem(); TestInsertAt(); TestRemoveAt(); TestInsertAfter(); TestRemoveLast(); TestEmptyList(); TestMemory(); if (!foundError) { printf("\nSuccessful!\n"); } else { printf("\n** Errors were found **\n"); } return 1; }