summaryrefslogtreecommitdiff
path: root/test/stdbigint.myr
blob: 0adb98b0cfd7cfd0a836a513de567867a10c6326 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std

const main = {
	var a, b, c, d, e
	var buf : byte[4096], n

	a = std.mkbigint(1234)
	b = std.mkbigint(0x7fffffff)
	c = std.mkbigint(7919)
	d = std.mkbigint(113051)
	e = std.mkbigint(11)

	std.bigmul(a, b)
	std.bigmul(a, b)
	std.bigadd(a, c)
	std.bigsub(a, d)
	std.bigdiv(a, e)

	std.bigfree(b)
	std.bigfree(c)
	std.bigfree(d)
	std.bigfree(e)

	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")
	;;

	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])

	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")
	;;

	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])

	match std.bigparse("5192296858534810493479828944327220")
	| `std.Some val: a = val
	n = std.bigbfmt(buf[:], a, 0)
	| `std.None: std.die("Failed to parse a\n")
	;;
	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])
}