File: ut_MeterFile.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 (221 lines) | stat: -rw-r--r-- 6,526 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
215
216
217
218
219
220
221
#include "gtest/gtest.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "Options.hpp"
#include "protocols/MeterFile.hpp"

// this is a dirty hack. we should think about better ways/rules to link against the
// test objects.
// e.g. single files in the tests directory that just include the real file. This way one
// will avoid multiple includes of the same file in a different test case
// (or linking against the real file from the CMakeLists.txt)

// #include "../src/protocols/MeterFile.cpp"

int writes(int fd, const char *str);

TEST(MeterFile, basic) {
	char tempfilename[L_tmpnam + 1];
	ASSERT_NE(tmpnam(tempfilename), (char *)0);
	std::list<Option> options;
	options.push_back(Option("path", tempfilename));
	options.push_back(Option("interval", 1));

	// test without format option
	MeterFile m(options);
	ASSERT_STREQ(m.path(), tempfilename) << "devicename not eq " << tempfilename;
	ASSERT_EQ(0, mkfifo(tempfilename, S_IRUSR | S_IWUSR));
	int fd = open(tempfilename, O_RDWR);
	ASSERT_NE(fd, -1);
	ASSERT_EQ(SUCCESS, m.open());

	// now we can simulate some input by simply writing into fd
	std::vector<Reading> rds;
	rds.resize(10);

	// write two good data set
	writes(fd, "32552\r\n");
	writes(fd, "32552.5\r\n");
	//	writes(fd, "32552.6\r\n"); // bug: with 2 only it hangs! Fixed with changing order in while
	//(i<n...) in MeterFile.cpp

	// now read two readings:
	EXPECT_EQ(2, m.read(rds, 2));
	// check obis data:
	ReadingIdentifier *p = rds[0].identifier().get();
	double value = rds[0].value();
	EXPECT_EQ(32552, value);
	StringIdentifier *o = dynamic_cast<StringIdentifier *>(p);
	ASSERT_NE((StringIdentifier *)0, o);
	EXPECT_EQ(StringIdentifier(""), *o);

	value = rds[1].value();
	p = rds[1].identifier().get();
	o = dynamic_cast<StringIdentifier *>(p);
	EXPECT_EQ(32552.5, value);
	ASSERT_NE((StringIdentifier *)0, o);
	EXPECT_EQ(StringIdentifier(""), *o);

	EXPECT_EQ(0, m.close());

	EXPECT_EQ(0, close(fd));
	EXPECT_EQ(0, unlink(tempfilename));
}

TEST(MeterFile, format1) {
	char tempfilename[L_tmpnam + 1];
	ASSERT_NE(tmpnam(tempfilename), (char *)0);
	std::list<Option> options;
	options.push_back(Option("path", tempfilename));
	options.push_back(Option("format", (char *)"$v"));
	options.push_back(Option("interval", 1));

	MeterFile m(options);
	ASSERT_STREQ(m.path(), tempfilename) << "devicename not eq " << tempfilename;
	ASSERT_EQ(0, mkfifo(tempfilename, S_IRUSR | S_IWUSR));
	int fd = open(tempfilename, O_RDWR);
	ASSERT_NE(fd, -1);
	ASSERT_EQ(SUCCESS, m.open());

	// now we can simulate some input by simply writing into fd
	std::vector<Reading> rds;
	rds.resize(10);

	// write two good data set
	writes(fd, "32552\r\n");
	writes(fd, "32552.5\r\n");

	// now read two readings:
	EXPECT_EQ(2, m.read(rds, 2));
	// check obis data:
	ReadingIdentifier *p = rds[0].identifier().get();
	double value = rds[0].value();
	EXPECT_EQ(32552, value);
	StringIdentifier *o = dynamic_cast<StringIdentifier *>(p);
	ASSERT_NE((StringIdentifier *)0, o);
	EXPECT_EQ(StringIdentifier("<null>"), *o);

	value = rds[1].value();
	p = rds[1].identifier().get();
	o = dynamic_cast<StringIdentifier *>(p);
	EXPECT_EQ(32552.5, value);
	ASSERT_NE((StringIdentifier *)0, o);
	EXPECT_EQ(StringIdentifier("<null>"), *o);

	EXPECT_EQ(0, m.close());

	EXPECT_EQ(0, close(fd));
	EXPECT_EQ(0, unlink(tempfilename));
}

TEST(MeterFile, format2) {
	char tempfilename[L_tmpnam + 1];
	ASSERT_NE(tmpnam(tempfilename), (char *)0);
	std::list<Option> options;
	options.push_back(Option("path", tempfilename));
	options.push_back(Option("format", (char *)"$i : $v"));
	options.push_back(Option("interval", 1));

	MeterFile m(options);
	ASSERT_STREQ(m.path(), tempfilename) << "devicename not eq " << tempfilename;
	ASSERT_EQ(0, mkfifo(tempfilename, S_IRUSR | S_IWUSR));
	int fd = open(tempfilename, O_RDWR);
	ASSERT_NE(fd, -1);
	ASSERT_EQ(SUCCESS, m.open());

	// now we can simulate some input by simply writing into fd
	std::vector<Reading> rds;
	rds.resize(2);

	// write two good data set
	writes(fd, "id1 : 32552\r\n");
	writes(fd, "id2 : 32552.5\r\n");

	// now read two readings:
	EXPECT_EQ(2, m.read(rds, 2));
	// check obis data:
	ReadingIdentifier *p = rds[0].identifier().get();
	double value = rds[0].value();
	EXPECT_EQ(32552, value);
	StringIdentifier *o = dynamic_cast<StringIdentifier *>(p);
	ASSERT_NE((StringIdentifier *)0, o);
	EXPECT_EQ(StringIdentifier("id1"), *o);

	value = rds[1].value();
	p = rds[1].identifier().get();
	o = dynamic_cast<StringIdentifier *>(p);
	EXPECT_EQ(32552.5, value);
	ASSERT_NE((StringIdentifier *)0, o);
	EXPECT_EQ(StringIdentifier("id2"), *o);

	EXPECT_EQ(0, m.close());

	EXPECT_EQ(0, close(fd));
	EXPECT_EQ(0, unlink(tempfilename));
}

TEST(MeterFile, reading_times) {
	Reading r;
	struct timeval t;
	t.tv_sec = 1001;
	t.tv_usec = 1;
	r.time(t);
	ASSERT_EQ(1001000ll, r.time_ms()) << r.time_ms();
	t.tv_usec = 1000;
	r.time(t);
	ASSERT_EQ(1001001ll, r.time_ms()) << r.time_ms();

	r.time_from_double(1002.00001);
	ASSERT_EQ(1002000ll, r.time_ms()) << r.time_ms();
}

TEST(MeterFile, format3) {
	char tempfilename[L_tmpnam + 1];
	ASSERT_NE(tmpnam(tempfilename), (char *)0);
	std::list<Option> options;
	options.push_back(Option("path", tempfilename));
	options.push_back(Option("format", (char *)"$t;$i : $v"));
	options.push_back(Option("interval", 1));

	MeterFile m(options);
	ASSERT_STREQ(m.path(), tempfilename) << "devicename not eq " << tempfilename;
	ASSERT_EQ(0, mkfifo(tempfilename, S_IRUSR | S_IWUSR));
	int fd = open(tempfilename, O_RDWR);
	ASSERT_NE(fd, -1);
	ASSERT_EQ(SUCCESS, m.open());

	// now we can simulate some input by simply writing into fd
	std::vector<Reading> rds;
	rds.resize(2);

	// write two good data set
	writes(fd, "1001.2;id1 : 32552\r\n");
	writes(fd, "1002.5;id2 : 32552.5\r\n");

	// now read two readings:
	EXPECT_EQ(2, m.read(rds, 2));
	// check obis data:
	ReadingIdentifier *p = rds[0].identifier().get();
	double value = rds[0].value();
	EXPECT_EQ(32552, value);
	EXPECT_EQ(1001200ll, rds[0].time_ms()) << rds[0].time_ms();

	StringIdentifier *o = dynamic_cast<StringIdentifier *>(p);
	ASSERT_NE((StringIdentifier *)0, o);
	EXPECT_EQ(StringIdentifier("id1"), *o);

	value = rds[1].value();
	p = rds[1].identifier().get();
	o = dynamic_cast<StringIdentifier *>(p);
	EXPECT_EQ(32552.5, value);
	EXPECT_EQ(1002500ll, rds[1].time_ms());
	ASSERT_NE((StringIdentifier *)0, o);
	EXPECT_EQ(StringIdentifier("id2"), *o);

	EXPECT_EQ(0, m.close());

	EXPECT_EQ(0, close(fd));
	EXPECT_EQ(0, unlink(tempfilename));
}