summaryrefslogtreecommitdiff
path: root/lib/std/test/bigint.myr
blob: c87f1f3935afbe024871e79f9f87374d26e5df4d (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use std
use testr

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[:]
;;

const main = {
	testr.run([
		[.name = "smoke-test", .fn = smoketest],
		[.name = "matches-small", .fn = matchsmall],
		[.name = "comparisons", .fn = comparisons],
		[.name = "format-zero", .fn = fmtzero],
		[.name = "division", .fn = smokediv],
		[.name = "modulo", .fn = smokemod],
		[.name = "add-negatives", .fn = addneg],
		[.name = "sub-negatives", .fn = subneg],
	][:])
}

const smoketest = {ct
	var a, b, c, d, e, f
	var buf : byte[64], n

	/* a few combined ops */
	a = std.mkbigint(1234)
	b = std.mkbigint(0x7fffffff)
	c = std.mkbigint(7919)
	d = std.mkbigint(113051)
	e = std.mkbigint(11)
	f = std.mkbigint((4294967296 : int64))

	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)
	testr.check(ct, std.eq(buf[:n], "517347321949036993306"), "simple smoke test failed")

	n = std.bigbfmt(buf[:], f, 0)
	testr.check(ct, std.eq(buf[:n], "4294967296"), "smoke test failed for 2^32 case")
}

const matchsmall = {c
	var nums = [ -5, -3, -2, -1, 0, 1, 2, 4, 8, 10 ][:]
	for a : nums
		for b : nums
			var p = std.mkbigint(a)
			var q = std.mkbigint(b)

			var radd = std.mkbigint(a + b)
			var sadd = std.bigdup(p)
			std.bigadd(sadd, q)
			testr.check(c, std.bigeq(radd, sadd), "{} + {} != {} (was {})", a, b, a + b, sadd)

			var rsub = std.mkbigint(a - b)
			var ssub = std.bigdup(p)
			std.bigsub(ssub, q)
			testr.check(c, std.bigeq(rsub, ssub), "{} - {} != {} (was {})", a, b, a - b, ssub)

			var rmul = std.mkbigint(a * b)
			var smul = std.bigdup(p)
			std.bigmul(smul, q)
			testr.check(c, std.bigeq(rmul, smul), "{} * {} != {} (was {})", a, b, a * b, smul)


			if b != 0
				if a > 0 && b > 0
					var rmod = std.mkbigint(a % b)
					var smod = std.bigdup(p)
					std.bigmod(smod, q)
					testr.check(c, std.bigeq(rmod, smod), "{} % {} != {} (was {})", a, b, a % b, smod)
					std.bigfree(rmod)
				;;

				var rdiv = std.mkbigint(a / b)
				var sdiv = std.bigdup(p)
				std.bigdiv(sdiv, q)
				testr.check(c, std.bigeq(rdiv, sdiv), "{} / {} != {} (was {})", a, b, a / b, sdiv)

				std.bigfree(rdiv)
			;;

			std.bigfree(p)
			std.bigfree(q)
			std.bigfree(radd)
			std.bigfree(rsub)
			std.bigfree(rmul)
		;;
	;;
}

const comparisons = {c
	/* some comparison tests */
	var a = try(std.bigparse("1234_5678_1234_6789_6666_7777_8888"))
	var b = try(std.bigparse("2234_5678_1234_6789_6666_7777_8888"))
	testr.check(c, std.bigcmp(a, b) == `std.Before, "{} should be < {}", a, b)
	std.bigfree(a)
	std.bigfree(b)

	a = try(std.bigparse("36028797018963964"))
	b = try(std.bigparse("36028797018963958"))
	testr.check(c, std.bigcmp(a, b) == `std.After, "{} should be > {}", a, b)
	std.bigfree(a)
	std.bigfree(b)
}

const fmtzero = {c
	/* make sure we format '0' correctly */
	run(c, std.mk(`Val "0"), "0")
}

const smokediv = {c
	/* smoke test for division */
	run(c, std.mk(`Div (\
		std.mk(`Val "1234_5678_1234_6789_6666_7777_8888"), \
		std.mk(`Val "1234_5678_1234_6789_6666_7777"))), \
		"10000")
	run(c, std.mk(`Div (\
		std.mk(`Val "0xffff_1234_1234_1234_1234"), \
		std.mk(`Val "0xf010_1234_2314"))), \
		"4580035496")
	run(c, std.mk(`Div (\
		std.mk(`Val "5192296858534810493479828944327220"), \
		std.mk(`Val "75557863709417659441940"))), \
		"68719476751")
	run(c, std.mk(`Div (\
		std.mk(`Val "75557863709417659441940"), \
		std.mk(`Val "5192296858534810493479828944327220"))), \
		"0")
}

const smokemod = {c
	/* smoke test for mod */
	run(c, std.mk(`Mod (\
		std.mk(`Val "5192296858534810493479828944327220"), \
		std.mk(`Val "75557863709417659441940"))),\
		"257025710597479990280")

	run(c, std.mk(`Modpow (\
		std.mk(`Val "1"), \
		std.mk(`Val "3"), \
		std.mk(`Val "2"))), \
		"1")

	run(c, std.mk(`Modpow (\
		std.mk(`Val "5192296858534810493479828944327220"), \
		std.mk(`Val "75557863709417659441940"), \
		std.mk(`Val "755578"))), \
		"49054")
	run(c, std.mk(`Modpow (\
		std.mk(`Val "2393"), \
		std.mk(`Val "2"), \
		std.mk(`Val "6737"))), \
		"6736")
	run(c, std.mk(`Modpow (\
		std.mk(`Val "6193257528475266832463188301662235"), \
		std.mk(`Val "6157075615645799356061575607567581"), \
		std.mk(`Val "12314151231291598712123151215135163"))), \
		"1540381241336817586803754632242117")
	run(c, std.mk(`Modpow (\
		std.mk(`Val "7220"), \
		std.mk(`Val "755578"), \
		std.mk(`Val "75557863709417659441940"))), \
		"27076504425474791131220")
}

const addneg = {c
	run(c, std.mk(`Add (\
		std.mk(`Val "-1"), \
		std.mk(`Val "1"))), \
		"0")

	run(c, std.mk(`Add (\
		std.mk(`Val "4"), \
		std.mk(`Val "-2"))), \
		"2")

	run(c, std.mk(`Add (\
		std.mk(`Val "3"), \
		std.mk(`Val "-6"))), \
		"-3")

	run(c, std.mk(`Add (\
		std.mk(`Val "-4"), \
		std.mk(`Val "8"))), \
		"4")

	run(c, std.mk(`Add (\
		std.mk(`Val "-10"), \
		std.mk(`Val "5"))), \
		"-5")

	run(c, std.mk(`Add (\
		std.mk(`Val "-6"), \
		std.mk(`Val "-12"))), \
		"-18")

	run(c, std.mk(`Add (\
		std.mk(`Val "7"), \
		std.mk(`Val "-7"))), \
		"0")
}

const subneg = {c
	run(c, std.mk(`Sub (\
		std.mk(`Val "-1"), \
		std.mk(`Val "1"))), \
		"-2")

	run(c, std.mk(`Sub (\
		std.mk(`Val "1"), \
		std.mk(`Val "-1"))), \
		"2")

	run(c, std.mk(`Sub (\
		std.mk(`Val "4"), \
		std.mk(`Val "-2"))), \
		"6")

	run(c, std.mk(`Sub (\
		std.mk(`Val "3"), \
		std.mk(`Val "-6"))), \
		"9")

	run(c, std.mk(`Sub (\
		std.mk(`Val "-4"), \
		std.mk(`Val "8"))), \
		"-12")

	run(c, std.mk(`Sub (\
		std.mk(`Val "-10"), \
		std.mk(`Val "5"))), \
		"-15")

	run(c, std.mk(`Sub (\
		std.mk(`Val "-6"), \
		std.mk(`Val "-12"))), \
		"6")

	run(c, std.mk(`Sub (\
		std.mk(`Val "-14"), \
		std.mk(`Val "-7"))), \
		"-7")

	run(c, std.mk(`Sub (\
		std.mk(`Val "-8"), \
		std.mk(`Val "-8"))), \
		"0")
}

const run = {c : testr.ctx#, e : cmd#, res : byte[:]
	var buf : byte[4096]
	var v, n

	v = eval(e)
	n = std.bigbfmt(buf[:], v, 0)
	testr.check(c, std.eq(buf[:n], res), "{} != {}", buf[:n], res)
}

const eval = {e : cmd#
	var buf : byte[2048]
	var a, b, c	/* scratch vars */
	var n		/* buf len */

	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:
		a = try(std.bigparse(x))
		n = std.bigbfmt(buf[:], a, 0)
		-> a
	| `Modpow (x, y, z):
		a = eval(x)
		b = eval(y)
		c = eval(z)
		-> std.bigmodpow(a, b, c)
	;;
}

const binop = {name, op, x, y
	var a, b

	a = eval(x)
	b = eval(y)
	op(a, b)
	std.bigfree(b)
	-> a
}

generic try = {x : std.option(@a)
	match x
	| `std.Some v:	-> v
	| `std.None:	std.die("failed to get val")
	;;
}