|
| 1 | + |
| 2 | +#include "software/ai/hl/stp/tactic/vis_proto_deduper.h" |
| 3 | + |
| 4 | +#include <gtest/gtest.h> |
| 5 | + |
| 6 | +#include <memory> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +#include "software/ai/navigator/obstacle/obstacle.hpp" |
| 10 | +#include "software/ai/navigator/obstacle/robot_navigation_obstacle_factory.h" |
| 11 | +#include "software/geom/point.h" |
| 12 | +#include "software/geom/polygon.h" |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +class VisProtoDeduperTest : public ::testing::Test |
| 17 | +{ |
| 18 | + protected: |
| 19 | + RobotNavigationObstacleFactory obstacle_factory = |
| 20 | + RobotNavigationObstacleFactory(TbotsProto::RobotNavigationObstacleConfig()); |
| 21 | + |
| 22 | + // Helper to extract the list of obstacles from the proto message for easy |
| 23 | + // verification |
| 24 | + std::vector<TbotsProto::Obstacle> getObstaclesFromProto( |
| 25 | + const TbotsProto::ObstacleList& msg) |
| 26 | + { |
| 27 | + std::vector<TbotsProto::Obstacle> obstacles; |
| 28 | + for (const auto& obs : msg.obstacles()) |
| 29 | + { |
| 30 | + obstacles.push_back(obs); |
| 31 | + } |
| 32 | + return obstacles; |
| 33 | + } |
| 34 | + |
| 35 | + // Helper to create a unique obstacle based on a position offset |
| 36 | + // This ensures we have distinct geometries to hash. |
| 37 | + ObstaclePtr createTestObstacle(double x, double y) |
| 38 | + { |
| 39 | + auto polygon = Polygon({Point(x, y), Point(x + 1.0, y), Point(x, y + 1.0)}); |
| 40 | + return obstacle_factory.createFromShape(polygon); |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +TEST_F(VisProtoDeduperTest, DeduplicatesRepeatedObstacles) |
| 45 | +{ |
| 46 | + VisProtoDeduper deduper(5); |
| 47 | + TbotsProto::ObstacleList output_msg; |
| 48 | + |
| 49 | + auto obs1 = createTestObstacle(10, 10); |
| 50 | + std::vector<ObstaclePtr> input = {obs1}; |
| 51 | + |
| 52 | + // First pass: Obstacle is new |
| 53 | + deduper.dedupeAndFill(input, output_msg); |
| 54 | + EXPECT_EQ(output_msg.obstacles_size(), 1); |
| 55 | + |
| 56 | + // Clear output for next step |
| 57 | + output_msg.Clear(); |
| 58 | + |
| 59 | + // Second pass: Same obstacle passed immediately again |
| 60 | + deduper.dedupeAndFill(input, output_msg); |
| 61 | + EXPECT_EQ(output_msg.obstacles_size(), 0) |
| 62 | + << "Should filter out recently sent obstacle"; |
| 63 | +} |
| 64 | + |
| 65 | +TEST_F(VisProtoDeduperTest, HandlesMixedNewAndOldObstacles) |
| 66 | +{ |
| 67 | + VisProtoDeduper deduper(5); |
| 68 | + TbotsProto::ObstacleList output_msg; |
| 69 | + |
| 70 | + auto obs_old = createTestObstacle(10, 10); |
| 71 | + auto obs_new = createTestObstacle(20, 20); |
| 72 | + |
| 73 | + // Step 1: Send first obstacle |
| 74 | + deduper.dedupeAndFill({obs_old}, output_msg); |
| 75 | + EXPECT_EQ(output_msg.obstacles_size(), 1); |
| 76 | + output_msg.Clear(); |
| 77 | + |
| 78 | + // Step 2: Send both. 'obs_old' should be deduped, 'obs_new' should pass. |
| 79 | + deduper.dedupeAndFill({obs_old, obs_new}, output_msg); |
| 80 | + |
| 81 | + ASSERT_EQ(output_msg.obstacles_size(), 1); |
| 82 | +} |
| 83 | + |
| 84 | +TEST_F(VisProtoDeduperTest, WindowEvictionLogic) |
| 85 | +{ |
| 86 | + // Window size of 2 |
| 87 | + // Frame 0: Send A (Stored in queue index 0) |
| 88 | + // Frame 1: Send empty (Stored in queue index 1) |
| 89 | + // Frame 2: Send empty (Stored in queue index 2) -> Window exceeded? |
| 90 | + // Logic check: if queue.size() > window. |
| 91 | + // After Frame 0: size 1. |
| 92 | + // After Frame 1: size 2. |
| 93 | + // After Frame 2: size 3. (3 > 2, so Frame 0 is evicted). |
| 94 | + |
| 95 | + VisProtoDeduper deduper(2); |
| 96 | + TbotsProto::ObstacleList output_msg; |
| 97 | + auto obs = createTestObstacle(5, 5); |
| 98 | + |
| 99 | + deduper.dedupeAndFill({obs}, output_msg); |
| 100 | + EXPECT_EQ(output_msg.obstacles_size(), 1); |
| 101 | + output_msg.Clear(); |
| 102 | + |
| 103 | + deduper.dedupeAndFill({}, output_msg); |
| 104 | + EXPECT_EQ(output_msg.obstacles_size(), 0); |
| 105 | + |
| 106 | + deduper.dedupeAndFill({}, output_msg); |
| 107 | + EXPECT_EQ(output_msg.obstacles_size(), 0); |
| 108 | + |
| 109 | + deduper.dedupeAndFill({obs}, output_msg); |
| 110 | + EXPECT_EQ(output_msg.obstacles_size(), 1) |
| 111 | + << "Obstacle should be resent after window expiration"; |
| 112 | +} |
| 113 | + |
| 114 | +TEST_F(VisProtoDeduperTest, ZeroWindowAlwaysSends) |
| 115 | +{ |
| 116 | + // If window size is 0, it should behave like a pass-through (or evict immediately) |
| 117 | + VisProtoDeduper deduper(0); |
| 118 | + TbotsProto::ObstacleList output_msg; |
| 119 | + auto obs = createTestObstacle(1, 1); |
| 120 | + |
| 121 | + // Pass 1 |
| 122 | + deduper.dedupeAndFill({obs}, output_msg); |
| 123 | + EXPECT_EQ(output_msg.obstacles_size(), 1); |
| 124 | + output_msg.Clear(); |
| 125 | + |
| 126 | + // Pass 2 - Should send again because window size is 0 (immediate eviction) |
| 127 | + deduper.dedupeAndFill({obs}, output_msg); |
| 128 | + EXPECT_EQ(output_msg.obstacles_size(), 1); |
| 129 | +} |
| 130 | + |
| 131 | +TEST_F(VisProtoDeduperTest, MultipleDistinctObstaclesInOneBatch) |
| 132 | +{ |
| 133 | + VisProtoDeduper deduper(5); |
| 134 | + TbotsProto::ObstacleList output_msg; |
| 135 | + |
| 136 | + auto obs1 = createTestObstacle(1, 1); |
| 137 | + auto obs2 = createTestObstacle(2, 2); |
| 138 | + auto obs3 = createTestObstacle(3, 3); |
| 139 | + |
| 140 | + // Send 3 unique obstacles at once |
| 141 | + deduper.dedupeAndFill({obs1, obs2, obs3}, output_msg); |
| 142 | + EXPECT_EQ(output_msg.obstacles_size(), 3); |
| 143 | + output_msg.Clear(); |
| 144 | + |
| 145 | + // Send 2 old, 1 new |
| 146 | + auto obs4 = createTestObstacle(4, 4); |
| 147 | + deduper.dedupeAndFill({obs1, obs3, obs4}, output_msg); |
| 148 | + |
| 149 | + ASSERT_EQ(output_msg.obstacles_size(), 1); |
| 150 | +} |
0 commit comments