@@ -28,6 +28,28 @@ CTStackTest >> testAlternatingPushPop [
2828 self assert: stack size equals: 2
2929]
3030
31+ { #category : ' tests' }
32+ CTStackTest >> testAsArray [
33+
34+ | result |
35+ stack push: ' first' ; push: ' second' ; push: ' third' .
36+ result := stack asArray.
37+
38+ self assert: result equals: #('third' 'second' 'first') .
39+ " Elements from top to bottom"
40+ ]
41+
42+ { #category : ' tests' }
43+ CTStackTest >> testAsOrderedCollection [
44+
45+ | result |
46+ stack push: ' a' ; push: ' b' ; push: ' c' .
47+ result := stack asOrderedCollection.
48+
49+ self assert: result class equals: OrderedCollection .
50+ self assert: result asArray equals: #('c' 'b' 'a') .
51+ ]
52+
3153{ #category : ' tests' }
3254CTStackTest >> testAvailableSpace [
3355
@@ -48,6 +70,29 @@ CTStackTest >> testCapacity [
4870 self assert: stack capacity equals: 5 . " Capacity doesn't change"
4971]
5072
73+ { #category : ' tests' }
74+ CTStackTest >> testCopyCreatesNewObject [
75+
76+ | copy |
77+ stack
78+ push: ' a' ;
79+ push: ' b' .
80+ copy := stack copy.
81+ self deny: stack identicalTo: copy
82+ ]
83+
84+ { #category : ' tests' }
85+ CTStackTest >> testCopyHasSameContents [
86+
87+ | copy |
88+ stack push: ' first' ; push: ' second' ; push: ' third' .
89+ copy := stack copy.
90+
91+ self assert: copy size equals: stack size.
92+ self assert: copy top equals: stack top.
93+ self assert: copy capacity equals: stack capacity
94+ ]
95+
5196{ #category : ' tests' }
5297CTStackTest >> testDefaultStackCreation [
5398
@@ -60,6 +105,18 @@ CTStackTest >> testDefaultStackCreation [
60105 self assert: defaultStack availableSpace equals: 10
61106]
62107
108+ { #category : ' tests' }
109+ CTStackTest >> testDoIteration [
110+
111+ | elements |
112+ stack push: ' first' ; push: ' second' ; push: ' third' .
113+ elements := OrderedCollection new .
114+
115+ stack do: [ :each | elements add: each ].
116+
117+ self assert: elements asArray equals: #('third' 'second' 'first')
118+ ]
119+
63120{ #category : ' tests' }
64121CTStackTest >> testIsEmpty [
65122
@@ -80,6 +137,45 @@ CTStackTest >> testIsFull [
80137 self deny: stack isFull
81138]
82139
140+ { #category : ' tests' }
141+ CTStackTest >> testLIFOOrdering [
142+
143+ stack push: ' first' ; push: ' second' ; push: ' third' ; push: ' fourth' .
144+
145+ self assert: stack pop equals: ' fourth' .
146+ self assert: stack pop equals: ' third' .
147+ self assert: stack pop equals: ' second' .
148+ self assert: stack pop equals: ' first'
149+ ]
150+
151+ { #category : ' tests' }
152+ CTStackTest >> testLargeCapacityStack [
153+
154+ | largeStack |
155+ largeStack := CTStack new : 1000000 .
156+
157+ 1 to: 1000000 do: [ :i | largeStack push: i ].
158+ self assert: largeStack isFull.
159+ self assert: largeStack top equals: 1000000 .
160+
161+ 1000000 to: 1 by: - 1 do: [ :i |
162+ self assert: largeStack pop equals: i
163+ ].
164+ self assert: largeStack isEmpty
165+ ]
166+
167+ { #category : ' tests' }
168+ CTStackTest >> testPopAllElements [
169+
170+ stack push: ' a' ; push: ' b' ; push: ' c' .
171+
172+ stack pop; pop; pop.
173+
174+ self assert: stack isEmpty.
175+ self assert: stack size equals: 0 .
176+ self assert: stack availableSpace equals: 5
177+ ]
178+
83179{ #category : ' tests' }
84180CTStackTest >> testPopMultipleElements [
85181
@@ -189,6 +285,44 @@ CTStackTest >> testPushToFullStack [
189285 self should: [ stack push: ' overflow' ] raise: Error
190286]
191287
288+ { #category : ' removing' }
289+ CTStackTest >> testRemoveAll [
290+
291+ stack push: ' a' ; push: ' b' ; push: ' c' .
292+ stack removeAll.
293+
294+ self assert: stack isEmpty.
295+ self assert: stack size equals: 0 .
296+ self assert: stack availableSpace equals: 5
297+ ]
298+
299+ { #category : ' tests' }
300+ CTStackTest >> testRemoveAllOnEmptyStack [
301+
302+ stack removeAll.
303+
304+ self assert: stack isEmpty.
305+ self assert: stack size equals: 0
306+ ]
307+
308+ { #category : ' tests' }
309+ CTStackTest >> testSearchExistingElement [
310+
311+ stack push: ' a' ; push: ' b' ; push: ' c' ; push: ' b' .
312+
313+ self assert: (stack search: ' b' ) equals: 1 . " Top element"
314+ self assert: (stack search: ' c' ) equals: 2 .
315+ self assert: (stack search: ' a' ) equals: 4 . " Bottom element"
316+ ]
317+
318+ { #category : ' tests' }
319+ CTStackTest >> testSearchNonExistentElement [
320+
321+ stack push: ' a' ; push: ' b' ; push: ' c' .
322+
323+ self assert: (stack search: ' x' ) equals: - 1
324+ ]
325+
192326{ #category : ' tests' }
193327CTStackTest >> testSize [
194328
0 commit comments