Repository navigation
Expand file tree
/
Copy pathtest-set.lua
More file actions
201 lines (144 loc) · 4.11 KB
/
Copy pathtest-set.lua
File metadata and controls
201 lines (144 loc) · 4.11 KB
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
class = require 'pl.class'
M = require 'pl.Map'
S = require 'pl.Set'
List = require 'pl.List'
asserteq = require 'pl.test' . asserteq
asserteq2 = require 'pl.test' . asserteq2
MultiMap = require 'pl.MultiMap'
OrderedMap = require 'pl.OrderedMap'
s1 = S{1,2}
s2 = S{1,2}
-- equality
asserteq(s1,s2)
-- union
asserteq(S{1,2} + S{2,3}, S{1,2,3})
asserteq(S{1,2} + 3, S{1,2,3})
-- intersection
asserteq(S{1,2} * S{2,3}, S{2})
-- difference
fruit = S{'apple','banana','orange','apricots'}
tropical = S{'banana','orange'}
asserteq(fruit - tropical, S{'apple','apricots'})
asserteq(tropical - 'orange', S{'banana'})
-- symmetric_difference
asserteq(S{1,2} ^ S{2,3}, S{1,3})
-- tostring - illustrative, because these assertions may or may not work,
-- due to no ordering in set elements
--asserteq(tostring(S{1,2}),'[1,2]')
--asserteq(tostring(S{1,S{2,3}}),'[1,[2,3]]')
s3 = S()
asserteq(S.isempty(s3),true)
s4 = S{1,2,3}
-- subsets/supersets
asserteq(s4 > s1,true)
S.set(s3,'one',true)
s3.two = true
asserteq(s3,S{'one','two'})
m = M{>
asserteq(m,M{>
m:update {three=3,four=4}
asserteq(m,M{>
class.Animal()
function Animal:_init(name)
self.name = name
end
function Animal:__tostring()
return self.name..': '..self:speak()
end
class.Dog(Animal)
function Dog:speak()
return 'bark'
end
class.Cat(Animal)
function Cat:_init(name,breed)
self:super(name) -- must init base!
self.breed = breed
end
function Cat:speak()
return 'meow'
end
Lion = class(Cat)
function Lion:speak()
return 'roar'
end
fido = Dog('Fido')
felix = Cat('Felix','Tabby')
leo = Lion('Leo','African')
asserteq(tostring(fido),'Fido: bark')
asserteq(tostring(felix),'Felix: meow')
asserteq(tostring(leo),'Leo: roar')
assert(Dog:class_of(fido))
assert(fido:is_a(Dog))
assert(leo:is_a(Animal))
m = MultiMap()
m:set('john',1)
m:set('jane',3)
m:set('john',2)
ms = MultiMap{john={1,2},jane={3}}
asserteq(m,ms)
m = OrderedMap()
m:set('one',1)
m:set('two',2)
m:set('three',3)
asserteq(m:values(),List{1,2,3})
-- usually exercized like this:
--for k,v in m:iter() do print(k,v) end
fn = m:iter()
asserteq2 ('one',1,fn())
asserteq2 ('two',2,fn())
asserteq2 ('three',3,fn())
-- Keys overriding methods can be used.
m:set('set', 4)
asserteq(m:values(),List{1,2,3,4})
o1 = OrderedMap {{z=2},{beta=1},{name='fred'}}
asserteq(tostring(o1),'{z=2,beta=1,name="fred"}')
-- order of keys is not preserved here!
o2 = OrderedMap {z=4,beta=1.1,name='alice',extra='dolly'}
o1:update(o2)
asserteq(tostring(o1),'{z=4,beta=1.1,name="alice",extra="dolly"}')
o1:set('beta',nil)
asserteq(o1,OrderedMap{{z=4},{name='alice'},{extra='dolly'}})
o3 = OrderedMap()
o3:set('dog',10)
o3:set('cat',20)
o3:set('mouse',30)
asserteq(o3:keys(),{'dog','cat','mouse'})
o3:set('dog',nil)
asserteq(o3:keys(),{'cat','mouse'})
-- Vadim found a problem when clearing a key which did not exist already.
-- The keys list would then contain the key, although the map would not
o3:set('lizard',nil)
asserteq(o3:keys(),{'cat','mouse'})
asserteq(o3:values(), {20,30})
asserteq(tostring(o3),'{cat=20,mouse=30}')
-- copy constructor
o4 = OrderedMap(o3)
asserteq(o4,o3)
-- constructor throws an error if the argument is bad
-- (errors same as OrderedMap:update)
asserteq(false,pcall(function()
m = OrderedMap('string')
end))
---- changing order of key/value pairs ----
o3 = OrderedMap{{cat=20},{mouse=30}}
o3:insert(1,'bird',5) -- adds key/value before specified position
o3:insert(1,'mouse') -- moves key keeping old value
asserteq(o3:keys(),{'mouse','bird','cat'})
asserteq(tostring(o3),'{mouse=30,bird=5,cat=20}')
o3:insert(2,'cat',21) -- moves key and sets new value
asserteq(tostring(o3),'{mouse=30,cat=21,bird=5}')
-- if you don't specify a value for an unknown key, nothing happens to the map
o3:insert(3,'alligator')
asserteq(tostring(o3),'{mouse=30,cat=21,bird=5}')
---- short-cut notation
o5 = OrderedMap()
o5.alpha = 1
o5.beta = 2
o5.gamma = 3
asserteq(o5,OrderedMap{{alpha=1},{beta=2},{gamma=3}})
o5.alpha = 10
o5.beta = 20
o5.gamma = 30
o5.delta = 40
o5.checked = false
asserteq(o5,OrderedMap{{alpha=10},{beta=20},{gamma=30},{delta=40},{checked=false}})
You can’t perform that action at this time.