From 21cf170b5aa09da0ffd7ae427129b6ce2c03f98b Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Wed, 25 Dec 2024 18:08:50 +0000 Subject: [PATCH] Create project skeleton --- CMakeLists.txt | 22 ++++++++++++++++++++++ engine/CMakeLists.txt | 5 +++++ engine/foo.c | 11 +++++++++++ engine/include/foo.h | 11 +++++++++++ tests/CMakeLists.txt | 21 +++++++++++++++++++++ tests/foo_tests.c | 20 ++++++++++++++++++++ tests/include/testing.h | 39 +++++++++++++++++++++++++++++++++++++++ tests/testing.c | 8 ++++++++ 8 files changed, 137 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 engine/CMakeLists.txt create mode 100644 engine/foo.c create mode 100644 engine/include/foo.h create mode 100644 tests/CMakeLists.txt create mode 100644 tests/foo_tests.c create mode 100644 tests/include/testing.h create mode 100644 tests/testing.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..40dc82b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.10) + +project(epec-mcu-emulator LANGUAGES C) + +option(TESTS "Build unit tests" ON) +option(WERROR "Treat warnings as errors" OFF) + +macro(set_default_target_options target) + set_property(TARGET ${target} PROPERTY C_STANDARD 11) + set_property(TARGET ${target} PROPERTY C_EXTENSIONS OFF) + target_compile_options(${target} PRIVATE -Wall -Wextra -pedantic) + if (${WERROR}) + target_compile_options(${target} PRIVATE -Werror) + endif() +endmacro() + +add_subdirectory(engine) + +if (${TESTS}) + enable_testing() + add_subdirectory(tests) +endif() diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt new file mode 100644 index 0000000..fdfb906 --- /dev/null +++ b/engine/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(engine + foo.c +) +set_default_target_options(engine) +target_include_directories(engine PUBLIC include) diff --git a/engine/foo.c b/engine/foo.c new file mode 100644 index 0000000..d8aa547 --- /dev/null +++ b/engine/foo.c @@ -0,0 +1,11 @@ +/* + * Copyright (c) Camden Dixie O'Brien + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#include "foo.h" + +int foo(void) +{ + return 42; +} diff --git a/engine/include/foo.h b/engine/include/foo.h new file mode 100644 index 0000000..271ad16 --- /dev/null +++ b/engine/include/foo.h @@ -0,0 +1,11 @@ +/* + * Copyright (c) Camden Dixie O'Brien + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#ifndef FOO_H +#define FOO_H + +int foo(void); + +#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..bca5248 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,21 @@ +add_library(testing testing.c) +set_default_target_options(testing) +target_include_directories(testing PUBLIC include) + +function(add_test_suite source) + string(REGEX REPLACE ".c$" "" name ${source}) + add_executable(${name} ${source}) + set_default_target_options(${name}) + target_link_libraries(${name} PRIVATE engine testing) + add_test(NAME ${name} COMMAND ${name}) +endfunction() + +function(add_test_suites) + foreach(source ${ARGN}) + add_test_suite(${source}) + endforeach() +endfunction() + +add_test_suites( + foo_tests.c +) diff --git a/tests/foo_tests.c b/tests/foo_tests.c new file mode 100644 index 0000000..1f3d21d --- /dev/null +++ b/tests/foo_tests.c @@ -0,0 +1,20 @@ +/* + * Copyright (c) Camden Dixie O'Brien + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#include "foo.h" +#include "testing.h" + +static void test_foo(void) +{ + const int res = foo(); + ASSERT_EQ(42, res); +} + +int main(void) +{ + TESTING_BEGIN(); + test_foo(); + return TESTING_END(); +} diff --git a/tests/include/testing.h b/tests/include/testing.h new file mode 100644 index 0000000..c319285 --- /dev/null +++ b/tests/include/testing.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) Camden Dixie O'Brien + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#ifndef TESTING_H +#define TESTING_H + +#include +#include +#include + +#define TESTING_BEGIN() fail_count = 0 +#define TESTING_END() fail_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE + +#define FAIL \ + do { \ + ++fail_count; \ + printf("%s: FAIL @ %s:%d\n", __func__, __FILE__, __LINE__); \ + return; \ + } while (0) + +#define ASSERT_FALSE(p) \ + do { \ + if (p) \ + FAIL; \ + } while (0) + +#define ASSERT_TRUE(p) ASSERT_FALSE(!(p)) +#define ASSERT_EQ(x, y) ASSERT_FALSE((x) != (y)) +#define ASSERT_NE(x, y) ASSERT_FALSE((x) == (y)) +#define ASSERT_LT(x, y) ASSERT_FALSE((x) <= (y)) +#define ASSERT_NULL(p) ASSERT_FALSE(NULL != (p)) +#define ASSERT_NOT_NULL(p) ASSERT_FALSE(NULL == (p)) +#define ASSERT_MEM_EQ(p, q, n) ASSERT_FALSE(memcmp(p, q, n) != 0) + +extern int fail_count; + +#endif diff --git a/tests/testing.c b/tests/testing.c new file mode 100644 index 0000000..2ea878d --- /dev/null +++ b/tests/testing.c @@ -0,0 +1,8 @@ +/* + * Copyright (c) Camden Dixie O'Brien + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#include "testing.h" + +int fail_count;