summaryrefslogtreecommitdiff
path: root/sys/src/libmp
diff options
context:
space:
mode:
authoraiju <devnull@localhost>2018-12-06 09:32:07 +0000
committeraiju <devnull@localhost>2018-12-06 09:32:07 +0000
commit17b80cbcf1d5a7c6afd74c7432ab64c100dbdc2f (patch)
tree9c612682aa4438cdfa5106657e7c779323dd60bc /sys/src/libmp
parent7e477cc7693dde922ce94df8cbec3301f920fb6c (diff)
libmp: add tests for integer conversions
Diffstat (limited to 'sys/src/libmp')
-rw-r--r--sys/src/libmp/test/convtest.c124
-rw-r--r--sys/src/libmp/test/fns.h1
-rw-r--r--sys/src/libmp/test/main.c1
-rw-r--r--sys/src/libmp/test/mkfile1
4 files changed, 127 insertions, 0 deletions
diff --git a/sys/src/libmp/test/convtest.c b/sys/src/libmp/test/convtest.c
new file mode 100644
index 000000000..d3721dfa7
--- /dev/null
+++ b/sys/src/libmp/test/convtest.c
@@ -0,0 +1,124 @@
+#include <u.h>
+#include <libc.h>
+#include <mp.h>
+
+/* these tests suck but better than nothing... but really should test more values than just 1<<i */
+
+#define MPTOX(_name,_type,_func) \
+void \
+_name(void) \
+{ \
+ mpint *m; \
+ int i, sign, mag; \
+ _type v, e; \
+ int fail; \
+ \
+ fail = 0; \
+ m = mpnew(0); \
+ for(i = 0; i < 256; i++){ \
+ sign = i >= 128 ? -1 : 1; \
+ mag = i % 128; \
+ itomp(sign, m); \
+ mpleft(m, mag, m); \
+ v = _func(m); \
+ e = 0xcafebabe; USED(e);
+#define MPTOX_END(_func,_format) \
+ if(v != e){ \
+ fprint(2, "FAIL: _func(%#B): return value: got "_format", expected "_format"\n", m, v, e); \
+ fail=1; \
+ } \
+ } \
+ mpfree(m); \
+ if(!fail) \
+ fprint(2, "_func: passed\n"); \
+}
+
+#define XTOMP(_name,_type,_func) \
+void \
+_name(void) \
+{ \
+ mpint *m, *r; \
+ int i, sign, mag; \
+ _type v; \
+ int fail; \
+ \
+ fail = 0; \
+ m = mpnew(0); \
+ r = mpnew(0); \
+ for(i = 0; i < 256; i++){ \
+ sign = i >= 128 ? -1 : 1; \
+ mag = i % 128; \
+ itomp(sign, r); \
+ mpleft(r, mag, r);
+#define XTOMP_END(_func,_type,_format) \
+ _func(sign * ((_type)1<<mag), m); \
+ if(mpcmp(r, m) != 0){ \
+ fprint(2, "FAIL: _func("_format"): return value: got %#B, expected %#B\n", sign * ((_type)1<<mag), m, r); \
+ fail=1; \
+ } \
+ } \
+ mpfree(m); \
+ mpfree(r); \
+ if(!fail) \
+ fprint(2, "_func: passed\n"); \
+}
+
+MPTOX(test_mptoi, int, mptoi)
+ if(mag < 31)
+ e = sign*(1<<mag);
+ else
+ e = sign > 0 ? (1<<31)-1 : 1<<31;
+MPTOX_END(mptoi, "%#x")
+
+MPTOX(test_mptoui, uint, mptoui)
+ if(mag < 32 && sign > 0)
+ e = 1<<mag;
+ else
+ e = sign > 0 ? -1 : 0;
+MPTOX_END(mptoui, "%#ux")
+
+
+MPTOX(test_mptov, vlong, mptov)
+ if(mag < 63)
+ e = sign*(1LL<<mag);
+ else
+ e = sign > 0 ? (1LL<<63)-1 : 1LL<<63;
+MPTOX_END(mptov, "%#llx")
+
+MPTOX(test_mptouv, uvlong, mptouv)
+ if(mag < 64 && sign > 0)
+ e = 1LL<<mag;
+ else
+ e = sign > 0 ? -1ULL : 0;
+MPTOX_END(mptouv, "%#llux")
+
+XTOMP(test_itomp, int, itomp)
+ if(mag >= 31) continue;
+XTOMP_END(vtomp, vlong, "%lld")
+
+XTOMP(test_uitomp, uint, uitomp)
+ if(mag >= 32 || sign < 0) continue;
+XTOMP_END(uitomp, uint, "%lld")
+
+XTOMP(test_vtomp, vlong, vtomp)
+ if(mag >= 63) continue;
+XTOMP_END(vtomp, vlong, "%lld")
+
+XTOMP(test_uvtomp, uvlong, uvtomp)
+ if(mag >= 64 || sign < 0) continue;
+XTOMP_END(uvtomp, vlong, "%lld")
+
+
+void
+convtests(void)
+{
+ test_mptoi();
+ test_mptoui();
+ test_mptov();
+ test_mptouv();
+ test_itomp();
+ test_uitomp();
+ test_vtomp();
+ test_uvtomp();
+}
+
diff --git a/sys/src/libmp/test/fns.h b/sys/src/libmp/test/fns.h
index c70d1c27e..30f12ecf5 100644
--- a/sys/src/libmp/test/fns.h
+++ b/sys/src/libmp/test/fns.h
@@ -6,3 +6,4 @@ mpint* ldtomp(ldint *, mpint *);
void mptarget(mpint *);
void tests(void);
void mpdiv_(mpint *, mpint *, mpint *, mpint *);
+void convtests(void);
diff --git a/sys/src/libmp/test/main.c b/sys/src/libmp/test/main.c
index e9f0ba6f5..bf2ac6ec2 100644
--- a/sys/src/libmp/test/main.c
+++ b/sys/src/libmp/test/main.c
@@ -48,5 +48,6 @@ main()
fmtinstall(L'β', mpdetfmt);
fmtinstall('L', ldfmt);
+ convtests();
tests();
}
diff --git a/sys/src/libmp/test/mkfile b/sys/src/libmp/test/mkfile
index 87cb46f5b..bfbdd6948 100644
--- a/sys/src/libmp/test/mkfile
+++ b/sys/src/libmp/test/mkfile
@@ -5,6 +5,7 @@ OFILES=\
main.$O\
ld.$O\
gen.tab.$O\
+ convtest.$O\
HFILES=dat.h fns.h