blob: 78d096ceff671be714641b2410472a789fca0468 (
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
|
(.module:
[lux #*
[control
[monad (#+ do Monad)]]
[data
["." number]
[text
format]
[collection
["." set
["&" ordered]]
["." list]]]
[math
["r" random]]]
lux/test)
(def: gen-nat
(r.Random Nat)
(|> r.nat
(:: r.monad map (n/% 100))))
(context: "Sets"
(<| (times 100)
(do @
[sizeL gen-nat
sizeR gen-nat
listL (|> (r.set number.hash sizeL gen-nat) (:: @ map set.to-list))
listR (|> (r.set number.hash sizeR gen-nat) (:: @ map set.to-list))
#let [(^open "&;.") &.equivalence
setL (&.from-list number.order listL)
setR (&.from-list number.order listR)
sortedL (list.sort n/< listL)
minL (list.head sortedL)
maxL (list.last sortedL)]]
($_ seq
(test "I can query the size of a set."
(n/= sizeL (&.size setL)))
(test "Can query minimum value."
(case [(&.min setL) minL]
[#.None #.None]
#1
[(#.Some reference) (#.Some sample)]
(n/= reference sample)
_
#0))
(test "Can query maximum value."
(case [(&.max setL) maxL]
[#.None #.None]
#1
[(#.Some reference) (#.Some sample)]
(n/= reference sample)
_
#0))
(test "Converting sets to/from lists can't change their values."
(|> setL
&.to-list (&.from-list number.order)
(&;= setL)))
(test "Order is preserved."
(let [listL (&.to-list setL)
(^open "L/.") (list.equivalence number.equivalence)]
(L/= listL
(list.sort n/< listL))))
(test "Every set is a sub-set of the union of itself with another."
(let [setLR (&.union setL setR)]
(and (&.sub? setLR setL)
(&.sub? setLR setR))))
(test "Every set is a super-set of the intersection of itself with another."
(let [setLR (&.intersection setL setR)]
(and (&.super? setLR setL)
(&.super? setLR setR))))
(test "Union with the empty set leaves a set unchanged."
(&;= setL
(&.union (&.new number.order)
setL)))
(test "Intersection with the empty set results in the empty set."
(let [empty-set (&.new number.order)]
(&;= empty-set
(&.intersection empty-set setL))))
(test "After substracting a set A from another B, no member of A can be a member of B."
(let [sub (&.difference setR setL)]
(not (list.any? (&.member? sub) (&.to-list setR)))))
(test "Every member of a set must be identifiable."
(list.every? (&.member? setL) (&.to-list setL)))
))))
|