From 2053f2132d258beac8dc5af90a2b37815bf4da17 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 19 Sep 2026 16:52:06 -0400 Subject: [PATCH] Spruce it up (#1) Reviewed-on: http://git.kundeng.us/phoenix/klist/pulls/1 --- .gitea/workflows/klist.yml | 61 +++++++++++++++++++++++++ CMakeLists.txt | 14 +++++- conanfile.py | 37 +++++++++++++++ src/klist.c | 8 ++-- tests/CMakeLists.txt | 16 +++++++ tests/test.c | 94 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 225 insertions(+), 5 deletions(-) create mode 100644 .gitea/workflows/klist.yml create mode 100644 conanfile.py create mode 100644 tests/CMakeLists.txt create mode 100644 tests/test.c diff --git a/.gitea/workflows/klist.yml b/.gitea/workflows/klist.yml new file mode 100644 index 0000000..fe8ec23 --- /dev/null +++ b/.gitea/workflows/klist.yml @@ -0,0 +1,61 @@ +name: klist + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + build: + runs-on: ubuntu-24.04 + + steps: + - uses: actions/checkout@v6 + + - name: Cache Conan dependencies + uses: actions/cache@v3 + with: + path: ~/.conan/data # Or your custom cache directory + key: ${{ runner.os }}-conan-${{ hashFiles('**/conanfile.*') }} + restore-keys: | + ${{ runner.os }}-conan- + + - name: Create tmp folder + run: mkdir tmp + + - name: Install Conan and other dependencies + working-directory: tmp + run: | + sudo apt update + sudo apt install -y cmake + cmake --version + + wget https://github.com/conan-io/conan/releases/download/2.32.0/conan-2.32.0-amd64.deb + sudo dpkg -i conan-2.32.0-amd64.deb + + - name: Configure Conan + run: | + conan profile detect --force + + - name: Create build directory + run: mkdir build + + - name: Install dependencies + working-directory: build + run: conan install .. --build=missing + + - name: build project + working-directory: build + run: | + source Release/generators/conanbuild.sh + cmake .. --preset conan-release + cmake --build Release + + - name: Test with CTest (optional) + working-directory: build/Release + run: ctest -C Release diff --git a/CMakeLists.txt b/CMakeLists.txt index e88e7a8..301e8be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,10 @@ cmake_minimum_required(VERSION 4.3.4) -project(klist) +project(klist VERSION 0.0.1 LANGUAGES C) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_C_EXTENSIONS OFF) set (INCLUDE include/klist.h @@ -14,3 +18,11 @@ set (SOURCES include_directories(${CMAKE_CURRENT_LIST_DIR}/include/) add_library(klist STATIC ${SOURCES} ${INCLUDE}) + +target_compile_options(klist + PRIVATE + -Wall -Wextra -Wpedantic # Added -Wpedantic for stricter checks +) + +enable_testing() +add_subdirectory(tests) diff --git a/conanfile.py b/conanfile.py new file mode 100644 index 0000000..039d23f --- /dev/null +++ b/conanfile.py @@ -0,0 +1,37 @@ +from conan import ConanFile +from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout +from conan.tools.files import copy +import os + +class klistConan(ConanFile): + name = "klist" + version = "0.0.1" + + description = "A list library" + url = "https://git.kundeng.us/phoenix/klist" + homepage = "https://git.kundeng.us/phoenix/klist" + + settings = "os", "compiler", "build_type", "arch" + generators = "CMakeToolchain", "CMakeDeps" + + # Only add dependencies if needed + requires = "cmocka/1.1.8" + + def layout(self): + cmake_layout(self) + + def build_requirements(self): + self.tool_requires("cmake/4.4.3") + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def package(self): + cmake = CMake(self) + cmake.install() + + def package_info(self): + self.cpp_info.libs = ["klist"] + self.cpp_info.includedirs = ["include"] diff --git a/src/klist.c b/src/klist.c index 8e6753f..ee34a0c 100644 --- a/src/klist.c +++ b/src/klist.c @@ -44,15 +44,13 @@ int is_list_init(struct node **list) else return 0; } + int list_size(struct node **list) { int count = 0; struct node *tmp = *list; - struct node *in_tmp = NULL; - while (tmp != NULL || tmp->next != NULL) { - in_tmp = tmp; tmp = tmp->next; ++count; @@ -81,6 +79,7 @@ void insert_at_begin(struct node **list, void *val, size_t val_size) tmp->next = *list; *list = tmp; } + void insert_at_end(struct node **list, void *val, size_t val_size) { struct node *tmp = (struct node*)malloc(sizeof(struct node)); @@ -102,6 +101,7 @@ void insert_at_end(struct node **list, void *val, size_t val_size) tmp->next->data = tmp_val; tmp->next->next = NULL; } + void free_list(struct node **list) { struct node *t = NULL; @@ -120,8 +120,8 @@ void free_list(struct node **list) return; } while (l->next != NULL); - } + void traverse(struct node *list, void (*print_val)(struct node*)) { if (list == NULL) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..76e7f28 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,16 @@ +find_package(cmocka REQUIRED) + +add_executable(test_klist + test.c +) + +target_link_libraries(test_klist + klist + cmocka::cmocka +) + +target_include_directories(test_klist PRIVATE + ${CMAKE_SOURCE_DIR}/include +) + +add_test(klist_PRINT test_klist COMMAND test_klist) diff --git a/tests/test.c b/tests/test.c new file mode 100644 index 0000000..99b0e92 --- /dev/null +++ b/tests/test.c @@ -0,0 +1,94 @@ +#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_simple(void **state) +{ + const int a = 400; + const int b = 20; + assert_int_equal((a+b), 420); +} +/* +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_simple), + // cmocka_unit_test(test_addition_success), + // cmocka_unit_test(test_print_multiple), + // cmocka_unit_test_setup_teardown(test_with_fixture_data, + cmocka_unit_test(test_with_mock), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} +