diff --git a/u02/src/tests.cpp b/u02/src/tests.cpp index 2fc9cd0..f0d205a 100644 --- a/u02/src/tests.cpp +++ b/u02/src/tests.cpp @@ -563,5 +563,26 @@ TEST_CASE("Star (prop: all points inside circle)") { REQUIRE(pass); } -// The Haskell implementation of regular_polygon_mod has many tests, +TEST_CASE("Star (prop: average of all points is centre)") { + const int x0 = GENERATE(take(5, random(-100, 100))); + const int y0 = GENERATE(take(5, random(-100, 100))); + const int n = GENERATE(take(5, random(3, 1000))); + const float r2 = GENERATE(take(5, random(0, 100))); + const float r_factor = GENERATE(take(5, random(0, 1))); + const float r1 = r_factor * r2; + const float angle = GENERATE(take(5, random(-16.0 * atan(1), 16 * atan(1)))); + + std::vector> points = star(n, r1, r2, x0, y0, angle); + + float x = 0, y = 0; + for (std::pair point : points) { + x += point.first; + y += point.second; + } + + REQUIRE_THAT(x / points.size(), WithinRel(x0, 0.1)); + REQUIRE_THAT(y / points.size(), WithinRel(y0, 0.1)); +} + +// The Haskell implementation of regular_polygon_mod has many more tests, // but they are not implemented here for brevity.