Updating built in Io code to use += instead of x = x + y
[io/quag.git] / libs / iovm / tests / ListTest.io
blob354704c3816c8b5f0e0449ecb1d4dfc37b2897a5
2 ListTest := UnitTest clone do(
3 setUp := method(
4 super(setUp)
5 self exampleList := List clone append("a", "beta", 3)
7 testClone := method(
8 assertNotSame(List, List clone)
9 clonedList := exampleList clone
10 assertNotSame(clonedList, exampleList)
11 assertEquals(3, clonedList size)
12 assertEquals(exampleList at(0), clonedList at(0))
13 assertEquals(exampleList at(2), clonedList at(2))
15 testAppend := method(
16 assertRaisesException(a append)
17 a := List clone append("a", "beta", 3)
18 a append(nil)
19 a append(88)
20 a append("blah", "fasel")
21 assertEquals("fasel", a at(6))
22 assertEquals(88, a at(4))
23 assertNil(a at(3))
25 testAppendIfAbsent := method(
26 a := List clone append("a", "beta", 3)
27 a appendIfAbsent
28 a appendIfAbsent(nil)
29 a appendIfAbsent(3)
30 a appendIfAbsent(3)
31 assertEquals(4, a size)
32 assertNil(a at(3))
33 a appendIfAbsent("blah", "blah", 3, "new1", "blah", "new2")
34 assertEquals(7, a size)
35 assertEquals("blah", a at(4))
36 assertEquals("new1", a at(5))
37 assertEquals("new2", a at(6))
39 testAppendSeq := method(
40 a := List clone append("a", "beta", 3)
41 assertRaisesException(a appendSeq(nil))
42 assertRaisesException(a appendSeq(a))
43 a appendSeq(List clone)
44 a appendSeq(List clone append("blah", "fasel"))
45 assertEquals(5, a size)
46 assertEquals("blah", a at(3))
47 assertEquals("fasel", a at(4))
49 testRemove := method(
50 a := List clone append("a", "beta", 3)
51 assertRaisesException(a remove)
52 a append(nil, 7, nil)
53 a remove(nil)
54 assertEquals(4, a size)
55 a remove(3)
56 assertEquals(3, a size)
57 assertEquals("a", a at(0))
58 assertEquals("beta", a at(1))
59 assertEquals(7, a at(2))
61 testIndexOf := method(
62 a := List clone append("a", "beta", 3)
63 assertRaisesException(a indexOf)
64 assertNil(a indexOf(nil))
65 a append("a")
66 assertEquals(0, a indexOf("a"))
67 a removeAt(0)
68 assertEquals(2, a indexOf("a"))
70 testContains := method(
71 a := List clone append("a", "beta", 3)
72 assertFalse(a contains)
73 assertFalse(a contains(nil))
74 assertFalse(a contains(333))
75 assertTrue(a contains("beta"))
77 testPush := method(
78 a := List clone append("a", "beta", 3)
79 assertRaisesException(a push)
80 a push(nil)
81 a push(88)
82 a push("blah", "fasel")
83 assertEquals("fasel", a at(6))
84 assertEquals(88, a at(4))
85 assertNil(a at(3))
87 testPop := method(
88 a := List clone append("a", "beta", 3)
89 assertEquals(3, a pop)
90 assertEquals(2, a size)
91 assertEquals("beta", a pop)
92 assertEquals(1, a size)
93 assertEquals("a", a pop)
94 assertEquals(0, a size)
95 assertNil(a pop)
97 testEmpty := method(
98 a := List clone append("a", "beta", 3)
99 a empty
100 assertEquals(0, a size)
102 testsize := method(
103 a := List clone append("a", "beta", 3)
104 assertEquals(0, List size)
105 assertEquals(3, a size)
106 a append("yo")
107 assertEquals(4, a size)
109 testAtInsert := method(
110 a := List clone append("a", "beta", 3)
111 assertRaisesException(a atInsert())
112 assertRaisesException(a atInsert(nil))
113 assertRaisesException(a atInsert(nil, "two"))
114 assertRaisesException(a atInsert(-1, "two"))
115 assertRaisesException(a atInsert(4, "two"))
117 a atInsert(0, "zero")
118 assertEquals(4, a size)
119 assertEquals(0, a indexOf("zero"))
121 a atInsert(1, "one")
122 assertEquals(5, a size)
123 assertEquals(1, a indexOf("one"))
125 a atInsert(5, "append")
126 assertEquals(6, a size)
127 assertEquals(5, a indexOf("append"))
129 testRemoveAt := method(
130 a := List clone append("a", "beta", 3)
131 assertRaisesException(a removeAt())
132 assertRaisesException(a removeAt(nil))
133 assertRaisesException(a removeAt(-1))
134 assertRaisesException(a removeAt(3))
136 a removeAt(0)
137 assertEquals(2, a size)
138 assertEquals("beta", a at(0))
139 assertEquals(3, a at(1))
141 a removeAt(1)
142 assertEquals(1, a size)
143 assertEquals("beta", a at(0))
145 testAt := method(
146 a := List clone append("a", "beta", 3)
147 assertRaisesException(a at())
148 assertRaisesException(a at(nil))
149 assertNil(a at(-1))
150 assertNil(a at(4))
151 assertEquals("a", a at(0))
152 assertEquals("beta", a at(1))
153 assertEquals(3, a at(2))
155 testAtPut := method(
156 a := List clone append("a", "beta", 3)
157 assertRaisesException(a atPut())
158 assertRaisesException(a atPut(nil))
159 assertRaisesException(a atPut(nil, "two"))
160 assertRaisesException(a atPut(-1, "two"))
161 assertRaisesException(a atPut(3, "two"))
163 a atPut(0, "zero")
164 assertEquals(3, a size)
165 assertEquals(0, a indexOf("zero"))
167 a atPut(2, "two")
168 assertEquals(3, a size)
169 assertEquals(2, a indexOf("two"))
171 testSwapIndices := method(
172 a := List clone append("a", "beta", 3)
173 assertRaisesException(a swapIndices())
174 assertRaisesException(a swapIndices(nil))
175 assertRaisesException(a swapIndices(nil, nil))
176 assertRaisesException(a swapIndices(1, nil))
177 assertRaisesException(a swapIndices(nil, 1))
178 assertRaisesException(a swapIndices(0, 3))
179 assertRaisesException(a swapIndices(-1, 2))
181 a swapIndices(0, 2)
182 a swapIndices(1, 1)
183 assertEquals("a", a at(2))
184 assertEquals("beta", a at(1))
185 assertEquals(3, a at(0))
187 testpreallocateToSize := method(
188 a := List clone append("a", "beta", 3)
189 assertRaisesException(a preallocateToSize)
190 assertRaisesException(a preallocateToSize(nil))
191 a preallocateToSize(1)
192 assertEquals(3, a size)
193 a preallocateToSize(10)
194 assertEquals(3, a size)
196 testFirst := method(
197 a := List clone append("a", "beta", 3)
198 assertNil(List first)
199 assertEquals("a", a first)
201 testLast := method(
202 a := List clone append("a", "beta", 3)
203 assertNil(List last)
204 assertEquals(3, a last)
206 _testPrint := method(
207 // would need to be able to read stdout or install a printCallback from Io to test print()
210 testSelect := method(
211 a := List clone append("a", "beta", 3)
212 assertRaisesException(a select)
213 selection := a select(index, value, index == 0 or value == 3)
214 assertEquals(2, selection size)
215 assertEquals("a", selection at(0))
216 assertEquals(3, selection at(1))
218 selection := a select(index, value, index == -1)
219 assertEquals(0, selection size)
222 testSelect2 := method(
223 a := List clone append("a", "beta", 3)
224 assertRaisesException(a select)
225 selection := a select(value, value == "a" or value == 3)
226 assertEquals(2, selection size)
227 assertEquals("a", selection at(0))
228 assertEquals(3, selection at(1))
230 selection := a select(index, value, index == -1)
231 assertEquals(0, selection size)
234 testSelectInPlace := method(
235 a := List clone append("a", "beta", 3)
236 assertRaisesException(a selectInPlace)
237 a selectInPlace(index, value, index == 0 or value == 3)
238 assertEquals(2, a size)
239 assertEquals("a", a at(0))
240 assertEquals(3, a at(1))
242 a selectInPlace(index, value, index == -1)
243 assertEquals(0, a size)
246 testSelectInPlace2 := method(
247 a := List clone append("a", "beta", 3)
248 a selectInPlace(index, value, value == "a" or value == 3)
249 assertEquals(2, a size)
250 assertEquals("a", a at(0))
251 assertEquals(3, a at(1))
253 a selectInPlace(index, value, index == -1)
254 assertEquals(0, a size)
256 testDetect := method(
257 a := List clone append("a", "beta", 3)
258 assertRaisesException(a detect)
259 assertRaisesException(a detect(nil))
260 a detect(index, value, index == 0 or value == 3)
261 assertEquals("a", a detect(index, value, index == 0 or value == 3))
262 assertEquals(3, a detect(index, value, value type == "Number" and value > 1))
263 assertNil(a detect(index, value, value > 3))
265 testDetect := method(
266 a := List clone append("a", "beta", 3)
267 assertRaisesException(a detect)
268 a detect(index, value, value = "a" or value == 3)
269 assertEquals("a", a detect(index, value, index == 0 or value == 3))
270 assertEquals(3, a detect(index, value, value type == "Number" and value > 1))
271 assertNil(a detect(index, value, value type == "Number" and value > 3))
273 testSort := method(
274 a := List clone append("a", "beta", "3")
275 List clone sortInPlace
276 a sortInPlace
277 assertEquals("3", a at(0))
278 assertEquals("a", a at(1))
279 assertEquals("beta", a at(2))
281 a append("0") sortInPlace
282 assertEquals("0", a at(0))
283 assertEquals("3", a at(1))
284 assertEquals("a", a at(2))
285 assertEquals("beta", a at(3))
288 testSortBy := method(
289 a := List clone append("beta", "3", "alpha")
290 assertRaisesException(a sortInPlaceBy)
291 assertRaisesException(a sortInPlaceBy(nil))
292 a sortInPlaceBy(block(v1, v2, v1 < v2))
293 assertEquals("3", a at(0))
294 assertEquals("alpha", a at(1))
295 assertEquals("beta", a at(2))
298 testEmptyListSortBy := method(
299 assertEquals(list sortInPlaceBy(block(x, y, x < y)), list)
302 testSortBySingleItem := method(
303 assertEquals(list(3) sortInPlaceBy(block(x, y, x < y)), list(3))
306 testSortByTwoItems := method(
307 assertEquals(list(3,1) sortInPlaceBy(block(x, y, x < y)), list(1,3))
310 testForeach := method(
311 a := List clone append("a", "beta", 3)
312 assertRaisesException(a foreach)
313 string := ""
314 assertEquals("abeta3", a foreach(index, value, string := string .. value asString))
316 string := ""
317 assertEquals("abeta3", a foreach(value, string := string .. value asString))
319 number := 0
320 assertEquals(0+1+2, a foreach(index, value, number := number + index))
323 testReverse := method(
324 a := List clone append("a", "beta", 3)
325 a reverse
326 assertEquals(3, a at(0))
327 assertEquals("beta", a at(1))
328 assertEquals("a", a at(2))
330 testReverseForeach := method(
331 a := List clone append("a", "beta", 3)
332 assertRaisesException(a reverseForeach)
333 assertRaisesException(a reverseForeach(nil))
334 string := ""
335 assertEquals("3betaa", a reverseForeach(index, value, string := string .. value asString))
337 string := ""
338 assertEquals("3betaa", a reverseForeach(value, string := string .. value asString))
340 number := 0
341 assertEquals(0+1+2, a reverseForeach(index, value, number := number + index))
344 testmap := method(
345 a := List clone append("a", "beta", 3)
346 assertRaisesException(a map)
347 a mapInPlace(index, value, value asString .. index asString)
348 assertEquals(3, a size)
349 assertEquals("a0", a at(0))
350 assertEquals("beta1", a at(1))
351 assertEquals("32", a at(2))
354 testmap2 := method(
355 a := List clone append("a", "beta", 3)
356 a mapInPlace(value, value asString)
357 assertEquals(3, a size)
358 assertEquals("a", a at(0))
359 assertEquals("beta", a at(1))
360 assertEquals("3", a at(2))
363 testMap := method(
364 a := List clone append("a", "beta", 3)
365 assertRaisesException(a map)
366 assertRaisesException(a mapInPlace)
367 a mapInPlace(index, value, value asString .. index asString)
368 assertEquals(3, a size)
369 assertEquals("a0", a at(0))
370 assertEquals("beta1", a at(1))
371 assertEquals("32", a at(2))
374 testMap2 := method(
375 a := List clone append("a", "beta", 3)
376 a mapInPlace(value, value asString)
377 assertEquals(3, a size)
378 assertEquals("a", a at(0))
379 assertEquals("beta", a at(1))
380 assertEquals("3", a at(2))