31 #include <gtest/gtest.h> 36 TEST(bin_parser, parse_string)
38 uint8_t buffer[] = { 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x20, 0x74, 0x6f, 0x20,
39 0x62, 0x65, 0x20, 0x70, 0x61, 0x72, 0x73, 0x65, 0x64 };
42 std::string expected_message =
"String to be parsed";
43 std::string parsed_message;
44 bp.
parse(parsed_message,
sizeof(buffer));
46 EXPECT_EQ(expected_message, parsed_message);
50 size_t partial_string = 6;
51 parsed_message.clear();
52 bp1.
parse(parsed_message, partial_string);
54 EXPECT_EQ(
"String", parsed_message);
57 parsed_message.clear();
60 EXPECT_EQ(
" to be parsed", parsed_message);
63 TEST(bin_parser, parse_array)
66 uint8_t buffer3d[] = { 0x40, 0x01, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xc0, 0x08, 0xcc, 0xcc,
67 0xcc, 0xcc, 0xcc, 0xcd, 0x40, 0x11, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcd };
70 vector3d_t expected_values3d = { 2.2, -3.1, 4.45 };
72 bp3d.
parse(parsed_values3d);
74 for (
unsigned int i = 0; i < parsed_values3d.size(); ++i)
76 EXPECT_EQ(expected_values3d[i], parsed_values3d[i]);
80 uint8_t buffer6d[] = {
81 0xc0, 0x01, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0x40, 0x08, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcd,
82 0x40, 0x11, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcd, 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a,
83 0x40, 0x08, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcd, 0xc0, 0x10, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcd
87 vector6d_t expected_values6d = { -2.2, 3.1, 4.45, 1.1, 3.1, -4.2 };
89 bp6d.
parse(parsed_values6d);
91 for (
unsigned int i = 0; i < parsed_values6d.size(); ++i)
93 EXPECT_EQ(expected_values6d[i], parsed_values6d[i]);
97 uint8_t buffer6i[] = { 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff,
98 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x05 };
103 bp6i.
parse(parsed_values6i);
105 for (
unsigned int i = 0; i < parsed_values6i.size(); ++i)
107 EXPECT_EQ(expected_values6i[i], parsed_values6i[i]);
111 uint8_t buffer6u[] = { 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01,
112 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x05 };
117 bp6u.
parse(parsed_values6u);
119 for (
unsigned int i = 0; i < parsed_values6u.size(); ++i)
121 EXPECT_EQ(expected_values6u[i], parsed_values6u[i]);
125 uint8_t buffer4i[] = { 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05,
126 0x00, 0x00, 0x00, 0x03, 0x0ff, 0x0ff, 0x0ff, 0x0ff };
129 std::array<int32_t, 4> expected_values4i = { 2, 5, 3, -1 };
130 std::array<int32_t, 4> parsed_values4i;
131 bp4i.
parse(parsed_values4i);
133 for (
unsigned int i = 0; i < parsed_values4i.size(); ++i)
135 EXPECT_EQ(expected_values4i[i], parsed_values4i[i]);
139 TEST(bin_parser, parse_data_types)
142 uint8_t buffer_float[] = { 0x41, 0xb2, 0xb8, 0x52 };
145 float expected_float = 22.34;
147 bp_float.
parse(parsed_float);
149 EXPECT_EQ(expected_float, parsed_float);
152 uint8_t buffer_double[] = { 0x41, 0xb2, 0xb8, 0x52, 0xcc, 0x55, 0x00, 0x00 };
155 float expected_double = 22.34;
157 bp_double.
parse(parsed_double);
159 EXPECT_EQ(expected_double, parsed_double);
162 uint8_t buffer_bool[] = { 0x01 };
165 bool expected_bool =
true;
167 bp_bool.
parse(parsed_bool);
169 EXPECT_EQ(expected_bool, parsed_bool);
172 uint8_t buffer_int[] = { 0xff, 0xff, 0xff, 0xea };
175 int32_t expected_int = -22;
177 bp_int.
parse<int32_t>(parsed_int);
179 EXPECT_EQ(expected_int, parsed_int);
184 uint8_t buffer[] = { 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x20, 0x74, 0x6f, 0x20,
185 0x62, 0x65, 0x20, 0x70, 0x61, 0x72, 0x73, 0x65, 0x64 };
187 size_t len =
sizeof(buffer);
192 std::string parsed_string;
193 bp.
parse(parsed_string, len - (len - 1));
208 uint8_t buffer[] = { 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x20, 0x74, 0x6f, 0x20,
209 0x62, 0x65, 0x20, 0x70, 0x61, 0x72, 0x73, 0x65, 0x64 };
213 EXPECT_TRUE(bp.
empty());
218 std::string expected_message =
"be parsed";
219 std::string parsed_message;
220 bp1.parseRemainder(parsed_message);
221 EXPECT_EQ(expected_message, parsed_message);
224 EXPECT_TRUE(bp1.empty());
227 TEST(bin_parser, raw_data_parser)
229 uint8_t buffer[] = { 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x20, 0x74, 0x6f, 0x20,
230 0x62, 0x65, 0x20, 0x70, 0x61, 0x72, 0x73, 0x65, 0x64 };
233 std::unique_ptr<uint8_t> raw_data;
238 EXPECT_TRUE(bp.
empty());
240 for (
unsigned int i = 0; i < size; ++i)
242 EXPECT_EQ(buffer[i], raw_data.get()[i]);
246 TEST(bin_parser, bitset_parser)
248 uint8_t buffer[] = { 0x00, 0x00, 0x00, 0x01 };
251 std::bitset<4> expected_set = std::bitset<4>(1);
252 std::bitset<4> parsed_set;
253 bp.
parse<int32_t>(parsed_set);
255 EXPECT_EQ(expected_set, parsed_set);
258 TEST(bin_parser, parse_outside_buffer_length)
260 uint8_t buffer[] = { 0x00, 0x00, 0x00, 0x01 };
263 int32_t expected_int = 1;
265 bp.
parse<int32_t>(parsed_int);
267 EXPECT_EQ(expected_int, parsed_int);
273 TEST(bin_parser, bin_parser_parent)
276 uint8_t buffer[] = { 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x20, 0x74, 0x6f, 0x20,
277 0x62, 0x65, 0x20, 0x70, 0x61, 0x72, 0x73, 0x65, 0x64 };
282 std::string expected_message =
"String to";
283 std::string parsed_message;
286 EXPECT_EQ(expected_message, parsed_message);
291 expected_message =
" be parsed";
292 parsed_message.clear();
295 EXPECT_EQ(expected_message, parsed_message);
298 int main(
int argc,
char* argv[])
300 ::testing::InitGoogleTest(&argc, argv);
302 return RUN_ALL_TESTS();
void parse(T &val)
Parses the next bytes as given type.
TEST(bin_parser, parse_string)
void parseRemainder(std::string &val)
Parses the remaining bytes as a string.
bool empty()
Checks if no unparsed bytes remain in the buffer.
The BinParser class handles a byte buffer and functionality to iteratively parse the content...
int main(int argc, char *argv[])
void rawData(std::unique_ptr< uint8_t > &buffer, size_t &buffer_length)
Writes the remaining bytes into a given buffer without parsing them.
std::array< double, 3 > vector3d_t
void consume()
Sets the current buffer position to the end of the buffer, finishing parsing.
bool checkSize(size_t bytes)
Checks if at least a given number of bytes is still remaining unparsed in the buffer.
~BinParser()
Deconstructor for the BinParser.
std::array< int32_t, 6 > vector6int32_t
std::array< uint32_t, 6 > vector6uint32_t
std::array< double, 6 > vector6d_t
Our base class for exceptions. Specialized exceptions should inherit from those.