Skip to content

Commit 2bcbd77

Browse files
committed
Completed the unit test of wrapper
1 parent a7ea2b4 commit 2bcbd77

2 files changed

Lines changed: 66 additions & 20 deletions

File tree

test/test_picowrapper.cpp

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ TEST(PicoWrapper, i2c_init) {
204204

205205
// Check the data from test spy. Call order.
206206
ASSERT_EQ(fff.call_history[0], (void *)i2c_init);
207-
ASSERT_EQ(fff.call_history[0], (void *)i2c_init);
207+
ASSERT_EQ(fff.call_history[1], (void *)i2c_init);
208208

209209
// Check the data from test spy. DrawRelative Parameters.
210210
ASSERT_EQ(i2c_init_fake.arg0_history[0], &i2c);
@@ -218,28 +218,74 @@ TEST(PicoWrapper, i2c_init) {
218218
TEST(PicoWrapper, i2c_read_blocking) {
219219
PicoWrapper pico;
220220
i2c_inst_t i2c = 17;
221-
const uint baud[] = {3, 100000};
221+
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};
222226

223227
FFF_RESET_HISTORY();
224228
RESET_FAKE(i2c_read_blocking);
225229

226-
uint myReturnVals[] = {1, 2};
227230
SET_RETURN_SEQ(i2c_read_blocking, myReturnVals,
228231
sizeof(myReturnVals) / sizeof(bool));
229232

230-
ASSERT_EQ(pico.i2c_read_blocking(&i2c, baud[0]), myReturnVals[0]);
231-
ASSERT_EQ(pico.i2c_read_blocking(&i2c, baud[1]), myReturnVals[1]);
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]);
237+
}
232238

233239
// Check the data from test spy. How many time called?
234-
ASSERT_EQ(i2c_read_blocking_fake.call_count, 2);
240+
ASSERT_EQ(i2c_read_blocking_fake.call_count, 8);
241+
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]);
251+
}
252+
}
235253

236-
// Check the data from test spy. Call order.
237-
ASSERT_EQ(fff.call_history[0], (void *)i2c_read_blocking);
238-
ASSERT_EQ(fff.call_history[0], (void *)i2c_read_blocking);
254+
// FAKE_VALUE_FUNC(int, i2c_write_blocking, i2c_inst_t *, uint8_t, const uint8_t
255+
// *,
256+
// size_t, bool);
257+
TEST(PicoWrapper, i2c_write_blocking) {
258+
PicoWrapper pico;
259+
i2c_inst_t i2c = 17;
260+
uint8_t buf[10];
261+
uint8_t addrs[8] = {3, 3, 3, 3, 5, 5, 5, 5};
262+
size_t bufsize[8] = {6, 6, 7, 7, 6, 6, 7, 7};
263+
bool nostop[8] = {true, false, true, false, true, false, true, false};
264+
int myReturnVals[] = {1, 2, 3, 4, 5, 6, 7, 8};
239265

240-
// Check the data from test spy. DrawRelative Parameters.
241-
ASSERT_EQ(i2c_read_blocking_fake.arg0_history[0], &i2c);
242-
ASSERT_EQ(i2c_read_blocking_fake.arg1_history[0], baud[0]);
243-
ASSERT_EQ(i2c_read_blocking_fake.arg0_history[1], &i2c);
244-
ASSERT_EQ(i2c_read_blocking_fake.arg1_history[1], baud[1]);
266+
FFF_RESET_HISTORY();
267+
RESET_FAKE(i2c_write_blocking);
268+
269+
SET_RETURN_SEQ(i2c_write_blocking, myReturnVals,
270+
sizeof(myReturnVals) / sizeof(bool));
271+
272+
for (int i = 0; i < 8; i++) {
273+
ASSERT_EQ(
274+
pico.i2c_write_blocking(&i2c, addrs[i], buf, bufsize[i], nostop[i]),
275+
myReturnVals[i]);
276+
}
277+
278+
// Check the data from test spy. How many time called?
279+
ASSERT_EQ(i2c_write_blocking_fake.call_count, 8);
280+
281+
for (int i = 0; i < 8; i++) {
282+
// Check the data from test spy. Call order.
283+
ASSERT_EQ(fff.call_history[i], (void *)i2c_write_blocking);
284+
// Check the data from test spy. DrawRelative Parameters.
285+
ASSERT_EQ(i2c_write_blocking_fake.arg0_history[i], &i2c);
286+
ASSERT_EQ(i2c_write_blocking_fake.arg1_history[i], addrs[i]);
287+
ASSERT_EQ(i2c_write_blocking_fake.arg2_history[i], buf);
288+
ASSERT_EQ(i2c_write_blocking_fake.arg3_history[i], bufsize[i]);
289+
ASSERT_EQ(i2c_write_blocking_fake.arg4_history[i], nostop[i]);
290+
}
245291
}

test/todo/test_picowrapper.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# test_picowrapper TDD
22

3-
- [] Create PicoWrapper.i2c_write_blocking() to fail.
4-
- [] Make it success.
5-
- [] Remove the duplication.
3+
- [x] Create PicoWrapper.i2c_write_blocking() to fail.
4+
- [x] Make it success.
5+
- [x] Remove the duplication.
66

7-
- [] Create PicoWrapper.i2c_read_blocking() to fail.
8-
- [] Make it success.
9-
- [] Remove the duplication.
7+
- [x] Create PicoWrapper.i2c_read_blocking() to fail.
8+
- [x] Make it success.
9+
- [x] Remove the duplication.
1010

1111
- [x] Create PicoWrapper.i2c_init() to fail.
1212
- [x] Make it success.

0 commit comments

Comments
 (0)