From 2168374b58fbca5a8ce80b7fd2de0c90b5bf9e80 Mon Sep 17 00:00:00 2001 From: RomanBapst Date: Tue, 28 Apr 2026 14:06:57 +0300 Subject: [PATCH] geofence utils unit tests: improve test for SymmetricPairIndex Signed-off-by: RomanBapst --- src/lib/geofence/GeofenceUtilsTest.cpp | 46 ++++++++++++++++++++------ 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/lib/geofence/GeofenceUtilsTest.cpp b/src/lib/geofence/GeofenceUtilsTest.cpp index 7567b541b58..50b7ad6062e 100644 --- a/src/lib/geofence/GeofenceUtilsTest.cpp +++ b/src/lib/geofence/GeofenceUtilsTest.cpp @@ -32,6 +32,7 @@ ****************************************************************************/ #include +#include #include "geofence_utils.h" using namespace matrix; @@ -91,16 +92,41 @@ TEST(GeofenceUtilsTest, PolygonIsCCW) } TEST(GeofenceUtilsTest, SymmetricPairIndex) { - EXPECT_EQ(geofence_utils::symmetricPairIndex(0, 1, 4), 0); - EXPECT_EQ(geofence_utils::symmetricPairIndex(0, 2, 4), 1); - EXPECT_EQ(geofence_utils::symmetricPairIndex(3, 2, 4), 5); - EXPECT_EQ(geofence_utils::symmetricPairIndex(1, 3, 4), 4); - EXPECT_EQ(geofence_utils::symmetricPairIndex(3, 1, 4), 4); + // create a NxN matrix and fill with random data. Then pack the upper triangle into a 1D array. + // Then loop through the upper triangle and read the values from the array using symmetricPairIndex + // and verify that the values match. + constexpr size_t N = 21; + constexpr size_t kPairs = N * (N - 1) / 2; - EXPECT_EQ(geofence_utils::symmetricPairIndex(0, 1, 5), 0); - EXPECT_EQ(geofence_utils::symmetricPairIndex(2, 4, 5), 8); - EXPECT_EQ(geofence_utils::symmetricPairIndex(3, 2, 5), 7); - EXPECT_EQ(geofence_utils::symmetricPairIndex(1, 3, 5), 5); - EXPECT_EQ(geofence_utils::symmetricPairIndex(3, 1, 5), 5); + float matrix[N][N]; + float packed[kPairs]; + + std::srand(42); + + for (size_t i = 0; i < N; ++i) { + for (size_t j = 0; j < N; ++j) { + matrix[i][j] = static_cast(std::rand()) / static_cast(RAND_MAX); + } + } + + int idx = 0; + + for (size_t i = 0; i < N; ++i) { + for (size_t j = i + 1; j < N; ++j) { + packed[idx++] = matrix[i][j]; + } + } + + int counter = 0; + + for (size_t i = 0; i < N; ++i) { + for (size_t j = i + 1; j < N; ++j) { + counter++; + EXPECT_FLOAT_EQ(matrix[i][j], packed[geofence_utils::symmetricPairIndex(i, j, N)]); + } + } + + // verify that we have the expected number of pairs + EXPECT_EQ(counter, kPairs); }