constrain fastA2I to [0-9] (vice [0-9A])
[inav.git] / src / test / unit / olc_unittest.cc
blobd930daad621991ae0b6f48c5186345e7cdcb09bf
1 #include <string>
3 extern "C" {
4 #include "common/olc.h"
5 #include "common/utils.h"
8 #include "gtest/gtest.h"
10 using std::string;
12 struct EncodeCase {
13 string result;
14 double lat;
15 double lon;
17 int length();
20 int EncodeCase::length()
22 int n = 0;
23 for (size_t ii = 0; ii < this->result.length(); ii++) {
24 if (result[ii] == '0') {
25 break;
27 if (result[ii] != '+') {
28 n++;
31 return n;
34 // Tests cases from https://github.com/google/open-location-code/blob/master/test_data/encodingTests.csv
35 struct EncodeCase encodeCases[] = {
36 {"7FG49Q00+", 20.375, 2.775},
37 {"7FG49QCJ+2V", 20.3700625, 2.7821875},
38 {"7FG49QCJ+2VX", 20.3701125, 2.782234375},
39 {"7FG49QCJ+2VXGJ", 20.3701135, 2.78223535156},
40 {"8FVC2222+22", 47.0000625, 8.0000625},
41 {"4VCPPQGP+Q9", -41.2730625, 174.7859375},
42 {"62G20000+", 0.5, -179.5},
43 {"22220000+", -89.5, -179.5},
44 {"7FG40000+", 20.5, 2.5},
45 {"22222222+22", -89.9999375, -179.9999375},
46 {"6VGX0000+", 0.5, 179.5},
47 {"6FH32222+222", 1, 1},
48 // Special cases over 90 latitude and 180 longitude
49 {"CFX30000+", 90, 1},
50 {"CFX30000+", 92, 1},
51 {"62H20000+", 1, 180},
52 {"62H30000+", 1, 181},
53 {"CFX3X2X2+X2", 90, 1},
54 // Test non-precise latitude/longitude value
55 {"6FH56C22+22", 1.2, 3.4},
58 TEST(OLCTest, TestEncode)
60 char buf[20];
62 for (unsigned ii = 0; ii < ARRAYLEN(encodeCases); ii++) {
63 struct EncodeCase c = encodeCases[ii];
64 int32_t lat = c.lat * OLC_DEG_MULTIPLIER;
65 int32_t lon = c.lon * OLC_DEG_MULTIPLIER;
66 EXPECT_GT(olc_encode(lat, lon, c.length(), buf, sizeof(buf)), 0);
67 EXPECT_EQ(c.result, (string)buf);