summaryrefslogtreecommitdiffstats
path: root/regress/usr.bin/bc
diff options
context:
space:
mode:
authorotto <otto@openbsd.org>2012-03-13 10:12:52 +0000
committerotto <otto@openbsd.org>2012-03-13 10:12:52 +0000
commite909c4f77048b2d61d283eab9108f19c351070b6 (patch)
treee2b43b8c75804f0f387751be82e015d9896e0fb8 /regress/usr.bin/bc
parentTeach pgrep/pkill to only match processes, not threads. ok guenther@ (diff)
downloadwireguard-openbsd-e909c4f77048b2d61d283eab9108f19c351070b6.tar.xz
wireguard-openbsd-e909c4f77048b2d61d283eab9108f19c351070b6.zip
rough test of the bc(1) math lib
Diffstat (limited to 'regress/usr.bin/bc')
-rw-r--r--regress/usr.bin/bc/Makefile12
-rw-r--r--regress/usr.bin/bc/t19.c75
2 files changed, 84 insertions, 3 deletions
diff --git a/regress/usr.bin/bc/Makefile b/regress/usr.bin/bc/Makefile
index ccc215b78b8..4476d11ba4e 100644
--- a/regress/usr.bin/bc/Makefile
+++ b/regress/usr.bin/bc/Makefile
@@ -1,8 +1,11 @@
-# $OpenBSD: Makefile,v 1.10 2007/10/05 16:58:21 otto Exp $
+# $OpenBSD: Makefile,v 1.11 2012/03/13 10:12:52 otto Exp $
BC=bc
-REGRESS_TARGETS=t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18
+LDFLAGS=-lm
+
+REGRESS_TARGETS=t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18\
+ t19test
# .in: input file
# .out: desired result
@@ -36,8 +39,11 @@ all: clean
@cmp -s ${.CURDIR}/${*}.out ${*}.log || \
(echo "XXX ${*} failed" && false)
+t19test: t19
+ ./t19
+
# Clean all files generated
clean:
- rm -f *.log
+ rm -f *.log t19
.include <bsd.regress.mk>
diff --git a/regress/usr.bin/bc/t19.c b/regress/usr.bin/bc/t19.c
new file mode 100644
index 00000000000..28b0cf8710f
--- /dev/null
+++ b/regress/usr.bin/bc/t19.c
@@ -0,0 +1,75 @@
+/* $OpenBSD: t19.c,v 1.1 2012/03/13 10:12:52 otto Exp $ */
+
+/*
+ * Copyright (c) 2012 Otto Moerbeek <otto@drijf.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+#include <float.h>
+#include <math.h>
+#include <stdio.h>
+
+int scale[] = { 1, 2, 3, 10, 15, 20, 30};
+long double num[] = {-10, -M_PI, -3, -2, -1, -0.5, -0.01, 0, 0.01,
+ 0.5, 1, 2, 3, M_PI, 10};
+
+struct func {const char *name; long double (*f)(long double);};
+
+struct func funcs[] = { {"s", sinl},
+ {"c", cosl},
+ {"e", expl},
+ {"l", logl},
+ {"a", atanl}
+};
+
+#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
+
+main()
+{
+ int ret, si, ni, fi;
+ int status = 0;
+
+ for (si = 0; si < nitems(scale); si++) {
+ for (ni = 0; ni < nitems(num); ni++) {
+ for (fi = 0; fi < nitems(funcs); fi++) {
+ char cmd[100];
+ FILE *fp;
+ long double v, d1, d2, diff, prec;
+
+ v = num[ni];
+ if (v == 0.0 && funcs[fi].f == logl)
+ continue;
+ snprintf(cmd, sizeof(cmd),
+ "bc -l -e scale=%d -e '%s(%.19Lf)' -equit",
+ scale[si], funcs[fi].name, v);
+ fp = popen(cmd, "r");
+ ret = fscanf(fp, "%Lf", &d1);
+ pclose(fp);
+ d2 = funcs[fi].f(v);
+ diff = fabsl(d1 - d2);
+ prec = pow(10, -scale[si]);
+ if (prec < LDBL_EPSILON)
+ prec = LDBL_EPSILON;
+ /* XXX cheating ? */
+ if (diff > 20*prec) {
+ printf("%s %d %Le %Le %Le %Le %Le\n",
+ funcs[fi].name, scale[si],
+ v, d1, d2, diff, prec);
+ status = 1;
+ }
+
+ }
+ }
+ }
+ return status;
+}