Spruce it up #1
@@ -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
|
||||||
+13
-1
@@ -1,6 +1,10 @@
|
|||||||
cmake_minimum_required(VERSION 4.3.4)
|
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
|
set (INCLUDE
|
||||||
include/klist.h
|
include/klist.h
|
||||||
@@ -14,3 +18,11 @@ set (SOURCES
|
|||||||
include_directories(${CMAKE_CURRENT_LIST_DIR}/include/)
|
include_directories(${CMAKE_CURRENT_LIST_DIR}/include/)
|
||||||
|
|
||||||
add_library(klist STATIC ${SOURCES} ${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)
|
||||||
|
|||||||
@@ -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"]
|
||||||
+4
-4
@@ -44,15 +44,13 @@ int is_list_init(struct node **list)
|
|||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int list_size(struct node **list)
|
int list_size(struct node **list)
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
struct node *tmp = *list;
|
struct node *tmp = *list;
|
||||||
|
|
||||||
struct node *in_tmp = NULL;
|
|
||||||
|
|
||||||
while (tmp != NULL || tmp->next != NULL) {
|
while (tmp != NULL || tmp->next != NULL) {
|
||||||
in_tmp = tmp;
|
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
|
|
||||||
++count;
|
++count;
|
||||||
@@ -81,6 +79,7 @@ void insert_at_begin(struct node **list, void *val, size_t val_size)
|
|||||||
tmp->next = *list;
|
tmp->next = *list;
|
||||||
*list = tmp;
|
*list = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void insert_at_end(struct node **list, void *val, size_t val_size)
|
void insert_at_end(struct node **list, void *val, size_t val_size)
|
||||||
{
|
{
|
||||||
struct node *tmp = (struct node*)malloc(sizeof(struct node));
|
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->data = tmp_val;
|
||||||
tmp->next->next = NULL;
|
tmp->next->next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_list(struct node **list)
|
void free_list(struct node **list)
|
||||||
{
|
{
|
||||||
struct node *t = NULL;
|
struct node *t = NULL;
|
||||||
@@ -120,8 +120,8 @@ void free_list(struct node **list)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
while (l->next != NULL);
|
while (l->next != NULL);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void traverse(struct node *list, void (*print_val)(struct node*))
|
void traverse(struct node *list, void (*print_val)(struct node*))
|
||||||
{
|
{
|
||||||
if (list == NULL)
|
if (list == NULL)
|
||||||
|
|||||||
@@ -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)
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <cmocka.h>
|
||||||
|
|
||||||
|
// #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);
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user