blob: 1eb207e19d15ef6ae878c179e3a9ecdd307cd31b (
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
|
(.module:
[lux #*
data/text/format
["_" test (#+ Test)]
[control
[monad (#+ do)]
{[0 #test]
[/
["$." monoid]]}]
[math
["r" random]]]
{1
["." /
["." // #_
["#." nat]]]})
(def: #export test
Test
(do r.monad
[pattern r.nat
idx (:: @ map (n/% /.width) r.nat)]
($_ _.and
($monoid.spec //nat.equivalence /.disjunction r.nat)
($monoid.spec //nat.equivalence /.conjunction r.nat)
(_.test "Clearing and settings bits should alter the count."
(and (n/= (dec (/.count (/.set idx pattern)))
(/.count (/.clear idx pattern)))
(|> (/.count pattern)
(n/- (/.count (/.clear idx pattern)))
(n/<= 1))
(|> (/.count (/.set idx pattern))
(n/- (/.count pattern))
(n/<= 1))))
(_.test "Can query whether a bit is set."
(and (or (and (/.set? idx pattern)
(not (/.set? idx (/.clear idx pattern))))
(and (not (/.set? idx pattern))
(/.set? idx (/.set idx pattern))))
(or (and (/.set? idx pattern)
(not (/.set? idx (/.flip idx pattern))))
(and (not (/.set? idx pattern))
(/.set? idx (/.flip idx pattern))))))
(_.test "The negation of a bit pattern should have a complementary bit-count."
(n/= /.width
(n/+ (/.count pattern)
(/.count (/.not pattern)))))
(_.test "Can do simple binary logic."
(and (n/= 0
(/.and pattern
(/.not pattern)))
(n/= (/.not 0)
(/.or pattern
(/.not pattern)))
(n/= (/.not 0)
(/.xor pattern
(/.not pattern)))
(n/= 0
(/.xor pattern
pattern))))
(_.test "rotate-left and rotate-right are inverses of one another."
(and (|> pattern
(/.rotate-left idx)
(/.rotate-right idx)
(n/= pattern))
(|> pattern
(/.rotate-right idx)
(/.rotate-left idx)
(n/= pattern))))
(_.test "Rotate as many spaces as the bit-pattern's width leaves the pattern unchanged."
(and (|> pattern
(/.rotate-left /.width)
(n/= pattern))
(|> pattern
(/.rotate-right /.width)
(n/= pattern))))
(_.test "Shift right respect the sign of ints."
(let [value (.int pattern)]
(if (i/< +0 value)
(i/< +0 (/.arithmetic-right-shift idx value))
(i/>= +0 (/.arithmetic-right-shift idx value)))))
)))
|