-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_dbc.cpp
More file actions
311 lines (243 loc) · 12 KB
/
Copy pathtest_dbc.cpp
File metadata and controls
311 lines (243 loc) · 12 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "testing_utils/common.hpp"
#include "testing_utils/defines.hpp"
#include <catch2/catch_approx.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>
#include <libdbc/dbc.hpp>
#include <libdbc/exceptions/error.hpp>
#include <string>
using Catch::Matchers::ContainsSubstring;
TEST_CASE("Testing dbc file loading error issues", "[fileio][error]") {
auto parser = std::unique_ptr<Libdbc::DbcParser>(new Libdbc::DbcParser());
SECTION("Loading a non dbc file should throw an error", "[error]") {
REQUIRE_THROWS_AS(parser->parse_file(TEXT_FILE), Libdbc::NonDbcFileFormatError);
REQUIRE_THROWS_WITH(parser->parse_file(TEXT_FILE), ContainsSubstring("TextFile.txt"));
}
SECTION("Loading a dbc with missing version header throws an error (VERSION)", "[error]") {
REQUIRE_THROWS_AS(parser->parse_file(MISSING_VERSION_DBC_FILE), Libdbc::DbcFileIsMissingVersion);
REQUIRE_THROWS_WITH(parser->parse_file(MISSING_VERSION_DBC_FILE), ContainsSubstring("line: (NS_ :)"));
}
SECTION("Loading a dbc without the required bit timing section (BS_:)", "[error]") {
REQUIRE_THROWS_AS(parser->parse_file(MISSING_BIT_TIMING_DBC_FILE), Libdbc::DbcFileIsMissingBitTiming);
REQUIRE_THROWS_WITH(parser->parse_file(MISSING_BIT_TIMING_DBC_FILE), ContainsSubstring("BU_: DBG DRIVER IO MOTOR SENSOR"));
}
SECTION("Loading a dbc with some missing namespace section tags (NS_ :)", "[error]") {
// Confusion about this type of error. it appears that the header isn't
// very well standardized for now we ignore this type of error.
REQUIRE_NOTHROW(parser->parse_file(MISSING_NEW_SYMBOLS_DBC_FILE));
}
SECTION("Verify that what() method is accessible for all exceptions", "[error]") {
auto generic_error = Libdbc::Exception();
REQUIRE(std::string{generic_error.what()} == "libdbc exception occurred");
auto validity_check = Libdbc::ValidityError();
REQUIRE(std::string{validity_check.what()} == "Invalid DBC file");
}
}
TEST_CASE("Testing dbc file loading", "[fileio]") {
auto parser = std::unique_ptr<Libdbc::DbcParser>(new Libdbc::DbcParser());
SECTION("Loading a single simple dbc file", "[dbc]") {
std::vector<std::string> nodes = {"DBG", "DRIVER", "IO", "MOTOR", "SENSOR"};
Libdbc::Message msg(500, "IO_DEBUG", 4, "IO");
std::vector<std::string> receivers{"DBG"};
Libdbc::Signal sig("IO_DEBUG_test_unsigned", false, 0, 8, false, false, 1, 0, 0, 0, "", receivers);
msg.append_signal(sig);
std::vector<Libdbc::Message> msgs = {msg};
parser->parse_file(SIMPLE_DBC_FILE);
REQUIRE(parser->get_version() == "1.0.0");
REQUIRE(parser->get_nodes() == nodes);
REQUIRE(parser->get_messages() == msgs);
REQUIRE(parser->get_messages().front().get_signals() == msg.get_signals());
}
}
TEST_CASE("Testing big endian, little endian") {
std::string dbc_contents = PRIMITIVE_DBC + R"(BO_ 234 MSG1: 8 Vector__XXX
SG_ Sig1 : 55|16@0- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX
SG_ Sig2 : 39|16@1- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX)";
const auto filename = create_temporary_dbc_with(dbc_contents.c_str());
auto parser = Libdbc::DbcParser();
parser.parse_file(filename.c_str());
REQUIRE(parser.get_messages().size() == 1);
REQUIRE(parser.get_messages().at(0).name() == "MSG1");
REQUIRE(parser.get_messages().at(0).size() == 8);
REQUIRE(parser.get_messages().at(0).get_signals().size() == 2);
{
const auto signal = parser.get_messages().at(0).get_signals().at(0);
REQUIRE(signal.is_bigendian == true);
}
{
const auto signal = parser.get_messages().at(0).get_signals().at(1);
REQUIRE(signal.is_bigendian == false);
}
}
TEST_CASE("Testing negative values") {
std::string dbc_contents = PRIMITIVE_DBC + R"(BO_ 234 MSG1: 58 Vector__XXX
SG_ Sig1 : 55|16@0- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX
SG_ Sig2 : 39|16@0- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX
SG_ Sig3 : 23|16@0- (10,0) [-3276.8|-3276.7] "C" Vector__XXX
SG_ Sig4 : 7|16@0- (1,-10) [0|32767] "" Vector__XXX)";
const auto filename = create_temporary_dbc_with(dbc_contents.c_str());
auto parser = Libdbc::DbcParser();
parser.parse_file(filename.c_str());
REQUIRE(parser.get_messages().size() == 1);
REQUIRE(parser.get_messages().at(0).name() == "MSG1");
REQUIRE(parser.get_messages().at(0).size() == 58);
REQUIRE(parser.get_messages().at(0).get_signals().size() == 4);
SECTION("Evaluating first message") {
const auto signal = parser.get_messages().at(0).get_signals().at(0);
REQUIRE(signal.factor == 0.1);
REQUIRE(signal.offset == 0);
REQUIRE(signal.min == -3276.8);
REQUIRE(signal.max == -3276.7);
}
SECTION("Evaluating second message") {
const auto signal = parser.get_messages().at(0).get_signals().at(1);
REQUIRE(signal.factor == 0.1);
REQUIRE(signal.offset == 0);
REQUIRE(signal.min == -3276.8);
REQUIRE(signal.max == -3276.7);
}
SECTION("Evaluating third message") {
const auto signal = parser.get_messages().at(0).get_signals().at(2);
REQUIRE(signal.factor == 10);
REQUIRE(signal.offset == 0);
REQUIRE(signal.min == -3276.8);
REQUIRE(signal.max == -3276.7);
}
SECTION("Evaluating fourth message") {
const auto signal = parser.get_messages().at(0).get_signals().at(3);
REQUIRE(signal.factor == 1);
REQUIRE(signal.offset == -10);
REQUIRE(signal.min == 0);
REQUIRE(signal.max == 32767);
}
}
TEST_CASE("Special characters in unit") {
std::string dbc_contents = PRIMITIVE_DBC + R"(BO_ 234 MSG1: 255 Vector__XXX
SG_ Speed : 0|8@1+ (1,0) [0|204] "Km/h" DEVICE1,DEVICE2,DEVICE3)";
const auto filename = create_temporary_dbc_with(dbc_contents.c_str());
auto parser = Libdbc::DbcParser();
parser.parse_file(filename.c_str());
REQUIRE(parser.get_messages().size() == 1);
REQUIRE(parser.get_messages().at(0).name() == "MSG1");
REQUIRE(parser.get_messages().at(0).size() == 255);
REQUIRE(parser.get_messages().at(0).get_signals().size() == 1);
SECTION("Checking that signal with special characters as unit is parsed correctly") {
const auto signal = parser.get_messages().at(0).get_signals().at(0);
REQUIRE(signal.unit.compare("Km/h") == 0);
}
}
TEST_CASE("Signal Value Description") {
std::string dbc_contents = PRIMITIVE_DBC + R"(BO_ 234 MSG1: 8 Vector__XXX
SG_ State1 : 0|8@1+ (1,0) [0|200] "Km/h" DEVICE1,DEVICE2,DEVICE3
SG_ State2 : 0|8@1+ (1,0) [0|204] "" DEVICE1,DEVICE2,DEVICE3
VAL_ 234 State1 123 "Description 1" 0 "Description 2" 90903489 "Big value and special characters &$§())!" ;)";
const auto filename = create_temporary_dbc_with(dbc_contents.c_str());
auto parser = Libdbc::DbcParser();
parser.parse_file(filename.c_str());
REQUIRE(parser.get_messages().size() == 1);
REQUIRE(parser.get_messages().at(0).name() == "MSG1");
REQUIRE(parser.get_messages().at(0).get_signals().size() == 2);
REQUIRE(parser.get_messages().at(0).get_signals().at(0).value_descriptions.size() == 3);
REQUIRE(parser.get_messages().at(0).get_signals().at(1).value_descriptions.size() == 0);
const auto signal = parser.get_messages().at(0).get_signals().at(0);
REQUIRE(signal.value_descriptions.at(0).value == 123);
REQUIRE(signal.value_descriptions.at(0).description == "Description 1");
REQUIRE(signal.value_descriptions.at(1).value == 0);
REQUIRE(signal.value_descriptions.at(1).description == "Description 2");
REQUIRE(signal.value_descriptions.at(2).value == 90903489);
REQUIRE(signal.value_descriptions.at(2).description == "Big value and special characters &$§())!");
}
TEST_CASE("Signal Value Description Extended CAN id") {
std::string dbc_contents = PRIMITIVE_DBC + R"(BO_ 3221225472 MSG1: 8 Vector__XXX
SG_ State1 : 0|8@1+ (1,0) [0|200] "Km/h" DEVICE1,DEVICE2,DEVICE3
SG_ State2 : 0|8@1+ (1,0) [0|204] "" DEVICE1,DEVICE2,DEVICE3
VAL_ 3221225472 State1 123 "Description 1" 0 "Description 2" 4000000000 "Big value and special characters &$§())!" ;)";
const auto filename = create_temporary_dbc_with(dbc_contents.c_str());
auto parser = Libdbc::DbcParser();
parser.parse_file(filename.c_str());
REQUIRE(parser.get_messages().size() == 1);
REQUIRE(parser.get_messages().at(0).name() == "MSG1");
REQUIRE(parser.get_messages().at(0).get_signals().size() == 2);
REQUIRE(parser.get_messages().at(0).get_signals().at(0).value_descriptions.size() == 3);
REQUIRE(parser.get_messages().at(0).get_signals().at(1).value_descriptions.size() == 0);
const auto signal = parser.get_messages().at(0).get_signals().at(0);
REQUIRE(signal.value_descriptions.at(0).value == 123);
REQUIRE(signal.value_descriptions.at(0).description == "Description 1");
REQUIRE(signal.value_descriptions.at(1).value == 0);
REQUIRE(signal.value_descriptions.at(1).description == "Description 2");
REQUIRE(signal.value_descriptions.at(2).value == 4000000000);
REQUIRE(signal.value_descriptions.at(2).description == "Big value and special characters &$§())!");
}
TEST_CASE("Signal Value Multiple VAL_") {
std::string dbc_contents = PRIMITIVE_DBC + R"(BO_ 3221225472 MSG1: 8 Vector__XXX
SG_ State1 : 0|8@1+ (1,0) [0|200] "Km/h" DEVICE1,DEVICE2,DEVICE3
SG_ State2 : 0|8@1+ (1,0) [0|204] "" DEVICE1,DEVICE2,DEVICE3"
BO_ 123 MSG2: 8 Vector__XXX
SG_ State1 : 0|8@1+ (1,0) [0|200] "Km/h" DEVICE1,DEVICE2,DEVICE3
SG_ State2 : 0|8@1+ (1,0) [0|204] "" DEVICE1,DEVICE2,DEVICE3
VAL_ 3221225472 State1 123 "Description 1" 0 "Description 2" ;
VAL_ 123 State1 123 "Description 3" 0 "Description 4" ;)";
const auto filename = create_temporary_dbc_with(dbc_contents.c_str());
auto parser = Libdbc::DbcParser();
parser.parse_file(filename.c_str());
REQUIRE(parser.get_messages().size() == 2);
REQUIRE(parser.get_messages().at(0).name() == "MSG1");
REQUIRE(parser.get_messages().at(1).name() == "MSG2");
REQUIRE(parser.get_messages().at(0).get_signals().size() == 2);
REQUIRE(parser.get_messages().at(0).get_signals().at(0).value_descriptions.size() == 2);
REQUIRE(parser.get_messages().at(0).get_signals().at(1).value_descriptions.size() == 0);
REQUIRE(parser.get_messages().at(1).get_signals().at(0).value_descriptions.size() == 2);
REQUIRE(parser.get_messages().at(1).get_signals().at(1).value_descriptions.size() == 0);
const auto signal = parser.get_messages().at(0).get_signals().at(0);
REQUIRE(signal.value_descriptions.at(0).value == 123);
REQUIRE(signal.value_descriptions.at(0).description == "Description 1");
REQUIRE(signal.value_descriptions.at(1).value == 0);
REQUIRE(signal.value_descriptions.at(1).description == "Description 2");
const auto signal2 = parser.get_messages().at(1).get_signals().at(0);
REQUIRE(signal2.value_descriptions.at(0).value == 123);
REQUIRE(signal2.value_descriptions.at(0).description == "Description 3");
REQUIRE(signal2.value_descriptions.at(1).value == 0);
REQUIRE(signal2.value_descriptions.at(1).description == "Description 4");
}
TEST_CASE("Should parse DBC with empty BU_", "[error][optional]") {
std::string contents = R"(VERSION ""
NS_ :
BS_:
BU_:
BO_ 293 Msg1: 2 Vector__XXX
SG_ Wert7 : 0|16@1- (1,0) [0|0] "" Vector__XXX
BO_ 292 Msg2: 1 Vector__XXX
SG_ Wert8 : 56|8@1- (1,0) [0|0] "" Vector__XXX
)";
const auto filename = create_temporary_dbc_with(contents.c_str());
auto parser = Libdbc::DbcParser();
REQUIRE_NOTHROW(parser.parse_file(filename.c_str()));
REQUIRE(parser.get_messages().size() == 2);
REQUIRE(parser.get_messages().at(0).name() == "Msg1");
REQUIRE(parser.get_messages().at(1).name() == "Msg2");
}
TEST_CASE("Should report unused lines since we don't have tracing.", "[parsing]") {
std::string contents = R"(VERSION ""
NS_ :
BS_:
BU_:
BO_ 293 Msg1: 2 Vector__XXX
SG_ Whitespace: | 0|16@1- (1,0) [0|0] "" Vector__XXX
SG_ Wert7 : 0|16@1- (1,0) [0|0] "" Vector__XXX
SG_ Wert8 : 0|16@1- (1,0) [0|0] "" Vector__XXX
BO_ 292 Msg2: 1 Vector__XXX
SG_ Wert8 : 56|8@1- (1,0) [0|0] "" Vector__XXX
SB_ not a correct line
BO_ have a issue here:
)";
const auto filename = create_temporary_dbc_with(contents.c_str());
auto parser = Libdbc::DbcParser();
REQUIRE_NOTHROW(parser.parse_file(filename.c_str()));
REQUIRE(parser.get_messages().size() == 2);
REQUIRE(parser.get_messages()[0].size() == 2);
REQUIRE(parser.get_messages()[1].size() == 1);
auto unused = parser.unused_lines();
// We could match them all here but i think just a check that the size is sufficent.
REQUIRE(unused.size() == 3);
}