forked from pharo-containers/Containers-UniqueOrdered
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTSmallOrderedSetTest.class.st
More file actions
174 lines (150 loc) · 4.56 KB
/
Copy pathCTSmallOrderedSetTest.class.st
File metadata and controls
174 lines (150 loc) · 4.56 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
Class {
#name : #CTSmallOrderedSetTest,
#superclass : #TestCase,
#instVars : [
'collection'
],
#category : #'Containers-UniqueOrdered-Tests'
}
{ #category : #running }
CTSmallOrderedSetTest >> setUp [
super setUp.
collection := CTSmallOrderedSet new
]
{ #category : #testing }
CTSmallOrderedSetTest >> testAdd [
| object |
object := Object new.
self assert: collection size equals: 0.
self assert: (collection add: object) == object.
self assert: collection size equals: 1.
self assert: (collection add: object) == object.
self assert: collection size equals: 1.
]
{ #category : #testing }
CTSmallOrderedSetTest >> testAddAll [
collection addAll: #(2 1 1).
self assert: collection size = 2.
self assert: (collection includes: 1).
self assert: (collection includes: 2)
]
{ #category : #testing }
CTSmallOrderedSetTest >> testAddingTwiceTheSameOnlyAddOne [
| object |
object := Object new.
self assert: collection size equals: 0.
collection add: object.
self assert: collection size equals: 1.
collection add: object.
self assert: collection size equals: 1.
]
{ #category : #testing }
CTSmallOrderedSetTest >> testCopy [
| copy |
collection add: 1.
copy := collection copy.
collection add: 2.
self assert: collection size equals: 2.
self assert: copy size equals: 1.
self assert: (collection includes: 2).
self deny: (copy includes: 2).
]
{ #category : #testing }
CTSmallOrderedSetTest >> testDo [
| seen |
collection addAll: #(2 1 1).
seen := Array streamContents: [ :stream |
collection do: [ :each |
stream nextPut: each ] ].
self assert: seen size equals: 2.
self assert: (seen at: 1) equals: 2.
self assert: (seen at: 2) equals: 1
]
{ #category : #testing }
CTSmallOrderedSetTest >> testDoShouldIterateInOrder [
| seen |
collection addAll: #(1 2 1 1).
seen := Array streamContents: [ :stream |
collection do: [ :each |
stream nextPut: each ] ].
self assert: seen size equals: 2.
self assert: (seen at: 1) equals: 1.
self assert: (seen at: 2) equals: 2
]
{ #category : #testing }
CTSmallOrderedSetTest >> testIncludes [
self deny: (collection includes: 0).
collection add: 0.
self assert: (collection includes: 0)
]
{ #category : #testing }
CTSmallOrderedSetTest >> testIsEmpty [
self assert: collection isEmpty.
collection add: 1.
self deny: collection isEmpty.
collection remove: 1.
self assert: collection isEmpty
]
{ #category : #testing }
CTSmallOrderedSetTest >> testRemove [
collection add: 1.
self assert: (collection remove: 1) equals: 1.
self should: [ collection remove: 1 ] raise: Error
]
{ #category : #testing }
CTSmallOrderedSetTest >> testRemoveIfAbsent [
| absent |
collection add: 1.
absent := false.
self assert: (collection remove: 1 ifAbsent: [ absent := true ]) equals: 1.
self deny: absent.
collection remove: 1 ifAbsent: [ absent := true ].
self assert: absent.
]
{ #category : #testing }
CTSmallOrderedSetTest >> testSize [
self assert: collection size equals: 0.
collection addAll: #(2 1 1).
self assert: collection size equals: 2.
]
{ #category : #testing }
CTSmallOrderedSetTest >> testUnion[
"Tests the union operation of CTSmallOrderedSet to ensure it combines two sets, maintaining uniqueness and preserving the order of elements."
"scope: class-variables & instance-variables: collection"
| set1 set2 result |
set1 := CTSmallOrderedSet withAll: #(1 2 3).
set2 := CTSmallOrderedSet withAll: #(2 3 4).
result := set1 union: set2.
self assert: result size equals: 4.
self assert: (result includes: 1).
self assert: (result includes: 2).
self assert: (result includes: 3).
self assert: (result includes: 4).
"Test order preservation"
self assert: result asArray equals: #(1 2 3 4).
]
{ #category : #testing }
CTSmallOrderedSetTest >> testDifference [
| set1 set2 result |
set1 := CTSmallOrderedSet withAll: #(1 2 3 4).
set2 := CTSmallOrderedSet withAll: #(2 4).
result := set1 difference: set2.
self assert: result size equals: 2.
self assert: (result includes: 1).
self assert: (result includes: 3).
self deny: (result includes: 2).
self deny: (result includes: 4).
]
{ #category : #testing }
CTSmallOrderedSetTest >> testIntersection [
| set1 set2 result |
set1 := CTSmallOrderedSet withAll: #(1 2 3 4).
set2 := CTSmallOrderedSet withAll: #(2 4 5).
result := set1 intersection: set2.
self assert: result size equals: 2.
self assert: (result includes: 2).
self assert: (result includes: 4).
self deny: (result includes: 1).
self deny: (result includes: 3).
self deny: (result includes: 5).
]