summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2012-12-18 12:44:23 -0500
committerTavian Barnes <tavianator@tavianator.com>2012-12-18 12:45:06 -0500
commit330ead5f47a7c4904cc6cecfa36b10d75718f1ff (patch)
tree66e98fcc24633141b178557d0565996bda4915e8 /libdimension
parent5e45a00b8f67281b2977c41625ed57a4caa7d341 (diff)
downloaddimension-330ead5f47a7c4904cc6cecfa36b10d75718f1ff.tar.xz
Convert the polynomial tests to the new testing framework.
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/tests/Makefile.am11
-rw-r--r--libdimension/tests/polynomial.c57
-rw-r--r--libdimension/tests/tests.h44
-rw-r--r--libdimension/tests/unit-test.c87
4 files changed, 164 insertions, 35 deletions
diff --git a/libdimension/tests/Makefile.am b/libdimension/tests/Makefile.am
index 072a111..46fc6a2 100644
--- a/libdimension/tests/Makefile.am
+++ b/libdimension/tests/Makefile.am
@@ -1,5 +1,5 @@
###########################################################################
-## Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> ##
+## Copyright (C) 2009-2012 Tavian Barnes <tavianator@tavianator.com> ##
## ##
## This file is part of The Dimension Build Suite. ##
## ##
@@ -20,7 +20,8 @@
AM_CFLAGS = -I$(top_srcdir)/libdimension
AM_CXXFLAGS = $(AM_CFLAGS)
-check_LTLIBRARIES = libdimension-tests.la
+check_LTLIBRARIES = libdimension-tests.la \
+ libdimension-unit-test.la
check_PROGRAMS = warning.test \
warning-as-error.test \
error.test \
@@ -48,6 +49,10 @@ libdimension_tests_la_SOURCES = test_canvas.c \
tests.h
libdimension_tests_la_LIBADD = $(top_builddir)/libdimension/libdimension.la
+libdimension_unit_test_la_SOURCES = unit-test.c
+libdimension_unit_test_la_CFLAGS = $(CHECK_CFLAGS) $(AM_CFLAGS)
+libdimension_unit_test_la_LIBADD = $(CHECK_LIBS) libdimension-tests.la
+
if GLX
libdimension_tests_la_SOURCES += display.c
libdimension_tests_la_LIBADD += -lX11 -lGL
@@ -68,7 +73,7 @@ custom_error_fn_test_SOURCES = custom-error-fn.c
custom_error_fn_test_LDADD = libdimension-tests.la
polynomial_test_SOURCES = polynomial.c
-polynomial_test_LDADD = libdimension-tests.la
+polynomial_test_LDADD = libdimension-unit-test.la
prtree_test_SOURCES = prtree.c
prtree_test_LDADD = libdimension-tests.la
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
diff --git a/libdimension/tests/tests.h b/libdimension/tests/tests.h
index 38d9107..3009164 100644
--- a/libdimension/tests/tests.h
+++ b/libdimension/tests/tests.h
@@ -1,5 +1,5 @@
/*************************************************************************
- * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> *
+ * Copyright (C) 2009-2012 Tavian Barnes <tavianator@tavianator.com> *
* *
* This file is part of The Dimension Test Suite. *
* *
@@ -21,12 +21,54 @@
#define TESTS_H
#include "dimension.h"
+#include <check.h>
#ifdef __cplusplus
/* We've been included from a C++ file; mark everything here as extern "C" */
extern "C" {
#endif
+/** @internal Map to known test cases from their names. */
+extern dmnsn_dictionary* dmnsn_test_cases;
+
+/** @internal Default test fixture. */
+void dmnsn_test_setup(void);
+/** @internal Default test fixture. */
+void dmnsn_test_teardown(void);
+
+/**
+ * Mark the beginning of a test.
+ * @param[in] tcase The name of the test case for this test.
+ * @param[in] test The name of the test itself.
+ */
+#define DMNSN_TEST(tcase, test) \
+ static void dmnsn_test_##test(int _i); \
+ \
+ __attribute__((constructor)) \
+ static void dmnsn_add_test_##test(void) \
+ { \
+ if (dmnsn_test_cases == NULL) { \
+ dmnsn_test_cases = dmnsn_new_dictionary(sizeof(TCase *)); \
+ } \
+ \
+ TCase *tc; \
+ TCase **tcp = dmnsn_dictionary_at(dmnsn_test_cases, tcase); \
+ if (tcp == NULL) { \
+ tc = tcase_create(tcase); \
+ tcase_add_checked_fixture(tc, dmnsn_test_setup, dmnsn_test_teardown); \
+ dmnsn_dictionary_insert(dmnsn_test_cases, tcase, &tc); \
+ } else { \
+ tc = *tcp; \
+ } \
+ \
+ tcase_add_test(tc, dmnsn_test_##test); \
+ } \
+ \
+ START_TEST(dmnsn_test_##test)
+
+/** Mark the end of a test. */
+#define DMNSN_END_TEST END_TEST
+
/*
* Test canvas
*/
diff --git a/libdimension/tests/unit-test.c b/libdimension/tests/unit-test.c
new file mode 100644
index 0000000..a15c960
--- /dev/null
+++ b/libdimension/tests/unit-test.c
@@ -0,0 +1,87 @@
+/*************************************************************************
+ * Copyright (C) 2010-2011 Tavian Barnes <tavianator@tavianator.com> *
+ * *
+ * This file is part of The Dimension Test Suite. *
+ * *
+ * The Dimension Test Suite is free software; you can redistribute it *
+ * and/or modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; either version 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Test Suite is distributed in the hope that it will be *
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+/**
+ * @file
+ * Shared entry point for tests based on the Check unit-testing framework.
+ */
+
+#include "tests.h"
+
+dmnsn_dictionary *dmnsn_test_cases = NULL;
+
+void
+dmnsn_test_setup(void)
+{
+ /* Treat warnings as errors for tests */
+ dmnsn_die_on_warnings(true);
+}
+
+void
+dmnsn_test_teardown(void)
+{
+ dmnsn_delete_dictionary(dmnsn_test_cases);
+}
+
+static Suite *dmnsn_suite;
+
+void
+dmnsn_add_test_cases(void *ptr)
+{
+ TCase **tcp = ptr;
+ suite_add_tcase(dmnsn_suite, *tcp);
+}
+
+Suite *
+dmnsn_test_suite()
+{
+ dmnsn_suite = suite_create("Dimension");
+
+ if (dmnsn_test_cases != NULL) {
+ dmnsn_dictionary_apply(dmnsn_test_cases, dmnsn_add_test_cases);
+ }
+
+ return dmnsn_suite;
+}
+
+int
+main()
+{
+ /* Treat warnings as errors for tests */
+ dmnsn_die_on_warnings(true);
+
+ /* Create the test suite */
+ Suite *suite = dmnsn_test_suite();
+ SRunner *sr = srunner_create(suite);
+
+ /* Run the tests */
+ srunner_run_all(sr, CK_VERBOSE);
+ int nfailed = srunner_ntests_failed(sr);
+
+ /* Clean up */
+ srunner_free(sr);
+ dmnsn_delete_dictionary(dmnsn_test_cases);
+
+ /* Return the right result code */
+ if (nfailed == 0) {
+ return EXIT_SUCCESS;
+ } else {
+ return EXIT_FAILURE;
+ }
+}