Skip to content

Commit 6068192

Browse files
committed
Merge branch 'feature/22' into develop
2 parents 5d99972 + ba4d2a6 commit 6068192

2 files changed

Lines changed: 60 additions & 66 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The issue #12 removed public class I2cMasterInterface. But it is referred intern
1212
- [Issue #10](https://github.com/suikan4github/rpp_driver/issues/10) Cover APIs of the Raspberry Pi Pico SDK.
1313
- [Issue #15](https://github.com/suikan4github/rpp_driver/issues/15) Remove the redundant parameter check code from ConfigureSRC()
1414
- [Issue #16](https://github.com/suikan4github/rpp_driver/issues/16) Change the member function name from ConfigureSRC() to ConfigureSrc().
15+
- [Issue #22](https://github.com/suikan4github/rpp_driver/issues/22) Refactor the test_i2cmaster.cpp to use the fixture.
1516
### Deprecated
1617
- [Issue #12](https://github.com/suikan4github/rpp_driver/issues/12) Is I2cMasterInterface needed?
1718
### Removed

test/test_i2cmaster.cpp

Lines changed: 59 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,33 @@
33

44
#include "i2c/i2cmaster.hpp"
55

6+
using testing::_;
7+
using testing::InSequence;
68
using testing::Return;
79

10+
class I2cMasterTest : public ::testing::Test {
11+
protected:
12+
::rpp_driver::MockSdkWrapper sdk_;
13+
i2c_inst_t i2c_inst_ = 0;
14+
::rpp_driver::I2cMaster *i2c_;
15+
16+
virtual void SetUp() {
17+
// Constructor will call these functiions.
18+
EXPECT_CALL(sdk_, i2c_init(&i2c_inst_, _));
19+
EXPECT_CALL(sdk_, gpio_set_function(_, GPIO_FUNC_I2C)).Times(2);
20+
EXPECT_CALL(sdk_, gpio_pull_up(_)).Times(2);
21+
22+
i2c_ = new ::rpp_driver::I2cMaster(sdk_, i2c_inst_, 100 * 1000, 17, 23);
23+
}
24+
25+
virtual void TearDown() {
26+
// We can ignore these call inside destructor
27+
EXPECT_CALL(sdk_, i2c_deinit(_));
28+
29+
delete i2c_;
30+
}
31+
}; // I2cMasterTest
32+
833
// Constructor test
934
TEST(I2cMaster, Constructor) {
1035
::rpp_driver::MockSdkWrapper sdk;
@@ -52,94 +77,62 @@ TEST(I2cMaster, Destructor) {
5277

5378
} // Destructor
5479

55-
TEST(I2cMaster, ReadBlocking) {
56-
::rpp_driver::MockSdkWrapper sdk;
57-
i2c_inst_t i2c_inst;
58-
80+
TEST_F(I2cMasterTest, ReadBlocking) {
5981
uint8_t addr = 3;
6082
uint8_t buf[5];
6183
bool nostop = true;
6284
int return_value = 11;
6385

64-
using ::testing::_;
65-
66-
// We can ignore these call inside constructor
67-
EXPECT_CALL(sdk, i2c_init(&i2c_inst, _));
68-
EXPECT_CALL(sdk, gpio_set_function(_, GPIO_FUNC_I2C)).Times(2);
69-
EXPECT_CALL(sdk, gpio_pull_up(_)).Times(2);
70-
71-
::rpp_driver::I2cMaster i2c(sdk, i2c_inst, 100 * 1000, 17, 23);
72-
73-
EXPECT_CALL(sdk, i2c_read_blocking(&i2c_inst, addr, buf, sizeof(buf), nostop))
74-
.WillOnce(Return(return_value));
86+
{
87+
InSequence dummy;
7588

76-
EXPECT_CALL(sdk,
77-
i2c_read_blocking(&i2c_inst, addr, buf, sizeof(buf), !nostop))
78-
.WillOnce(Return(return_value));
89+
EXPECT_CALL(sdk_,
90+
i2c_read_blocking(&i2c_inst_, addr, buf, sizeof(buf), nostop))
91+
.WillOnce(Return(return_value));
7992

80-
EXPECT_EQ(i2c.ReadBlocking(addr, buf, sizeof(buf), nostop), return_value);
81-
EXPECT_EQ(i2c.ReadBlocking(addr, buf, sizeof(buf), !nostop), return_value);
82-
// We can ignore these call inside destructor
83-
EXPECT_CALL(sdk, i2c_deinit(_));
93+
EXPECT_CALL(sdk_,
94+
i2c_read_blocking(&i2c_inst_, addr, buf, sizeof(buf), !nostop))
95+
.WillOnce(Return(return_value));
96+
}
97+
EXPECT_EQ(i2c_->ReadBlocking(addr, buf, sizeof(buf), nostop), return_value);
98+
EXPECT_EQ(i2c_->ReadBlocking(addr, buf, sizeof(buf), !nostop), return_value);
8499
} // ReadBlocking
85100

86-
TEST(I2cMaster, WriteBlocking) {
87-
::rpp_driver::MockSdkWrapper sdk;
88-
i2c_inst_t i2c_inst;
89-
101+
TEST_F(I2cMasterTest, WriteBlocking) {
90102
uint8_t addr = 3;
91103
uint8_t buf[5];
92104
bool nostop = true;
93105
int return_value = 11;
94106

95-
using ::testing::_;
96-
97-
// We can ignore these call inside constructor
98-
EXPECT_CALL(sdk, i2c_init(&i2c_inst, _));
99-
EXPECT_CALL(sdk, gpio_set_function(_, GPIO_FUNC_I2C)).Times(2);
100-
EXPECT_CALL(sdk, gpio_pull_up(_)).Times(2);
101-
102-
::rpp_driver::I2cMaster i2c(sdk, i2c_inst, 100 * 1000, 17, 23);
107+
{
108+
InSequence dummy;
103109

104-
EXPECT_CALL(sdk,
105-
i2c_write_blocking(&i2c_inst, addr, buf, sizeof(buf), nostop))
106-
.WillOnce(Return(return_value));
107-
EXPECT_CALL(sdk,
108-
i2c_write_blocking(&i2c_inst, addr, buf, sizeof(buf), !nostop))
109-
.WillOnce(Return(return_value));
110+
EXPECT_CALL(sdk_,
111+
i2c_write_blocking(&i2c_inst_, addr, buf, sizeof(buf), nostop))
112+
.WillOnce(Return(return_value));
113+
EXPECT_CALL(sdk_,
114+
i2c_write_blocking(&i2c_inst_, addr, buf, sizeof(buf), !nostop))
115+
.WillOnce(Return(return_value));
116+
}
117+
EXPECT_EQ(i2c_->WriteBlocking(addr, buf, sizeof(buf), nostop), return_value);
118+
EXPECT_EQ(i2c_->WriteBlocking(addr, buf, sizeof(buf), !nostop), return_value);
110119

111-
EXPECT_EQ(i2c.WriteBlocking(addr, buf, sizeof(buf), nostop), return_value);
112-
EXPECT_EQ(i2c.WriteBlocking(addr, buf, sizeof(buf), !nostop), return_value);
113-
// We can ignore these call inside destructor
114-
EXPECT_CALL(sdk, i2c_deinit(_));
115120
} // WriteBlocking
116121

117-
TEST(I2cMaster, IsDeviceExisting) {
118-
::rpp_driver::MockSdkWrapper sdk;
119-
i2c_inst_t i2c_inst;
120-
122+
TEST_F(I2cMasterTest, IsDeviceExisting) {
121123
uint8_t addr[] = {17, 23};
122124
bool nostop = false;
123125
int return_value[] = {-1, 1};
124126

125-
using ::testing::_;
126-
127-
// We can ignore these call inside constructor
128-
EXPECT_CALL(sdk, i2c_init(&i2c_inst, _));
129-
EXPECT_CALL(sdk, gpio_set_function(_, GPIO_FUNC_I2C)).Times(2);
130-
EXPECT_CALL(sdk, gpio_pull_up(_)).Times(2);
131-
132-
::rpp_driver::I2cMaster i2c(sdk, i2c_inst, 100 * 1000, 17, 23);
127+
{
128+
InSequence dummy;
133129

134-
EXPECT_CALL(sdk, i2c_read_blocking(_, addr[0], _, 1, nostop))
135-
.WillOnce(Return(return_value[0]));
130+
EXPECT_CALL(sdk_, i2c_read_blocking(_, addr[0], _, 1, nostop))
131+
.WillOnce(Return(return_value[0]));
136132

137-
EXPECT_CALL(sdk, i2c_read_blocking(_, addr[1], _, 1, nostop))
138-
.WillOnce(Return(return_value[1]));
139-
140-
EXPECT_FALSE(i2c.IsDeviceExisting(addr[0]));
141-
EXPECT_TRUE(i2c.IsDeviceExisting(addr[1]));
142-
143-
// We can ignore these call inside destructor
144-
EXPECT_CALL(sdk, i2c_deinit(_));
133+
EXPECT_CALL(sdk_, i2c_read_blocking(_, addr[1], _, 1, nostop))
134+
.WillOnce(Return(return_value[1]));
135+
}
136+
EXPECT_FALSE(i2c_->IsDeviceExisting(addr[0]));
137+
EXPECT_TRUE(i2c_->IsDeviceExisting(addr[1]));
145138
}

0 commit comments

Comments
 (0)