[goffice] Tests: add a quad test.
- From: Morten Welinder <mortenw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [goffice] Tests: add a quad test.
- Date: Tue, 5 Nov 2013 02:56:10 +0000 (UTC)
commit 319f81286868ce39d175cefcad060f53fc1eafee
Author: Morten Welinder <terra gnome org>
Date: Mon Nov 4 21:54:44 2013 -0500
Tests: add a quad test.
This is a beginning of a GOQuad test. So far, this mostly makes
sure the GOQuad routines compute something in the same ball park
as the double routines.
tests/Makefile.am | 7 ++-
tests/test-quad.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 145 insertions(+), 1 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b5adf7f..cfee370 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,11 +1,13 @@
if WITH_GTK
-check_PROGRAMS = pie-demo go-demo shapes-demo mf-demo
+check_PROGRAMS = pie-demo go-demo shapes-demo mf-demo test-quad
endif
include $(top_srcdir)/goffice.mk
AM_CFLAGS = $(GOFFICE_CFLAGS)
+TESTS = test-quad
+
pie_demo_LDADD = $(GOFFICE_PLUGIN_LIBADD)
pie_demo_SOURCES = pie-demo.c
@@ -18,5 +20,8 @@ shapes_demo_SOURCES = shapes-demo.c
mf_demo_LDADD = $(GOFFICE_PLUGIN_LIBADD)
mf_demo_SOURCES = mf-demo.c
+test_quad_LDADD = $(GOFFICE_PLUGIN_LIBADD)
+test_quad_SOURCES = test-quad.c
+
EXTRA_DIST = go-demo.ui
diff --git a/tests/test-quad.c b/tests/test-quad.c
new file mode 100644
index 0000000..886d5ff
--- /dev/null
+++ b/tests/test-quad.c
@@ -0,0 +1,139 @@
+#include <goffice/goffice.h>
+
+
+#define BINTEST1(a_,b_,QOP,OP,txt) \
+do { \
+ double a, b, p; \
+ GOQuad qa, qb, qc; \
+ a = (a_); \
+ b = (b_); \
+ go_quad_init (&qa, a); \
+ go_quad_init (&qb, b); \
+ QOP (&qc, &qa, &qb); \
+ p = OP (a, b); \
+ g_printerr ("%s(%g,%g) = %g [%g]\n", \
+ txt, a, b, go_quad_value (&qc), p); \
+ if (p == floor (p)) \
+ g_assert (go_quad_value (&qc) == p); \
+ else \
+ g_assert (fabs (go_quad_value (&qc) - p) / p < 1e-14); \
+} while (0)
+
+/* ------------------------------------------------------------------------- */
+
+static void
+pow_tests (void)
+{
+#define QUAD_POW(r_,a_,b_) go_quad_pow ((r_),NULL,(a_),(b_))
+#define TEST1(a_,b_) BINTEST1(a_,b_,QUAD_POW,pow,"pow")
+ TEST1 (+2.3, +1.2);
+ TEST1 (+2.3, -1.2);
+ TEST1 (+2.3, +0.2);
+ TEST1 (+2.3, -0.2);
+ TEST1 (+0.2, +2.3);
+ TEST1 (+0.2, -2.3);
+ TEST1 (+2.3, +3);
+ TEST1 (+2.3, -3);
+ TEST1 (-2.3, +3);
+ TEST1 (-2.3, -3);
+ TEST1 (+2.3, +4);
+ TEST1 (+2.3, -4);
+ TEST1 (-2.3, +4);
+ TEST1 (-2.3, -4);
+ TEST1 (2, 100);
+ TEST1 (2, -100);
+ TEST1 (1.0/256, 1.0/1024);
+ TEST1 (exp(1), 1);
+ TEST1 (exp(1), -1);
+ TEST1 (exp(1), log(2));
+ TEST1 (exp(1), -log(2));
+
+ {
+ GOQuad a, p;
+ double x = 1e6;
+ double e, e0 = 1442695;
+ double m, m0 = 1.028747527600573533664645566932422897;
+
+ /* Test that exp(1e6) ~ m0 * 2^e0. */
+
+ go_quad_init (&a, 1e6);
+ go_quad_exp (&p, &e, &a);
+ m = ldexp (go_quad_value (&p), e - e0);
+ g_printerr ("exp(%g) = %.16g*2^%.0f [%.16g*2^%.0f]\n",
+ x, go_quad_value (&p), e, m0, e0);
+ g_assert (e > e0 - 5 && e < e0 + 5);
+ g_assert (fabs (m - m0) / m0 < 1e-14);
+ }
+}
+#undef TEST1
+
+/* ------------------------------------------------------------------------- */
+
+#define TEST1(a_,b_) \
+do { \
+ BINTEST1(a_,b_,go_quad_add,ADD,"add"); \
+ BINTEST1(a_,b_,go_quad_sub,SUB,"sub"); \
+ BINTEST1(a_,b_,go_quad_mul,MUL,"mul"); \
+ BINTEST1(a_,b_,go_quad_div,DIV,"div"); \
+} while (0)
+#define ADD(a_,b_) ((a_)+(b_))
+#define SUB(a_,b_) ((a_)-(b_))
+#define MUL(a_,b_) ((a_)*(b_))
+#define DIV(a_,b_) ((a_)/(b_))
+
+static void
+basic4_tests (void)
+{
+ TEST1 (1, 2);
+ TEST1 (1, -2);
+ TEST1 (1.0/3, 12345.0);
+ TEST1 (-1e10, 0.1);
+ TEST1 (0.1, 1e100);
+}
+#undef TEST1
+#undef ADD
+#undef SUB
+#undef MUL
+#undef DIV
+
+/* ------------------------------------------------------------------------- */
+
+int
+main (int argc, char **argv)
+{
+ void *state;
+ GOQuad a, b, c;
+
+ state = go_quad_start ();
+
+ go_quad_init (&a, 42.125);
+ g_assert (go_quad_value (&a) == 42.125);
+
+ go_quad_init (&b, ldexp (1.0, -80));
+ g_assert (go_quad_value (&b) == ldexp (1.0, -80));
+
+ go_quad_add (&c, &a, &b);
+ g_assert (go_quad_value (&c) >= 42.125);
+ g_assert (go_quad_value (&c) < 42.125 + ldexp (1.0, -40));
+
+ go_quad_sub (&c, &c, &a);
+ g_assert (go_quad_value (&c) == ldexp (1.0, -80));
+
+ g_assert (fabs (go_quad_value (&go_quad_pi) - M_PI) < 1e-14);
+ g_assert (fabs (go_quad_value (&go_quad_e) - exp(1)) < 1e-14);
+ g_assert (fabs (go_quad_value (&go_quad_ln2) - log(2)) < 1e-14);
+ g_assert (go_quad_value (&go_quad_zero) == 0);
+ g_assert (go_quad_value (&go_quad_one) == 1);
+
+ go_quad_mul (&a, &go_quad_sqrt2, &go_quad_sqrt2);
+ go_quad_sub (&a, &a, &go_quad_one);
+ go_quad_sub (&a, &a, &go_quad_one);
+ g_assert (go_quad_value (&a) < ldexp (1.0, -100));
+
+ basic4_tests ();
+ pow_tests ();
+
+ go_quad_end (state);
+
+ return 0;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]