James Kuszmaul | 18008f8 | 2023-02-23 20:52:50 -0800 | [diff] [blame] | 1 | #include "y2023/localizer/map_expander_lib.h" |
| 2 | |
| 3 | #include "gtest/gtest.h" |
| 4 | #include "aos/testing/flatbuffer_eq.h" |
| 5 | |
| 6 | namespace y2023::localizer::testing { |
| 7 | class MapExpanderTest : public ::testing::Test { |
| 8 | protected: |
| 9 | MapExpanderTest() |
| 10 | : relative_map_(aos::JsonFileToFlatbuffer<RelativeScoringMap>( |
| 11 | "y2023/constants/relative_scoring_map.json")), |
| 12 | target_map_(aos::JsonFileToFlatbuffer<frc971::vision::TargetMap>( |
| 13 | "y2023/vision/maps/target_map.json")), |
| 14 | absolute_map_( |
| 15 | ExpandMap(&relative_map_.message(), &target_map_.message())) {} |
| 16 | const aos::FlatbufferDetachedBuffer<RelativeScoringMap> relative_map_; |
| 17 | const aos::FlatbufferDetachedBuffer<frc971::vision::TargetMap> target_map_; |
| 18 | const aos::FlatbufferDetachedBuffer<ScoringMap> absolute_map_; |
| 19 | }; |
| 20 | TEST_F(MapExpanderTest, BackAndForthConsistent) { |
| 21 | // Use FlatbufferToJson instead of FlatbufferEq because we don't want |
| 22 | // equivalent but different encoded floating point numbers to get |
| 23 | // evaluated differently. |
| 24 | #define CHECK_REVERSE(color, grid) \ |
| 25 | { \ |
| 26 | ASSERT_TRUE(absolute_map_.message().has_##color()); \ |
| 27 | auto half = absolute_map_.message().color(); \ |
| 28 | ASSERT_TRUE(half->has_##grid##_grid()); \ |
| 29 | auto grid = half->grid##_grid(); \ |
| 30 | auto relative_grid = \ |
| 31 | RelativeGridForTag(grid, &target_map_.message(), \ |
| 32 | relative_map_.message().color()->grid()); \ |
| 33 | EXPECT_EQ(aos::FlatbufferToJson(relative_map_.message().nominal_grid()), \ |
| 34 | aos::FlatbufferToJson(&relative_grid.message())); \ |
| 35 | } |
| 36 | CHECK_REVERSE(blue, left); |
| 37 | CHECK_REVERSE(blue, middle); |
| 38 | CHECK_REVERSE(blue, right); |
| 39 | CHECK_REVERSE(red, left); |
| 40 | CHECK_REVERSE(red, middle); |
| 41 | CHECK_REVERSE(red, right); |
| 42 | } |
| 43 | |
| 44 | // Test that the currently checked-in map is consistent with the results of |
| 45 | // ExpandMap. |
| 46 | TEST_F(MapExpanderTest, ExpandMap) { |
| 47 | const std::string stored_map = |
| 48 | aos::util::ReadFileToStringOrDie("y2023/constants/scoring_map.json"); |
| 49 | // TODO: Provide coherent error messages so that changes can be accommodated. |
| 50 | EXPECT_EQ(aos::FlatbufferToJson(absolute_map_, |
| 51 | {.multi_line = true, .max_multi_line = true}), |
| 52 | stored_map); |
| 53 | } |
| 54 | } // namespace y2023::localizer::testing |