summaryrefslogtreecommitdiff
path: root/lib/std/test/bitset.myr
blob: 91125378ba5ee09f725f0817b023d184076a921e (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
use std
use testr

const main = {
	testr.run([
		[.name="basic", .fn={ctx
			var bs = mkset([0, 1, 16][:])
			testr.check(ctx, std.bshas(bs, 0), "missing 0")
			testr.check(ctx, std.bshas(bs, 1), "missing 1")
			testr.check(ctx, std.bshas(bs, 16), "missing 16")
			testr.check(ctx, !std.bshas(bs, -1), "negative val")
			testr.check(ctx, !std.bshas(bs, 2), "extra 2")
			testr.check(ctx, !std.bshas(bs, 15), "extra 15")
			testr.check(ctx, !std.bshas(bs, 17), "extra 2")
			std.bsfree(bs)
		}],
		[.name="bigsets", .fn={ctx
			/* multiple chunks */
			var bs = mkset([0, 63, 64, 65, 73, 127, 128, 129][:])
			testr.check(ctx, std.bshas(bs, 0), "missing 0")
			testr.check(ctx, std.bshas(bs, 63), "missing 63")
			testr.check(ctx, std.bshas(bs, 64), "missing 64")
			testr.check(ctx, std.bshas(bs, 65), "missing 65")
			testr.check(ctx, std.bshas(bs, 127), "missing 127")
			testr.check(ctx, std.bshas(bs, 128), "missing 128")
			testr.check(ctx, std.bshas(bs, 129), "missing 129")
			std.bsfree(bs)
		}],
		[.name="iter", .fn={ctx
			var expected = [0,1,3,7,16][:]
			var bs = mkset(expected)
			var i = 0

			std.bsput(bs, 1)
			std.bsput(bs, 0)
			std.bsput(bs, 16)
			std.bsput(bs, 7)
			std.bsput(bs, 3)
			for e : std.bybsvalue(bs)
				testr.check(ctx, e == expected[i], "expected[{}] ({}) != {}", i, e, expected[i])
				i++
			;;
			std.bsfree(bs)
		}],
		[.name="count", .fn={ctx
			var bs = mkset([0, 63, 64, 65, 73, 127, 128, 129][:])
			testr.check(ctx, std.bscount(bs) == 8, "wrong element count, expected {}", std.bscount(bs))
			std.bsfree(bs)
		}],
		[.name="hash", .fn={ctx
			var bs = mkset([][:])
			testr.check(ctx, std.bshash(bs) == 2580988821, "wrong hash, got {}", std.bshash(bs))
			std.bsput(bs, 123456)
			testr.check(ctx, std.bshash(bs) == 2020624217, "wrong hash, got {}", std.bshash(bs))
			std.bsdel(bs, 123456)
			testr.check(ctx, std.bshash(bs) == 2580988821, "wrong hash, got {}", std.bshash(bs))
			std.bsfree(bs)
		}]
	][:])
}

const mkset = {elts
	var bs

	bs = std.mkbs()
	for e : elts
		std.bsput(bs, e)
	;;
	-> bs
}