-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_breadth_first.cpp
More file actions
55 lines (47 loc) · 1.91 KB
/
test_breadth_first.cpp
File metadata and controls
55 lines (47 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#define TCODPATH_ValueType int16_t
#define TCODPATH_VALUE_MAX INT16_MAX
#define TCODPATH_VALUE_MIN INT16_MIN
#define TCODPATH_IndexType int16_t
#include <libtcod-path/breadth_first_search.h>
#include <libtcod-path/flow_tools.h>
#include <array>
#include <catch2/catch_all.hpp>
#include <limits>
#include <stdexcept>
#include "common.h"
TEST_CASE("TCODPATH_bfs", "") {
static const auto TEST_DATA = std::vector<std::string>{
"0#44567",
"1#34##7",
"2234567",
};
auto costs = wall_costs_from_test_data(TEST_DATA);
auto distance = Map2D(costs.get_shape(), std::numeric_limits<Map2D<>::value_type>::max());
distance[{0, 0}] = 0;
auto graph = as_2d_graph(costs, 1, 1);
auto flow_data = std::vector<TCODPATH_IndexType>(distance.get_shape().at(0) * distance.get_shape().at(1) * 2);
auto flow_shape = std::array<TCODPATH_IndexType, 3>{distance.get_shape().at(0), distance.get_shape().at(1), 2};
auto flow_map = TCODPATH_Map{};
TCODPATH_map_init_contigious_from(
&flow_map,
3,
flow_shape.data(),
static_cast<int>(sizeof(TCODPATH_IndexType)) * (std::is_signed_v<TCODPATH_IndexType> ? -1 : 1),
static_cast<void*>(flow_data.data()));
TCODPATH_flow_reset(&flow_map);
TCODPATH_bfs(&graph, distance.c_data(), &flow_map);
REQUIRE(as_string(TEST_DATA) == as_string(distance));
static const auto EXPECTED_PATH = std::vector<std::array<TCODPATH_IndexType, 2>>{{1, 2}, {2, 1}, {1, 0}, {0, 0}};
auto path = get_path(flow_map, {0, 2});
REQUIRE(path == EXPECTED_PATH);
}
TEST_CASE("TCODPATH_bfs large", "[.slow]") {
static constexpr auto SIZE = 2048;
auto costs = Map2D{{SIZE, SIZE}, 1};
auto distance = Map2D(costs.get_shape(), std::numeric_limits<Map2D<>::value_type>::max());
distance[{0, 0}] = 0;
auto graph = as_2d_graph(costs, 1, 1);
TCODPATH_bfs(&graph, distance.c_data(), nullptr);
CHECK(distance[{0, 0}] == 0);
CHECK(distance[{SIZE - 1, SIZE - 1}] == SIZE - 1);
}