Add unit test infrastructure

Signed-off-by: Matthias Blankertz <matthias@blankertz.org>
This commit is contained in:
2024-06-01 16:23:59 +02:00
parent b7c980b6eb
commit 910774cbe6
7 changed files with 116 additions and 3 deletions

View File

@@ -0,0 +1,22 @@
name: Run unit tests on host
on:
push:
jobs:
Run-Unit-Tests:
runs-on: ubuntu-22.04-full
steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Build and test
run: |
cmake software -B build
cmake --build build
ctest --test-dir build
- name: Upload test report
uses: actions/upload-artifact@v3
with:
name: junit-xml
path: build/junit.xml
# Use always() to always run this step to publish test results when there are test failures
if: ${{ always() }}

View File

@@ -1,12 +1,60 @@
cmake_minimum_required(VERSION 3.14)
# SPDX-License-Identifier: MIT
# Copyright (c) 2024 Matthias Blankertz <matthias@blankertz.org>
project(tonberry-pico)
cmake_minimum_required(VERSION 3.29)
project(tonberry-pico LANGUAGES C)
include(FetchContent)
FetchContent_Declare(unity
GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git
GIT_TAG v2.6.0
)
FetchContent_MakeAvailable(unity)
include(CTest)
function(make_unity_test )
set(options "")
set(oneValueArgs NAME TEST_SOURCE)
set(multiValueArgs SOURCES INCLUDES)
cmake_parse_arguments(PARSE_ARGV 0 MAKE_UNITY_TEST
"${options}" "${oneValueArgs}" "${multiValueArgs}"
)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MAKE_UNITY_TEST_NAME}_runner.c
COMMAND ${unity_SOURCE_DIR}/auto/generate_test_runner.rb ${MAKE_UNITY_TEST_TEST_SOURCE} ${CMAKE_CURRENT_BINARY_DIR}/${MAKE_UNITY_TEST_NAME}_runner.c
DEPENDS ${MAKE_UNITY_TEST_TEST_SOURCE}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
add_executable(${MAKE_UNITY_TEST_NAME}
${MAKE_UNITY_TEST_TEST_SOURCE}
${MAKE_UNITY_TEST_SOURCES}
${CMAKE_CURRENT_BINARY_DIR}/${MAKE_UNITY_TEST_NAME}_runner.c
)
target_link_libraries(${MAKE_UNITY_TEST_NAME} PUBLIC unity::framework)
target_include_directories(${MAKE_UNITY_TEST_NAME}
PRIVATE ${MAKE_UNITY_TEST_INCLUDES})
add_test(NAME ${MAKE_UNITY_TEST_NAME}
COMMAND sh -c "$<TARGET_FILE:${MAKE_UNITY_TEST_NAME}> > ${PROJECT_BINARY_DIR}/reports/${MAKE_UNITY_TEST_NAME}.testresults"
)
set_tests_properties(${MAKE_UNITY_TEST_NAME} PROPERTIES FIXTURES_REQUIRED Report)
endfunction()
add_test(NAME clean-reports
COMMAND sh -c "rm -rf ${PROJECT_BINARY_DIR}/reports; mkdir ${PROJECT_BINARY_DIR}/reports"
)
add_test(NAME generate-xml-report
COMMAND ${unity_SOURCE_DIR}/auto/stylize_as_junit.rb -r ${PROJECT_BINARY_DIR}/reports/ -o junit.xml
)
set_tests_properties(clean-reports PROPERTIES FIXTURES_SETUP "Report")
set_tests_properties(generate-xml-report PROPERTIES FIXTURES_CLEANUP "Report")
add_subdirectory(src/audiocore)
add_custom_target(check-format
find . -iname '*.[ch]' -exec clang-format -Werror --dry-run {} +
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src
)
add_custom_target(clang-format
find . -iname '*.[ch]' -exec clang-format -i {} +
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src

View File

@@ -0,0 +1,8 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2024 Matthias Blankertz <matthias@blankertz.org>
make_unity_test(NAME test_audiocore
TEST_SOURCE test/test_audiocore.c
SOURCES audiocore.c
INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/../test/include" "${CMAKE_CURRENT_SOURCE_DIR}"
)

View File

@@ -5,6 +5,7 @@
#include <hardware/sync.h>
#include <stdint.h>
#include <string.h>
/* Access rules

View File

@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2024 Matthias Blankertz <matthias@blankertz.org>
#include "audiocore.h"
#include "i2s.h"
#include "unity.h"
struct audiocore_shared_context shared_context;
bool i2s_init(int out_pin, int sideset_base, int samplerate) {}
void i2s_deinit(void) {}
void multicore_fifo_push_blocking(unsigned val) {}
unsigned multicore_fifo_pop_blocking(void) {}
void test_something(void) { TEST_ASSERT_TRUE(1); }

View File

@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2024 Matthias Blankertz <matthias@blankertz.org>
#pragma once
struct spin_lock;
typedef struct spin_lock spin_lock_t;
void multicore_fifo_push_blocking(unsigned val);
unsigned multicore_fifo_pop_blocking(void);

View File

@@ -0,0 +1,5 @@
#pragma once
#include <errno.h>
#define MP_EIO EIO