Skip to content

Commit 0528553

Browse files
authored
Merge pull request #10 from HossamSaberr/master
Implement growable stack and max-depth tracking (#8)
2 parents 0e21e4d + 472aa64 commit 0528553

File tree

4 files changed

+268
-0
lines changed

4 files changed

+268
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
Class {
2+
#name : 'CTGrowableStackTest',
3+
#superclass : 'TestCase',
4+
#instVars : [
5+
'stack'
6+
],
7+
#category : 'Containers-Stack-Tests',
8+
#package : 'Containers-Stack-Tests'
9+
}
10+
11+
{ #category : 'running' }
12+
CTGrowableStackTest >> setUp [
13+
super setUp.
14+
stack := CTGrowableStack new
15+
]
16+
17+
{ #category : 'tests' }
18+
CTGrowableStackTest >> testExplicitCapacityDoubling [
19+
20+
| internalCollection |
21+
22+
stack := CTGrowableStack new.
23+
internalCollection := OrderedCollection new: 4.
24+
stack instVarNamed: 'elements' put: internalCollection.
25+
26+
1 to: 4 do: [ :i | stack push: i ].
27+
self assert: internalCollection capacity equals: 4.
28+
29+
stack push: 5.
30+
31+
self assert: (stack instVarNamed: 'elements') capacity equals: 8.
32+
self assert: stack size equals: 5.
33+
]
34+
35+
{ #category : 'tests' }
36+
CTGrowableStackTest >> testInternalCapacityDoubling [
37+
38+
| internalCollection |
39+
40+
internalCollection := stack instVarNamed: 'elements'.
41+
42+
1 to: 100 do: [ :i | stack push: i ].
43+
44+
self assert: stack size equals: 100.
45+
self assert: internalCollection capacity >= 100.
46+
]
47+
48+
{ #category : 'tests' }
49+
CTGrowableStackTest >> testInternalGrowth [
50+
51+
1 to: 20 do: [ :i | stack push: i ].
52+
53+
self assert: stack size equals: 20.
54+
self assert: stack top equals: 20.
55+
]
56+
57+
{ #category : 'tests' }
58+
CTGrowableStackTest >> testIsEmpty [
59+
self assert: stack isEmpty.
60+
61+
stack push: 'x'.
62+
self deny: stack isEmpty.
63+
64+
stack pop.
65+
self assert: stack isEmpty.
66+
]
67+
68+
{ #category : 'tests' }
69+
CTGrowableStackTest >> testPop [
70+
stack push: 'first'.
71+
stack push: 'second'.
72+
73+
self assert: stack pop equals: 'second'.
74+
self assert: stack size equals: 1.
75+
self assert: stack pop equals: 'first'.
76+
self assert: stack isEmpty.
77+
]
78+
79+
{ #category : 'tests' }
80+
CTGrowableStackTest >> testPush [
81+
stack push: 'a'.
82+
83+
self assert: stack size equals: 1.
84+
self deny: stack isEmpty.
85+
self assert: stack top equals: 'a'.
86+
]
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
Class {
2+
#name : 'CTTrackedGrowableStackTest',
3+
#superclass : 'TestCase',
4+
#instVars : [
5+
'stack'
6+
],
7+
#category : 'Containers-Stack-Tests',
8+
#package : 'Containers-Stack-Tests'
9+
}
10+
11+
{ #category : 'running' }
12+
CTTrackedGrowableStackTest >> setUp [
13+
super setUp.
14+
stack := CTTrackedGrowableStack new
15+
]
16+
17+
{ #category : 'tests' }
18+
CTTrackedGrowableStackTest >> testErrorHandlingDoesNotAffectMaxDepth [
19+
self should: [ stack pop ] raise: Error.
20+
self assert: stack maxDepth equals: 0.
21+
22+
stack push: 'temp'.
23+
stack pop.
24+
25+
self should: [ stack pop ] raise: Error.
26+
self assert: stack maxDepth equals: 1.
27+
]
28+
29+
{ #category : 'tests' }
30+
CTTrackedGrowableStackTest >> testInitialMaxDepth [
31+
self assert: stack maxDepth equals: 0.
32+
self assert: stack isEmpty.
33+
]
34+
35+
{ #category : 'tests' }
36+
CTTrackedGrowableStackTest >> testMaxDepthAfterPop [
37+
stack push: 'apple'.
38+
stack push: 'banana'.
39+
self assert: stack maxDepth equals: 2.
40+
41+
stack pop.
42+
43+
self assert: stack size equals: 1.
44+
self assert: stack maxDepth equals: 2.
45+
]
46+
47+
{ #category : 'tests' }
48+
CTTrackedGrowableStackTest >> testMaxDepthDuringInternalGrowth [
49+
50+
1 to: 50 do: [ :i | stack push: i ].
51+
self assert: stack maxDepth equals: 50.
52+
self assert: stack size equals: 50.
53+
54+
1 to: 25 do: [ :i | stack pop ].
55+
self assert: stack maxDepth equals: 50.
56+
self assert: stack size equals: 25.
57+
]
58+
59+
{ #category : 'tests' }
60+
CTTrackedGrowableStackTest >> testMaxDepthWithExplicitInternalGrowth [
61+
62+
| internalCollection |
63+
stack := CTTrackedGrowableStack new.
64+
internalCollection := OrderedCollection new: 4.
65+
stack instVarNamed: 'elements' put: internalCollection.
66+
67+
1 to: 4 do: [ :i | stack push: i ].
68+
self assert: stack maxDepth equals: 4.
69+
self assert: internalCollection capacity equals: 4.
70+
71+
stack push: 5.
72+
73+
self assert: stack maxDepth equals: 5.
74+
self assert: (stack instVarNamed: 'elements') capacity equals: 8.
75+
]
76+
77+
{ #category : 'tests' }
78+
CTTrackedGrowableStackTest >> testMaxDepthWithFluctuations [
79+
stack push: 1.
80+
stack push: 2.
81+
stack pop.
82+
stack pop.
83+
84+
stack push: 3.
85+
stack push: 4.
86+
stack push: 5.
87+
88+
self assert: stack maxDepth equals: 3.
89+
]
90+
91+
{ #category : 'tests' }
92+
CTTrackedGrowableStackTest >> testMaxDepthWithLargeDataset [
93+
1 to: 100 do: [ :i | stack push: i ].
94+
95+
self assert: stack maxDepth equals: 100.
96+
self assert: stack size equals: 100.
97+
98+
1 to: 50 do: [ :i | stack pop ].
99+
100+
self assert: stack maxDepth equals: 100.
101+
self assert: stack size equals: 50.
102+
]
103+
104+
{ #category : 'tests' }
105+
CTTrackedGrowableStackTest >> testTopDoesNotAffectMaxDepth [
106+
stack push: 'hello'.
107+
self assert: stack maxDepth equals: 1.
108+
109+
self assert: stack top equals: 'hello'.
110+
self assert: stack maxDepth equals: 1.
111+
self assert: stack size equals: 1.
112+
]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Class {
2+
#name : 'CTGrowableStack',
3+
#superclass : 'Object',
4+
#instVars : [
5+
'elements'
6+
],
7+
#category : 'Containers-Stack',
8+
#package : 'Containers-Stack'
9+
}
10+
11+
{ #category : 'initialization' }
12+
CTGrowableStack >> initialize [
13+
super initialize.
14+
elements := OrderedCollection new
15+
]
16+
17+
{ #category : 'testing' }
18+
CTGrowableStack >> isEmpty [
19+
^ elements isEmpty
20+
]
21+
22+
{ #category : 'removing' }
23+
CTGrowableStack >> pop [
24+
self isEmpty ifTrue: [ self error: 'Stack is empty' ].
25+
^ elements removeLast
26+
]
27+
28+
{ #category : 'adding' }
29+
CTGrowableStack >> push: anObject [
30+
elements addLast: anObject.
31+
^ self
32+
]
33+
34+
{ #category : 'accessing' }
35+
CTGrowableStack >> size [
36+
^ elements size
37+
]
38+
39+
{ #category : 'accessing' }
40+
CTGrowableStack >> top [
41+
self isEmpty ifTrue: [ self error: 'Stack is empty' ].
42+
^ elements last
43+
]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Class {
2+
#name : 'CTTrackedGrowableStack',
3+
#superclass : 'CTGrowableStack',
4+
#instVars : [
5+
'maxDepth'
6+
],
7+
#category : 'Containers-Stack',
8+
#package : 'Containers-Stack'
9+
}
10+
11+
{ #category : 'initialization' }
12+
CTTrackedGrowableStack >> initialize [
13+
super initialize.
14+
maxDepth := 0
15+
]
16+
17+
{ #category : 'accessing' }
18+
CTTrackedGrowableStack >> maxDepth [
19+
^ maxDepth
20+
]
21+
22+
{ #category : 'adding' }
23+
CTTrackedGrowableStack >> push: anObject [
24+
super push: anObject.
25+
maxDepth := maxDepth max: self size.
26+
^ self
27+
]

0 commit comments

Comments
 (0)