Added memory checks
This commit is contained in:
68
test/util/test_array.c
Normal file
68
test/util/test_array.c
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user