diff --git a/map.cpp b/map.cpp index b89d6b5..513331e 100644 --- a/map.cpp +++ b/map.cpp @@ -5,8 +5,8 @@ using std::vector, std::pair; using namespace fmt; -void dump_map(Matrix &map) { - println("-----------------"); +void dump_map(const std::string &msg, Matrix &map) { + println("----------------- {}", msg); for(auto row : map) { for(auto col : row) { print("{} ", col); @@ -40,15 +40,15 @@ Matrix dijkstra_map(Matrix &input_map, Matrix &walls_map, int limit) { size_t w = input_map[0].size(); // Initialize the new array with every pixel at limit distance - Matrix new_arr = Matrix(h, MatrixRow(w, 1)); + // NOTE: this is normally ones() * limit + Matrix new_arr = Matrix(h, MatrixRow(w, limit)); Matrix closed = walls_map; PairList starting_pixels; PairList open_pixels; limit = limit == 0 ? h * w : limit; - // First pass: Add starting pixels and put them in closed - for(size_t counter = 0; counter < h * w; counter++) { + for(size_t counter = 0; counter < h * w; counter++) { size_t i = counter % w; size_t j = counter / w; if(input_map[j][i] == 0) { diff --git a/map.hpp b/map.hpp index cf0030c..0805877 100644 --- a/map.hpp +++ b/map.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include struct Pair { size_t j = 0; @@ -11,7 +12,7 @@ typedef std::vector PairList; typedef std::vector MatrixRow; typedef std::vector Matrix; -void dump_map(Matrix &map); +void dump_map(const std::string &msg, Matrix &map); void add_neighbors(Matrix &closed, size_t j, size_t i); Matrix dijkstra_map(Matrix &input_map, Matrix &walls_map, int limit=0); diff --git a/tests/map.cpp b/tests/map.cpp index 1cafe62..b40bf40 100644 --- a/tests/map.cpp +++ b/tests/map.cpp @@ -1,35 +1,34 @@ #include #include "map.hpp" #include +#include +#include using namespace fmt; +using namespace nlohmann; +using std::string; + +json load_test_data(const string &fname) { + std::ifstream infile(fname); + return json::parse(infile); +} TEST_CASE("dijkstra algo test", "[map]") { - Matrix in_map = { - {1, 1, 1, 0}, - {1, 0, 1, 1}, - {1, 0, 1, 1}, - {1, 1, 1, 1}, - }; - Matrix walls = { - {0, 0, 0, 0}, - {0, 0, 0, 0}, - {0, 0, 1, 0}, - {0, 0, 1, 0}, - }; - Matrix expected = { - {1, 1, 1, 0}, - {1, 0, 1, 1}, - {1, 0, 0, 2}, - {1, 1, 0, 3}, - }; + json data = load_test_data("./tests/dijkstra.json"); - Matrix res = dijkstra_map(in_map, walls); + for(auto &test : data) { + Matrix in_map = test["input"]; + Matrix walls = test["walls"]; + Matrix expected = test["expected"]; + int limit = test["limit"]; + Matrix res = dijkstra_map(in_map, walls, limit); - println("--- EXPECTED:"); - dump_map(expected); - println("--- RESULT:"); - dump_map(res); + if(res != expected) { + println("ERROR! ------"); + dump_map("EXPECTED", expected); + dump_map("RESULT", res); + } - REQUIRE(res == expected); + REQUIRE(res == expected); + } }