Skip to content

Commit 82b78c9

Browse files
julianoesbkueng
authored andcommitted
Add test for Unicore parser
Signed-off-by: Julian Oes <[email protected]>
1 parent 0214ed3 commit 82b78c9

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build

CMakeLists.txt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
project(gps-parser-test CXX)
4+
5+
set(CMAKE_CXX_STANDARD 17)
6+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7+
8+
add_executable(gps-parser-test
9+
gps-parser-test.cpp
10+
src/unicore.cpp
11+
src/crc.cpp
12+
)
13+
14+
target_compile_options(gps-parser-test
15+
PRIVATE
16+
-Wall
17+
-Wextra
18+
-Wconversion
19+
-Wpedantic
20+
)
21+
22+
target_include_directories(gps-parser-test
23+
PRIVATE
24+
src/
25+
)

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,13 @@ All platform-specific stuff is done via a callback function and a
1111

1212
In order for the project to build, `definitions.h` must include definitions for `sensor_gnss_relative_s`, `sensor_gps_s` and `satellite_info_s`.
1313
For example, check the implementation in [PX4 Autopilot](https://github.com/PX4/PX4-Autopilot/blob/master/src/drivers/gps/definitions.h) or [QGroundControl](https://github.com/mavlink/qgroundcontrol/blob/master/src/GPS/definitions.h).
14+
15+
16+
## Parser tests
17+
18+
To test parsers, build and run the cmake project:
19+
20+
```
21+
cmake -Bbuild -H.
22+
cmake --build build && build/gps-parser-test
23+
```

gps-parser-test.cpp

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include "unicore.h"
2+
#include <cassert>
3+
#include <cstdio>
4+
5+
void test_empty()
6+
{
7+
const char str[] = " ";
8+
9+
UnicoreParser unicore_parser;
10+
11+
for (unsigned i = 0; i < sizeof(str); ++i) {
12+
auto result = unicore_parser.parseChar(str[i]);
13+
assert(result == UnicoreParser::Result::None);
14+
}
15+
}
16+
17+
void test_garbage()
18+
{
19+
const char str[] = "#GARBAGE,BLA";
20+
21+
UnicoreParser unicore_parser;
22+
23+
for (unsigned i = 0; i < sizeof(str); ++i) {
24+
auto result = unicore_parser.parseChar(str[i]);
25+
assert(result == UnicoreParser::Result::None);
26+
}
27+
}
28+
29+
void test_too_long()
30+
{
31+
const char str[] =
32+
"#UNIHEADINGA,89,GPS,FINE,2251,168052600,0,0,18,11;SOL_COMPUTED,"
33+
"NARROW_INT,0.3718,67.0255,-0.7974,0.0000,0.8065,3.3818,\"999\","
34+
"31,21,21,18,3,01,3,f3,1234567890,1234567890,1234567890,1234567890,"
35+
"1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
36+
"1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
37+
"1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
38+
"1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
39+
"1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
40+
"1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
41+
"1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
42+
"1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,"
43+
"1234567890,ffffffff";
44+
45+
UnicoreParser unicore_parser;
46+
47+
for (unsigned i = 0; i < sizeof(str); ++i) {
48+
auto result = unicore_parser.parseChar(str[i]);
49+
assert(result == UnicoreParser::Result::None);
50+
}
51+
}
52+
53+
void test_uniheadinga_wrong_crc()
54+
{
55+
const char str[] =
56+
"#UNIHEADINGA,89,GPS,FINE,2251,168052600,0,0,18,11;SOL_COMPUTED,NARROW_INT,"
57+
"0.3718,67.0255,-0.7974,0.0000,0.8065,3.3818,\"999\",31,21,21,18,3,01,3,f3*"
58+
"ffffffff";
59+
60+
UnicoreParser unicore_parser;
61+
62+
for (unsigned i = 0; i < sizeof(str); ++i) {
63+
auto result = unicore_parser.parseChar(str[i]);
64+
65+
if (result == UnicoreParser::Result::WrongCrc) {
66+
return;
67+
}
68+
}
69+
70+
assert(false);
71+
}
72+
73+
74+
void test_uniheadinga()
75+
{
76+
const char str[] =
77+
"#UNIHEADINGA,89,GPS,FINE,2251,168052600,0,0,18,11;SOL_COMPUTED,NARROW_INT,0.3718,67.0255,-0.7974,0.0000,0.8065,3.3818,\"999\",31,21,21,18,3,01,3,f3*ece5bb07";
78+
79+
UnicoreParser unicore_parser;
80+
81+
for (unsigned i = 0; i < sizeof(str); ++i) {
82+
auto result = unicore_parser.parseChar(str[i]);
83+
84+
if (result == UnicoreParser::Result::GotHeading) {
85+
assert(unicore_parser.heading().heading_deg == 67.0255f);
86+
assert(unicore_parser.heading().baseline_m == 0.3718f);
87+
return;
88+
}
89+
}
90+
91+
assert(false);
92+
}
93+
94+
void test_unicore()
95+
{
96+
test_empty();
97+
test_garbage();
98+
test_too_long();
99+
test_uniheadinga_wrong_crc();
100+
test_uniheadinga();
101+
}
102+
103+
int main(int, char **)
104+
{
105+
test_unicore();
106+
107+
return 0;
108+
}

0 commit comments

Comments
 (0)