Skip to content

Commit 502607f

Browse files
authored
Merge pull request #95 from google/cpp-recovery
Fix recovery bug in cpp
2 parents 65e5513 + de81b72 commit 502607f

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

cpp/BUILD

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,12 @@ cc_library(
2626
visibility = ["//visibility:private"],
2727
)
2828

29-
filegroup(
30-
name = "testdata",
31-
srcs = glob([
32-
"test_data/*",
33-
]),
34-
)
35-
3629
cc_test(
3730
name = "openlocationcode_test",
3831
size = "small",
3932
srcs = ["openlocationcode_test.cc"],
4033
data = [
41-
"testdata",
34+
"//test_data:test_data",
4235
],
4336
deps = [
4437
"@gtest//:main",
@@ -56,4 +49,4 @@ cc_binary(
5649
deps = [
5750
":openlocationcode",
5851
],
59-
)
52+
)

cpp/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ bazel build openlocationcode
2020
To run the tests, use:
2121

2222
```
23-
bazel test openlocationcode_test
23+
bazel test --test_output=all openlocationcode_test
2424
```
2525

2626
The tests use the CSV files in the test_data folder. Make sure you copy this folder to the
27-
root of your local workspace.
27+
root of your local workspace.

cpp/openlocationcode.cc

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -316,30 +316,31 @@ std::string RecoverNearest(const std::string &short_code,
316316
double resolution = pow_neg(
317317
internal::kEncodingBase, 2.0 - (padding_length / 2.0));
318318
// Distance from the center to an edge (in degrees).
319-
double area_to_edge = resolution / 2.0;
319+
double half_res = resolution / 2.0;
320320
// Use the reference location to pad the supplied short code and decode it.
321321
LatLng latlng = {latitude, longitude};
322322
std::string padding_code = Encode(latlng);
323323
CodeArea code_rect =
324324
Decode(std::string(padding_code.substr(0, padding_length)) +
325325
std::string(short_code));
326326
// How many degrees latitude is the code from the reference? If it is more
327-
// than half the resolution, we need to move it east or west.
327+
// than half the resolution, we need to move it north or south but keep it
328+
// within -90 to 90 degrees.
328329
double center_lat = code_rect.GetCenter().latitude;
329330
double center_lng = code_rect.GetCenter().longitude;
330-
if (center_lat - latitude > area_to_edge) {
331-
// If the center of the short code is more than half a cell east,
332-
// then the best match will be one position west.
331+
if (latitude + half_res < center_lat && center_lat - resolution > internal::kLatitudeMaxDegrees) {
332+
// If the proposed code is more than half a cell north of the reference location,
333+
// it's too far, and the best match will be one cell south.
333334
center_lat -= resolution;
334-
} else if (center_lat - latitude < -area_to_edge) {
335-
// If the center of the short code is more than half a cell west,
336-
// then the best match will be one position east.
335+
} else if (latitude - half_res > center_lat && center_lat + resolution < internal::kLatitudeMaxDegrees) {
336+
// If the proposed code is more than half a cell south of the reference location,
337+
// it's too far, and the best match will be one cell north.
337338
center_lat += resolution;
338339
}
339340
// How many degrees longitude is the code from the reference?
340-
if (center_lng - longitude > area_to_edge) {
341+
if (longitude + half_res < center_lng) {
341342
center_lng -= resolution;
342-
} else if (center_lng - longitude < -area_to_edge) {
343+
} else if (longitude - half_res > center_lng) {
343344
center_lng += resolution;
344345
}
345346
LatLng center_latlng = {center_lat, center_lng};
@@ -472,4 +473,4 @@ size_t CodeLength(const std::string &code) {
472473
return clean_code.size();
473474
}
474475

475-
} // namespace openlocationcode
476+
} // namespace openlocationcode

cpp/openlocationcode_test.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ struct ShortCodeTestData {
151151
double reference_lat;
152152
double reference_lng;
153153
std::string short_code;
154+
std::string test_type;
154155
};
155156

156157
class ShortCodeChecks : public ::testing::TestWithParam<ShortCodeTestData> {};
@@ -167,6 +168,7 @@ std::vector<ShortCodeTestData> GetShortCodeDataFromCsv() {
167168
test_data.reference_lat = atof(csv_records[i][1].c_str());
168169
test_data.reference_lng = atof(csv_records[i][2].c_str());
169170
test_data.short_code = csv_records[i][3];
171+
test_data.test_type = csv_records[i][4];
170172
data_results.push_back(test_data);
171173
}
172174
return data_results;
@@ -177,11 +179,15 @@ TEST_P(ShortCodeChecks, ShortCode) {
177179
LatLng reference_loc =
178180
LatLng{test_data.reference_lat, test_data.reference_lng};
179181
// Shorten the code using the reference location and check.
180-
std::string actual_short = Shorten(test_data.full_code, reference_loc);
181-
EXPECT_EQ(test_data.short_code, actual_short);
182+
if (test_data.test_type == "B" || test_data.test_type == "S") {
183+
std::string actual_short = Shorten(test_data.full_code, reference_loc);
184+
EXPECT_EQ(test_data.short_code, actual_short);
185+
}
182186
// Now extend the code using the reference location and check.
183-
std::string actual_full = RecoverNearest(test_data.short_code, reference_loc);
184-
EXPECT_EQ(test_data.full_code, actual_full);
187+
if (test_data.test_type == "B" || test_data.test_type == "R") {
188+
std::string actual_full = RecoverNearest(test_data.short_code, reference_loc);
189+
EXPECT_EQ(test_data.full_code, actual_full);
190+
}
185191
}
186192

187193
INSTANTIATE_TEST_CASE_P(OLC_Tests, ShortCodeChecks,
@@ -206,4 +212,4 @@ TEST(MaxCodeLengthChecks, MaxCodeLength) {
206212
}
207213

208214
} // namespace
209-
} // namespace openlocationcode
215+
} // namespace openlocationcode

0 commit comments

Comments
 (0)