-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_csv.c
212 lines (181 loc) · 5.82 KB
/
test_csv.c
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
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "olc.h"
#define BASE_PATH "test_data"
typedef int (TestFunc)(char* cp[], int cn);
static int test_short_code(char* cp[], int cn);
static int test_encoding(char* cp[], int cn);
static int test_validity(char* cp[], int cn);
static int process_file(const char* file, TestFunc func);
int main(int argc, char* argv[])
{
struct Data {
const char* file;
TestFunc* func;
} data[] = {
{ "shortCodeTests.csv", test_short_code },
{ "encodingTests.csv" , test_encoding },
{ "validityTests.csv" , test_validity },
};
for (int j = 0; j < sizeof(data) / sizeof(data[0]); ++j) {
process_file(data[j].file, data[j].func);
}
return 0;
}
static int process_file(const char* file, TestFunc func)
{
char full[1024];
sprintf(full, "%s/%s", BASE_PATH, file);
FILE* fp = fopen(full, "r");
if (!fp) {
printf("Could not open [%s]\n", full);
return 0;
}
int count = 0;
printf("============ %s ============\n", file);
while (1) {
char line[1024];
if (!fgets(line, 1024, fp)) {
break;
}
int blanks = 1;
char* cp[100];
int cn = 0;
int first = -1;
for (int j = 0; line[j] != '\0'; ++j) {
if (isspace(line[j]) && blanks) {
continue;
}
if (line[j] == '#' && blanks) {
break;
}
blanks = 0;
if (first < 0) {
first = j;
}
if (line[j] == ',') {
line[j] = '\0';
cp[cn++] = line + first;
first = j + 1;
continue;
}
if (line[j] == '\n') {
line[j] = '\0';
cp[cn++] = line + first;
first = -1;
break;
}
}
if (cn <= 0) {
continue;
}
func(cp, cn);
++count;
}
fclose(fp);
printf("============ %s => %d records ============\n", file, count);
return count;
}
static int test_encoding(char* cp[], int cn)
{
if (cn != 7) {
printf("test_encoding needs 7 columns per row, not %d\n", cn);
return 0;
}
// code,lat,lng,latLo,lngLo,latHi,lngHi
int ok = 0;
char* code = cp[0];
int len = OLC_CodeLength(code, 0);
OLC_LatLon data_pos = { strtod(cp[1], 0), strtod(cp[2], 0) };
// Encode the test location and make sure we get the expected code.
char encoded[256];
OLC_Encode(&data_pos, len, encoded, 256);
ok = strcmp(code, encoded) == 0;
printf("%-3.3s ENC_CODE [%s:%s] [%s] [%s]\n", ok ? "OK" : "BAD", cp[1], cp[2], encoded, code);
// Now decode the code and check we get the correct coordinates.
OLC_CodeArea data_area = {
{ strtod(cp[3], 0), strtod(cp[4], 0) },
{ strtod(cp[5], 0), strtod(cp[6], 0) },
len,
};
OLC_LatLon data_center;
OLC_GetCenter(&data_area, &data_center);
OLC_CodeArea decoded_area;
OLC_Decode(code, 0, &decoded_area);
OLC_LatLon decoded_center;
OLC_GetCenter(&decoded_area, &decoded_center);
ok = fabs(data_center.lat - decoded_center.lat) < 1e-10;
printf("%-3.3s ENC_LAT [%f:%f]\n", ok ? "OK" : "BAD", decoded_center.lat, data_center.lat);
ok = fabs(data_center.lon - decoded_center.lon) < 1e-10;
printf("%-3.3s ENC_LON [%f:%f]\n", ok ? "OK" : "BAD", decoded_center.lon, data_center.lon);
return 0;
}
static int test_short_code(char* cp[], int cn)
{
if (cn != 5) {
printf("test_short_code needs 5 columns per row, not %d\n", cn);
return 0;
}
// full code,lat,lng,shortcode,test_type
// test_type is R for recovery only, S for shorten only, or B for both.
int ok = 0;
char code[256];
char* full_code = cp[0];
char* short_code = cp[3];
char* type = cp[4];
OLC_LatLon reference = { strtod(cp[1], 0), strtod(cp[2], 0) };
// Shorten the code using the reference location and check.
if (strcmp(type, "B") == 0 || strcmp(type, "S") == 0) {
OLC_Shorten(full_code, 0, &reference, code, 256);
ok = strcmp(short_code, code) == 0;
printf("%-3.3s SHORTEN [%s] [%s:%s]: [%s] [%s]\n", ok ? "OK" : "BAD", full_code, cp[1], cp[2], code, short_code);
}
// Now extend the code using the reference location and check.
if (strcmp(type, "B") == 0 || strcmp(type, "R") == 0) {
OLC_RecoverNearest(short_code, 0, &reference, code, 256);
ok = strcmp(full_code, code) == 0;
printf("%-3.3s RECOVER [%s] [%s:%s]: [%s] [%s]\n", ok ? "OK" : "BAD", short_code, cp[1], cp[2], code, full_code);
}
return 0;
}
static int to_boolean(const char* s)
{
if (!s || s[0] == '\0') {
return 0;
}
if (strcasecmp(s, "false") == 0 ||
strcasecmp(s, "no") == 0 ||
strcasecmp(s, "f") == 0 ||
strcasecmp(s, ".f.") == 0 ||
strcasecmp(s, "n") == 0) {
return 0;
}
return 1;
}
static int test_validity(char* cp[], int cn)
{
if (cn != 4) {
printf("test_validity needs 4 columns per row, not %d\n", cn);
return 0;
}
// code,isValid,isShort,isFull
int ok = 0;
int got;
char* code = cp[0];
int is_valid = to_boolean(cp[1]);
int is_short = to_boolean(cp[2]);
int is_full = to_boolean(cp[3]);
got = OLC_IsValid(code, 0);
ok = got == is_valid;
printf("%-3.3s IsValid [%s]: [%d] [%d]\n", ok ? "OK" : "BAD", code, got, is_valid);
got = OLC_IsFull(code, 0);
ok = got == is_full;
printf("%-3.3s IsFull [%s]: [%d] [%d]\n", ok ? "OK" : "BAD", code, got, is_full);
got = OLC_IsShort(code, 0);
ok = got == is_short;
printf("%-3.3s IsShort [%s]: [%d] [%d]\n", ok ? "OK" : "BAD", code, got, is_short);
return 0;
}