@@ -136,12 +136,12 @@ INSTANTIATE_TEST_CASE_P(OLC_Tests, DecodingChecks,
136
136
struct EncodingTestData {
137
137
double lat_deg;
138
138
double lng_deg;
139
+ long long int lat_int;
140
+ long long int lng_int;
139
141
size_t length;
140
142
std::string code;
141
143
};
142
144
143
- class EncodingChecks : public ::testing::TestWithParam<EncodingTestData> {};
144
-
145
145
const std::string kEncodingTestsFile = " test_data/encoding.csv" ;
146
146
147
147
std::vector<EncodingTestData> GetEncodingDataFromCsv () {
@@ -152,19 +152,71 @@ std::vector<EncodingTestData> GetEncodingDataFromCsv() {
152
152
EncodingTestData test_data = {};
153
153
test_data.lat_deg = strtod (csv_records[i][0 ].c_str (), nullptr );
154
154
test_data.lng_deg = strtod (csv_records[i][1 ].c_str (), nullptr );
155
- test_data.length = atoi (csv_records[i][2 ].c_str ());
156
- test_data.code = csv_records[i][3 ];
155
+ test_data.lat_int = strtoll (csv_records[i][2 ].c_str (), nullptr , 10 );
156
+ test_data.lng_int = strtoll (csv_records[i][3 ].c_str (), nullptr , 10 );
157
+ test_data.length = atoi (csv_records[i][4 ].c_str ());
158
+ test_data.code = csv_records[i][5 ];
157
159
data_results.push_back (test_data);
158
160
}
159
161
return data_results;
160
162
}
161
163
162
- TEST_P (EncodingChecks, Encode) {
164
+ // TolerantTestParams runs a test with a permitted failure rate.
165
+ struct TolerantTestParams {
166
+ double allowed_failure_rate;
167
+ std::vector<EncodingTestData> test_data;
168
+ };
169
+
170
+ class TolerantEncodingChecks
171
+ : public ::testing::TestWithParam<TolerantTestParams> {};
172
+
173
+ TEST_P (TolerantEncodingChecks, EncodeDegrees) {
174
+ const TolerantTestParams& test_params = GetParam ();
175
+ int failure_count = 0 ;
176
+
177
+ for (EncodingTestData tc : test_params.test_data ) {
178
+ LatLng lat_lng = LatLng{tc.lat_deg , tc.lng_deg };
179
+ // Encode the test location and make sure we get the expected code.
180
+ std::string got_code = Encode (lat_lng, tc.length );
181
+ if (tc.code .compare (got_code) != 0 ) {
182
+ failure_count++;
183
+ printf (" ENCODING FAILURE: Got: '%s', expected: '%s'\n " ,
184
+ got_code.c_str (), tc.code .c_str ());
185
+ }
186
+ }
187
+ double actual_failure_rate =
188
+ double (failure_count) / test_params.test_data .size ();
189
+ EXPECT_LE (actual_failure_rate, test_params.allowed_failure_rate )
190
+ << " Failure rate " << actual_failure_rate << " exceeds allowed rate "
191
+ << test_params.allowed_failure_rate ;
192
+ }
193
+
194
+ // Allow a 5% error rate encoding from degree coordinates (because of floating
195
+ // point precision).
196
+ INSTANTIATE_TEST_SUITE_P (OLC_Tests, TolerantEncodingChecks,
197
+ ::testing::Values (TolerantTestParams{
198
+ 0.05 , GetEncodingDataFromCsv ()}));
199
+
200
+ class EncodingChecks : public ::testing::TestWithParam<EncodingTestData> {};
201
+
202
+ TEST_P (EncodingChecks, OLC_EncodeIntegers) {
163
203
EncodingTestData test_data = GetParam ();
164
- LatLng lat_lng = LatLng{test_data.lat_deg , test_data.lng_deg };
165
204
// Encode the test location and make sure we get the expected code.
166
- std::string actual_code = Encode (lat_lng, test_data.length );
167
- EXPECT_EQ (test_data.code , actual_code);
205
+ std::string got_code = internal::encodeIntegers (
206
+ test_data.lat_int , test_data.lng_int , test_data.length );
207
+ EXPECT_EQ (test_data.code , got_code);
208
+ }
209
+
210
+ TEST_P (EncodingChecks, OLC_LocationToIntegers) {
211
+ EncodingTestData test_data = GetParam ();
212
+ int64_t got_lat = internal::latitudeToInteger (test_data.lat_deg );
213
+ // Due to floating point precision limitations, we may get values 1 less than
214
+ // expected.
215
+ EXPECT_LE (got_lat, test_data.lat_int );
216
+ EXPECT_GE (got_lat + 1 , test_data.lat_int );
217
+ int64_t got_lng = internal::longitudeToInteger (test_data.lng_deg );
218
+ EXPECT_LE (got_lng, test_data.lng_int );
219
+ EXPECT_GE (got_lng + 1 , test_data.lng_int );
168
220
}
169
221
170
222
INSTANTIATE_TEST_CASE_P (OLC_Tests, EncodingChecks,
0 commit comments