CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_CSVReader/tests/test_csvreader.cpp
Views: 1799
1
#include <AP_gtest.h>
2
#include <AP_Common/AP_Common.h>
3
4
#include <AP_Math/AP_Math.h>
5
6
#include <AP_CSVReader/AP_CSVReader.h>
7
8
TEST(AP_CSVReader, basic)
9
{
10
static const char *basic_csv =
11
"A 1\n"
12
"B 2\n"
13
"C 3\n"
14
"Fred 31\n"
15
;
16
static const char *basic_csv_crlf =
17
"A 1\r\n"
18
"B 2\r\n"
19
"C 3\r\n"
20
"Fred 31\r\n"
21
;
22
static const char *basic_csv_results[][2] = {
23
{"A", "1"},
24
{"B", "2"},
25
{"C", "3"},
26
{"Fred", "31"}
27
};
28
29
uint8_t term[16];
30
AP_CSVReader csvreader{term, ARRAY_SIZE(term), ' '};
31
32
const char *csvs[] {
33
basic_csv,
34
basic_csv_crlf
35
};
36
37
for (const char *csv : csvs) {
38
uint8_t termcount = 0;
39
uint8_t linecount = 0;
40
for (uint8_t i=0; i<strlen(csv); i++) {
41
switch (csvreader.feed(csv[i])) {
42
case AP_CSVReader::RetCode::ERROR:
43
abort();
44
case AP_CSVReader::RetCode::OK:
45
continue;
46
case AP_CSVReader::RetCode::TERM_DONE:
47
EXPECT_STREQ(basic_csv_results[linecount][termcount], (char*)term);
48
termcount++;
49
continue;
50
case AP_CSVReader::RetCode::VECTOR_DONE:
51
EXPECT_STREQ(basic_csv_results[linecount][termcount], (char*)term);
52
termcount++;
53
EXPECT_EQ(termcount, 2);
54
termcount = 0;
55
linecount++;
56
continue;
57
}
58
}
59
60
EXPECT_EQ(linecount, 4);
61
EXPECT_EQ(termcount, 0);
62
}
63
}
64
65
TEST(AP_CSVReader, commabasic)
66
{
67
static const char *basic_csv =
68
"A,1\n"
69
"B,2\n"
70
"C,3\n"
71
"Fred,31\n"
72
;
73
static const char *basic_csv_results[][2] = {
74
{"A", "1"},
75
{"B", "2"},
76
{"C", "3"},
77
{"Fred", "31"}
78
};
79
80
uint8_t term[16];
81
AP_CSVReader csvreader{term, ARRAY_SIZE(term), ','};
82
83
uint8_t termcount = 0;
84
uint8_t linecount = 0;
85
for (uint8_t i=0; i<strlen(basic_csv); i++) {
86
switch (csvreader.feed(basic_csv[i])) {
87
case AP_CSVReader::RetCode::ERROR:
88
abort();
89
case AP_CSVReader::RetCode::OK:
90
continue;
91
case AP_CSVReader::RetCode::TERM_DONE:
92
EXPECT_STREQ(basic_csv_results[linecount][termcount], (char*)term);
93
termcount++;
94
continue;
95
case AP_CSVReader::RetCode::VECTOR_DONE:
96
EXPECT_STREQ(basic_csv_results[linecount][termcount], (char*)term);
97
termcount++;
98
EXPECT_EQ(termcount, 2);
99
termcount = 0;
100
linecount++;
101
continue;
102
}
103
}
104
105
EXPECT_EQ(linecount, 4);
106
EXPECT_EQ(termcount, 0);
107
}
108
109
TEST(AP_CSVReader, missinglastcr)
110
{
111
static const char *basic_csv =
112
"A,1"
113
;
114
uint8_t term[16];
115
AP_CSVReader csvreader{term, ARRAY_SIZE(term), ','};
116
117
uint8_t termcount = 0;
118
uint8_t linecount = 0;
119
for (uint8_t i=0; i<strlen(basic_csv); i++) {
120
switch (csvreader.feed(basic_csv[i])) {
121
case AP_CSVReader::RetCode::ERROR:
122
abort();
123
case AP_CSVReader::RetCode::OK:
124
continue;
125
case AP_CSVReader::RetCode::TERM_DONE:
126
if (linecount == 0 && termcount == 0) {
127
EXPECT_STREQ((char*)term, "A");
128
}
129
termcount++;
130
continue;
131
case AP_CSVReader::RetCode::VECTOR_DONE:
132
abort();
133
}
134
}
135
136
EXPECT_STREQ((char*)term, "1");
137
}
138
139
AP_GTEST_MAIN()
140
141
142
int hal = 0; // bizarrely, this fixes an undefined-symbol error but doesn't raise a type exception. Yay.
143
144