Skip to content

Commit 5b0f29e

Browse files
UPDATED UNITY TESTING (#341)
1 parent 9b8b80a commit 5b0f29e

14 files changed

Lines changed: 583 additions & 8 deletions

File tree

Testing/Makefile

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
3+
# Compiler
4+
CC := gcc
5+
6+
# Project directories
7+
PROJECT_DIR := /home/app
8+
TEST_DIR := $(PROJECT_DIR)/Tests
9+
MOCKS_DIR := $(TEST_DIR)/Mocks
10+
TEST_BUILD := $(TEST_DIR)/build
11+
TEST_BUILD_BIN := $(TEST_BUILD)/bin
12+
UNITY_SRC := /cmock_portable/vendor/unity/src
13+
CMOCK_SRC := /cmock_portable/src
14+
EMEBEDDED_TESTING_DIR := Drivers/Embedded-Base/Testing
15+
16+
# Command line variables (must be provided)
17+
TEST_NAME ?=
18+
TEST_FILE ?=
19+
TEST_PACKAGE ?=
20+
C_SOURCES ?=
21+
INCLUDE_DIRS ?=
22+
23+
TEST_BUILD_OBJS := $(TEST_BUILD)/objs/$(TEST_PACKAGE)
24+
25+
# Sanity check
26+
ifeq ($(TEST_NAME),)
27+
$(error You must define TEST_NAME)
28+
endif
29+
ifeq ($(TEST_FILE),)
30+
$(error You must define TEST_FILE)
31+
endif
32+
ifeq ($(TEST_PACKAGE),)
33+
$(error You must define TEST_PACKAGE)
34+
endif
35+
ifeq ($(C_SOURCES),)
36+
$(error You must define C_SOURCES)
37+
endif
38+
39+
# Output executable
40+
TARGET := $(TEST_BUILD_BIN)/$(TEST_NAME)
41+
42+
# Include dirs
43+
C_INCLUDES := \
44+
-I$(EMEBEDDED_TESTING_DIR)/manual_mocks \
45+
-I$(UNITY_SRC) \
46+
-I$(CMOCK_SRC) \
47+
$(addprefix -I,$(INCLUDE_DIRS)) \
48+
-I$(MOCKS_DIR)/$(TEST_PACKAGE) \
49+
-I$(TEST_DIR)/Inc
50+
51+
CFLAGS = -w -DTESTING $(C_INCLUDES)
52+
53+
TEST_SOURCES = $(CMOCK_SRC)/cmock.c $(UNITY_SRC)/unity.c
54+
55+
# All source files
56+
SOURCES := $(TEST_FILE) $(C_SOURCES)
57+
TEST_SRCS := $(TEST)/cmock.c $(UNITY_SRC)/unity.c
58+
59+
# Object files
60+
OBJS := $(patsubst %.c,$(TEST_BUILD_OBJS)/%.o,$(SOURCES))
61+
TEST_OBJS := $(TEST_BUILD_OBJS)/cmock.o $(TEST_BUILD_OBJS)/unity.o
62+
63+
ALL_OBJS := $(OBJS) $(TEST_OBJS)
64+
65+
# Default target
66+
all: $(TARGET)
67+
68+
# Link executable
69+
$(TARGET): $(ALL_OBJS)
70+
@mkdir -p $(dir $@)
71+
$(CC) $(CFLAGS) $^ -o $@ -lm
72+
73+
# Compile object files
74+
$(TEST_BUILD_OBJS)/%.o: %.c
75+
@mkdir -p $(dir $@)
76+
$(CC) $(CFLAGS) -c $< -o $@ -lm
77+
78+
$(TEST_BUILD_OBJS)/cmock.o: $(CMOCK_SRC)/cmock.c
79+
@mkdir -p $(dir $@)
80+
$(CC) $(CFLAGS) -c $< -o $@
81+
82+
$(TEST_BUILD_OBJS)/unity.o: $(UNITY_SRC)/unity.c
83+
@mkdir -p $(dir $@)
84+
@echo "Compiling $< -> $@"
85+
$(CC) $(CFLAGS) -c $< -o $@
86+
87+
88+
# Clean
89+
clean:
90+
rm -rf $(TEST_BUILD)

Testing/cmock-config.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
:cmock:
3+
:mock_prefix: mock_
4+
:includes:
5+
- stubs.h
6+
:plugins:
7+
- :ignore
8+
- :ignore_arg
9+
- :callback
10+
- :expect
11+
- :expect_any_args
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
#ifndef _TEST_EXAMPLE_H
3+
#define _TEST_EXAMPLE_H
4+
5+
#include "unity.h"
6+
7+
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#include "test_example.h"
3+
#include <stdbool.h>
4+
#include <stdlib.h>
5+
6+
// clang-format off
7+
8+
// gets run before every test function
9+
void setUp(void) {
10+
11+
}
12+
13+
// gets run after every test function
14+
void tearDown(void) {
15+
16+
}
17+
18+
// test function
19+
void test_exmaple(void) {
20+
TEST_ASSERT_EQUAL_INT(9 + 10, 19);
21+
}
22+
23+
// run tests in main function
24+
int main(void) {
25+
UNITY_BEGIN();
26+
RUN_TEST(test_exmaple);
27+
return UNITY_END();
28+
}
29+
30+
// clang-format on
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Example NER Testing Config
2+
3+
title = "Example NER Testing"
4+
5+
# Files that will be mocked for every test package
6+
mocked-files = [
7+
"Drivers/Embedded-Base/Testing/manual_mocks/stubs.h",
8+
]
9+
10+
# sources to compile for each test exe
11+
sources = [
12+
"Drivers/Embedded-Base/middleware/src/bitstream.c",
13+
"Drivers/Embedded-Base/middleware/src/timer.c",
14+
"Drivers/adbms/adbms6830/program/src/serialPrintResult.c",
15+
"Drivers/Embedded-Base/middleware/src/c_utils.c",
16+
]
17+
18+
# headers directories to include in build
19+
include-dirs = [
20+
"Core/Inc",
21+
"Tests/Inc",
22+
"Drivers/Embedded-Base/middleware/include",
23+
"Drivers/Embedded-Base/platforms/stm32f405/include",
24+
"Drivers/Embedded-Base/general/include",
25+
]
26+
27+
# Test Package definitionss
28+
29+
[test-packages.example1]
30+
mocked-files = ["Drivers/Embedded-Base/middleware/include/timer.h"]
31+
32+
[test-packages.example2]
33+
parent-package = "example1"
34+
mocked-files = ["Drivers/Embedded-Base/middleware/include/bitstream.h"]
35+
36+
# Test definitions
37+
38+
[test.example]
39+
test-package = "example2"
40+
test-file = "Tests/Src/example.c"
41+
42+
43+

Testing/manual_mocks/stubs.h

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#ifndef MOCK_STUBS_H
2+
#define MOCK_STUBS_H
3+
4+
#include <stdint.h>
5+
#include <math.h>
6+
7+
// clang-format off
8+
9+
#define osWaitForever 0xFFFFFFFFU
10+
#define osTimerPeriodic 0
11+
#define osTimerOnce 1
12+
13+
typedef enum {
14+
HAL_OK = 0x00U,
15+
HAL_ERROR = 0x01U,
16+
HAL_BUSY = 0x02U,
17+
HAL_TIMEOUT = 0x03U
18+
} HAL_StatusTypeDef;
19+
20+
typedef enum {
21+
osOK = 0, ///< Operation completed successfully.
22+
osError =
23+
-1, ///< Unspecified RTOS error: run-time error but no other error message fits.
24+
osErrorTimeout =
25+
-2, ///< Operation not completed within the timeout period.
26+
osErrorResource = -3, ///< Resource not available.
27+
osErrorParameter = -4, ///< Parameter error.
28+
osErrorNoMemory =
29+
-5, ///< System is out of memory: it was impossible to allocate or reserve memory for the operation.
30+
osErrorISR =
31+
-6, ///< Not allowed in ISR context: the function cannot be called from interrupt service routines.
32+
osStatusReserved =
33+
0x7FFFFFFF ///< Prevents enum down-size compiler optimization.
34+
} osStatus_t;
35+
36+
typedef enum {
37+
osPriorityLow,
38+
osPriorityBelowNormal,
39+
osPriorityNormal,
40+
osPriorityHigh,
41+
osPriorityRealtime,
42+
osPriorityRealtime1,
43+
osPriorityRealtime2,
44+
osPriorityRealtime3,
45+
osPriorityRealtime4,
46+
osPriorityRealtime5,
47+
osPriorityRealtime6,
48+
osPriorityRealtime7,
49+
} osPriority_t;
50+
51+
typedef void *ADC_HandleTypeDef;
52+
typedef void *I2C_HandleTypeDef;
53+
typedef void *CAN_HandleTypeDef;
54+
typedef void *SPI_HandleTypeDef;
55+
typedef void *TIM_HandleTypeDef;
56+
typedef void *IWDG_HandleTypeDef;
57+
58+
typedef void *osMutexId_t;
59+
typedef void *osTimerId_t;
60+
typedef void *osThreadId_t;
61+
typedef void *osMessageQueueId_t;
62+
typedef void (*osTimerFunc_t)(void *);
63+
64+
#define osTimerId osTimerId_t
65+
#define osMutexId osMutexId_t
66+
#define osThreadId osThreadId_t
67+
#define osMessageQueueId osMessageQueueId_t
68+
69+
typedef struct {
70+
const char *name;
71+
uint32_t stack_size;
72+
osPriority_t priority;
73+
} osThreadAttr_t;
74+
75+
typedef struct {
76+
const char *name;
77+
} osTimerAttr_t;
78+
79+
typedef struct {
80+
const char *name;
81+
} osMutexAttr_t;
82+
83+
typedef struct {
84+
uint32_t StdId;
85+
uint32_t ExtId;
86+
uint32_t IDE;
87+
uint32_t RTR;
88+
uint32_t DLC;
89+
} CAN_RxHeaderTypeDef;
90+
91+
typedef struct {
92+
volatile uint32_t ODR;
93+
} GPIO_TypeDef;
94+
95+
uint32_t HAL_GetTick(void);
96+
97+
98+
osMutexId_t osMutexNew(const osMutexAttr_t *attr);
99+
uint32_t osMutexAcquire(osMutexId_t mutex, uint32_t timeout);
100+
uint32_t osMutexRelease(osMutexId_t mutex);
101+
osTimerId_t osTimerNew(osTimerFunc_t func, uint32_t type, void *arg,
102+
void *attr);
103+
uint32_t osTimerStart(osTimerId_t timer_id, uint32_t ticks);
104+
uint32_t osTimerStop(osTimerId_t timer_id);
105+
void osDelay(uint32_t ms);
106+
osMessageQueueId_t osMessageQueueNew(uint32_t msg_count, uint32_t msg_size,
107+
void *attr);
108+
uint32_t osMessageQueuePut(osMessageQueueId_t queue_id, const void *msg_ptr,
109+
uint8_t msg_prio, uint32_t timeout);
110+
uint32_t osMessageQueueGet(osMessageQueueId_t queue_id, void *msg_ptr,
111+
uint8_t *msg_prio, uint32_t timeout);
112+
uint32_t osThreadFlagsWait(uint32_t flags, uint32_t options, uint32_t timeout);
113+
uint32_t osThreadFlagsSet(osThreadId_t thread_id, uint32_t flags);
114+
int HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, uint32_t fifo,
115+
CAN_RxHeaderTypeDef *rx_header, uint8_t *rx_data);
116+
uint32_t pdMS_TO_TICKS(uint32_t ms);
117+
HAL_StatusTypeDef HAL_ADC_Start_DMA(void *hadc, uint32_t *pData,
118+
uint32_t Length);
119+
120+
HAL_StatusTypeDef HAL_I2C_Mem_Write(void *hi2c, uint16_t devAddress,
121+
uint16_t memAddress, uint16_t memAddSize,
122+
uint8_t *pData, uint16_t size,
123+
uint32_t timeout);
124+
125+
HAL_StatusTypeDef HAL_I2C_Mem_Read(void *hi2c, uint16_t devAddress,
126+
uint16_t memAddress, uint16_t memAddSize,
127+
uint8_t *pData, uint16_t size,
128+
uint32_t timeout);
129+
HAL_StatusTypeDef HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin,
130+
uint32_t PinState);
131+
HAL_StatusTypeDef HAL_CAN_GetTxMailboxesFreeLevel(CAN_HandleTypeDef *hcan);
132+
void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
133+
uint32_t HAL_GetTick(void);
134+
HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c,
135+
uint16_t DevAddress, uint8_t *pData,
136+
uint16_t Size, uint32_t Timeout);
137+
HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c,
138+
uint16_t DevAddress, uint8_t *pData,
139+
uint16_t Size, uint32_t Timeout);
140+
int HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
141+
int __HAL_TIM_GET_COUNTER(TIM_HandleTypeDef *htim);
142+
int HAL_Delay(int delay);
143+
144+
#define I2C_MEMADD_SIZE_8BIT 1
145+
#define HAL_MAX_DELAY 0xFFFFFFFFU
146+
147+
#define GPIO_PIN_RESET 0
148+
#define GPIO_PIN_SET 1
149+
#define GPIO_PIN_1 1
150+
#define GPIO_PIN_2 2
151+
#define GPIO_PIN_3 3
152+
#define GPIO_PIN_4 4
153+
#define GPIO_PIN_5 5
154+
#define GPIO_PIN_6 6
155+
#define GPIO_PIN_7 7
156+
#define GPIO_PIN_8 8
157+
#define GPIO_PIN_9 9
158+
#define GPIO_PIN_10 10
159+
#define GPIO_PIN_11 11
160+
#define GPIO_PIN_12 12
161+
#define GPIO_PIN_13 13
162+
#define GPIO_PIN_14 14
163+
#define GPIO_PIN_15 15
164+
#define GPIO_PIN_16 16
165+
166+
#define CAN_RX_FIFO0 0
167+
#define CAN_ID_EXT 1
168+
169+
#define GPIOA ((GPIO_TypeDef *)0x40020000)
170+
#define GPIOB ((GPIO_TypeDef *)0x40020400)
171+
#define GPIOC ((GPIO_TypeDef *)0x40020800)
172+
173+
#define osFlagsWaitAny 0x00000001U
174+
175+
// clang-format on
176+
177+
#endif

dev/Dockerfile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ RUN apt-get update && apt-get install -y \
2323
cmake \
2424
libssl-dev \
2525
python3 \
26-
python3-pip
27-
26+
python3-pip \
27+
ruby
2828

2929
RUN wget https://github.com/renode/renode/releases/download/v1.15.3/renode-1.15.3.linux-portable.tar.gz
3030
RUN mkdir renode_portable && tar -xvf renode-*.linux-portable.tar.gz -C renode_portable --strip-components=1
3131
ENV PATH $PATH:/renode_portable
3232

33+
RUN wget https://github.com/ThrowTheSwitch/CMock/archive/refs/tags/v2.6.0.tar.gz -O cmock.tar.gz
34+
RUN mkdir cmock_portable && tar -xvf cmock.tar.gz -C cmock_portable --strip-components=1
35+
ENV PATH $PATH:/cmock_portable
36+
37+
RUN wget https://github.com/ThrowTheSwitch/Unity/archive/refs/tags/v2.6.1.tar.gz -O unity.tar.gz
38+
RUN mkdir -p /cmock_portable/vendor/unity && tar -xvf unity.tar.gz -C /cmock_portable/vendor/unity --strip-components=1
39+
3340
# Set up a development tools directory
3441
WORKDIR /home/dev
3542
ADD . /home/dev

general/include/stm32xx_hal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
#include "stm32g4xx_hal.h"
1818
#endif
1919

20-
#endif /* STM32XX_HAL_H*/
20+
#endif /* STM32XX_HAL_H */

0 commit comments

Comments
 (0)