Iterators are now working far more reliably and have more extensive tests that randomize inputs and fuzz them to check they keep working.

This commit is contained in:
Zed A. Shaw 2024-12-15 19:38:16 -05:00
parent 8e470df554
commit 70cd963e5c
11 changed files with 318 additions and 84 deletions

View file

@ -10,33 +10,35 @@
using namespace lighting;
TEST_CASE("lighting a map works", "[lighting]") {
Map map(20,20);
WorldBuilder builder(map);
builder.generate();
for(int i = 0; i < 5; i++) {
Map map(20+i,23+i);
WorldBuilder builder(map);
builder.generate();
Point light1 = map.place_entity(0);
Point light2 = map.place_entity(1);
LightSource source1{7,1};
LightSource source2{3,2};
Point light1 = map.place_entity(0);
Point light2 = map.place_entity(1);
LightSource source1{7,1};
LightSource source2{3,2};
LightRender lr(map.width(), map.height());
LightRender lr(map.width(), map.height());
lr.reset_light();
lr.reset_light();
lr.set_light_target(light1);
lr.set_light_target(light2);
lr.set_light_target(light1);
lr.set_light_target(light2);
lr.path_light(map.walls());
lr.path_light(map.walls());
lr.render_light(source1, light1);
lr.render_light(source2, light2);
lr.render_light(source1, light1);
lr.render_light(source2, light2);
lr.clear_light_target(light1);
lr.clear_light_target(light2);
lr.clear_light_target(light1);
lr.clear_light_target(light2);
const auto &lighting = lr.lighting();
const auto &lighting = lr.lighting();
// confirm light is set at least at and around the two points
REQUIRE(lighting[light1.y][light1.x] == lighting::LEVELS[source1.strength]);
REQUIRE(lighting[light2.y][light2.x] == lighting::LEVELS[source2.strength]);
// confirm light is set at least at and around the two points
REQUIRE(lighting[light1.y][light1.x] == lighting::LEVELS[source1.strength]);
REQUIRE(lighting[light2.y][light2.x] == lighting::LEVELS[source2.strength]);
}
}

View file

@ -3,6 +3,7 @@
#include <string>
#include "config.hpp"
#include "matrix.hpp"
#include "rand.hpp"
#include "components.hpp"
#include <nlohmann/json.hpp>
#include <fstream>
@ -19,34 +20,104 @@ TEST_CASE("basic matrix iterator", "[matrix]") {
Matrix walls = test["walls"];
matrix::dump("ITERATOR DUMP", walls);
println("VS matrix::each_row ------");
for(matrix::each_row it{walls}; it.next();) {
REQUIRE(walls[it.y][it.x] == it.cell);
print("{} ", it.cell);
if(it.row) print("\n");
}
// tests going through straight cells but also
// using two iterators on one matrix (or two)
matrix::each_cell cells{walls};
cells.next(); // kick it off
size_t row_count = 0;
for(matrix::each_row it{walls};
it.next(); cells.next())
{
REQUIRE(walls[cells.y][cells.x] == it.cell);
REQUIRE(walls[cells.y][cells.x] == walls[it.y][it.x]);
row_count += it.row;
}
println("END TEST=============");
REQUIRE(row_count == walls.size());
{
// test getting the correct height in the middle
row_count = 0;
matrix::in_box box{walls, 2,2, 1};
while(box.next()) {
row_count += box.x == box.left;
walls[box.y][box.x] = 3;
}
matrix::dump("2,2 WALLS", walls, 2, 2);
REQUIRE(row_count == 3);
}
{
matrix::dump("1:1 POINT", walls, 1,1);
// confirm boxes have the right number of rows
// when x goes to 0 on first next call
row_count = 0;
matrix::in_box box{walls, 1, 1, 1};
while(box.next()) {
row_count += box.x == box.left;
}
REQUIRE(row_count == 3);
}
}
TEST_CASE("matrix_assign works", "[matrix]") {
inline void random_matrix(Matrix &out) {
for(size_t y = 0; y < out.size(); y++) {
for(size_t x = 0; x < out[0].size(); x++) {
out[y][x] = Random::uniform<int>(-10,10);
}
}
}
TEST_CASE("matrix_dump works", "[matrix]") {
TEST_CASE("thash matrix iterators", "[matrix]") {
for(int count = 0; count < Random::uniform<int>(10,30); count++) {
size_t width = Random::uniform<size_t>(1, 100);
size_t height = Random::uniform<size_t>(1, 100);
Matrix test(width, matrix::Row(height));
random_matrix(test);
// first make a randomized matrix
matrix::each_cell cells{test};
cells.next(); // kick off the other iterator
for(matrix::each_row it{test};
it.next(); cells.next())
{
REQUIRE(test[cells.y][cells.x] == test[it.y][it.x]);
}
}
}
TEST_CASE("thrash box iterators", "[matrix]") {
for(int count = 0; count < 20; count++) {
size_t width = Random::uniform<size_t>(1, 25);
size_t height = Random::uniform<size_t>(1, 33);
Matrix test(height, matrix::Row(width));
random_matrix(test);
// this will be greater than the random_matrix cells
int test_i = Random::uniform<size_t>(20,30);
// go through every cell
for(matrix::each_cell target{test}; target.next();) {
PointList result;
// make a random size box
size_t size = Random::uniform<int>(1, 33);
matrix::in_box box{test, target.x, target.y, size};
while(box.next()) {
test[box.y][box.x] = test_i;
result.push_back({box.x, box.y});
}
for(auto point : result) {
REQUIRE(test[point.y][point.x] == test_i);
test[point.y][point.x] = 10; // kind of reset it for another try
}
}
}
}

View file

@ -10,25 +10,27 @@ using namespace nlohmann;
using std::string;
TEST_CASE("bsp algo test", "[builder]") {
Map map(20, 20);
Map map(31, 20);
WorldBuilder builder(map);
builder.generate();
}
TEST_CASE("dumping and debugging", "[builder]") {
Map map(20, 20);
WorldBuilder builder(map);
builder.generate();
matrix::dump("GENERATED", map.paths());
map.dump();
}
TEST_CASE("pathing", "[builder]") {
Map map(20, 20);
Map map(23, 14);
WorldBuilder builder(map);
builder.generate();
REQUIRE(map.can_move({0,0}) == false);
REQUIRE(map.iswall(0,0) == true);
matrix::dump("WALLS", map.$walls, 0,0);
println("wall at 0,0=={}", map.$walls[0][0]);
for(matrix::each_cell it{map.$walls}; it.next();) {
if(map.$walls[it.y][it.x] == WALL_VALUE) {
REQUIRE(map.iswall(it.x, it.y) == true);
REQUIRE(map.can_move({it.x, it.y}) == false);
} else {
REQUIRE(map.iswall(it.x, it.y) == false);
REQUIRE(map.can_move({it.x, it.y}) == true);
}
}
}