diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2012-12-18 16:54:23 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2012-12-18 17:02:56 -0500 |
commit | 7da79cf1e24fbc58e0ff2e498a7f3d189a51cbd6 (patch) | |
tree | 702a05f5cfb2490462c1ce93a44591cfaec5d332 /libdimension/tests/polynomial.c | |
parent | 330ead5f47a7c4904cc6cecfa36b10d75718f1ff (diff) | |
download | dimension-7da79cf1e24fbc58e0ff2e498a7f3d189a51cbd6.tar.xz |
Improve the unit test macro API a bit.
Diffstat (limited to 'libdimension/tests/polynomial.c')
-rw-r--r-- | libdimension/tests/polynomial.c | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/libdimension/tests/polynomial.c b/libdimension/tests/polynomial.c index 8fd34df..208edf9 100644 --- a/libdimension/tests/polynomial.c +++ b/libdimension/tests/polynomial.c @@ -25,7 +25,7 @@ #include "tests.h" /* poly[] = 2*(x + 1)*(x - 1.2345)*(x - 2.3456)*(x - 5)*(x - 100) */ -const double poly[6] = { +static const double poly[6] = { [5] = 2.0, [4] = -215.1602, [3] = 1540.4520864, @@ -34,24 +34,35 @@ const double poly[6] = { [0] = 2895.6432, }; -DMNSN_TEST("polynomial", finds_positive_roots) +static double roots[5]; +static size_t nroots; + +#define DMNSN_CLOSE_ENOUGH 1.0e-6 + +DMNSN_TEST_SETUP(polynomial) { - double x[5]; - size_t n = dmnsn_polynomial_solve(poly, 5, x); - ck_assert_int_eq(n, 4); + nroots = dmnsn_polynomial_solve(poly, 5, roots); } -DMNSN_END_TEST -DMNSN_TEST("polynomial", accurate_roots) +DMNSN_TEST(polynomial, finds_positive_roots) { - double x[5]; - size_t n = dmnsn_polynomial_solve(poly, 5, x); + ck_assert_int_eq(nroots, 4); +} - 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); +DMNSN_TEST(polynomial, local_min_roots) +{ + for (size_t i = 0; i < nroots; ++i) { + double evmin = dmnsn_polynomial_evaluate(poly, 5, roots[i] - dmnsn_epsilon); + double ev = dmnsn_polynomial_evaluate(poly, 5, roots[i]); + double evmax = dmnsn_polynomial_evaluate(poly, 5, roots[i] + dmnsn_epsilon); ck_assert(fabs(ev) < fabs(evmin) && fabs(ev) < fabs(evmax)); } } -DMNSN_END_TEST + +DMNSN_TEST(polynomial, accurate_roots) +{ + for (size_t i = 0; i < nroots; ++i) { + double ev = dmnsn_polynomial_evaluate(poly, 5, roots[i]); + ck_assert(fabs(ev) < DMNSN_CLOSE_ENOUGH); + } +} |