test_distance_map.cpp
Go to the documentation of this file.
1 // Copyright 2022-2023 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 
18 #include <array>
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstdlib>
22 #include <vector>
23 
25 
26 namespace {
27 
28 auto array_distance(std::size_t first, std::size_t second) {
29  return std::abs(static_cast<intmax_t>(first) - static_cast<intmax_t>(second));
30 }
31 
32 auto make_neighbors_function(std::size_t size) {
33  return [=](std::size_t index) {
34  auto result = std::vector<std::size_t>{};
35  if (index > 0) {
36  result.push_back(index - 1);
37  }
38  if (index < size - 1) {
39  result.push_back(index + 1);
40  }
41  return result;
42  };
43 }
44 
45 TEST(DistanceMap, None) {
46  auto map = std::vector<bool>{};
47  auto distance_map = beluga::nearest_obstacle_distance_map(map, array_distance, make_neighbors_function(6));
48  ASSERT_EQ(distance_map.size(), 0);
49 }
50 
51 TEST(DistanceMap, Empty) {
52  auto map = std::array<bool, 6>{false, false, false, false, false, false};
53  auto distance_map = beluga::nearest_obstacle_distance_map(map, array_distance, make_neighbors_function(6));
54  ASSERT_THAT(distance_map, testing::ElementsAre(0, 0, 0, 0, 0, 0));
55 }
56 
57 TEST(DistanceMap, Full) {
58  auto map = std::array<bool, 6>{true, true, true, true, true, true};
59  auto distance_map = beluga::nearest_obstacle_distance_map(map, array_distance, make_neighbors_function(6));
60  ASSERT_THAT(distance_map, testing::ElementsAre(0, 0, 0, 0, 0, 0));
61 }
62 
63 TEST(DistanceMap, Case1) {
64  auto map = std::array<bool, 6>{false, true, false, false, false, true};
65  auto distance_map = beluga::nearest_obstacle_distance_map(map, array_distance, make_neighbors_function(6));
66  ASSERT_THAT(distance_map, testing::ElementsAre(1, 0, 1, 2, 1, 0));
67 }
68 
69 TEST(DistanceMap, Case2) {
70  auto map = std::array<bool, 6>{true, true, false, false, false, false};
71  auto distance_map = beluga::nearest_obstacle_distance_map(map, array_distance, make_neighbors_function(6));
72  ASSERT_THAT(distance_map, testing::ElementsAre(0, 0, 1, 2, 3, 4));
73 }
74 
75 TEST(DistanceMap, Case3) {
76  auto map = std::array<bool, 6>{false, false, false, false, false, true};
77  auto distance_map = beluga::nearest_obstacle_distance_map(map, array_distance, make_neighbors_function(6));
78  ASSERT_THAT(distance_map, testing::ElementsAre(5, 4, 3, 2, 1, 0));
79 }
80 
81 } // namespace
result
result[0]
beluga::nearest_obstacle_distance_map
auto nearest_obstacle_distance_map(Range &&obstacle_map, DistanceFunction &&distance_function, NeighborsFunction &&neighbors_function)
Returns a map where the value of each cell is the distance to the nearest obstacle.
Definition: distance_map.hpp:54
distance_map.hpp
Implementation of algorithm to calculate distance from obstacles.
beluga::TEST
TEST(Bresenham, MultiPassGuarantee)
Definition: test_bresenham.cpp:27


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53