Updated release image date.
[cslatevm.git] / tests / skiplist.slate
blob386099aeaa5d37e80de6c84ed1cd585d4e986b31
1 UnitTests definePrototype: #SkipList &basis: TestCase.
3 tc@(UnitTests SkipList traits) isEmpty
5   sl ::= SkipList new.
6   tc assert: sl isEmpty description: 'new SkipList states that it is empty'.
7 ].
9 tc@(UnitTests SkipList traits) size
11   sl ::= SkipList new.
12   tc assert: sl size = 0 description: 'empty SkipList has non-zero size'.
15 tc@(UnitTests SkipList traits) newPointerLength
17   sl ::= SkipList new.
18   sum ::= 0.
19   10 timesRepeat: [sum += sl newPointerLength].
20   tc assert: (sum between: 15 and: 25) description: 
21     'pointerlength average is incorrect'.
24 tc@(UnitTests SkipList traits) forward
26   sln ::= SkipList Node clone.
27   sln pointers: (sln pointers newSize: 5).
28   0 upTo: 4 do: [| :i | sln pointers at: i put: i].
29   0 upTo: 4 do: [| :i | 
30     tc assert: (sln forward: i) = i description: 'forward answers wrong value'.].
33 tc@(UnitTests SkipList traits) add
35   sl ::= SkipList new.
36   sl add: 2.
37   tc assert: (sl size) = 1 description: 'added one item but size is not one'.
38   sl add: 5.
39   tc assert: (sl size) = 2 description: 'could not add item at last place'.
40   sl add: 1.
41   tc assert: (sl size) = 3 description: 'added as first does not increase size'.
44 tc@(UnitTests SkipList traits) includes
46   sl ::= SkipList new.
47   sl add: 2.
48   tc assert: (sl includes: 2) description: 'first added object is not included'.
49   sl add: 5.
50   tc assert: (sl includes: 5) description: 'second added object is not included'.
51   sl add: 1.
52   tc assert: (sl includes: 1) description: 'added as first not included'.
53   sl add: 3.
54   tc assert: (sl includes: 3) description: 'inserted not included'. 
55   tc assert: (sl includes: 4) not description: 'includes unadded object'.
58 tc@(UnitTests SkipList traits) remove
60   sl ::= SkipList new.
61   sl add: 4.
62   sl remove: 4.
63   tc assert: sl isEmpty description: 'not empty after add-remove'.
64   0 upTo: 10 do: [| :i | sl add: i].
65   tc assert: (sl size) = 11 description: 'pre-test check'.
66   sl remove: 3.
67   tc assert: (sl size) = 10 description: 'remove does not decrease size'.
68   tc assert: (sl includes: 3) not description: 'found after removal'.
71 tc@(UnitTests SkipList traits) do
72 [| sum counter |
73   sl ::= SkipList new.
74   0 upTo: 9 do: [| :i | sl add: i].
75   sum := 0.
76   sl do: [| :obj | obj < 5 ifTrue: [sum += obj]].
77   tc assert: sum = 10 description: 'do on first elements failed'.
78   sum := 0.
79   sl do: [| :obj | obj > 4 ifTrue: [sum += obj]].
80   tc assert: sum = 35 description: 'do on last elements failed'.
81   sum := 0.
82   counter := 0.
83   sl do: [| :obj |
84     obj < 5 /\ [counter < 5] 
85       ifTrue: [sum += obj. counter += 1]].
86   tc assert: sum = 10 description: 'summed incorrect elements'.
89 tc@(UnitTests SkipList traits) reverseDoAndSequence
90 "Test reverseDo and sequence of elements."
91 [| sum counter |
92   sl ::= SkipList new.
93   0 upTo: 9 do: [| :i | sl add: i].
94   sum := 0.
95   counter := 0.
96   sl reverseDo: [| :obj |
97     counter < 5 ifTrue: [
98       counter += 1.
99       obj < 8 ifTrue: [sum += obj]]].
100   tc assert: sum = 18 description: 'reverse do on selected from first half failed'.
101   sum := 0.
102   counter := 0.
103   sl reverseDo: [| :obj |
104     counter < 8 ifTrue: [
105       counter += 1.
106       obj < 5 ifTrue: [sum += obj]]].
107   tc assert: sum = 9 description: 'reverse do on selection failed'.
110 tc@(UnitTests SkipList traits) addIfPresent
112   sl ::= SkipList new.
113   0 upTo: 9 do: [| :i | sl add: i].
114   counter := 0.
115   0 upTo: 9 do: [| :i | sl add: i ifPresent: [counter += 1]].
116   tc assert: counter = 10 description: 'added object when present'.
119 tc@(UnitTests SkipList traits) removeIfAbsent
120 [| counter |
121   sl ::= SkipList new.
122   counter := 0.
123   0 upTo: 9 do: [| :i | 
124     sl remove: i ifAbsent: [
125       counter += 1.
126       sl add: i]].
127   0 upTo: 9 do: [| :i | sl remove: i ifAbsent: [counter -= 1]].
128   tc assert: counter = 10 description: 'removing absent object?'.
131 tc@(UnitTests SkipList traits) suite
132 [tc suiteForSelectors: {
133   #size.
134   #newPointerLength.
135   #isEmpty.
136   #forward.
137   #add.
138   #includes.
139   #remove.
140   #do.
141   #reverseDoAndSequence.
142   #addIfPresent.
143   #removeIfAbsent.