@@ -50,18 +50,6 @@ CTStackTest >> testAsOrderedCollection [
5050 self assert: result asArray equals: #('c' 'b' 'a') .
5151]
5252
53- { #category : ' tests' }
54- CTStackTest >> testAvailableSpace [
55-
56- self assert: stack availableSpace equals: 5 .
57- stack push: ' a' .
58- self assert: stack availableSpace equals: 4 .
59- stack push: ' b' ; push: ' c' .
60- self assert: stack availableSpace equals: 2 .
61- stack pop.
62- self assert: stack availableSpace equals: 3
63- ]
64-
6553{ #category : ' tests' }
6654CTStackTest >> testCapacity [
6755
@@ -70,6 +58,20 @@ CTStackTest >> testCapacity [
7058 self assert: stack capacity equals: 5 . " Capacity doesn't change"
7159]
7260
61+ { #category : ' tests' }
62+ CTStackTest >> testCapacityGrowth [
63+
64+ | originalCapacity |
65+ originalCapacity := stack capacity.
66+
67+ 1 to: originalCapacity do: [ :i | stack push: i ].
68+ self assert: stack capacity equals: originalCapacity.
69+
70+ stack push: ' overflow' .
71+ self assert: stack capacity equals: originalCapacity * 2 .
72+ self assert: stack size equals: originalCapacity + 1
73+ ]
74+
7375{ #category : ' tests' }
7476CTStackTest >> testCopyCreatesNewObject [
7577
@@ -85,24 +87,25 @@ CTStackTest >> testCopyCreatesNewObject [
8587CTStackTest >> testCopyHasSameContents [
8688
8789 | copy |
88- stack push: ' first' ; push: ' second' ; push: ' third' .
90+ stack
91+ push: ' first' ;
92+ push: ' second' ;
93+ push: ' third' .
8994 copy := stack copy.
90-
95+
9196 self assert: copy size equals: stack size.
92- self assert: copy top equals: stack top.
93- self assert: copy capacity equals: stack capacity
97+ self assert: copy top equals: stack top
9498]
9599
96100{ #category : ' tests' }
97101CTStackTest >> testDefaultStackCreation [
98102
99103 | defaultStack |
100104 defaultStack := CTStack new .
101-
105+
102106 self assert: defaultStack capacity equals: 10 .
103107 self assert: defaultStack isEmpty.
104- self assert: defaultStack size equals: 0 .
105- self assert: defaultStack availableSpace equals: 10
108+ self assert: defaultStack size equals: 0
106109]
107110
108111{ #category : ' tests' }
@@ -118,23 +121,44 @@ CTStackTest >> testDoIteration [
118121]
119122
120123{ #category : ' tests' }
121- CTStackTest >> testIsEmpty [
124+ CTStackTest >> testDynamicStack [
122125
123- self assert: stack isEmpty.
126+ | testStack |
127+ testStack := CTStack new : 2 .
128+
129+ testStack
130+ push: ' a' ;
131+ push: ' b' .
132+ self assert: testStack capacity equals: 2 .
133+
134+ " Push one more - should double capacity"
135+ testStack push: ' c' .
136+ self assert: testStack capacity equals: 4 .
137+ self assert: testStack size equals: 3 .
138+ self assert: testStack top equals: ' c'
139+ ]
140+
141+ { #category : ' tests' }
142+ CTStackTest >> testErrorHandling [
143+
144+ self should: [ stack pop ] raise: Error .
145+ self should: [ stack top ] raise: Error .
146+
124147 stack push: ' test' .
125- self deny: stack isEmpty.
126148 stack pop.
127- self assert: stack isEmpty
149+
150+ self should: [ stack pop ] raise: Error .
151+ self should: [ stack top ] raise: Error
128152]
129153
130154{ #category : ' tests' }
131- CTStackTest >> testIsFull [
155+ CTStackTest >> testIsEmpty [
132156
133- self deny : stack isFull .
134- stack push: ' a ' ; push: ' b ' ; push: ' c ' ; push: ' d ' ; push: ' e ' .
135- self assert : stack isFull .
157+ self assert : stack isEmpty .
158+ stack push: ' test ' .
159+ self deny : stack isEmpty .
136160 stack pop.
137- self deny : stack isFull
161+ self assert : stack isEmpty
138162]
139163
140164{ #category : ' tests' }
@@ -167,13 +191,18 @@ CTStackTest >> testLargeCapacityStack [
167191{ #category : ' tests' }
168192CTStackTest >> testPopAllElements [
169193
170- stack push: ' a' ; push: ' b' ; push: ' c' .
171-
172- stack pop; pop; pop.
173-
194+ stack
195+ push: ' a' ;
196+ push: ' b' ;
197+ push: ' c' .
198+
199+ stack
200+ pop;
201+ pop;
202+ pop.
203+
174204 self assert: stack isEmpty.
175- self assert: stack size equals: 0 .
176- self assert: stack availableSpace equals: 5
205+ self assert: stack size equals: 0
177206]
178207
179208{ #category : ' tests' }
@@ -190,110 +219,71 @@ CTStackTest >> testPopMultipleElements [
190219{ #category : ' tests' }
191220CTStackTest >> testPopSingleElement [
192221
193- | element |
222+ | element |
194223 stack push: ' test' .
195224 element := stack pop.
196-
225+
197226 self assert: element equals: ' test' .
198227 self assert: stack isEmpty.
199- self assert: stack size equals: 0 .
200- self assert: stack availableSpace equals: 5
201- ]
202-
203- { #category : ' tests' }
204- CTStackTest >> testPushAllOverflow [
205-
206- stack push: ' existing' .
207-
208- self should: [ stack pushAll: #('a' 'b' 'c' 'd' 'e') ] raise: Error
228+ self assert: stack size equals: 0
209229]
210230
211231{ #category : ' tests' }
212- CTStackTest >> testPushAllWithArray [
232+ CTStackTest >> testPushAll [
213233
214- | result |
234+ | result |
215235 result := stack pushAll: #('a' 'b' 'c') .
216236
217237 self assert: stack size equals: 3 .
218- self assert: result equals: ' c' .
219- self assert: stack top equals: ' c '
238+ self assert: stack top equals: ' c' .
239+ self assert: result identicalTo: stack
220240]
221241
222242{ #category : ' tests' }
223243CTStackTest >> testPushAllWithEmptyCollection [
224244
225245 | result |
226246 result := stack pushAll: #() .
227-
228- self assert: stack size equals: 0 .
229- self assert: result isNil.
230- self assert: stack isEmpty
231- ]
232-
233- { #category : ' tests' }
234- CTStackTest >> testPushAllWithOrderedCollection [
235247
236- | collection result |
237- collection := OrderedCollection with: ' x' with: ' y' with: ' z' .
238- result := stack pushAll: collection.
239-
240- self assert: stack size equals: 3 .
241- self assert: result equals: ' z' .
242- self assert: stack top equals: ' z'
248+ self assert: stack isEmpty.
249+ self assert: result identicalTo: stack.
243250]
244251
245252{ #category : ' tests' }
246253CTStackTest >> testPushMultipleElements [
247254
248- stack push: ' first' ; push: ' second' ; push: ' third' .
249-
255+ stack
256+ push: ' first' ;
257+ push: ' second' ;
258+ push: ' third' .
259+
250260 self assert: stack size equals: 3 .
251- self assert: stack top equals: ' third' .
252- self assert: stack availableSpace equals: 2 .
253- self deny: stack isEmpty.
254- self deny: stack isFull
261+ self assert: stack top equals: ' third' .
262+ self deny: stack isEmpty
255263]
256264
257265{ #category : ' tests' }
258266CTStackTest >> testPushSingleElement [
259267
260268 stack push: ' first' .
261-
269+
262270 self assert: stack size equals: 1 .
263271 self deny: stack isEmpty.
264272 self deny: stack isFull.
265- self assert: stack top equals: ' first' .
266- self assert: stack availableSpace equals: 4
267- ]
268-
269- { #category : ' tests' }
270- CTStackTest >> testPushToCapacity [
271-
272- stack push: ' a' ; push: ' b' ; push: ' c' ; push: ' d' ; push: ' e' .
273-
274- self assert: stack size equals: 5 .
275- self assert: stack isFull.
276- self assert: stack availableSpace equals: 0 .
277- self assert: stack top equals: ' e'
278- ]
279-
280- { #category : ' tests' }
281- CTStackTest >> testPushToFullStack [
282-
283- stack push: ' a' ; push: ' b' ; push: ' c' ; push: ' d' ; push: ' e' .
284-
285- self should: [ stack push: ' overflow' ] raise: Error
273+ self assert: stack top equals: ' first'
286274]
287275
288276{ #category : ' removing' }
289277CTStackTest >> testRemoveAll [
290278
291- stack push: ' a' ; push: ' b' ; push: ' c' .
279+ stack
280+ push: ' a' ;
281+ push: ' b' ;
282+ push: ' c' .
292283 stack removeAll.
293-
284+
294285 self assert: stack isEmpty.
295- self assert: stack size equals: 0 .
296- self assert: stack availableSpace equals: 5
286+ self assert: stack size equals: 0
297287]
298288
299289{ #category : ' tests' }
@@ -337,24 +327,36 @@ CTStackTest >> testSize [
337327
338328{ #category : ' tests' }
339329CTStackTest >> testStackCreationWithCapacity [
340-
341- | testStack |
330+ | testStack |
342331 testStack := CTStack new : 3 .
343-
332+
344333 self assert: testStack capacity equals: 3 .
345334 self assert: testStack isEmpty.
346- self assert: testStack size equals: 0 .
347- self assert: testStack availableSpace equals: 3
335+ self assert: testStack size equals: 0
348336]
349337
350338{ #category : ' tests' }
351339CTStackTest >> testStackCreationWithInvalidCapacity [
352340
353- self should: [ CTStack new : 0 ] raise: Error .
354341 self should: [ CTStack new : - 1 ] raise: Error .
355342 self should: [ CTStack new : - 10 ] raise: Error
356343]
357344
345+ { #category : ' tests' }
346+ CTStackTest >> testStackCreationWithZeroCapacity [
347+
348+ | zeroStack |
349+ zeroStack := CTStack new : 0 .
350+
351+ self assert: zeroStack capacity equals: 0 .
352+ self assert: zeroStack isEmpty.
353+ self assert: zeroStack size equals: 0 .
354+
355+ zeroStack push: ' first' .
356+ self assert: zeroStack capacity equals: 10 .
357+ self assert: zeroStack size equals: 1
358+ ]
359+
358360{ #category : ' tests' }
359361CTStackTest >> testTopWithoutRemoving [
360362
0 commit comments