Skip to content

Commit

Permalink
AP_OLC: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khancyr committed Oct 3, 2023
1 parent dfac26b commit eed07da
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
52 changes: 52 additions & 0 deletions libraries/AP_OLC/tests/test_olc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <AP_gtest.h>
#include "AP_Math/AP_Math.h"
#include "AP_OLC/AP_OLC.h"

AP_OLC olc;
const AP_HAL::HAL& hal = AP_HAL::get_HAL();

// Test the olc_encode function of AP_OLC
TEST(AP_OLCTest, OLC_EncodeTest) {
// Test cases
struct TestCase {
int32_t lat;
int32_t lon;
size_t length;
std::string expectedCode;
};

// Define the test cases
std::vector<TestCase> testCases = {
{500000000, 1000000000, 6, "9P2222"},
{900000000, 2000000000, 8, "C3X2X2X2"},
{000000000, 500000000, 9, "6HGG2222+"},
{300000000, 500000000, 10, "8H2G2222+2"},
{300000000, 500000000, 11, "8H2G2222+22"},
{300000000, 500000000, 12, "8H2G2222+22"},
{300000000, -1000000000, 10, "86222222+2"},
{950000000, -1900000000, 12, "CVXGX2X2+X2"},
// Add more test cases as needed
};
// Run the test cases
for (const auto& testCase : testCases) {
std::string buf(testCase.length + 2, '\0'); // buf should be > length +1 as the coding add \0
uint32_t result = olc.olc_encode(testCase.lat, testCase.lon, testCase.length, &buf[0], buf.size());
if (testCase.length < 9) {
EXPECT_EQ(result, 9u); // 9 is separator min position
} else {
EXPECT_EQ(result, 11u); // length max is 11
}
EXPECT_EQ(buf.substr(0, MIN(testCase.length, 11u)), testCase.expectedCode);
}
std::string buf(2, '\0');
uint32_t result = olc.olc_encode(testCases[0].lat, testCases[0].lon, testCases[0].length, &buf[0], buf.size());
EXPECT_EQ(result, 0u);
std::string empty_code(2, '\0');
EXPECT_EQ(buf, empty_code);
std::string buf2(16, '\0'); // buf should be > length +1 as the coding add \0
result = olc.olc_encode(testCases[0].lat, testCases[0].lon, 11, &buf2[0], buf2.size());
EXPECT_EQ(result, 12u);
EXPECT_EQ(buf2.substr(0, 12), "9P222222+222");
}

AP_GTEST_MAIN()
7 changes: 7 additions & 0 deletions libraries/AP_OLC/tests/wscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python
# encoding: utf-8

def build(bld):
bld.ap_find_tests(
use='ap',
)

0 comments on commit eed07da

Please sign in to comment.