SilverMUD/source/tests/lists-test.c

173 lines
5.5 KiB
C
Raw Normal View History

// =========================================
// | SilverMUD Tests - lists-test.c |
// | Copyright (C) 2023, Barra Ó Catháin |
// | See end of file for copyright notice. |
// =========================================
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "tests.h"
#include "../server/lists.h"
SETUP_TESTS;
bool compareChar (void * a, void * b)
{
return *(char *)a == *(char *)b;
}
void printListHeader(struct List * list)
{
printf("Items in list: %zu\nType: %d\nHead: %p\nTail: %p\n\n",
list->itemCount, list->type, list->head, list->tail);
}
void printListNode(struct ListNode * node)
{
printf("%p\nNext: %p\nPrevious: %p\nData: %c\n\n",
node, node->next, node->previous, *(char *)(node->data));
}
int main (int argc, char ** argv)
{
char testDataA = 'A', testDataB = 'B', testDataC = 'C';
// Test 1:
struct List * testList = createList(PLAYER);
TEST("Creating list",
(testList->head == NULL &&
testList->tail == NULL &&
testList->itemCount == 0));
// Test 2:
appendToList(PLAYER, testList, &testDataA);
TEST("Adding a single item to the list",
(testList->itemCount == 1 &&
testList->head != NULL &&
testList->tail != NULL &&
testList->tail == testList->head &&
*(char *)(testList->head->data) == 'A'));
// Test 3:
deleteListNodeFromList(0, testList);
TEST("Deleting a single item from the list",
(testList->head == NULL &&
testList->tail == NULL &&
testList->itemCount == 0));
// Test 4:
appendToList(PLAYER, testList, &testDataA);
appendToList(PLAYER, testList, &testDataB);
TEST("Adding two items to the list",
(testList->itemCount == 2 &&
testList->head != NULL &&
testList->tail != NULL &&
*(char *)testList->head->data == 'A' &&
*(char *)testList->tail->data == 'B' &&
testList->tail != testList->head &&
testList->head->next == testList->tail &&
testList->tail->previous == testList->head));
// Test 5:
deleteListNodeFromList(1, testList);
TEST("Deleting tail from the list",
(testList->itemCount == 1 &&
testList->head != NULL &&
testList->tail != NULL &&
testList->tail == testList->head &&
*(char *)(testList->head->data) == 'A'));
// Test 6:
appendToList(PLAYER, testList, &testDataB);
deleteListNodeFromList(0, testList);
TEST("Deleting head from the list",
(testList->itemCount == 1 &&
testList->head != NULL &&
testList->tail != NULL &&
testList->tail == testList->head &&
*(char *)(testList->head->data) == 'B'));
// Test 7:
deleteListNodeFromList(0, testList);
appendToList(PLAYER, testList, &testDataA);
appendToList(PLAYER, testList, &testDataB);
appendToList(PLAYER, testList, &testDataC);
TEST("Adding three items to the list",
(testList->itemCount == 3 &&
testList->head != NULL &&
testList->tail != NULL &&
*(char *)testList->head->data == 'A' &&
*(char *)testList->tail->data == 'C' &&
*(char *)testList->head->next->data == 'B' &&
*(char *)testList->tail->previous->data == 'B' &&
testList->tail != testList->head &&
testList->tail->previous == testList->head->next));
// Test 8:
deleteListNodeFromList(1, testList);
TEST("Deleting an item in middle of the list",
(testList->itemCount == 2 &&
testList->head != NULL &&
testList->tail != NULL &&
*(char *)testList->head->data == 'A' &&
*(char *)testList->tail->data == 'C' &&
testList->tail != testList->head &&
testList->head->next == testList->tail &&
testList->tail->previous == testList->head));
// Test 9:
deleteListNodeFromList(1, testList);
appendToList(PLAYER, testList, &testDataB);
appendToList(PLAYER, testList, &testDataC);
TEST("Checking for data in the list using comparison function",
(isInList(compareChar, &testDataB, testList) == true));
// Test 10:
TEST("Checking for data NOT in the list using comparison function",
(isInList(compareChar, &(char){'D'}, testList) == true));
// Test 11:
TEST("Checking for data in the list using pointer",
(isPointerInList(&testDataB, testList) == true));
// Test 12:
TEST("Checking for data NOT in the list using pointer",
(isPointerInList(&(char){'D'}, testList) == false));
// Test 13:
TEST("Checking index of data in the list",
(indexOfFromList(compareChar, &testDataB, testList) == 1));
// Test 14:
TEST("Checking index of data NOT in the list",
(indexOfFromList(compareChar, &(char){'D'}, testList) == -1));
// Test 15:
appendToList(PLAYER, testList, &testDataA);
TEST("Checking first of data in the list",
(getFirstFromList(compareChar, &testDataA, testList) == testList->head));
// Test 16:
TEST("Checking first of data NOT in the list",
(getFirstFromList(compareChar, &(char){'D'}, testList) == NULL));
FINISH_TESTING;
}
// ==================================================
// | End of lists-test.c, copyright notice follows. |
// ==================================================
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.