From d84dcd9f3b14da5cc87e9488b67a3815f76b2799 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 19 Sep 2026 16:02:08 -0400 Subject: [PATCH] Adding test --- tests/CMakeLists.txt | 27 ++++++++++++++ tests/tests.c | 89 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/tests.c diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..db3efb2 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,27 @@ +find_package(cmocka REQUIRED) + +# Test executable +add_executable(test_klist + test.c +) + + +# Link libraries +target_link_libraries(test_klist + klistlib + cmocka::cmocka + # ${CURSES_LIBRARIES} +) + +target_include_directories(test_klist PRIVATE + ${CMAKE_SOURCE_DIR}/include +) + +# Add to CTest +add_test(klist_PRINT test_klist COMMAND test_klist) + +# set_tests_properties(klist_PRINT PROPERTIES +# WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} +# ENVIRONMENT "TERM=xterm-256color" +# ) + diff --git a/tests/tests.c b/tests/tests.c new file mode 100644 index 0000000..754b06d --- /dev/null +++ b/tests/tests.c @@ -0,0 +1,89 @@ +#include +#include +#include + #include +#include +#include +#include + +// #include "prntconsl.h" // Your library's header +#include "klist.h" + +/* ===== Setup and Teardown Functions ===== */ +static int setup(void **state) +{ + int *test_data = malloc(sizeof(int)); + *test_data = 42; + *state = test_data; + return 0; +} + +static int teardown(void **state) +{ + free(*state); + return 0; +} + +/* ===== Test Cases ===== */ +/* +static void test_addition_success(void **state) +{ + const int result = 5 + 3; + assert_int_equal(result, 8); +} + +static void test_with_fixture_data(void **state) +{ + int *value = *state; + assert_int_equal(*value, 42); + + const int result = *value * 2; + assert_int_equal(result, 84); +} + +static void test_print(void **state) +{ + print("Hello", 0); + print("Hello New line", 1); + print("Another one", 1); +} + +static void test_print_multiple(void **state) +{ + print_msg_multiple_times("Biscut", 10, 1); +} +*/ + +/* ===== Test with Mock Objects ===== */ +/* Mock function that returns values set by the test */ +int __wrap_dependency_function(int param) +{ + check_expected(param); // Verifies param matches expectation + return (int) mock(); // Returns value from will_return() +} + +static void test_with_mock(void **state) +{ + /* Set up expectations: when called with 10, return 20 */ + // expect_value(__wrap_dependency_function, param, 10); + // will_return(__wrap_dependency_function, 20); + + // const int result = 20; + // assert_int_equal(result, 20); +} + +/* ===== Run All Tests ===== */ +int main(void) +{ + const struct CMUnitTest tests[] = { + // cmocka_unit_test(test_print), + // cmocka_unit_test(test_addition_success), + // cmocka_unit_test(test_print_multiple), + // cmocka_unit_test_setup_teardown(test_with_fixture_data, + setup, teardown), + cmocka_unit_test(test_with_mock), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} +