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