forked from sl4m/gnu_smalltalk_koans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestCollectionsRevisited.st
173 lines (118 loc) · 4.8 KB
/
TestCollectionsRevisited.st
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
Koan subclass: TestCollectionsRevisited [
<comment: 'A collection of advanced collection tests.'>
testDo [
| array sum orderedCollection |
"Iterate over collection."
array := #(1 2 3 4).
sum := 0.
array do: [ :number | sum := sum + number ].
self expect: (10) toEqual: (sum).
orderedCollection := OrderedCollection new addAll: array; yourself.
sum := 0.
orderedCollection do: [ :number | sum := sum + number ].
self expect: (self fillMeIn) toEqual: (sum).
"#do: also works on SortedCollection, Bag, Dictionary, and Set."
]
testConform [
| array result sortedCollection |
"Meets condition(s)."
array := #($m $n $o $p).
result := array conform: [ :character | character > $l & character < $q ].
self expect: (self fillMeIn) toEqual: (result).
sortedCollection := SortedCollection new addAll: array; yourself.
result := sortedCollection conform: [ :character | character > $l & character < $q ].
self expect: (self fillMeIn) toEqual: (result).
"#conform: also works on OrderedCollection, Bag, Dictionary, and Set."
]
testSelect [
| array result bag |
"Return collection of elements that meets condition(s)."
array := #(1 2 3 4 5).
result := array select: [ :number | number even ].
self expect: (self fillMeIn) toEqual: (result).
bag := Bag new addAll: array; yourself.
result := bag select: [ :number | number even ].
self expect: (self fillMeIn) toEqual: (result).
"#select: also works on OrderedCollection, SortedCollection, Dictionary, and Set."
]
testReject [
| array result dictionary |
"Return collection of elements that do not meet condition(s)."
array := #(1 2 3 4 5).
result := array reject: [ :number | number even ].
self expect: (self fillMeIn) toEqual: (result).
dictionary := Dictionary new
add: #a -> 1;
add: #b -> 2;
add: #c -> 3;
add: #d -> 4;
add: #e -> 5;
yourself.
result := dictionary reject: [ :number | number even ].
self expect: (self fillMeIn) toEqual: (result).
"#reject: also works on OrderedCollection, SortedCollection, Bag, and Set."
]
testCollect [
| array result set |
"Transform each element and return in a new collection."
array := #($a $b $c $d $e).
result := array collect: [ :character | character asUppercase ].
self expect: (self fillMeIn) toEqual: (result).
set := Set new addAll: array; yourself.
result := set collect: [ :character | character asUppercase ].
self expect: (self fillMeIn) toEqual: (result).
"#collect: also works on OrderedCollection, SortedCollection, Bag, and Dictionary."
]
testDetect [
| array result |
array := #(56 81 16 77).
result := array detect: [ :number | number sqrt = 4 ].
self expect: (self fillMeIn) toEqual: (result).
"#detect: also works on OrderedCollection, SortedCollection, Bag, Dictionary, and Set."
]
testInject [
| array result |
array := #(4 8 12 16).
result := array inject: 0 into: [ :numOne :numTwo | numOne + numTwo ].
self expect: (self fillMeIn) toEqual: (result).
"#inject: also works on OrderedCollection, SortedCollection, Bag, Dictionary, and Set."
]
testCollectionConversion [
| array orderedCollection sortedCollection bag set dictionary |
array := Array with: 1 with: 2 with: 3.
orderedCollection := OrderedCollection with: 1 with: 2 with: 3.
sortedCollection := SortedCollection with: 1 with: 2 with: 3.
bag := Bag with: 1 with: 2 with: 3.
set := Set with: 1 with: 2 with: 3.
dictionary := Dictionary new;
add: #a -> 1;
add: #b -> 2;
add: #c -> 3;
yourself.
self expect: (self fillMeIn) toEqual: (array = orderedCollection asArray).
self expect: (self fillMeIn) toEqual: (array = sortedCollection asArray).
self expect: (self fillMeIn) toEqual: (array = bag asArray).
self expect: (self fillMeIn) toEqual: (array = set asArray).
self expect: (self fillMeIn) toEqual: (array = dictionary asArray).
"
The following messages also can be used to convert from one collection to another:
* asOrderedCollection
* asSortedCollection
* asBag
* asSet
"
]
testFailSafeAccessToElements [
| array |
array := #(0 1 2 3).
self expect: (self fillMeIn) toEqual: (array at: 0 ifAbsent: [nil]).
self expect: [ array at: 0 ] toRaise: (self fillMeIn).
"#at:ifAbsent: also works on OrderedCollection, SortedCollection, and Dictionary."
]
testFailSafeRemoveElementsFromCollection [
| orderedCollection |
orderedCollection := OrderedCollection new addAll: #(0 1 2 3); yourself.
self expect: (self fillMeIn) toEqual: (orderedCollection remove: 4 ifAbsent: [nil]).
self expect: [ orderedCollection remove: 4 ] toRaise: (self fillMeIn).
]
]