Test assert
This commit is contained in:
@@ -104,15 +104,6 @@
|
|||||||
const char *message
|
const char *message
|
||||||
);
|
);
|
||||||
|
|
||||||
void assertMemoryRangeMatchesImpl(
|
|
||||||
const char *file,
|
|
||||||
const int32_t line,
|
|
||||||
const void *start,
|
|
||||||
const void *end,
|
|
||||||
const size_t size,
|
|
||||||
const char *message
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts a given value to be true.
|
* Asserts a given value to be true.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
# This software is released under the MIT License.
|
# This software is released under the MIT License.
|
||||||
# https://opensource.org/licenses/MIT
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
add_subdirectory(assert)
|
||||||
add_subdirectory(display)
|
add_subdirectory(display)
|
||||||
# add_subdirectory(rpg)
|
# add_subdirectory(rpg)
|
||||||
add_subdirectory(item)
|
add_subdirectory(item)
|
||||||
|
|||||||
9
test/assert/CMakeLists.txt
Normal file
9
test/assert/CMakeLists.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
include(dusktest)
|
||||||
|
|
||||||
|
# Tests
|
||||||
|
dusktest(test_assert.c)
|
||||||
154
test/assert/test_assert.c
Normal file
154
test/assert/test_assert.c
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "dusktest.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
static void test_assertTrueImpl(void **state) {
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
// Should not assert when true false statement
|
||||||
|
assertTrueImpl(__FILE__, __LINE__, true, "This should not assert");
|
||||||
|
|
||||||
|
// Should assert when passing false statement
|
||||||
|
expect_assert_failure(assertTrueImpl(__FILE__, __LINE__, false, "asserts"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_assertFalseImpl(void **state) {
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
// Should not assert when passing false statement
|
||||||
|
assertFalseImpl(__FILE__, __LINE__, false, "This should not assert");
|
||||||
|
|
||||||
|
// Should assert when passing true statement
|
||||||
|
expect_assert_failure(assertFalseImpl(__FILE__, __LINE__, true, "asserts"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_assertUnreachableImpl(void **state) {
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
// Should always assert false
|
||||||
|
expect_assert_failure(assertUnreachableImpl(__FILE__, __LINE__, "asserts"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_assertNotNullImpl(void **state) {
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
int someValue = 42;
|
||||||
|
|
||||||
|
// Should not assert with a valid pointer
|
||||||
|
assertNotNullImpl(__FILE__, __LINE__, &someValue, "not assert");
|
||||||
|
|
||||||
|
// Should assert with a NULL pointer
|
||||||
|
expect_assert_failure(assertNotNullImpl(__FILE__, __LINE__, NULL, "asserts"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_assertDeprecatedImpl(void **state) {
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
// Always asserts when called
|
||||||
|
expect_assert_failure(assertDeprecatedImpl(__FILE__, __LINE__, "deprecated"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_assertTrue(void **state) {
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
// Should not assert when passing true statement
|
||||||
|
assertTrue(true, "This should not assert");
|
||||||
|
|
||||||
|
// Should assert when passing false statement
|
||||||
|
expect_assert_failure(assertTrue(false, "asserts"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_assertFalse(void **state) {
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
// Should not assert when passing true statement
|
||||||
|
assertFalse(false, "This should not assert");
|
||||||
|
|
||||||
|
// Should assert when passing false statement
|
||||||
|
expect_assert_failure(assertFalse(true, "asserts"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_assertUnreachable(void **state) {
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
// Should always assert false
|
||||||
|
expect_assert_failure(assertUnreachable("asserts"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_assertNotNull(void **state) {
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
int someValue = 42;
|
||||||
|
|
||||||
|
// Should not assert with a valid pointer
|
||||||
|
assertNotNull(&someValue, "not assert");
|
||||||
|
|
||||||
|
// Should assert with a NULL pointer
|
||||||
|
expect_assert_failure(assertNotNull(NULL, "asserts"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_assertNull(void **state) {
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
int someValue = 42;
|
||||||
|
|
||||||
|
// Should assert with a valid pointer
|
||||||
|
expect_assert_failure(assertNull(&someValue, "asserts"));
|
||||||
|
|
||||||
|
// Should not assert with a NULL pointer
|
||||||
|
assertNull(NULL, "not assert");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_assertDeprecated(void **state) {
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
// Always asserts when called
|
||||||
|
expect_assert_failure(assertDeprecated("deprecated"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_assertStrLenMax(void **state) {
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
// Should not assert when string length is within limit
|
||||||
|
assertStrLenMax("test", 10, "This should not assert");
|
||||||
|
|
||||||
|
// Should assert when string length exceeds limit
|
||||||
|
expect_assert_failure(assertStrLenMax("exceeding", 5, "asserts"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_assertStrLenMin(void **state) {
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
// Should not assert when string length meets minimum
|
||||||
|
assertStrLenMin("sufficient", 5, "This should not assert");
|
||||||
|
|
||||||
|
// Should assert when string length is below minimum
|
||||||
|
expect_assert_failure(assertStrLenMin("tiny", 10, "asserts"));
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
const struct CMUnitTest tests[] = {
|
||||||
|
cmocka_unit_test(test_assertTrueImpl),
|
||||||
|
cmocka_unit_test(test_assertFalseImpl),
|
||||||
|
cmocka_unit_test(test_assertUnreachableImpl),
|
||||||
|
cmocka_unit_test(test_assertNotNullImpl),
|
||||||
|
cmocka_unit_test(test_assertDeprecatedImpl),
|
||||||
|
cmocka_unit_test(test_assertTrue),
|
||||||
|
cmocka_unit_test(test_assertFalse),
|
||||||
|
cmocka_unit_test(test_assertUnreachable),
|
||||||
|
cmocka_unit_test(test_assertNotNull),
|
||||||
|
cmocka_unit_test(test_assertNull),
|
||||||
|
cmocka_unit_test(test_assertDeprecated),
|
||||||
|
cmocka_unit_test(test_assertStrLenMax),
|
||||||
|
cmocka_unit_test(test_assertStrLenMin),
|
||||||
|
};
|
||||||
|
|
||||||
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user