From 085b8fcdb93ae1b2e4aec49764aa9142b90664d8 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Sat, 13 May 2023 22:06:37 +0200 Subject: [PATCH] u02/tests: Fix edge case for invalid points --- u02/src/tests.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/u02/src/tests.cpp b/u02/src/tests.cpp index 81521ae..c8e5947 100644 --- a/u02/src/tests.cpp +++ b/u02/src/tests.cpp @@ -438,16 +438,22 @@ TEST_CASE("Barycentric coordinates (prop: Σ = 1)") { int x2 = GENERATE(take(2, random(-100, 100))); int y2 = GENERATE(take(2, random(-100, 100))); + // If all points are on a straight line, the property does not hold. + // Checking this is equivalent to checking if the area of the triangle is 0. + if ((x0 - x1) * (y0 - y2) - (y0 - y1) * (x0 - x2) == 0) { + // see above + std::cerr << "Points do not form reasonable triangle, skipping" + << std::endl; + return; + } + int x = GENERATE(take(5, random(-100, 100))); int y = GENERATE(take(5, random(-100, 100))); float b1, b2, b3; std::tie(b1, b2, b3) = barycentric_coordinates(x0, y0, x1, y1, x2, y2, x, y); - // If all points are on a straight line, the property does not hold - if (!(x0 == x1 && x1 == x2) && !(y0 == y1 && y1 == y2)) { - REQUIRE_THAT(b1 + b2 + b3, WithinAbs(1.0, 0.01)); - } + REQUIRE_THAT(b1 + b2 + b3, WithinAbs(1.0, 0.01)); } TEST_CASE("Sort triangle points") {