Skip to content

Commit e3d2fd9

Browse files
committed
Issue #32: Migrate to using gTest testing framework
1 parent e6783b2 commit e3d2fd9

2 files changed

Lines changed: 179 additions & 9 deletions

File tree

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
set(CFLAGS -Og -g3 -Wall -Wpedantic -Wno-pointer-to-int-cast)
2+
set(CPPFLAGS -std=c++20 -Og -g3 -Wall -Wpedantic)
23

34
cmake_minimum_required(VERSION 3.25.1)
45

5-
project(buzzer_testing C)
6+
project(buzzer_testing)
7+
8+
include(FetchContent)
9+
FetchContent_Declare(
10+
googletest
11+
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
12+
)
13+
14+
FetchContent_MakeAvailable(googletest)
615

716
add_library(scheduler STATIC src/scheduler.c)
817
add_library(timers STATIC src/timers.c)
@@ -12,7 +21,7 @@ add_library(cmsis_os2 STATIC src/cmsis_os2.c)
1221
add_library(driver STATIC ../driver.c)
1322
add_library(generic STATIC ../generic.c)
1423

15-
add_executable(buzzer-driver test/buzzer-driver.c)
24+
add_executable(buzzer-driver test/buzzer-driver.cpp)
1625

1726
target_include_directories(scheduler PRIVATE . inc)
1827
target_include_directories(timers PRIVATE . inc)
@@ -21,20 +30,16 @@ target_include_directories(stm32f4xx_hal_systick PRIVATE inc)
2130
target_include_directories(driver PRIVATE .. . inc)
2231
target_include_directories(cmsis_os2 PRIVATE . inc)
2332
target_include_directories(generic PRIVATE . inc)
24-
target_include_directories(buzzer-driver PRIVATE .. . inc)
33+
target_include_directories(buzzer-driver PRIVATE .. . inc ${CMAKE_BINARY_DIR}/googletest)
2534

2635
target_link_libraries(scheduler timers)
2736
target_link_libraries(driver timers stm32f4xx_hal_gpio stm32f4xx_hal_systick)
2837
target_link_libraries(cmsis_os2 timers)
29-
target_link_libraries(buzzer-driver scheduler timers stm32f4xx_hal_gpio driver cmsis_os2 generic)
38+
target_link_libraries(buzzer-driver gtest gtest_main scheduler timers stm32f4xx_hal_gpio driver cmsis_os2 generic)
3039

3140
target_compile_options(scheduler PRIVATE ${CFLAGS})
3241
target_compile_options(timers PRIVATE ${CFLAGS})
3342
target_compile_options(stm32f4xx_hal_gpio PRIVATE ${CFLAGS})
3443
target_compile_options(driver PRIVATE ${CFLAGS})
3544
target_compile_options(cmsis_os2 PRIVATE ${CFLAGS})
36-
target_compile_options(buzzer-driver PRIVATE ${CFLAGS})
37-
38-
# include actual driver dir to compile
39-
40-
# then try to compile whole project
45+
target_compile_options(buzzer-driver PRIVATE ${CPPFLAGS})
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#include <iostream>
2+
3+
#include <stdint.h>
4+
#include <string.h>
5+
6+
#include <gtest/gtest.h>
7+
8+
extern "C" {
9+
#include "main.h"
10+
#include "scheduler.h"
11+
#include "cmsis_os2.h"
12+
//#include "test.h"
13+
14+
#include "driver.h"
15+
}
16+
17+
#define LEN(a) sizeof(a) / sizeof(a[0])
18+
19+
#define PREP(...) \
20+
struct BuzzerObject bo; \
21+
struct StaticTimer_t tim; \
22+
memset(&bo, 0, sizeof(bo)); \
23+
memset(&tim, 0, sizeof(tim)); \
24+
system_ticks = 0; \
25+
gpio_init(); \
26+
timers_init(); \
27+
osTimerAttr_t attr = { \
28+
.name = NULL, \
29+
.attr_bits = 0, \
30+
.cb_mem = &tim, \
31+
.cb_size = sizeof(tim) \
32+
}; \
33+
osTimerNew(timer_cb, 1, &bo, &attr); \
34+
osTimerStart(&tim, 10)
35+
36+
void timer_cb(TimerHandle_t const tim_ptr)
37+
{
38+
Buzzer_TimerTask((struct BuzzerObject *) tim_ptr->pvTimerID);
39+
}
40+
41+
TEST(buzzer_driver, configure_correct)
42+
{
43+
PREP();
44+
45+
for (int i = 0x8000; i > 0; i >>= 1)
46+
EXPECT_EQ(Buzzer_ConfigurePort(&bo, GPIOD, i), 0) << i << " fails the configuration" << ::std::endl;
47+
}
48+
49+
TEST(buzzer_driver, configure_wrong)
50+
{
51+
PREP();
52+
53+
uint16_t broken_values[] = {
54+
0xF000,
55+
0xFFFF,
56+
0x0000,
57+
0xA5A5,
58+
0xDEAD,
59+
0xBEEF
60+
};
61+
62+
for (unsigned int i = 0; i < LEN(broken_values); i++)
63+
EXPECT_EQ(BUZZER_PIN_ERROR, Buzzer_ConfigurePort(&bo, GPIOD, broken_values[i])) << i << " does not cause pin error" << ::std::endl;
64+
}
65+
66+
TEST(buzzer_driver, enable_correct)
67+
{
68+
PREP();
69+
70+
EXPECT_EQ(0, Buzzer_ConfigurePort(&bo, GPIOD, 0x8000));
71+
72+
Buzzer_Enable(&bo);
73+
74+
scheduler_run_for(15);
75+
76+
EXPECT_EQ(0x8000, GPIOD->ODR);
77+
}
78+
79+
TEST(buzzer_driver, enable_wrong)
80+
{
81+
PREP();
82+
83+
EXPECT_EQ(BUZZER_PIN_ERROR, Buzzer_ConfigurePort(&bo, GPIOD, 0xF000));
84+
85+
Buzzer_Enable(&bo);
86+
87+
scheduler_run_for(15);
88+
89+
EXPECT_EQ(0x0000, GPIOD->ODR);
90+
}
91+
92+
TEST(buzzer_driver, disable_correct)
93+
{
94+
PREP();
95+
GPIOD->ODR = 0x0001;
96+
97+
EXPECT_EQ(0, Buzzer_ConfigurePort(&bo, GPIOD, 0x0001));
98+
99+
Buzzer_Disable(&bo);
100+
101+
scheduler_run_for(15);
102+
103+
EXPECT_EQ(0x0000, GPIOD->ODR);
104+
}
105+
106+
TEST(buzzer_driver, disable_wrong)
107+
{
108+
PREP();
109+
GPIOD->ODR = 0x0001;
110+
111+
EXPECT_EQ(BUZZER_PIN_ERROR, Buzzer_ConfigurePort(&bo, GPIOD, 0x0101));
112+
113+
Buzzer_Disable(&bo);
114+
115+
scheduler_run_for(15);
116+
117+
EXPECT_EQ(0x0001, GPIOD->ODR);
118+
}
119+
120+
121+
TEST(buzzer_driver, pulse_correct)
122+
{
123+
PREP();
124+
125+
EXPECT_EQ(0, Buzzer_ConfigurePort(&bo, GPIOD, 0x0001));
126+
127+
EXPECT_EQ(0, Buzzer_Pulse(&bo, 200, 500, 2000));
128+
129+
scheduler_run_for(15);
130+
131+
EXPECT_EQ(0x0001, GPIOD->ODR);
132+
133+
scheduler_run_for(210);
134+
135+
EXPECT_EQ(0x0000, GPIOD->ODR);
136+
137+
scheduler_run_for(310);
138+
139+
EXPECT_EQ(0x0001, GPIOD->ODR);
140+
141+
scheduler_run_for(210);
142+
143+
EXPECT_EQ(0x0000, GPIOD->ODR);
144+
145+
scheduler_run_for(1310);
146+
147+
EXPECT_EQ(0x0000, GPIOD->ODR);
148+
}
149+
150+
TEST(buzzer_driver, preemptive_pulse_stop)
151+
{
152+
PREP();
153+
154+
EXPECT_EQ(0, Buzzer_ConfigurePort(&bo, GPIOD, 0x0001));
155+
156+
Buzzer_Pulse(&bo, 100, 200, 1000);
157+
158+
scheduler_run_for(530);
159+
160+
Buzzer_Disable(&bo);
161+
162+
scheduler_run_for(15);
163+
164+
EXPECT_EQ(0x0000, GPIOD->ODR);
165+
}

0 commit comments

Comments
 (0)