summaryrefslogtreecommitdiffstats
path: root/libdimension/tests/polynomial.c
diff options
context:
space:
mode:
Diffstat (limited to 'libdimension/tests/polynomial.c')
-rw-r--r--libdimension/tests/polynomial.c57
1 files changed, 26 insertions, 31 deletions
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 <tavianator@tavianator.com> *
+ * Copyright (C) 2010-2012 Tavian Barnes <tavianator@tavianator.com> *
* *
* This file is part of The Dimension Test Suite. *
* *
@@ -17,46 +17,41 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
-/*
- * Basic test of numerical polynomial root-finder
+/**
+ * @file
+ * Basic tests of the polynomial root-finder.
*/
-#include "dimension.h"
-#include <stddef.h>
-#include <stdio.h>
+#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