Added memory checks
Some checks failed
Build Dusk / run-tests (push) Successful in 2m6s
Build Dusk / build-linux (push) Successful in 2m6s
Build Dusk / build-psp (push) Failing after 1m47s

This commit is contained in:
2026-01-06 11:02:26 -06:00
parent af5bf987c8
commit 0df7845f2c
19 changed files with 640 additions and 13 deletions

View File

@@ -6,6 +6,8 @@
include(dusktest)
# Tests
dusktest(test_array.c)
dusktest(test_math.c)
dusktest(test_memory.c)
dusktest(test_string.c)
dusktest(test_string.c)
dusktest(test_sort.c)

68
test/util/test_array.c Normal file
View File

@@ -0,0 +1,68 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dusktest.h"
#include "util/memory.h"
#include "util/array.h"
static void test_arrayReverse(void **state) {
(void)state;
uint8_t array1[] = { 1, 2, 3, 4, 5 };
arrayReverse(array1, 5, sizeof(uint8_t));
uint8_t expected1[] = {5,4,3,2,1};
assert_memory_equal(array1, expected1, sizeof(uint8_t) * 5);
uint8_t array2[] = { 10, 20, 30, 40 };
arrayReverse(array2, 4, sizeof(uint8_t));
uint8_t expected2[] = {40,30,20,10};
assert_memory_equal(array2, expected2, sizeof(uint8_t) * 4);
uint8_t array3[] = { 7 };
arrayReverse(array3, 1, sizeof(uint8_t));
uint8_t expected3[] = {7};
assert_memory_equal(array3, expected3, sizeof(uint8_t) * 1);
uint8_t array4[] = {};
arrayReverse(array4, 0, sizeof(uint8_t));
uint8_t expected4[] = {};
assert_memory_equal(array4, expected4, sizeof(uint8_t) * 0);
// Can reverse arrays of larger element sizes
uint16_t array5[] = { 1000, 2000, 3000 };
arrayReverse(array5, 3, sizeof(uint16_t));
uint16_t expected5[] = {3000,2000,1000};
assert_memory_equal(array5, expected5, sizeof(uint16_t) * 3);
uint64_t array6[] = { 100000, 200000, 300000, 400000 };
arrayReverse(array6, 4, sizeof(uint64_t));
uint64_t expected6[] = {400000,300000,200000,100000};
assert_memory_equal(array6, expected6, sizeof(uint64_t) * 4);
// Will only sort provided array length, anything after is untouched
uint8_t array7[] = { 1, 2, 3, 4, 5, 6, 7 };
arrayReverse(array7, 5, sizeof(uint8_t));
uint8_t expected7[] = {5,4,3,2,1,6,7};
assert_memory_equal(array7, expected7, sizeof(uint8_t) * 7);
// Should fail when given NULL array pointer
expect_assert_failure(arrayReverse(NULL, 5, sizeof(uint8_t)));
// Should fail when given zero size
expect_assert_failure(arrayReverse(array1, 5, 0));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_arrayReverse),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

View File

@@ -11,31 +11,64 @@
static void test_memoryAllocate(void **state) {
(void)state;
size_t size = 128;
void *ptr = memoryAllocate(size);
// Memory should allocate
void *ptr = memoryAllocate(128);
assert_non_null(ptr);
// Allocating should be tracked
assert_int_equal(memoryGetAllocatedCount(), 1);
void *ptr2 = memoryAllocate(256);
assert_non_null(ptr2);
assert_int_equal(memoryGetAllocatedCount(), 2);
// Free memory
memoryFree(ptr);
assert_int_equal(memoryGetAllocatedCount(), 1);
memoryFree(ptr2);
assert_int_equal(memoryGetAllocatedCount(), 0);
// Should not be able to allocate 0 bytes
expect_assert_failure(memoryAllocate(0));
// Should not be able to allocate more memory than possible
expect_assert_failure(memoryAllocate(SIZE_MAX));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_memoryFree(void **state) {
(void)state;
// Create some memory
size_t size = 64;
void *ptr = memoryAllocate(size);
void *ptr = memoryAllocate(64);
assert_non_null(ptr);
assert_int_equal(memoryGetAllocatedCount(), 1);
// Free
memoryFree(ptr);
// Allocated count should decrease
assert_int_equal(memoryGetAllocatedCount(), 0);
ptr = memoryAllocate(32);
assert_non_null(ptr);
void *ptr2 = memoryAllocate(32);
assert_non_null(ptr2);
assert_int_equal(memoryGetAllocatedCount(), 2);
// Free both
memoryFree(ptr);
assert_int_equal(memoryGetAllocatedCount(), 1);
memoryFree(ptr2);
assert_int_equal(memoryGetAllocatedCount(), 0);
// Expect unable to free NULL
expect_assert_failure(memoryFree(NULL));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_memoryCopy(void **state) {
@@ -73,6 +106,9 @@ static void test_memoryCopy(void **state) {
memoryFree(src);
memoryFree(dest);
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_memorySet(void **state) {
@@ -104,6 +140,9 @@ static void test_memorySet(void **state) {
}
memoryFree(ptr);
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_memoryZero(void **state) {
@@ -129,6 +168,9 @@ static void test_memoryZero(void **state) {
expect_assert_failure(memoryZero(ptr, 0));
memoryFree(ptr);
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_memoryCopyRangeSafe(void **state) {
@@ -175,6 +217,9 @@ static void test_memoryCopyRangeSafe(void **state) {
memoryFree(src);
memoryFree(dest);
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_memoryMove(void **state) {
@@ -225,6 +270,9 @@ static void test_memoryMove(void **state) {
expect_assert_failure(memoryMove(ptr, ptr, size));
memoryFree(ptr);
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_memoryCompare(void **state) {
@@ -273,6 +321,9 @@ static void test_memoryCompare(void **state) {
memoryFree(a);
memoryFree(b);
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_memoryReallocate(void **state) {
@@ -303,6 +354,9 @@ static void test_memoryReallocate(void **state) {
// All we really care about is that the pointer is valid after reallocations
memoryFree(ptr);
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_memoryResize(void **state) {
@@ -356,6 +410,9 @@ static void test_memoryResize(void **state) {
expect_assert_failure(memoryResize(&ptr, smallerSize, SIZE_MAX));
memoryFree(ptr);
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
int main(int argc, char **argv) {

149
test/util/test_sort.c Normal file
View File

@@ -0,0 +1,149 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dusktest.h"
#include "util/sort.h"
#include "util/memory.h"
typedef struct {
int_t key;
char value;
} teststruct_t;
void testSortMethod(
void (*sortMethod)(
void *array,
const size_t count,
const size_t size,
const sortcompare_t compare
),
const char *methodName
) {
// Create an array of integers
size_t count = 5;
int_t *array = (int_t*)memoryAllocate(count * sizeof(int_t));
assert_non_null(array);
// Initialize with unsorted data
array[0] = 5;
array[1] = 3;
array[2] = 4;
array[3] = 1;
array[4] = 2;
// Comparator function
int_t compareInts(const void *a, const void *b) {
int_t intA = *(const int_t*)a;
int_t intB = *(const int_t*)b;
return (intA > intB) - (intA < intB);
}
// Sort the array
sortMethod(array, count, sizeof(int_t), compareInts);
// Check that the array is sorted
for(size_t i = 0; i < count; i++) {
assert_int_equal(array[i], (int_t)(i + 1));
}
// Test with custom struct comparator
teststruct_t unsortedStructs[4] = {
{3, 'b'},
{1, 'd'},
{4, 'a'},
{2, 'c'}
};
teststruct_t structArray[4];
// Test with number comparator
memoryCopy(structArray, unsortedStructs, sizeof(unsortedStructs));
int_t compareStructsKeys(const void *a, const void *b) {
const teststruct_t *structA = (const teststruct_t*)a;
const teststruct_t *structB = (const teststruct_t*)b;
return (structA->key > structB->key) - (structA->key < structB->key);
}
sortMethod(structArray, 4, sizeof(teststruct_t), compareStructsKeys);
for(size_t i = 0; i < 4; i++) {
assert_int_equal(structArray[i].key, (int_t)(i + 1));
}
// Test with char comparator
memoryCopy(structArray, unsortedStructs, sizeof(unsortedStructs));
int_t compareStructsValues(const void *a, const void *b) {
const teststruct_t *structA = (const teststruct_t*)a;
const teststruct_t *structB = (const teststruct_t*)b;
return (structA->value > structB->value) - (structA->value < structB->value);
}
sortMethod(structArray, 4, sizeof(teststruct_t), compareStructsValues);
char expectedValues[4] = {'a', 'b', 'c', 'd'};
for(size_t i = 0; i < 4; i++) {
assert_int_equal(structArray[i].value, expectedValues[i]);
}
// Cannot sort NULL base
expect_assert_failure(sortMethod(NULL, count, sizeof(int_t), compareInts));
// Cannot sort 0 elements
expect_assert_failure(sortMethod(array, 0, sizeof(int_t), compareInts));
// Cannot sort with size 0
expect_assert_failure(sortMethod(array, count, 0, compareInts));
// Cannot sort with NULL comparator
expect_assert_failure(sortMethod(array, count, sizeof(int_t), NULL));
memoryFree(array);
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_sortBubble(void **state) {
(void)state;
testSortMethod(sortBubble, "sortBubble");
}
static void test_sortQuick(void **state) {
(void)state;
testSortMethod(sortQuick, "sortQuick");
}
static void test_sortArrayU8(void **state) {
(void)state;
// Create an array of uint8_t
size_t count = 6;
uint8_t array[6] = {5, 2, 4, 1, 3, 0};
// Sort the array
sortArrayU8(array, count);
// Check that the array is sorted
for(size_t i = 0; i < count; i++) {
assert_int_equal(array[i], (uint8_t)i);
}
// Cannot sort NULL array
expect_assert_failure(sortArrayU8(NULL, count));
// Cannot sort 0 elements
expect_assert_failure(sortArrayU8(array, 0));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_sortBubble),
cmocka_unit_test(test_sortQuick),
cmocka_unit_test(test_sortArrayU8),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

View File

@@ -7,6 +7,7 @@
#include "dusktest.h"
#include "util/string.h"
#include "util/memory.h"
static void test_stringIsWhitespace(void **state) {
(void)state;
@@ -20,6 +21,9 @@ static void test_stringIsWhitespace(void **state) {
assert_false(stringIsWhitespace('1')); // '1' is not whitespace
assert_false(stringIsWhitespace('_')); // '_' is not whitespace
assert_false(stringIsWhitespace('-')); // '-' is not whitespace
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_stringCopy(void **state) {
@@ -47,6 +51,9 @@ static void test_stringCopy(void **state) {
// Must have destSize large enough for source
expect_assert_failure(stringCopy(dest, "this string is too long", 10));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_stringCompare(void **state) {
@@ -69,6 +76,9 @@ static void test_stringCompare(void **state) {
// Cannot compare NULL strings
expect_assert_failure(stringCompare(NULL, "abc"));
expect_assert_failure(stringCompare("abc", NULL));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_stringCompareInsensitive(void **state) {
@@ -84,6 +94,9 @@ static void test_stringCompareInsensitive(void **state) {
// Cannot compare NULL strings
expect_assert_failure(stringCompareInsensitive(NULL, "abc"));
expect_assert_failure(stringCompareInsensitive("abc", NULL));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_stringTrim(void **state) {
@@ -127,6 +140,9 @@ static void test_stringTrim(void **state) {
stringCopy(buf, "", sizeof(buf));
stringTrim(buf);
assert_string_equal(buf, "");
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_stringFindLastChar(void **state) {
@@ -148,6 +164,9 @@ static void test_stringFindLastChar(void **state) {
// Cannot search NULL string
expect_assert_failure(stringFindLastChar(NULL, 'a'));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_stringFormat(void **state) {
@@ -204,6 +223,9 @@ static void test_stringFormat(void **state) {
// Must have destSize > 0 if dest is not NULL
expect_assert_failure(stringFormat(buffer, 0, "Hello"));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_stringToI32(void **state) {
@@ -330,6 +352,9 @@ static void test_stringToI32(void **state) {
// Cannot have NULL output pointer
stringCopy(buffer, "123", sizeof(buffer));
expect_assert_failure(stringToI32(buffer, NULL));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_stringToI64(void **state) {
@@ -456,6 +481,9 @@ static void test_stringToI64(void **state) {
// Cannot have NULL output pointer
stringCopy(buffer, "123", sizeof(buffer));
expect_assert_failure(stringToI64(buffer, NULL));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_stringToI16(void **state) {
@@ -574,6 +602,9 @@ static void test_stringToI16(void **state) {
// Cannot have NULL output pointer
stringCopy(buffer, "123", sizeof(buffer));
expect_assert_failure(stringToI16(buffer, NULL));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_stringToU16(void **state) {
@@ -688,6 +719,9 @@ static void test_stringToU16(void **state) {
// Cannot have NULL output pointer
stringCopy(buffer, "123", sizeof(buffer));
expect_assert_failure(stringToU16(buffer, NULL));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_stringToF32(void **state) {
@@ -819,6 +853,9 @@ static void test_stringToF32(void **state) {
// Cannot have NULL output pointer
stringCopy(buffer, "1.23", sizeof(buffer));
expect_assert_failure(stringToF32(buffer, NULL));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_stringEndsWith(void **state) {
@@ -834,6 +871,9 @@ static void test_stringEndsWith(void **state) {
// Cannot have NULL strings
expect_assert_failure(stringEndsWith(NULL, "world"));
expect_assert_failure(stringEndsWith("hello", NULL));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_stringEndsWithCaseInsensitive(void **state) {
@@ -848,6 +888,9 @@ static void test_stringEndsWithCaseInsensitive(void **state) {
// Cannot have NULL strings
expect_assert_failure(stringEndsWithCaseInsensitive(NULL, "WORLD"));
expect_assert_failure(stringEndsWithCaseInsensitive("hello", NULL));
// Expect no leaks
assert_int_equal(memoryGetAllocatedCount(), 0);
}
int main(int argc, char **argv) {