Skip to content

Commit

Permalink
Increase test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregersn committed Nov 12, 2024
1 parent 04ac8e2 commit 9fbb0ee
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 2 deletions.
66 changes: 66 additions & 0 deletions screenshot-tests/test-graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,69 @@ TEST_CASE("Draw multiple times")
std::remove("multidraw_b_1.png");
std::remove("multidraw_b_2.png");
}

TEST_CASE("Test smoothing")
{
std::filesystem::path filename = "graphics_smooth.png";

Pyro::Graphics *smoothing = Pyro::creategraphics(9 * 50, 50);
smoothing->ellipsemode(Pyro::CENTER);
for (unsigned int i = 0; i < 9; i++)
{
smoothing->smooth(i);
smoothing->ellipse(25, 25, 25, 25);
smoothing->translate(50, 0);
}

smoothing->save(current_folder / filename);
delete smoothing;
CHECK_THAT(current_folder / filename, LooksLike(actual_folder / filename));
}

TEST_CASE("Test blend modes")
{
std::filesystem::path filename = "graphics_blend.png";
Pyro::Graphics *blending = Pyro::creategraphics(300, 300);

blending->nostroke();

blending->blendmode(Pyro::BLEND);
blending->fill(1.0f, 0.0f, 0.0f, .5f);
blending->rect(0, 0, 125, 125);

blending->blendmode(Pyro::REPLACE);
blending->fill(0.0f, 1.0f, 0.0f, .5f);
blending->rect(100, 0, 125, 125);

blending->blendmode(Pyro::ADD);
blending->fill(0.0f, 0.0f, 1.0f, .5f);
blending->rect(200, 0, 125, 125);

blending->blendmode(Pyro::LIGHTEST);
blending->fill(1.0f, 0.5f, 0.0f, .5f);
blending->rect(0, 100, 125, 125);

blending->blendmode(Pyro::DARKEST);
blending->fill(0.0f, 1.0f, 0.5f, .5f);
blending->rect(100, 100, 125, 125);

blending->blendmode(Pyro::DIFFERENCE);
blending->fill(0.5f, 0.0f, 1.0f, .5f);
blending->rect(200, 100, 125, 125);

blending->blendmode(Pyro::EXCLUSION);
blending->fill(0.0f, 0.5f, 1.0f, .5f);
blending->rect(0, 200, 125, 125);

blending->blendmode(Pyro::MULTIPLY);
blending->fill(1.0f, 0.0f, 0.5f, .5f);
blending->rect(100, 200, 125, 125);

blending->blendmode(Pyro::SCREEN);
blending->fill(0.5f, 1.0f, 0.0f, .5f);
blending->rect(200, 200, 125, 125);

blending->save(current_folder / filename);
delete blending;
CHECK_THAT(current_folder / filename, LooksLike(actual_folder / filename));
}
3 changes: 3 additions & 0 deletions screenshots/expected/graphics_blend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions screenshots/expected/graphics_smooth.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 79 additions & 2 deletions tests/test-math.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <catch2/catch_all.hpp>

#include <complex>
#include "pyro/constants.h"
#include "pyro/math.h"

Expand Down Expand Up @@ -39,6 +39,78 @@ TEST_CASE("Random generation seems sane", "[random]")
}
}

SECTION("Test random with negative range")
{
int firsts[100] = {0};
for (unsigned int i = 0; i < 100; i++)
{
Pyro::randomseed(i);
int v = Pyro::random(-100);
REQUIRE(v < 0);
firsts[abs(v)]++;
}

for (unsigned int i = 0; i < 100; i++)
{
INFO("The number is " << i);
REQUIRE(firsts[i] < 99);
}
}

SECTION("Test random with upper and lower range")
{
int firsts[100] = {0};
for (unsigned int i = 0; i < 100; i++)
{
Pyro::randomseed(i);
int v = Pyro::random(10u, 90u);
firsts[v]++;
}

for (unsigned int i = 0; i < 10; i++)
{
INFO("The number is " << i);
REQUIRE(firsts[i] == 0);
}
for (unsigned int i = 10; i < 90; i++)
{
INFO("The number is " << i);
REQUIRE(firsts[i] < 99);
}
for (unsigned int i = 90; i < 100; i++)
{
INFO("The number is " << i);
REQUIRE(firsts[i] == 0);
}
}

SECTION("Test random with upper and lower negative range")
{
int firsts[100] = {0};
for (unsigned int i = 0; i < 100; i++)
{
Pyro::randomseed(i);
int v = Pyro::random(-40, 40);
firsts[v + 50]++;
}

for (unsigned int i = 0; i < 10; i++)
{
INFO("The number is " << i);
REQUIRE(firsts[i] == 0);
}
for (unsigned int i = 10; i < 90; i++)
{
INFO("The number is " << i);
REQUIRE(firsts[i] < 99);
}
for (unsigned int i = 90; i < 100; i++)
{
INFO("The number is " << i);
REQUIRE(firsts[i] == 0);
}
}

SECTION("Test random number standard deviation")
{
unsigned int tests = 1000000;
Expand All @@ -64,7 +136,8 @@ TEST_CASE("Values can be...", "[math]")
REQUIRE(Pyro::constrain(30, 10, 20) == 20);
}

SECTION("normalized") {
SECTION("normalized")
{
REQUIRE(Pyro::norm(20, 0, 50) == 0.4f);
REQUIRE(Pyro::norm(-10, 0, 100) == -0.1f);
}
Expand All @@ -87,6 +160,10 @@ TEST_CASE("We have functions that can...", "[math]")
REQUIRE(Pyro::max(53, 3) == 53);
REQUIRE(Pyro::max(23, -34) == 23);
}
SECTION("give eulers number")
{
REQUIRE(2.7182817 == Approx(Pyro::exp(1.0)));
}
}

SCENARIO("Degrees can be converted to radians and back", "[math]")
Expand Down
79 changes: 79 additions & 0 deletions tests/test-transformer2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ TEST_CASE("Basic transformations", "[transformer]")
REQUIRE(t.getcurrent()(2, 2) == 1);
}

SECTION("Translate to position with vector")
{
Transformer2D t = Transformer2D();
t.translate(Vector(2, 4));
REQUIRE(t.getcurrent()(0, 0) == 1);
REQUIRE(t.getcurrent()(0, 1) == 0);
REQUIRE(t.getcurrent()(0, 2) == 2);

REQUIRE(t.getcurrent()(1, 0) == 0);
REQUIRE(t.getcurrent()(1, 1) == 1);
REQUIRE(t.getcurrent()(1, 2) == 4);

REQUIRE(t.getcurrent()(2, 0) == 0);
REQUIRE(t.getcurrent()(2, 1) == 0);
REQUIRE(t.getcurrent()(2, 2) == 1);
}

SECTION("Rotate")
{
Transformer2D t = Transformer2D();
Expand Down Expand Up @@ -69,6 +86,22 @@ TEST_CASE("Basic transformations", "[transformer]")
REQUIRE(t.getcurrent()(1, 1) == Approx(2.3));
REQUIRE(t.getcurrent()(1, 2) == 0);

REQUIRE(t.getcurrent()(2, 0) == 0);
REQUIRE(t.getcurrent()(2, 1) == 0);
REQUIRE(t.getcurrent()(2, 2) == 1);
}
SECTION("Scale with vector")
{
Transformer2D t = Transformer2D();
t.scale(Vector(1.5, 2.3));
REQUIRE(t.getcurrent()(0, 0) == 1.5);
REQUIRE(t.getcurrent()(0, 1) == 0);
REQUIRE(t.getcurrent()(0, 2) == 0);

REQUIRE(t.getcurrent()(1, 0) == 0);
REQUIRE(t.getcurrent()(1, 1) == Approx(2.3));
REQUIRE(t.getcurrent()(1, 2) == 0);

REQUIRE(t.getcurrent()(2, 0) == 0);
REQUIRE(t.getcurrent()(2, 1) == 0);
REQUIRE(t.getcurrent()(2, 2) == 1);
Expand Down Expand Up @@ -139,3 +172,49 @@ TEST_CASE("Get screen coordinates from Pyro", "[transform]")
REQUIRE(Pyro::screen_y(4, 0, 0) == Approx(0.0));
}
}

TEST_CASE("Push and pop matrix")
{
Transformer2D t = Transformer2D();
t.translate(2, 4);
REQUIRE(t.getcurrent()(0, 0) == 1);
REQUIRE(t.getcurrent()(0, 1) == 0);
REQUIRE(t.getcurrent()(0, 2) == 2);

REQUIRE(t.getcurrent()(1, 0) == 0);
REQUIRE(t.getcurrent()(1, 1) == 1);
REQUIRE(t.getcurrent()(1, 2) == 4);

REQUIRE(t.getcurrent()(2, 0) == 0);
REQUIRE(t.getcurrent()(2, 1) == 0);
REQUIRE(t.getcurrent()(2, 2) == 1);

t.pushmatrix();

t.translate(2, 4);
REQUIRE(t.getcurrent()(0, 0) == 1);
REQUIRE(t.getcurrent()(0, 1) == 0);
REQUIRE(t.getcurrent()(0, 2) == 4);

REQUIRE(t.getcurrent()(1, 0) == 0);
REQUIRE(t.getcurrent()(1, 1) == 1);
REQUIRE(t.getcurrent()(1, 2) == 8);

REQUIRE(t.getcurrent()(2, 0) == 0);
REQUIRE(t.getcurrent()(2, 1) == 0);
REQUIRE(t.getcurrent()(2, 2) == 1);

t.popmatrix();

REQUIRE(t.getcurrent()(0, 0) == 1);
REQUIRE(t.getcurrent()(0, 1) == 0);
REQUIRE(t.getcurrent()(0, 2) == 2);

REQUIRE(t.getcurrent()(1, 0) == 0);
REQUIRE(t.getcurrent()(1, 1) == 1);
REQUIRE(t.getcurrent()(1, 2) == 4);

REQUIRE(t.getcurrent()(2, 0) == 0);
REQUIRE(t.getcurrent()(2, 1) == 0);
REQUIRE(t.getcurrent()(2, 2) == 1);
}

0 comments on commit 9fbb0ee

Please sign in to comment.