Skip to content

Commit

Permalink
Documentation for cmocka based testing.
Browse files Browse the repository at this point in the history
Signed-off-by: Rule Timothy (CC/EMT2) <[email protected]>
  • Loading branch information
timrulebosch committed Nov 2, 2023
1 parent 288b8d3 commit 9202eb2
Show file tree
Hide file tree
Showing 6 changed files with 689 additions and 0 deletions.
47 changes: 47 additions & 0 deletions doc/content/docs/devel/modelc_cmocka/examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export NAMESPACE = fsil
export MODULE = runnable
export PROJECT_URL ?= $(GITHUB_ORG)/$(NAMESPACE).$(MODULE).git
export DOCKER_REPO ?= ghcr.io/boschglobal
export EXTERNAL_BUILD_DIR ?= /tmp/$(NAMESPACE).$(MODULE)
export PACKAGE_ARCH ?= linux-amd64
export CMAKE_TOOLCHAIN_FILE ?= $(shell pwd -P)/extra/cmake/$(PACKAGE_ARCH).cmake
export PACKAGE_VERSION ?= 0.0.2
export GCC_BUILDER_IMAGE ?= $(DOCKER_REPO)/dse-gcc-builder:main

ifneq ($(CI), true)
DOCKER_BUILDER_CMD := docker run -it --rm \
--volume $$(pwd):/tmp/repo \
--volume $(EXTERNAL_BUILD_DIR):$(EXTERNAL_BUILD_DIR) \
--volume ~/.ccache:/root/.ccache \
--env CMAKE_TOOLCHAIN_FILE=/tmp/repo/extra/cmake/$(PACKAGE_ARCH).cmake \
--env EXTERNAL_BUILD_DIR=$(EXTERNAL_BUILD_DIR) \
--env PACKAGE_ARCH=$(PACKAGE_ARCH) \
--env HTTP_PROXY=$(http_proxy) \
--env HTTPS_PROXY=$(https_proxy) \
--env NO_PROXY=$(no_proxy) \
--env AR_USER=$(AR_USER) \
--env AR_TOKEN=$(AR_TOKEN) \
--env GHE_USER=$(GHE_USER) \
--env GHE_TOKEN=$(GHE_TOKEN) \
--env DOCKER_REPO=$(DOCKER_REPO) \
--env PROJECT_URL=$(PROJECT_URL) \
--env GDB_CMD="$(GDB_CMD)" \
--env PACKAGE_VERSION=$(PACKAGE_VERSION) \
--env NAMESPACE=$(NAMESPACE) \
--env MODULE=$(MODULE) \
--workdir /tmp/repo \
$(GCC_BUILDER_IMAGE)
endif

do-test_cmocka-build:
$(MAKE) -C tests/cmocka build
do-test_cmocka-run:
$(MAKE) -C tests/cmocka run

.PHONY: test_cmocka
test_cmocka:
@${DOCKER_BUILDER_CMD} $(MAKE) do-test_cmocka-build
@${DOCKER_BUILDER_CMD} $(MAKE) do-test_cmocka-run

.PHONY: test
test: test_cmocka
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
cmake_minimum_required(VERSION 3.21)

project(test_project)

include(FetchContent)
include(GNUInstallDirs)

set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/_out)
set(CMAKE_ENABLE_EXPORTS ON)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED TRUE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -ggdb")
list(APPEND C_CXX_WARNING_FLAGS
-Wall
-W
-Wwrite-strings
-Wno-missing-field-initializers
-Wno-misleading-indentation
)
add_compile_options(${C_CXX_WARNING_FLAGS})

FetchContent_Declare(dse_clib
URL https://github.com/boschglobal/dse.clib/archive/refs/tags/v1.0.5.tar.gz
SOURCE_DIR "$ENV{EXTERNAL_BUILD_DIR}/dse.clib"
)
FetchContent_MakeAvailable(dse_clib)
set(DSE_CLIB_SOURCE_DIR ${dse_clib_SOURCE_DIR}/dse/clib)
set(DSE_CLIB_INCLUDE_DIR "${DSE_CLIB_SOURCE_DIR}/../..")

add_executable(test_target
runnable/__test__.c
runnable/test_foo.c
)
target_include_directories(test_target
PRIVATE
${DSE_CLIB_INCLUDE_DIR}
./
)
target_compile_definitions(test_target
PUBLIC
CMOCKA_TESTING
)
target_link_libraries(test_target
PRIVATE
cmocka
dl
m
)
install(TARGETS test_target)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

GDB_CMD ?= valgrind -q --leak-check=yes
# GDB_CMD ?= gdb -q -ex='set confirm on' -ex=run -ex=quit

default: build

setup:
mkdir build;
cd build; cmake ..

build:
# Build from scratch if no build dir.
if [ ! -d "build" ]; then make setup; fi
# Build.
cd build; make
cd build; make install

.PHONY: run
run:
cd build/_out; $(GDB_CMD) bin/test_target

.PHONY: clean
clean:
rm -rf build

.PHONY: cleanall
cleanall: clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include <dse/testing.h>

extern int run_foo_tests(void);

int main()
{
int rc = 0;
rc |= run_foo_tests();
return rc;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <dse/testing.h>

#define UNUSED(x) ((void)x)
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))


typedef struct FooMock {
int foo_value;
} FooMock;

static int test_setup(void** state)
{
FooMock* mock = calloc(1, sizeof(FooMock));
mock->foo_value = 4;
*state = mock;
return 0;
}

static int test_teardown(void** state)
{
FooMock* mock = *state;
if (mock) free(mock);
return 0;
}

typedef struct FooTest {
int test_value;
int remainder;
}

void test_foo__data_driven(void** state)
{
FooMock* mock = *state;
FooTest tests[] = {
{ .test_value = 5, .remainder = 1 },
{ .test_value = 8, .remainder = 4 },
{ .test_value = 2, .remainder = -2 },
};

for (size_t i = 0; i < ARRAY_SIZE(tests); i++) {
int remainder = tests[i].test_value - mock->foo_value;
assert_int_equal(remainder, tests[i].remainder);
}
}

int run_foo_tests(void)
{
void* s = test_setup;
void* t = test_teardown;
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(test_foo__data_driven, s, t),
};
return cmocka_run_group_tests_name("FOO", tests, NULL, NULL);
}
Loading

0 comments on commit 9202eb2

Please sign in to comment.