File: mock_MeterS0.cpp

package info (click to toggle)
vzlogger 0.8.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,140 kB
  • sloc: cpp: 12,020; sh: 331; ansic: 157; makefile: 25
file content (214 lines) | stat: -rw-r--r-- 6,265 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <gmock/gmock.h>
using ::testing::_;
using ::testing::AtLeast;
using ::testing::Eq;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::ReturnRef;

#include <gtest/gtest.h>
using ::testing::Test;

#include "Meter.hpp"
#include "protocols/MeterS0.hpp"

namespace mock_MeterS0 {

class mock_S0hwif : public MeterS0::HWIF {
  public:
	mock_S0hwif(){};
	virtual ~mock_S0hwif(){};
	MOCK_METHOD0(_open, bool());
	MOCK_METHOD0(_close, bool());
	MOCK_METHOD1(waitForImpulse, bool(bool &));
	MOCK_METHOD0(status, int());
	MOCK_CONST_METHOD0(is_blocking, bool());

  protected:
};

TEST(mock_MeterS0, timespec_add_ms) {
	struct timespec a;
	a.tv_sec = 1;
	a.tv_nsec = 0;
	timespec_add_ms(a, 1001);
	ASSERT_EQ(2, a.tv_sec);
	ASSERT_EQ(1000000, a.tv_nsec);
}

TEST(mock_MeterS0, timespec_add_ms2) {
	struct timespec a;
	a.tv_sec = 1;
	a.tv_nsec = 1000000000;
	timespec_add_ms(a, 1000);
	EXPECT_EQ(3, a.tv_sec);
	ASSERT_EQ(0, a.tv_nsec);
}

TEST(mock_MeterS0, timespec_add_ms3) {
	struct timespec a;
	a.tv_sec = 1;
	a.tv_nsec = 1001000000;
	timespec_add_ms(a, 1999);
	EXPECT_EQ(4, a.tv_sec);
	EXPECT_EQ(0, a.tv_nsec);
}

TEST(mock_MeterS0, timespec_sub_ms) {
	struct timespec a;
	a.tv_sec = 1;
	a.tv_nsec = 0;
	struct timespec b;
	b = a;
	timespec_add_ms(a, 1999);
	EXPECT_EQ(1999ul, timespec_sub_ms(a, b));
}

TEST(mock_MeterS0, timespec_sub_ms2) {
	struct timespec a;
	a.tv_sec = 2;
	a.tv_nsec = 0;
	struct timespec b;
	b.tv_sec = 1;
	b.tv_nsec = 1000000;
	EXPECT_EQ(999ul, timespec_sub_ms(a, b));
}

TEST(mock_MeterS0, basic_noopen) {
	mock_S0hwif *hwif = new mock_S0hwif();
	std::list<Option> opt;

	ASSERT_NO_THROW(MeterS0 m(opt, hwif));
}

TEST(mock_MeterS0, basic_open_fail) {
	mock_S0hwif *hwif = new mock_S0hwif();
	std::list<Option> opt;

	EXPECT_CALL(*hwif, _open()).Times(1).WillRepeatedly(Return(false));
	EXPECT_CALL(*hwif, _close()).Times(1).WillOnce(Return(false));
	MeterS0 m(opt, hwif);
	ASSERT_EQ(ERR, m.open());
	m.close(); // this might be called and should not cause problems
}

TEST(mock_MeterS0, basic_blocking) {
	mock_S0hwif *hwif = new mock_S0hwif();
	std::list<Option> opt;
	opt.push_back(Option("send_zero", true));

	EXPECT_CALL(*hwif, _open()).Times(1).WillRepeatedly(Return(true));
	EXPECT_CALL(*hwif, _close()).Times(1).WillOnce(Return(true));
	EXPECT_CALL(*hwif, is_blocking()).Times(2).WillRepeatedly(Return(true));
	MeterS0 m(opt, hwif);
	ASSERT_EQ(SUCCESS, m.open());
	m.close(); // this might be called and should not cause problems
}

TEST(mock_MeterS0, basic_blocking_no_send_zero) {
	mock_S0hwif *hwif = new mock_S0hwif();
	std::list<Option> opt;
	opt.push_back(Option("send_zero", false));

	EXPECT_CALL(*hwif, _open()).Times(1).WillRepeatedly(Return(true));
	EXPECT_CALL(*hwif, _close()).Times(1).WillOnce(Return(true));
	EXPECT_CALL(*hwif, is_blocking()).Times(2).WillRepeatedly(Return(true));
	MeterS0 m(opt, hwif);
	ASSERT_EQ(SUCCESS, m.open());
	m.close(); // this might be called and should not cause problems
}

TEST(mock_MeterS0, basic_non_blocking) {
	mock_S0hwif *hwif = new mock_S0hwif();
	std::list<Option> opt;
	opt.push_back(Option("send_zero", true));

	EXPECT_CALL(*hwif, _open()).Times(1).WillRepeatedly(Return(true));
	EXPECT_CALL(*hwif, _close()).Times(1).WillOnce(Return(true));
	EXPECT_CALL(*hwif, is_blocking()).Times(2).WillRepeatedly(Return(false));
	EXPECT_CALL(*hwif, status()).Times(AtLeast(0)).WillRepeatedly(Return(0));
	MeterS0 m(opt, hwif);
	ASSERT_EQ(SUCCESS, m.open());
	m.close(); // this might be called and should not cause problems
}

TEST(mock_MeterS0, basic_non_blocking_read) {
	mock_S0hwif *hwif = new mock_S0hwif();
	std::list<Option> opt;
	opt.push_back(Option("send_zero", true));

	EXPECT_CALL(*hwif, _open()).Times(1).WillRepeatedly(Return(true));
	EXPECT_CALL(*hwif, _close()).Times(1).WillOnce(Return(true));
	EXPECT_CALL(*hwif, is_blocking()).Times(3).WillRepeatedly(Return(false));
	EXPECT_CALL(*hwif, status()).Times(AtLeast(1)).WillRepeatedly(Return(0));
	MeterS0 m(opt, hwif);
	ASSERT_EQ(SUCCESS, m.open());
	std::vector<Reading> rds(4);
	ASSERT_EQ(m.read(rds, 4), 2); // no Powers for first impulse

	m.close(); // this might be called and should not cause problems
}

TEST(mock_MeterS0, basic_blocking_send_zero_2reads) {
	mock_S0hwif *hwif = new mock_S0hwif();
	std::list<Option> opt;
	opt.push_back(Option("send_zero", true));

	EXPECT_CALL(*hwif, _open()).Times(1).WillRepeatedly(Return(true));
	EXPECT_CALL(*hwif, _close()).Times(1).WillOnce(Return(true));
	EXPECT_CALL(*hwif, is_blocking()).Times(4).WillRepeatedly(Return(true));
	EXPECT_CALL(*hwif, waitForImpulse(_)).Times(AtLeast(1)).WillRepeatedly(Return(false));
	MeterS0 m(opt, hwif);
	ASSERT_EQ(SUCCESS, m.open());
	std::vector<Reading> rds(4);
	ASSERT_EQ(m.read(rds, 4), 2); // no Powers for first impulse
	std::vector<Reading> rds2(4);
	ASSERT_EQ(m.read(rds2, 4), 4); // no Powers for first impulse
	// check that time has roughly 1s distance and values reported are zero:
	ASSERT_EQ(rds[0].value(), 0);
	ASSERT_EQ(rds[1].value(), 0);
	ASSERT_EQ(rds2[0].value(), 0);
	ASSERT_EQ(rds2[1].value(), 0);

	m.close(); // this might be called and should not cause problems
}

/* time out -> endless waiting for first impulse
TEST(mock_MeterS0, basic_non_blocking_read_no_send_zero)
{
	mock_S0hwif *hwif = new mock_S0hwif();
	std::list<Option> opt;
	opt.push_back(Option("send_zero", false));

	EXPECT_CALL(*hwif, _open()).Times(1).WillRepeatedly(Return(true));
	EXPECT_CALL(*hwif, _close()).Times(1).WillOnce(Return(true));
	EXPECT_CALL(*hwif, is_blocking()).Times(2).WillRepeatedly(Return(false));
	MeterS0 m(opt, hwif);
	ASSERT_EQ(SUCCESS, m.open());
	std::vector<Reading> rds(4);
	ASSERT_EQ(m.read(rds, 4), 0);

	m.close(); // this might be called and should not cause problems
} */

} // namespace mock_MeterS0

void print(log_level_t l, char const *s1, char const *s2, ...) {
	//	if (l!= log_debug)
	{
		fprintf(stdout, "\n  %s:", s2);
		va_list argp;
		va_start(argp, s2);
		vfprintf(stdout, s1, argp);
		va_end(argp);
		fprintf(stdout, "\n");
	}
}

int main(int argc, char **argv) {
	// The following line must be executed to initialize Google Mock
	// (and Google Test) before running the tests.
	::testing::InitGoogleMock(&argc, argv);
	return RUN_ALL_TESTS();
}