+ exponents are now valid: 1.212121e+18
[io/quag.git] / libs / iovm / tests / ListTest.io
blob569d340de037994202684ccb87cad82b9c24cdda
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("a", "beta", "3")
290 assertRaisesException(a sortInPlaceBy)
291 assertRaisesException(a sortInPlaceBy(nil))
292 a sortInPlaceBy(block(value1, value2, if(value1 < value2, return nil, return value2)))
293 assertEquals("beta", a at(0))
294 assertEquals("a", a at(1))
295 assertEquals("3", a at(2))
298 testForeach := method(
299 a := List clone append("a", "beta", 3)
300 assertRaisesException(a foreach)
301 string := ""
302 assertEquals("abeta3", a foreach(index, value, string := string .. value asString))
304 string := ""
305 assertEquals("abeta3", a foreach(value, string := string .. value asString))
307 number := 0
308 assertEquals(0+1+2, a foreach(index, value, number := number + index))
311 testReverse := method(
312 a := List clone append("a", "beta", 3)
313 a reverse
314 assertEquals(3, a at(0))
315 assertEquals("beta", a at(1))
316 assertEquals("a", a at(2))
318 testReverseForeach := method(
319 a := List clone append("a", "beta", 3)
320 assertRaisesException(a reverseForeach)
321 assertRaisesException(a reverseForeach(nil))
322 string := ""
323 assertEquals("3betaa", a reverseForeach(index, value, string := string .. value asString))
325 string := ""
326 assertEquals("3betaa", a reverseForeach(value, string := string .. value asString))
328 number := 0
329 assertEquals(0+1+2, a reverseForeach(index, value, number := number + index))
332 testmap := method(
333 a := List clone append("a", "beta", 3)
334 assertRaisesException(a map)
335 a mapInPlace(index, value, value asString .. index asString)
336 assertEquals(3, a size)
337 assertEquals("a0", a at(0))
338 assertEquals("beta1", a at(1))
339 assertEquals("32", a at(2))
342 testmap2 := method(
343 a := List clone append("a", "beta", 3)
344 a mapInPlace(value, value asString)
345 assertEquals(3, a size)
346 assertEquals("a", a at(0))
347 assertEquals("beta", a at(1))
348 assertEquals("3", a at(2))
351 testMap := method(
352 a := List clone append("a", "beta", 3)
353 assertRaisesException(a map)
354 assertRaisesException(a mapInPlace)
355 a mapInPlace(index, value, value asString .. index asString)
356 assertEquals(3, a size)
357 assertEquals("a0", a at(0))
358 assertEquals("beta1", a at(1))
359 assertEquals("32", a at(2))
362 testMap2 := method(
363 a := List clone append("a", "beta", 3)
364 a mapInPlace(value, value asString)
365 assertEquals(3, a size)
366 assertEquals("a", a at(0))
367 assertEquals("beta", a at(1))
368 assertEquals("3", a at(2))