-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_set.lua
executable file
·135 lines (132 loc) · 3.61 KB
/
test_set.lua
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
#!/usr/bin/env lua
-- Copyright © 2025 Mark Summerfield. All rights reserved.
local Set = require("set")
local tx = require("tx")
local check, report = require("check").checker()
local even = Set(2, 4, 6, 8)
check(not even:isempty())
check("{2 4 6 8}" == even:tostring(true), even)
check(#even == 4, 4)
check(even:len() == 4, 4)
local odd = Set(1, 3, 5, 7, 9)
check(not odd:isempty())
check("{1 3 5 7 9}" == odd:tostring(true), odd)
check(#odd == 5, 5)
check(odd:len() == 5, 5)
even:add(10)
check("{10 2 4 6 8}" == even:tostring(true), even)
check(#even == 5, 5, 6)
odd:add(11, 13)
check("{1 11 13 3 5 7 9}" == odd:tostring(true), odd)
check(#odd == 7, 7)
check(even:contains(8), true)
check(not even:contains(9), false)
check(odd:contains(1), true)
check(not odd:contains(2), false)
local e2 = Set()
check(e2:isempty())
check("{}" == e2:tostring(true), e2)
e2 = Set(1, 2, 3, 4, 5)
check(not e2:isempty())
check("{1 2 3 4 5}" == e2:tostring(true), e2)
e2 = tx.clone(even)
check(not e2:isempty())
check("{10 2 4 6 8}" == e2:tostring(true), e2)
check(#e2 == 5, 5)
check(e2 == even, true)
check(e2 ~= odd, true)
check(not e2:isempty())
e2:clear()
check(e2:isempty())
check("{}" == e2:tostring(true), e2)
check(#e2 == 0, 0)
local a = even:union(odd)
check("{1 10 11 13 2 3 4 5 6 7 8 9}" == a:tostring(true), a)
local b = even | odd
check("{1 10 11 13 2 3 4 5 6 7 8 9}" == b:tostring(true), b)
local c = a & b
check(c == a, c)
c = a:intersection(b)
check(c == a, c)
local d = Set(5, 6, 7, 8, 20, 21)
check("{20 21 5 6 7 8}" == d:tostring(true), d)
c = d | even
check("{10 2 20 21 4 5 6 7 8}" == c:tostring(true), c)
c:clear()
c = d:union(even)
check("{10 2 20 21 4 5 6 7 8}" == c:tostring(true), c)
c = d & even
check("{6 8}" == c:tostring(true), c)
c:clear()
c = d:intersection(even)
check("{6 8}" == c:tostring(true), c)
c = d | even
check("{10 2 20 21 4 5 6 7 8}" == c:tostring(true), c)
c:remove(10, 20, 21)
check("{2 4 5 6 7 8}" == c:tostring(true), c)
c:remove(5)
check("{2 4 6 7 8}" == c:tostring(true), c)
d = Set(2, 4, 6, 7, 8)
check(c == d, c)
c:remove(8)
check(c ~= d, c)
a = d:difference(even)
check("{7}" == a:tostring(true), a)
a = d:difference(odd)
check("{2 4 6 8}" == a:tostring(true), a)
a = Set(1, 2, 3, 4, 5)
b = Set(4, 5, 6, 7)
c = a:symmetric_difference(b)
check("{1 2 3 6 7}" == c:tostring(true), c)
check(not a:issubset(b))
check(not b:issubset(a))
check(not a:issuperset(b))
check(not b:issuperset(a))
c = a | b
c:add(8, 9, 10, 11, 13)
check("{1 10 11 13 2 3 4 5 6 7 8 9}" == c:tostring(true), c)
check(even:issubset(c))
check(c:issuperset(even))
check(odd:issubset(c))
check(c:issuperset(odd))
check(not c:isdisjoint(odd))
check(c:isdisjoint(Set(99)))
check(even ~= odd)
check(a ~= b)
d = Set(2, 4, 6, 8)
check(d ~= even)
d:add(10)
check(d == even)
local f = even - Set(4, 6)
check(f == Set(2, 8, 10))
f:unite(Set(1, 2, 3))
check(f == Set(1, 2, 3, 8, 10))
f = Set(2, 8, 10)
f:add(1, 2, 3)
check(f == Set(1, 2, 3, 8, 10))
local seen = { a = 0, b = 0, c = 0, d = 0, e = 0, f = 0 }
a = Set("a", "b", "c", "d", "e", "f")
check(a:tostring(true) == '{"a" "b" "c" "d" "e" "f"}')
while true do
local element = a:random_value()
seen[element] = seen[element] + 1
local done = true
for _, value in pairs(seen) do
if value == 0 then done = false end
end
if done then break end
end
local strs = {}
for x in a:iter(true) do
table.insert(strs, x)
end
check("a b c d e f" == table.concat(strs, " "))
check("a b c d e f" == table.concat(a:values(true), " "))
strs = {}
for x in d:iter(true) do
table.insert(strs, tostring(x))
end
check("2 4 6 8 10" == table.concat(strs, " "))
check(a:typeof() == "Set")
check(a.typeof() == "Set")
report("Set")