changelog shortlog tags branches changeset file revisions annotate raw help

Mercurial > hg > plan9front / sys/src/libmp/test/convtest.c

revision 6940: 9155f29f0eab
     1.1new file mode 100644
     1.2--- /dev/null
     1.3+++ b/sys/src/libmp/test/convtest.c
     1.4@@ -0,0 +1,124 @@
     1.5+#include <u.h>
     1.6+#include <libc.h>
     1.7+#include <mp.h>
     1.8+
     1.9+/* these tests suck but better than nothing... but really should test more values than just 1<<i */
    1.10+
    1.11+#define MPTOX(_name,_type,_func)  \
    1.12+void \
    1.13+_name(void) \
    1.14+{ \
    1.15+	mpint *m; \
    1.16+	int i, sign, mag; \
    1.17+	_type v, e; \
    1.18+	int fail; \
    1.19+	 \
    1.20+	fail = 0; \
    1.21+	m = mpnew(0); \
    1.22+	for(i = 0; i < 256; i++){ \
    1.23+		sign = i >= 128 ? -1 : 1; \
    1.24+		mag = i % 128; \
    1.25+		itomp(sign, m); \
    1.26+		mpleft(m, mag, m); \
    1.27+		v = _func(m); \
    1.28+		e = 0xcafebabe; USED(e);
    1.29+#define MPTOX_END(_func,_format)  \
    1.30+		if(v != e){ \
    1.31+			fprint(2, "FAIL: _func(%#B): return value: got "_format", expected "_format"\n", m, v, e); \
    1.32+			fail=1; \
    1.33+		} \
    1.34+	} \
    1.35+	mpfree(m); \
    1.36+	if(!fail) \
    1.37+		fprint(2, "_func: passed\n"); \
    1.38+}
    1.39+
    1.40+#define XTOMP(_name,_type,_func)  \
    1.41+void \
    1.42+_name(void) \
    1.43+{ \
    1.44+	mpint *m, *r; \
    1.45+	int i, sign, mag; \
    1.46+	_type v; \
    1.47+	int fail; \
    1.48+	 \
    1.49+	fail = 0; \
    1.50+	m = mpnew(0); \
    1.51+	r = mpnew(0); \
    1.52+	for(i = 0; i < 256; i++){ \
    1.53+		sign = i >= 128 ? -1 : 1; \
    1.54+		mag = i % 128; \
    1.55+		itomp(sign, r); \
    1.56+		mpleft(r, mag, r);
    1.57+#define XTOMP_END(_func,_type,_format)  \
    1.58+		_func(sign * ((_type)1<<mag), m); \
    1.59+		if(mpcmp(r, m) != 0){ \
    1.60+			fprint(2, "FAIL: _func("_format"): return value: got %#B, expected %#B\n", sign * ((_type)1<<mag), m, r); \
    1.61+			fail=1; \
    1.62+		} \
    1.63+	} \
    1.64+	mpfree(m); \
    1.65+	mpfree(r); \
    1.66+	if(!fail) \
    1.67+		fprint(2, "_func: passed\n"); \
    1.68+}
    1.69+
    1.70+MPTOX(test_mptoi, int, mptoi)
    1.71+	if(mag < 31)
    1.72+		e = sign*(1<<mag);
    1.73+	else
    1.74+		e = sign > 0 ? (1<<31)-1 : 1<<31;
    1.75+MPTOX_END(mptoi, "%#x")
    1.76+
    1.77+MPTOX(test_mptoui, uint, mptoui)
    1.78+	if(mag < 32 && sign > 0)
    1.79+		e = 1<<mag;
    1.80+	else
    1.81+		e = sign > 0 ? -1 : 0;
    1.82+MPTOX_END(mptoui, "%#ux")
    1.83+
    1.84+
    1.85+MPTOX(test_mptov, vlong, mptov)
    1.86+	if(mag < 63)
    1.87+		e = sign*(1LL<<mag);
    1.88+	else
    1.89+		e = sign > 0 ? (1LL<<63)-1 : 1LL<<63;
    1.90+MPTOX_END(mptov, "%#llx")
    1.91+
    1.92+MPTOX(test_mptouv, uvlong, mptouv)
    1.93+	if(mag < 64 && sign > 0)
    1.94+		e = 1LL<<mag;
    1.95+	else
    1.96+		e = sign > 0 ? -1ULL : 0;
    1.97+MPTOX_END(mptouv, "%#llux")
    1.98+
    1.99+XTOMP(test_itomp, int, itomp)
   1.100+	if(mag >= 31) continue;
   1.101+XTOMP_END(vtomp, vlong, "%lld")
   1.102+
   1.103+XTOMP(test_uitomp, uint, uitomp)
   1.104+	if(mag >= 32 || sign < 0) continue;
   1.105+XTOMP_END(uitomp, uint, "%lld")
   1.106+
   1.107+XTOMP(test_vtomp, vlong, vtomp)
   1.108+	if(mag >= 63) continue;
   1.109+XTOMP_END(vtomp, vlong, "%lld")
   1.110+
   1.111+XTOMP(test_uvtomp, uvlong, uvtomp)
   1.112+	if(mag >= 64 || sign < 0) continue;
   1.113+XTOMP_END(uvtomp, vlong, "%lld")
   1.114+
   1.115+
   1.116+void
   1.117+convtests(void)
   1.118+{
   1.119+	test_mptoi();
   1.120+	test_mptoui();
   1.121+	test_mptov();
   1.122+	test_mptouv();
   1.123+	test_itomp();
   1.124+	test_uitomp();
   1.125+	test_vtomp();
   1.126+	test_uvtomp();
   1.127+}
   1.128+