From 96fe8194386421855279c52a7061b8d21ba60767 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 8 Feb 2023 20:46:54 +0100 Subject: Test lines.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- common.h | 6 ++++ lines.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/common.h b/common.h index 805ba0b..2e8b8bc 100644 --- a/common.h +++ b/common.h @@ -145,6 +145,12 @@ tolerant_eq(double a, double b) return iszeroish((a - b) / 100.0); } +static inline int +intolerant_eq(double a, double b) +{ + return fabs(a - b) < 1e-11; +} + # define ASSERT(ASSERTION)\ do {\ if (!(ASSERTION)) {\ diff --git a/lines.c b/lines.c index dfe2448..d43c7fe 100644 --- a/lines.c +++ b/lines.c @@ -217,10 +217,129 @@ draw_bounded_line(RASTER *restrict raster, double x1, double y1, double x2, doub #else +static RASTER *raster; +static RASTER *refraster; + + +static double +frand(double a, double b) +{ + int i = rand(); + double f = (double)i / (double)RAND_MAX; + double max = fmax(a, b); + double min = fmin(a, b); + return fma(f, max - min, min); +} + + +static void +check_against_reference(double x1, double y1, double x2, double y2) +{ + size_t i; + alarm(5); + draw_linear_bezier_reference(refraster, x1, y1, x2, y2); + alarm(0); + for (i = 0; i < 100; i++) { + ASSERT(intolerant_eq(raster->cells[i].cell_coverage, refraster->cells[i].cell_coverage)); + ASSERT(intolerant_eq(raster->cells[i].opposite_coverage, refraster->cells[i].opposite_coverage)); + } +} + + +static void +check_vertical_line(double x, double y1, double y2) +{ + alarm(5); + draw_vertical_line(raster, x, y1, y2, SIGNUM(y2 - y1)); + alarm(0); + check_against_reference(x, y1, x, y2); +} + + +static void +check_diagonal_line(double x1, double y1, double x2, double y2) +{ + double dx = x2 - x1; + double dy = y2 - y1; + alarm(5); + draw_diagonal_line(raster, x1, y1, x2, y2, dx, dy, SIGNUM(dx), SIGNUM(dy)); + alarm(0); + check_against_reference(x1, y1, x2, y2); +} + + +static void +check_bounded_line(double x1, double y1, double x2, double y2) +{ + alarm(5); + draw_bounded_line(raster, x1, y1, x2, y2); + alarm(0); + check_against_reference(x1, y1, x2, y2); +} + + +#ifndef ROWWISE_RESET_INKLEVEL +static void +check_vertical_line_opposite_only(double y1, double y2) +{ + alarm(5); + draw_vertical_line_opposite_only(raster, 9, y1, y2, SIGNUM(y2 - y1)); + alarm(0); + check_against_reference(20, y1, 20, y2); +} +#endif + + +static void +reset(void) +{ + rtgrpblib_reset_raster(raster, 10, 10); + rtgrpblib_reset_raster(refraster, 10, 10); +} + + int main(void) { - return 0; /* TODO add test */ + double x1, x2; + double y1, y2; + size_t i; + + raster = rtgrpblib_create_raster(10, 10); + ASSERT(raster); + refraster = rtgrpblib_create_raster(10, 10); + ASSERT(refraster); + srand((unsigned)(uintptr_t)raster); + + for (i = 0; i < 10000UL; i++) { + x1 = frand(0, 10); + y1 = frand(0, 10); + x2 = frand(0, 10); + y2 = frand(0, 10); + + check_vertical_line(x1, y1, y2); + reset(); + + if (x1 != x2) { + check_diagonal_line(x1, y1, x2, y2); + reset(); + } + + check_bounded_line(x1, y1, x1, y2); + reset(); + + check_bounded_line(x1, y1, x2, y2); + reset(); + +#ifndef ROWWISE_RESET_INKLEVEL + check_vertical_line_opposite_only(y1, y2); + reset(); +#endif + } + + free(raster); + free(refraster); + return 0; } -- cgit v1.2.3-70-g09d2