Skip to content

Commit

Permalink
[#59] Update unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSomeMan committed Oct 17, 2023
1 parent 494734c commit c174dfd
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ceedling.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ jobs:
# Runs a set of commands using the runners shell
- name: Run unit tests
run: |
ceedling test:all
make test_all
2 changes: 1 addition & 1 deletion CMock
Submodule CMock updated 1 files
+20 −10 scripts/create_makefile.rb
56 changes: 46 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ CXX=gcc

PROJ_DIR := src
# csv, errorfile, fullhtml, html, tasklist, xml
LOG_FORMAT=fullhtml
DOXYGEN_DIR=./doxygen

SDK_ROOT := nRF5_SDK_15.3.0_59ac345
Expand All @@ -229,31 +228,68 @@ ANALYSIS=$(SOURCES:.c=.a)
EXECUTABLE=gw_nrf
SONAR=nrf_analysis

.PHONY: clean doxygen sonar astyle all
BUILD_DIR = ./build
TEST_BUILD_DIR = ${BUILD_DIR}/test
TEST_OUT_DIR = ${TEST_BUILD_DIR}/out

all: astyle clean doxygen sonar
# Setup environment variables for ${CMOCK_DIR}/scripts/create_makefile.rb:
export CMOCK_DIR ?= ./CMock
UNITY_DIR = ${CMOCK_DIR}/vendor/unity
DISABLE_CMOCK_TEST_SUMMARY_PER_PROJECT=1

.c.o:
TEST_MAKEFILE = ${TEST_BUILD_DIR}/MakefileTestSupport

-include ${TEST_MAKEFILE}

.PHONY: all clean doxygen sonar astyle

all: clean astyle doxygen sonar

#.c.o:
# Build
$(CXX) $(CFLAGS) $< $(DFLAGS) $(INC_PARAMS) $(OFLAGS) $(LDFLAGS) -o $@
# $(CXX) $(CFLAGS) $< $(DFLAGS) $(INC_PARAMS) $(OFLAGS) $(LDFLAGS) -o $@

doxygen:
doxygen: clean
doxygen

sonar: $(SOURCES) $(SONAR)
sonar: clean $(SOURCES) $(SONAR)
$(SONAR): $(ANALYSIS)

.c.a:
# Build
$(ANALYSIS): %.a: %.c
$(CXX) $(CFLAGS) $< $(DFLAGS) $(INC_PARAMS) $(OFLAGS) -o $@

astyle:
astyle --project=".astylerc" "src/*.c" "src/*.h" "src/config/*.h" "test/*.c"

clean:
rm -f $(ANALYSIS)
rm -f $(OBJECTS) $(IOBJECTS) $(POBJECTS)
rm -rf $(DOXYGEN_DIR)/html
rm -rf $(DOXYGEN_DIR)/latex
rm -f *.gcov
mkdir -p $(DOXYGEN_DIR)

rm -rf $(BUILD_DIR)
make setup_test
make generate_cmock_mocks_and_runners

test_all:
rm -rf build_ceedling
CEEDLING_MAIN_PROJECT_FILE=./project.yml ceedling test:all

test_all_gcov:
rm -rf build_ceedling
CEEDLING_MAIN_PROJECT_FILE=./project.yml ceedling test:all
CEEDLING_MAIN_PROJECT_FILE=./project.yml ceedling gcov:all utils:gcov
gcov -b -c build_ceedling/gcov/out/*.gcno

test:
@UNITY_DIR=${UNITY_DIR} BUILD_DIR=${BUILD_DIR} TEST_BUILD_DIR= ruby ${CMOCK_DIR}/scripts/test_summary.rb

setup_test:
mkdir -p ${BUILD_DIR}
CEEDLING_MAIN_PROJECT_FILE=./project.yml \
BUILD_DIR=${BUILD_DIR} \
TEST_BUILD_DIR=${TEST_BUILD_DIR} \
TEST_OUT_DIR=${TEST_OUT_DIR} \
TEST_MAKEFILE=${TEST_MAKEFILE} \
ruby ${CMOCK_DIR}/scripts/create_makefile.rb
54 changes: 40 additions & 14 deletions project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,49 @@
:use_exceptions: FALSE
:use_test_preprocessor: TRUE
:use_auxiliary_dependencies: TRUE
:build_root: build
:build_root: build_ceedling
# :release_build: TRUE
:test_file_prefix: test_
:which_ceedling: gem
:default_tasks:
- test:all



:flags:
:test:
:compile:
:*:
- -std=c11
:gcov:
:compile:
:*:
- -std=c11

:environment:

:extension:
:executable: .out

:tools:
# Ceedling defaults to using gcc for compiling, linking, etc.
# As [:tools] is blank, gcc will be used (so long as it's in your system path)
# See documentation to configure a given toolchain for use
:test_linker:
:executable: gcc #absolute file path
:name: 'gcc linker'
:arguments:
- ${1} #list of object files to link (Ruby method call param list sub)
- -lm #link with math header
- -o ${2} #executable file output (Ruby method call param list sub)

:tools_gcov_linker:
:arguments:
- -lm

:paths:
:test:
- +:test/**
- -:test/support
:source:
- src/**
- +:src/*
- +:src/ruuvi.boards.c/**
- +:src/ruuvi.drivers.c/BME280_driver/**
- +:src/ruuvi.drivers.c/STMems_Standard_C_drivers/**
- +:src/ruuvi.drivers.c/embedded-sht/**
- -:src/ruuvi.drivers.c/embedded-sht/sample-projects/**
- +:src/ruuvi.drivers.c/ruuvi.dps310.c/**
- +:src/ruuvi.drivers.c/src/**
- +:src/ruuvi.endpoints.c/src/**
- +:src/ruuvi.libraries.c/src/**
:support:
- test/support

Expand Down Expand Up @@ -66,6 +79,7 @@
:unity_helper_path: 'test/unity_helper.h'
:plugins:
- :ignore
- :ignore_arg
- :callback
- :return_thru_ptr
- :array
Expand Down Expand Up @@ -109,4 +123,16 @@
- stdout_pretty_tests_report
- module_generator
- gcov

:flags:
:test:
:compile:
:*:
- -Wall
- -std=c11
:gcov:
:compile:
:*:
- -Wall
- -std=c11
...
44 changes: 37 additions & 7 deletions src/app_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@
#define APP_UART_RING_BUFFER_MAX_LEN (128U) //!< Ring buffer len
#define APP_UART_RING_DEQ_BUFFER_MAX_LEN (APP_UART_RING_BUFFER_MAX_LEN >>1) //!< Decode buffer len

/*!
* @brief UART response type enum
*/
typedef enum
{
APP_UART_RESP_TYPE_NONE = 0,
APP_UART_RESP_TYPE_ACK,
APP_UART_RESP_TYPE_DEVICE_ID,
APP_UART_RESP_TYPE_NONE = 0, //!< No response
APP_UART_RESP_TYPE_ACK, //!< Ack response
APP_UART_RESP_TYPE_DEVICE_ID, //!< Device ID response
} app_uart_resp_type_e;

#ifndef CEEDLING
Expand All @@ -58,18 +61,35 @@ static
volatile bool m_uart_ack = false;

static rl_ringbuffer_t m_uart_ring_buffer =
(rl_ringbuffer_t)
{
.head = 0,
.tail = 0,
.block_size = sizeof (uint8_t),
.storage_size = sizeof (buffer_data),
.index_mask = (sizeof (buffer_data) / sizeof (uint8_t)) - 1,
.storage = buffer_data,
.lock = app_uart_ringbuffer_lock_dummy,
.lock = &app_uart_ringbuffer_lock_dummy,
.writelock = &buffer_wlock,
.readlock = &buffer_rlock
};

#ifndef CEEDLING
static
#endif
void app_uart_init_globs (void)
{
buffer_wlock = false;
buffer_rlock = false;
g_flag_uart_tx_in_progress = false;
g_resp_type = APP_UART_RESP_TYPE_NONE;
g_resp_ack_cmd = (re_ca_uart_cmd_t)0;
g_resp_ack_state = false;
m_uart_ack = false;
m_uart_ring_buffer.head = 0;
m_uart_ring_buffer.tail = 0;
}

/** Dummy function to lock/unlock buffer */
#ifndef CEEDLING
static
Expand Down Expand Up @@ -139,7 +159,10 @@ static rd_status_t app_uart_send_ack (const re_ca_uart_cmd_t cmd, const bool is_
return err_code;
}

static void app_uart_on_evt_tx_finish (void * p_data, uint16_t data_len)
#ifndef CEEDLING
static
#endif
void app_uart_on_evt_tx_finish (void * p_data, uint16_t data_len)
{
g_flag_uart_tx_in_progress = false;

Expand All @@ -160,7 +183,10 @@ static void app_uart_on_evt_tx_finish (void * p_data, uint16_t data_len)
}
}

static void app_uart_on_evt_send_device_id (void * p_data, uint16_t data_len)
#ifndef CEEDLING
static
#endif
void app_uart_on_evt_send_device_id (void * p_data, uint16_t data_len)
{
g_resp_type = APP_UART_RESP_TYPE_DEVICE_ID;

Expand All @@ -170,7 +196,10 @@ static void app_uart_on_evt_send_device_id (void * p_data, uint16_t data_len)
}
}

static void app_uart_on_evt_send_ack (void * p_data, uint16_t data_len)
#ifndef CEEDLING
static
#endif
void app_uart_on_evt_send_ack (void * p_data, uint16_t data_len)
{
g_resp_type = APP_UART_RESP_TYPE_ACK;

Expand Down Expand Up @@ -485,6 +514,7 @@ rd_status_t app_uart_init (void)
{
rd_status_t err_code = RD_SUCCESS;
ri_uart_init_t config = { 0 };
app_uart_init_globs();
setup_uart_init (&config);
err_code |= ri_uart_init (&m_uart);

Expand Down
4 changes: 4 additions & 0 deletions src/app_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@

#ifdef CEEDLING
// Assist function for unit tests.
void app_uart_init_globs (void);
bool app_uart_ringbuffer_lock_dummy (volatile uint32_t * const flag, bool lock);
void app_uart_parser (void * p_data, uint16_t data_len);
void app_uart_on_evt_send_device_id (void * p_data, uint16_t data_len);
void app_uart_on_evt_send_ack (void * p_data, uint16_t data_len);
void app_uart_on_evt_tx_finish (void * p_data, uint16_t data_len);
#if 0
void app_uart_repeat_send (void * p_data, uint16_t data_len);
#endif
Expand Down
35 changes: 27 additions & 8 deletions test/test_app_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ static ri_comm_channel_t dummy_uart_fail =
void setUp (void)
{
mock_sends = 0;
app_uart_init_globs();
}

void tearDown (void)
Expand Down Expand Up @@ -304,12 +305,18 @@ void test_app_uart_parser_get_device_id_ok (void)
(re_ca_uart_payload_t *) &payload, RD_SUCCESS);
re_ca_uart_decode_ReturnThruPtr_cmd ((re_ca_uart_payload_t *) &expect_payload);
rl_ringbuffer_dequeue_ExpectAnyArgsAndReturn (RL_ERROR_NO_DATA);
ri_scheduler_event_put_ExpectAndReturn (NULL, 0, &app_uart_on_evt_send_device_id,
RD_SUCCESS);
ri_watchdog_feed_IgnoreAndReturn (RD_SUCCESS);
ri_scheduler_event_put_ExpectAndReturn (NULL, 0, &app_uart_on_evt_tx_finish,
RD_SUCCESS);
ri_radio_address_get_ExpectAnyArgsAndReturn (RD_SUCCESS);
ri_comm_id_get_ExpectAnyArgsAndReturn (RD_SUCCESS);
re_ca_uart_encode_ExpectAnyArgsAndReturn (RD_SUCCESS);
ri_watchdog_feed_IgnoreAndReturn (RD_SUCCESS);
app_uart_parser ((void *) &data[0], 6);
TEST_ASSERT (1 == mock_sends);
app_uart_on_evt_send_device_id (NULL, 0);
app_uart_on_evt_tx_finish (NULL, 0);
TEST_ASSERT_EQUAL (1, mock_sends);
}

void test_app_uart_isr_received (void)
Expand Down Expand Up @@ -495,10 +502,14 @@ void test_app_uart_parser_ok (void)
re_ca_uart_decode_ExpectAndReturn ((uint8_t *) &data[0],
(re_ca_uart_payload_t *) &payload, RD_SUCCESS);
rl_ringbuffer_dequeue_ExpectAnyArgsAndReturn (RL_ERROR_NO_DATA);
re_ca_uart_encode_ExpectAnyArgsAndReturn (RD_SUCCESS);
ri_scheduler_event_put_ExpectAndReturn (NULL, 0, &app_uart_on_evt_send_ack, RD_SUCCESS);
ri_watchdog_feed_IgnoreAndReturn (RD_SUCCESS);
ri_scheduler_event_put_ExpectAndReturn (NULL, 0, &app_uart_on_evt_tx_finish, RD_SUCCESS);
re_ca_uart_encode_ExpectAnyArgsAndReturn (RD_SUCCESS);
app_uart_parser ((void *) data, 8);
TEST_ASSERT (1 == mock_sends);
app_uart_on_evt_send_ack (NULL, 0);
app_uart_on_evt_tx_finish (NULL, 0);
TEST_ASSERT_EQUAL (1, mock_sends);
}

void test_app_uart_parser_clean_old (void)
Expand All @@ -525,10 +536,14 @@ void test_app_uart_parser_clean_old (void)
rl_ringbuffer_dequeue_ExpectAnyArgsAndReturn (RL_SUCCESS);
rl_ringbuffer_dequeue_ReturnMemThruPtr_data (&p_data_0, sizeof (uint8_t *));
rl_ringbuffer_dequeue_ExpectAnyArgsAndReturn (RL_ERROR_NO_DATA);
re_ca_uart_encode_ExpectAnyArgsAndReturn (RD_SUCCESS);
ri_scheduler_event_put_ExpectAndReturn (NULL, 0, &app_uart_on_evt_send_ack, RD_SUCCESS);
ri_watchdog_feed_IgnoreAndReturn (RD_SUCCESS);
ri_scheduler_event_put_ExpectAndReturn (NULL, 0, &app_uart_on_evt_tx_finish, RD_SUCCESS);
re_ca_uart_encode_ExpectAnyArgsAndReturn (RD_SUCCESS);
app_uart_parser ((void *) data, 8);
TEST_ASSERT (1 == mock_sends);
app_uart_on_evt_send_ack (NULL, 0);
app_uart_on_evt_tx_finish (NULL, 0);
TEST_ASSERT_EQUAL (1, mock_sends);
}

void test_app_uart_parser_part_1_ok (void)
Expand Down Expand Up @@ -602,8 +617,12 @@ void test_app_uart_parser_part_2_ok (void)
rl_ringbuffer_dequeue_ExpectAnyArgsAndReturn (RL_SUCCESS);
rl_ringbuffer_dequeue_ExpectAnyArgsAndReturn (RL_ERROR_NO_DATA);
re_ca_uart_decode_ExpectAnyArgsAndReturn (RD_SUCCESS);
re_ca_uart_encode_ExpectAnyArgsAndReturn (RD_SUCCESS);
ri_scheduler_event_put_ExpectAndReturn (NULL, 0, &app_uart_on_evt_send_ack, RD_SUCCESS);
ri_watchdog_feed_IgnoreAndReturn (RD_SUCCESS);
ri_scheduler_event_put_ExpectAndReturn (NULL, 0, &app_uart_on_evt_tx_finish, RD_SUCCESS);
re_ca_uart_encode_ExpectAnyArgsAndReturn (RD_SUCCESS);
app_uart_parser ((void *) data_part1, 5);
TEST_ASSERT (1 == mock_sends);
app_uart_on_evt_send_ack (NULL, 0);
app_uart_on_evt_tx_finish (NULL, 0);
TEST_ASSERT_EQUAL (1, mock_sends);
}

0 comments on commit c174dfd

Please sign in to comment.