diff --git a/meson.build b/meson.build index 90eab10..76629db 100644 --- a/meson.build +++ b/meson.build @@ -37,6 +37,7 @@ runtests = executable('runtests', [ 'systems.cpp', 'gui.cpp', 'worldbuilder.cpp', + 'tests/matrix.cpp', 'tests/fsm.cpp', 'tests/dbc.cpp', 'tests/map.cpp', diff --git a/status.txt b/status.txt index 616708e..3b4dfac 100644 --- a/status.txt +++ b/status.txt @@ -1,5 +1,10 @@ TODAY'S GOAL: +* Change the test matrix to be irregular dimensions. +* Study https://github.com/hirdrac/gx_lib/blob/main/gx/Unicode.hh +* Study this https://en.cppreference.com/w/cpp/language/explicit +* Study https://en.cppreference.com/w/cpp/language/member_functions#Special_member_functions + * Light should flood using the dijkstra map rather than use a box. -1. Learn std::initializer_list by using it. diff --git a/systems.cpp b/systems.cpp index a8611be..2203ef9 100644 --- a/systems.cpp +++ b/systems.cpp @@ -10,7 +10,7 @@ #include "dbc.hpp" #include "lights.hpp" -const bool DEBUG_MAP=true; +const bool DEBUG_MAP=false; using std::string; using namespace fmt; diff --git a/tests/matrix.cpp b/tests/matrix.cpp new file mode 100644 index 0000000..3ee0ddc --- /dev/null +++ b/tests/matrix.cpp @@ -0,0 +1,221 @@ +#include +#include +#include +#include "config.hpp" +#include "matrix.hpp" +#include "components.hpp" +#include +#include + +using namespace nlohmann; +using namespace fmt; +using std::string; + + + +#include +#include + +struct ItStep { + int cell; + size_t x; + size_t y; + bool row; +}; + +class MatrixIterator1 { + public: + class iterator + { + public: + Matrix &mat; + size_t x = 0; + size_t y = 0; + + using iterator_category = std::input_iterator_tag; + using value_type = ItStep; + using difference_type = ItStep; + using pointer = ItStep *; + using reference = ItStep; + + explicit iterator(Matrix &_mat) + : mat(_mat) {} + + iterator& operator++() { + x++; + if(x < mat[0].size()) { + return *this; + } else { + x = 0; + y++; + return *this; + } + } + + iterator operator++(int) { + iterator retval = *this; + ++(*this); + return retval; + } + + bool operator==(iterator other) const { + return x == other.x && y == other.y; + } + + reference operator*() const { + return { + .cell = mat[y][x], + .x = x, + .y = y, + .row = x >= mat[0].size() - 1 + }; + } + }; + + Matrix &mat; + + MatrixIterator1(Matrix &mat_) : + mat(mat_) + { + } + + iterator begin() { + return iterator(mat); + } + + iterator end() { + iterator it(mat); + it.y = mat.size(); + it.x = 0; + return it; + } +}; + +struct MatrixIterator2 { + Matrix &$mat; + size_t x = ~0; + size_t y = ~0; + size_t width = 0; + size_t height = 0; + int cell = 0; + bool row = false; + + MatrixIterator2(Matrix &mat) : + $mat(mat) + { + height = $mat.size(); + width = $mat[0].size(); + } + + bool next() { + x++; + x *= (x < width); + y = y + (x == 0); + row = x == width - 1; + + if(y < height) { + cell = $mat[y][x]; + return true; + } else { + return false; + } + } +}; + +TEST_CASE("basic matrix iterator", "[matrix]") { + std::ifstream infile("./tests/dijkstra.json"); + json data = json::parse(infile); + auto test = data[0]; + + Matrix walls = test["walls"]; + + matrix_dump("ITERATOR DUMP", walls); + + println("VS Matrix1 ------"); + + for(auto [cell, x, y, row] : MatrixIterator1(walls)) { + REQUIRE(walls[y][x] == cell); + print("{} ", cell); + if(row) print("\n"); + } + + println("VS Matrix2------"); + + for(MatrixIterator2 mit{walls}; mit.next();) { + REQUIRE(walls[mit.y][mit.x] == mit.cell); + print("{} ", mit.cell); + if(mit.row) print("\n"); + } + + println("END TEST============="); +} + + +template +class Range { + public: + class iterator + { + long num = FROM; + + public: + using iterator_category = std::input_iterator_tag; + using value_type = long; + using difference_type = long; + using pointer = const long *; + using reference = long; + + explicit iterator(long _num = 0) : num(_num) {} + + iterator& operator++() { + num = TO >= FROM ? num + 1 : num - 1; + return *this; + } + + iterator operator++(int) { + iterator retval = *this; + ++(*this); + return retval; + } + + bool operator==(iterator other) const { + return num == other.num; + } + + bool operator!=(iterator other) const { + return !(*this == other); + } + + reference operator*() const { + return num; + } + }; + + iterator begin() { + return iterator(FROM); + } + + iterator end() { + return iterator(TO >= FROM ? TO + 1 : TO - 1); + } +}; + +TEST_CASE("basic matrix example", "[matrix]") { + auto range = Range<15, 25>(); + auto itr = std::find(range.begin(), range.end(), 18); + std::cout << *itr << '\n'; + + for(long l : Range<3, 5>()) { + std::cout << l << ' '; + } + + std::cout << '\n'; +} + +TEST_CASE("matrix_assign works", "[matrix]") { + +} + +TEST_CASE("matrix_dump works", "[matrix]") { + +}