summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-02-04 22:24:02 -0800
committerOri Bernstein <ori@eigenstate.org>2015-02-04 22:24:02 -0800
commit94eff65320912b8192b353d74413db461c19f16a (patch)
tree033c9a501bd73230923f65047db813b786821049 /test
parent063b2a4e134589a8a1d635708eb0eb39d5a76bfb (diff)
downloadmc-94eff65320912b8192b353d74413db461c19f16a.tar.gz
Clean up bigint division and tests.
Diffstat (limited to 'test')
-rw-r--r--test/stdbigint.myr120
1 files changed, 75 insertions, 45 deletions
diff --git a/test/stdbigint.myr b/test/stdbigint.myr
index 0adb98b..e152c92 100644
--- a/test/stdbigint.myr
+++ b/test/stdbigint.myr
@@ -1,5 +1,18 @@
use std
+type cmd = union
+ `Add (cmd#, cmd#)
+ `Sub (cmd#, cmd#)
+ `Mul (cmd#, cmd#)
+ `Div (cmd#, cmd#)
+ `Mod (cmd#, cmd#)
+ `Shl (cmd#, cmd#)
+ `Shr (cmd#, cmd#)
+ `Modpow (cmd#, cmd#, cmd#)
+ `Val byte[:]
+ `Res cmd#
+;;
+
const main = {
var a, b, c, d, e
var buf : byte[4096], n
@@ -24,63 +37,80 @@ const main = {
n = std.bigbfmt(buf[:], a, 0)
std.put("%s\n", buf[:n])
- /* smoke test */
- match std.bigparse("1234_5678_1234_6789_6666_7777_8888")
- | `std.Some val: a = val
- | `std.None: std.die("Failed to parse a\n")
- ;;
- match std.bigparse("1234_5678_1234_6789_6666_7777")
- | `std.Some val: b = val
- | `std.None: std.die("Failed to parse b\n")
- ;;
+ /* smoke test for division */
+ run(std.mk(`Res \
+ std.mk(`Div (\
+ std.mk(`Val "1234_5678_1234_6789_6666_7777_8888"), \
+ std.mk(`Val "1234_5678_1234_6789_6666_7777")))))
+ run(std.mk(`Res \
+ std.mk(`Div (\
+ std.mk(`Val "0xffff_1234_1234_1234_1234"), \
+ std.mk(`Val "0xf010_1234_2314")))))
+ run(std.mk(`Res \
+ std.mk(`Div (\
+ std.mk(`Val "5192296858534810493479828944327220"), \
+ std.mk(`Val "75557863709417659441940")))))
+
+ /*
+ a = try(std.bigparse("2988348162058574136915891421498819466320163312926952423791023078876139"))
+ b = try(std.bigparse("2351399303373464486466122544523690094744975233415544072992656881240319"))
+ c = try(std.bigparse("10000000000000000000000000000000000000000"))
n = std.bigbfmt(buf[:], a, 0)
- std.put("%s / ", buf[:n])
+ std.put("%s ^ ", buf[:n])
n = std.bigbfmt(buf[:], b, 0)
+ std.put("%s %% ", buf[:n])
+ n = std.bigbfmt(buf[:], c, 0)
std.put("%s == ", buf[:n])
- std.bigdiv(a, b)
+ std.bigmodpow(a, b, c)
n = std.bigbfmt(buf[:], a, 0)
std.put("%s\n", buf[:n])
+ */
+}
- match std.bigparse("0xffff_1234_1234_1234_1234")
- | `std.Some val: a = val
- n = std.bigbfmt(buf[:], a, 0)
- | `std.None: std.die("Failed to parse a\n")
- ;;
- match std.bigparse("0xf010_1234_2314")
- | `std.Some val: b = val
- | `std.None: std.die("Failed to parse b\n")
+const run = {e : cmd#
+ var buf : byte[4096]
+ var v, n
+
+ match e#
+ | `Add (x, y): -> binop("+", std.bigadd, x, y)
+ | `Sub (x, y): -> binop("-", std.bigsub, x, y)
+ | `Mul (x, y): -> binop("*", std.bigmul, x, y)
+ | `Div (x, y): -> binop("/", std.bigdiv, x, y)
+ | `Mod (x, y): -> binop("%", std.bigmod, x, y)
+ | `Shl (x, y): -> binop("<<", std.bigshl, x, y)
+ | `Shr (x, y): -> binop(">>", std.bigshr, x, y)
+ | `Val x:
+ v = try(std.bigparse(x))
+ n = std.bigbfmt(buf[:], v, 0)
+ std.put("%s", buf[:n])
+ -> v
+ | `Res r:
+ v = run(r)
+ n = std.bigbfmt(buf[:], v, 0)
+ std.put(" == %s\n", buf[:n])
+ | `Modpow (x, y, m):
+ n = std.bigbfmt(buf[:], std.bigmodpow(run(x), run(y), run(m)), 0)
+ std.put("(%s ^ %s) % %s = %s\n", x, y, m, buf[:n])
;;
+}
- n = std.bigbfmt(buf[:], a, 0)
- std.put("%s / ", buf[:n])
- n = std.bigbfmt(buf[:], b, 0)
- std.put("%s == ", buf[:n])
-
- std.bigdiv(a, b)
+const binop = {name, op, x, y
+ var a, b
- n = std.bigbfmt(buf[:], a, 0)
- std.put("%s\n", buf[:n])
+ a = run(x)
+ std.put(" %s ", name)
+ b = run(y)
+ op(a, b)
+ std.bigfree(b)
+ -> a
+}
- match std.bigparse("5192296858534810493479828944327220")
- | `std.Some val: a = val
- n = std.bigbfmt(buf[:], a, 0)
- | `std.None: std.die("Failed to parse a\n")
+generic try = {x : std.option(@a)
+ match x
+ | `std.Some v: -> v
+ | `std.None: std.die("failed to get val")
;;
- match std.bigparse("75557863709417659441940")
- | `std.Some val: b = val
- | `std.None: std.die("Failed to parse b\n")
- ;;
-
- n = std.bigbfmt(buf[:], a, 0)
- std.put("%s / ", buf[:n])
- n = std.bigbfmt(buf[:], b, 0)
- std.put("%s == ", buf[:n])
-
- std.bigdiv(a, b)
-
- n = std.bigbfmt(buf[:], a, 0)
- std.put("%s\n", buf[:n])
}