From 330ead5f47a7c4904cc6cecfa36b10d75718f1ff Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 18 Dec 2012 12:44:23 -0500 Subject: Convert the polynomial tests to the new testing framework. --- libdimension/tests/polynomial.c | 57 +++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 31 deletions(-) (limited to 'libdimension/tests/polynomial.c') diff --git a/libdimension/tests/polynomial.c b/libdimension/tests/polynomial.c index b0d3b77..8fd34df 100644 --- a/libdimension/tests/polynomial.c +++ b/libdimension/tests/polynomial.c @@ -1,5 +1,5 @@ /************************************************************************* - * Copyright (C) 2010-2011 Tavian Barnes * + * Copyright (C) 2010-2012 Tavian Barnes * * * * This file is part of The Dimension Test Suite. * * * @@ -17,46 +17,41 @@ * along with this program. If not, see . * *************************************************************************/ -/* - * Basic test of numerical polynomial root-finder +/** + * @file + * Basic tests of the polynomial root-finder. */ -#include "dimension.h" -#include -#include +#include "tests.h" -int -main(void) -{ - /* Treat warnings as errors for tests */ - dmnsn_die_on_warnings(true); +/* poly[] = 2*(x + 1)*(x - 1.2345)*(x - 2.3456)*(x - 5)*(x - 100) */ +const double poly[6] = { + [5] = 2.0, + [4] = -215.1602, + [3] = 1540.4520864, + [2] = -2430.5727856, + [1] = -1292.541872, + [0] = 2895.6432, +}; - double poly[6], x[5]; - /* poly[] = 2*(x + 1)*(x - 1.2345)*(x - 2.3456)*(x - 5)*(x - 100) */ - poly[5] = 2.0; - poly[4] = -215.1602; - poly[3] = 1540.4520864; - poly[2] = -2430.5727856; - poly[1] = -1292.541872; - poly[0] = 2895.6432; +DMNSN_TEST("polynomial", finds_positive_roots) +{ + double x[5]; + size_t n = dmnsn_polynomial_solve(poly, 5, x); + ck_assert_int_eq(n, 4); +} +DMNSN_END_TEST +DMNSN_TEST("polynomial", accurate_roots) +{ + double x[5]; size_t n = dmnsn_polynomial_solve(poly, 5, x); - if (n != 4) { - fprintf(stderr, - "--- Wrong number of roots found (%zu, should be %u) ---\n", - n, 4); - return EXIT_FAILURE; - } for (size_t i = 0; i < n; ++i) { double evmin = dmnsn_polynomial_evaluate(poly, 5, x[i] - dmnsn_epsilon); double ev = dmnsn_polynomial_evaluate(poly, 5, x[i]); double evmax = dmnsn_polynomial_evaluate(poly, 5, x[i] + dmnsn_epsilon); - if (fabs(evmin) < fabs(ev) || fabs(evmax) < fabs(ev)) { - fprintf(stderr, "--- Root %.15g is inaccurate! ---\n", x[i]); - return EXIT_FAILURE; - } + ck_assert(fabs(ev) < fabs(evmin) && fabs(ev) < fabs(evmax)); } - - return EXIT_SUCCESS; } +DMNSN_END_TEST -- cgit v1.2.3