Skip to content

Commit 242d45e

Browse files
committed
Refined test up to i2cRefined test up to i2c_read_blocking
1 parent a5187f3 commit 242d45e

2 files changed

Lines changed: 93 additions & 90 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ enable_testing()
1010

1111

1212
# GoogleTest requires at least C++14
13-
set(CMAKE_CXX_STANDARD 14)
13+
# To use std::size(), we need c++17
14+
set(CMAKE_CXX_STANDARD 17)
1415
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1516

1617
include(FetchContent)

test/test_picowrapper.cpp

Lines changed: 91 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -36,99 +36,89 @@ FAKE_VALUE_FUNC(int, i2c_write_blocking, i2c_inst_t *, uint8_t, const uint8_t *,
3636

3737
TEST(PicoWrapper, gpio_init) {
3838
PicoWrapper pico;
39-
uint gpio = 17;
39+
uint gpioarray[] = {17, 11};
4040

4141
FFF_RESET_HISTORY();
4242
RESET_FAKE(gpio_init);
4343

44-
pico.gpio_init(gpio);
44+
for (auto &&gpio : gpioarray) {
45+
pico.gpio_init(gpio);
46+
}
4547

4648
// Check the data from test spy. How many time called?
47-
ASSERT_EQ(gpio_init_fake.call_count, 1);
48-
49-
// Check the data from test spy. Call order.
50-
ASSERT_EQ(fff.call_history[0], (void *)gpio_init);
49+
ASSERT_EQ(gpio_init_fake.call_count, std::size(gpioarray));
5150

52-
// Check the data from test spy. DrawRelative Parameters.
53-
ASSERT_EQ(gpio_init_fake.arg0_history[0], gpio);
54-
55-
// Try another parameter
56-
FFF_RESET_HISTORY();
57-
RESET_FAKE(gpio_init);
58-
59-
gpio = 11;
60-
pico.gpio_init(gpio);
61-
62-
// Check the data from test spy. DrawRelative Parameters.
63-
ASSERT_EQ(gpio_init_fake.arg0_history[0], gpio);
51+
int index = 0;
52+
for (auto &&gpio : gpioarray) {
53+
// Check the data from test spy. Call order.
54+
ASSERT_EQ(fff.call_history[index], (void *)gpio_init);
55+
// Check the data from test spy. DrawRelative Parameters.
56+
ASSERT_EQ(gpio_init_fake.arg0_history[index], gpio);
57+
index++;
58+
}
6459
}
6560

6661
TEST(PicoWrapper, gpio_set_dir) {
6762
PicoWrapper pico;
68-
uint gpio = 17;
69-
bool dir = true;
63+
uint gpioarray[] = {17, 11};
64+
bool dirarray[] = {true, false};
7065

7166
FFF_RESET_HISTORY();
7267
RESET_FAKE(gpio_set_dir);
7368

74-
pico.gpio_set_dir(gpio, dir);
69+
for (auto &&gpio : gpioarray) {
70+
for (auto &&dir : dirarray) {
71+
pico.gpio_set_dir(gpio, dir);
72+
}
73+
}
7574

7675
// Check the data from test spy. How many time called?
77-
ASSERT_EQ(gpio_set_dir_fake.call_count, 1);
78-
79-
// Check the data from test spy. Call order.
80-
ASSERT_EQ(fff.call_history[0], (void *)gpio_set_dir);
81-
82-
// Check the data from test spy. DrawRelative Parameters.
83-
ASSERT_EQ(gpio_set_dir_fake.arg0_history[0], gpio);
84-
ASSERT_EQ(gpio_set_dir_fake.arg1_history[0], dir);
85-
86-
FFF_RESET_HISTORY();
87-
RESET_FAKE(gpio_set_dir);
88-
89-
// Test another parameters.
90-
gpio = 11;
91-
dir = false;
92-
93-
pico.gpio_set_dir(gpio, dir);
94-
95-
// Check the data from test spy. DrawRelative Parameters.
96-
ASSERT_EQ(gpio_set_dir_fake.arg0_history[0], gpio);
97-
ASSERT_EQ(gpio_set_dir_fake.arg1_history[0], dir);
76+
ASSERT_EQ(gpio_set_dir_fake.call_count,
77+
std::size(gpioarray) * std::size(dirarray));
78+
79+
uint index = 0;
80+
for (auto &&gpio : gpioarray) {
81+
for (auto &&dir : dirarray) {
82+
// Check the data from test spy. Call order.
83+
ASSERT_EQ(fff.call_history[index], (void *)gpio_set_dir);
84+
// Check the data from test spy. DrawRelative Parameters.
85+
ASSERT_EQ(gpio_set_dir_fake.arg0_history[index], gpio);
86+
ASSERT_EQ(gpio_set_dir_fake.arg1_history[index], dir);
87+
index++;
88+
}
89+
}
9890
}
9991

10092
TEST(PicoWrapper, gpio_put) {
10193
PicoWrapper pico;
102-
uint gpio = 17;
103-
bool value = true;
94+
uint gpioarray[] = {17, 11};
95+
bool valuearray[] = {true, false};
10496

10597
FFF_RESET_HISTORY();
10698
RESET_FAKE(gpio_put);
10799

108-
pico.gpio_put(gpio, value);
100+
for (auto &&gpio : gpioarray) {
101+
for (auto &&value : valuearray) {
102+
pico.gpio_put(gpio, value);
103+
}
104+
}
109105

110106
// Check the data from test spy. How many time called?
111-
ASSERT_EQ(gpio_put_fake.call_count, 1);
112-
113-
// Check the data from test spy. Call order.
114-
ASSERT_EQ(fff.call_history[0], (void *)gpio_put);
115-
116-
// Check the data from test spy. DrawRelative Parameters.
117-
ASSERT_EQ(gpio_put_fake.arg0_history[0], gpio);
118-
ASSERT_EQ(gpio_put_fake.arg1_history[0], value);
119-
120-
FFF_RESET_HISTORY();
121-
RESET_FAKE(gpio_put);
122-
123-
// Test another parameters.
124-
gpio = 11;
125-
value = false;
126-
127-
pico.gpio_put(gpio, value);
128-
129-
// Check the data from test spy. DrawRelative Parameters.
130-
ASSERT_EQ(gpio_put_fake.arg0_history[0], gpio);
131-
ASSERT_EQ(gpio_put_fake.arg1_history[0], value);
107+
ASSERT_EQ(gpio_put_fake.call_count,
108+
std::size(gpioarray) * std::size(valuearray));
109+
110+
// Test all combination of the parameter.
111+
uint index = 0;
112+
for (auto &&gpio : gpioarray) {
113+
for (auto &&value : valuearray) {
114+
// Check the data from test spy. Call order.
115+
ASSERT_EQ(fff.call_history[index], (void *)gpio_put);
116+
// Check the data from test spy. DrawRelative Parameters.
117+
ASSERT_EQ(gpio_put_fake.arg0_history[index], gpio);
118+
ASSERT_EQ(gpio_put_fake.arg1_history[index], value);
119+
index++;
120+
}
121+
}
132122
}
133123

134124
TEST(PicoWrapper, gpio_get) {
@@ -219,41 +209,53 @@ TEST(PicoWrapper, i2c_read_blocking) {
219209
PicoWrapper pico;
220210
i2c_inst_t i2c = 17;
221211
uint8_t buf[10];
222-
uint8_t addrs[8] = {3, 3, 3, 3, 5, 5, 5, 5};
223-
size_t bufsize[8] = {6, 6, 7, 7, 6, 6, 7, 7};
224-
bool nostop[8] = {true, false, true, false, true, false, true, false};
225-
int myReturnVals[] = {1, 2, 3, 4, 5, 6, 7, 8};
212+
uint8_t addrs_array[] = {3, 5};
213+
size_t bufsize_array[2] = {2, 7};
214+
bool nostop_array[2] = {true, false};
215+
int myReturnVals_array[8] = {1, 2, 3, 4, 5, 6, 7, 8};
226216

227217
FFF_RESET_HISTORY();
228218
RESET_FAKE(i2c_read_blocking);
229219

230-
SET_RETURN_SEQ(i2c_read_blocking, myReturnVals,
231-
sizeof(myReturnVals) / sizeof(bool));
232-
233-
for (int i = 0; i < 8; i++) {
234-
ASSERT_EQ(
235-
pico.i2c_read_blocking(&i2c, addrs[i], buf, bufsize[i], nostop[i]),
236-
myReturnVals[i]);
220+
SET_RETURN_SEQ(i2c_read_blocking, myReturnVals_array,
221+
std::size(myReturnVals_array));
222+
223+
// Check wether return value is correct.
224+
int index = 0;
225+
for (auto &&addrs : addrs_array) {
226+
for (auto &&bufsize : bufsize_array) {
227+
for (auto &&nostop : nostop_array) {
228+
ASSERT_EQ(pico.i2c_read_blocking(&i2c, addrs, buf, bufsize, nostop),
229+
myReturnVals_array[index]);
230+
index++;
231+
}
232+
}
237233
}
238234

239235
// Check the data from test spy. How many time called?
240236
ASSERT_EQ(i2c_read_blocking_fake.call_count, 8);
241237

242-
for (int i = 0; i < 8; i++) {
243-
// Check the data from test spy. Call order.
244-
ASSERT_EQ(fff.call_history[i], (void *)i2c_read_blocking);
245-
// Check the data from test spy. DrawRelative Parameters.
246-
ASSERT_EQ(i2c_read_blocking_fake.arg0_history[i], &i2c);
247-
ASSERT_EQ(i2c_read_blocking_fake.arg1_history[i], addrs[i]);
248-
ASSERT_EQ(i2c_read_blocking_fake.arg2_history[i], buf);
249-
ASSERT_EQ(i2c_read_blocking_fake.arg3_history[i], bufsize[i]);
250-
ASSERT_EQ(i2c_read_blocking_fake.arg4_history[i], nostop[i]);
238+
// Check wether return value is correct.
239+
index = 0;
240+
for (auto &&addrs : addrs_array) {
241+
for (auto &&bufsize : bufsize_array) {
242+
for (auto &&nostop : nostop_array) {
243+
// Check the data from test spy. Call order.
244+
ASSERT_EQ(fff.call_history[index], (void *)i2c_read_blocking);
245+
// Check the data from test spy. DrawRelative Parameters.
246+
ASSERT_EQ(i2c_read_blocking_fake.arg0_history[index], &i2c);
247+
ASSERT_EQ(i2c_read_blocking_fake.arg1_history[index], addrs);
248+
ASSERT_EQ(i2c_read_blocking_fake.arg2_history[index], buf);
249+
ASSERT_EQ(i2c_read_blocking_fake.arg3_history[index], bufsize);
250+
ASSERT_EQ(i2c_read_blocking_fake.arg4_history[index], nostop);
251+
index++;
252+
}
253+
}
251254
}
252255
}
253256

254-
// FAKE_VALUE_FUNC(int, i2c_write_blocking, i2c_inst_t *, uint8_t, const uint8_t
255-
// *,
256-
// size_t, bool);
257+
// FAKE_VALUE_FUNC(int, i2c_write_blocking, i2c_inst_t *, uint8_t,
258+
// const uint8_t *, size_t, bool);
257259
TEST(PicoWrapper, i2c_write_blocking) {
258260
PicoWrapper pico;
259261
i2c_inst_t i2c = 17;

0 commit comments

Comments
 (0)