Skip to content
Merged
13 changes: 2 additions & 11 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,5 @@ jobs:
- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y cmake gcc make

- name: Configure CMake
working-directory: test
run: cmake -B build -S .

- name: Build tests
working-directory: test
run: cmake --build build -- -j$(nproc)

- name: Run tests with CTest
working-directory: test/build
run: ctest --output-on-failure
- name: Run test script
run: bash scripts/run_native_tests.sh
22 changes: 22 additions & 0 deletions scripts/run_native_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

set -o pipefail

# Find every test dir that has cmake
TEST_DIRS=$(find . -type d -name test -exec test -f "{}/CMakeLists.txt" \; -print)

if [ -z "$TEST_DIRS" ]; then
echo "No test found."
exit 0
fi

for test_dir in $TEST_DIRS; do
echo "Running tests in: $test_dir"
build_dir="$test_dir/build"
mkdir -p "$build_dir"
cd "$build_dir"
cmake ..
make -j
ctest -V
cd - > /dev/null
done
21 changes: 21 additions & 0 deletions scripts/run_target_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!usr/bin/env bash

<<comment
Each modules have their own test dir.
To make pio embedded test work, we will
symlink each test dir into pio's test dir.
comment

set -o pipefail

for module in src/modules/*; do
[ -d "$module/test" ] || continue

module_name=$(basename "$module")

link_name="test/test_${module_name}"

ln -snf ../"$module"/test "$link_name"
done

pio test -e esp32_unit_test -vv
10 changes: 8 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# This file was automatically generated for projects
# without default 'CMakeLists.txt' file.

FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)
file(GLOB_RECURSE APP_SOURCES ${CMAKE_SOURCE_DIR}/src/*.*)

idf_component_register(SRCS ${app_sources})
foreach(file ${APP_SOURCES})
if(file MATCHES ".*/test/.*")
list(REMOVE_ITEM APP_SOURCES ${file})
endif()
endforeach()

idf_component_register(SRCS ${APP_SOURCES})
2 changes: 1 addition & 1 deletion src/modules/decode/test/test_decode_parameters.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void test_decode_throttle_percentage(void)

void test_decode_manifold_pressure(void)
{
TEST_ASSERT_FLOAT_WITHIN(0.5f, -41.f, decode_manifold_pressure(0))
TEST_ASSERT_FLOAT_WITHIN(0.5f, -41.f, decode_manifold_pressure(0));
TEST_ASSERT_FLOAT_WITHIN(0.5f, 18.f, decode_manifold_pressure(255));
}

Expand Down
24 changes: 24 additions & 0 deletions src/modules/display/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.16.0)
project(test_display)

enable_testing()

set(UNITY_PATH ${CMAKE_SOURCE_DIR}/../../../../test/unity/src)

include_directories(
${UNITY_PATH}
${CMAKE_SOURCE_DIR}/../../../core/include
${CMAKE_SOURCE_DIR}/../src
${CMAKE_SOURCE_DIR}/../../../drivers/include
)

add_executable(unit_tests
${UNITY_PATH}/unity.c
${CMAKE_SOURCE_DIR}/test_main.c
${CMAKE_SOURCE_DIR}/test_display.c
${CMAKE_SOURCE_DIR}/../src/display.c
)

add_compile_definitions(NDEBUG)

add_test(NAME display COMMAND unit_tests)
32 changes: 25 additions & 7 deletions src/modules/display/test/test_display.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <unity.h>
#include <stdio.h>
#include "fsm_data.h"
#include "src/display.h"
#include "../src/display.h"

void setUp(void) {}
void tearDown(void) {}
Expand Down Expand Up @@ -34,31 +34,49 @@ void test_empty_romid_display(void)

display_print_state(&data);

TEST_ASSERT_EQUAL_STRING("ROM ID: --.--.-- ", line0);
TEST_ASSERT_EQUAL_STRING("ROM ID: --.--.-- ", line0);
TEST_ASSERT_EQUAL_STRING(" ", line1);
}

void test_romid_display(void)
{
struct fsm_data data = {0};
data.state = STATE_ROMID;
data.romid[0] = 0x12;
data.romid[1] = 0x34;
data.romid[2] = 0x56;
data.params.romid[0] = 0x12;
data.params.romid[1] = 0x34;
data.params.romid[2] = 0x56;

display_print_state(&data);

TEST_ASSERT_EQUAL_STRING("ROM ID: 12.34.56 ", line0);
TEST_ASSERT_EQUAL_STRING(" ", line1);
}

void test_romid_clear_row1(void)
{
struct fsm_data data = {0};
data.state = STATE_ROMID;
display_print_state(&data);

TEST_ASSERT_EQUAL_STRING(" ", line1);

data.state = STATE_ACTIVE_CODE_ONE;
display_print_state(&data);

data.state = STATE_ROMID;
display_print_state(&data);

TEST_ASSERT_EQUAL_STRING(" ", line1);

}

void test_vehicle_speed_display(void)
{
struct fsm_data data = {0};
data.state = STATE_VEHICLE_SPEED;
data.decoded_data.u8 = 55;
data.params.vehicle_speed = 55;

lcd_print_state(&data);
display_print_state(&data);

TEST_ASSERT_EQUAL_STRING("VSPD: 55mph ", line0);
}
3 changes: 3 additions & 0 deletions src/modules/display/test/test_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

void test_empty_romid_display(void);
void test_romid_display(void);
void test_romid_clear_row1(void);
void test_vehicle_speed_display(void);

#if defined(ESP_PLATFORM)
Expand All @@ -14,6 +15,7 @@ void test_task(void *pvParameters)

RUN_TEST(test_empty_romid_display);
RUN_TEST(test_romid_display);
RUN_TEST(test_romid_clear_row1);
RUN_TEST(test_vehicle_speed_display);

UNITY_END();
Expand All @@ -33,6 +35,7 @@ int main(void)

RUN_TEST(test_empty_romid_display);
RUN_TEST(test_romid_display);
RUN_TEST(test_romid_clear_row1);
RUN_TEST(test_vehicle_speed_display);

return UNITY_END();
Expand Down
23 changes: 23 additions & 0 deletions src/modules/fsm/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.16.0)
project(test_fsm)

enable_testing()

set(UNITY_PATH ${CMAKE_SOURCE_DIR}/../../../../test/unity/src)

include_directories(
${UNITY_PATH}
${CMAKE_SOURCE_DIR}/../../../core/include
${CMAKE_SOURCE_DIR}/../src
)

add_executable(unit_tests
${UNITY_PATH}/unity.c
${CMAKE_SOURCE_DIR}/test_main.c
${CMAKE_SOURCE_DIR}/test_fsm.c
${CMAKE_SOURCE_DIR}/../src/fsm.c
)

add_compile_definitions(NDEBUG)

add_test(NAME fsm COMMAND unit_tests)
25 changes: 25 additions & 0 deletions src/modules/uart/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.16.0)
project(test_uart)

enable_testing()

set(UNITY_PATH ${CMAKE_SOURCE_DIR}/../../../../test/unity/src)

include_directories(
${UNITY_PATH}
${CMAKE_SOURCE_DIR}/../../../core/include
${CMAKE_SOURCE_DIR}/../src
)

add_executable(unit_tests
${UNITY_PATH}/unity.c
${CMAKE_SOURCE_DIR}/test_main.c
${CMAKE_SOURCE_DIR}/test_ssm1.c
${CMAKE_SOURCE_DIR}/test_addr_map.c
${CMAKE_SOURCE_DIR}/../src/ssm1.c
${CMAKE_SOURCE_DIR}/../src/addr_map.c
)

add_compile_definitions(NDEBUG)

add_test(NAME uart COMMAND unit_tests)
15 changes: 15 additions & 0 deletions src/modules/uart/test/test_addr_map.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <unity.h>
#include "../src/addr_map.h"

void test_lookup_addr(void)
{
uint16_t addr;
addr = lookup_addr(STATE_BATTERY_V);
TEST_ASSERT_EQUAL_UINT16(0X1404, addr);

addr = lookup_addr(STATE_VEHICLE_SPEED);
TEST_ASSERT_EQUAL_UINT16(0X154B, addr);

addr = lookup_addr((fsm_state_e)100);
TEST_ASSERT_EQUAL_UINT16(0, addr);
}
48 changes: 48 additions & 0 deletions src/modules/uart/test/test_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <unity.h>

void test_get_romid_command(void);
void test_get_read_command(void);
void test_get_clear_command(void);
void test_lookup_addr(void);

void setUp(void) {}
void tearDown(void) {}

#if defined(ESP_PLATFORM)
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

void test_task(void *pvParameters)
{
UNITY_BEGIN();

RUN_TEST(test_get_romid_command);
RUN_TEST(test_get_read_command);
RUN_TEST(test_get_clear_command);
RUN_TEST(test_lookup_addr);

UNITY_END();

vTaskDelete(NULL);
}

void app_main(void)
{
xTaskCreate(&test_task, "test_task", 4096, NULL, 5, NULL);
}

#else

int main(void)
{
UNITY_BEGIN();

RUN_TEST(test_get_romid_command);
RUN_TEST(test_get_read_command);
RUN_TEST(test_get_clear_command);
RUN_TEST(test_lookup_addr);

return UNITY_END();
}

#endif
58 changes: 58 additions & 0 deletions src/modules/uart/test/test_ssm1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <unity.h>
#include "../src/ssm1.h"

void test_get_romid_command(void)
{
struct romid_ctx ctx = {0};
uint8_t cmd[4] = {0};

ctx.cmd_index = 0;
uint8_t expected[4] = {0x78, 0xFF, 0xFE, 0x00};
ssm1_get_romid_command(&ctx, cmd);
TEST_ASSERT_EQUAL_INT8_ARRAY(expected, cmd, 4);

*expected = 0x00;
*(expected + 1) = *(expected + 2) = 0xFF;
ssm1_get_romid_command(&ctx, cmd);
TEST_ASSERT_EQUAL_INT8_ARRAY(expected, cmd, 4);

*expected = 0x78;
*(expected + 1) = 0xFF;
*(expected + 2) = 0xFE;
ssm1_get_romid_command(&ctx, cmd);
TEST_ASSERT_EQUAL_INT8_ARRAY(expected, cmd, 4);
}

void test_get_read_command(void)
{
struct read_ctx ctx = {0};
uint8_t cmd[4] = {0};

ctx.addr = 0x1010;
uint8_t expected[4] = {0x78, 0x10, 0x10, 0x00};
ssm1_get_read_command(&ctx, cmd);
TEST_ASSERT_EQUAL_INT8_ARRAY(expected, cmd, 4);

ctx.addr = 0x1234;
*(expected + 1) = 0x12;
*(expected + 2) = 0x34;
ssm1_get_read_command(&ctx, cmd);
TEST_ASSERT_EQUAL_INT8_ARRAY(expected, cmd, 4);
}

void test_get_clear_command(void)
{
struct read_ctx ctx = {0};
uint8_t cmd[4] = {0};

ctx.addr = 0x1010;
uint8_t expected[4] = {0xAA, 0x10, 0x10, 0x00};
ssm1_get_clear_command(&ctx, cmd);
TEST_ASSERT_EQUAL_INT8_ARRAY(expected, cmd, 4);

ctx.addr = 0x1234;
*(expected + 1) = 0x12;
*(expected + 2) = 0x34;
ssm1_get_clear_command(&ctx, cmd);
TEST_ASSERT_EQUAL_INT8_ARRAY(expected, cmd, 4);
}
Loading
Loading