1 | Error subclass: #EmptyStackError
|
---|
2 | instanceVariableNames: ''
|
---|
3 | classVariableNames: ''
|
---|
4 | poolDictionaries: ''
|
---|
5 | category: 'HowTo-SUnit'!
|
---|
6 |
|
---|
7 | |
---|
8 |
|
---|
9 | Object subclass: #MyElement
|
---|
10 | instanceVariableNames: 'item next'
|
---|
11 | classVariableNames: ''
|
---|
12 | poolDictionaries: ''
|
---|
13 | category: 'HowTo-SUnit'!
|
---|
14 |
|
---|
15 | !MyElement methodsFor: 'accessing' stamp: 'wm 10/26/2012 20:03:47.176'!
|
---|
16 | item
|
---|
17 | "Answer the value of item"
|
---|
18 |
|
---|
19 | ^ item! !
|
---|
20 |
|
---|
21 | !MyElement methodsFor: 'accessing' stamp: 'wm 10/26/2012 20:03:47.193'!
|
---|
22 | item: anObject
|
---|
23 | "Set the value of item"
|
---|
24 |
|
---|
25 | item := anObject! !
|
---|
26 |
|
---|
27 | !MyElement methodsFor: 'accessing' stamp: 'wm 10/26/2012 20:03:47.211'!
|
---|
28 | next
|
---|
29 | "Answer the value of next"
|
---|
30 |
|
---|
31 | ^ next! !
|
---|
32 |
|
---|
33 | !MyElement methodsFor: 'accessing' stamp: 'wm 10/26/2012 20:03:47.224'!
|
---|
34 | next: anObject
|
---|
35 | "Set the value of next"
|
---|
36 |
|
---|
37 | next := anObject! !
|
---|
38 |
|
---|
39 | "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
|
---|
40 |
|
---|
41 | MyElement class
|
---|
42 | instanceVariableNames: ''!
|
---|
43 |
|
---|
44 | !MyElement class methodsFor: 'as yet unclassified' stamp: 'wm 10/26/2012 20:04:59.441'!
|
---|
45 | newWith: anObject andNext: anotherObject
|
---|
46 | ^ self new
|
---|
47 | item: anObject;
|
---|
48 | next: anotherObject! !
|
---|
49 |
|
---|
50 | |
---|
51 |
|
---|
52 | Object subclass: #MyStack
|
---|
53 | instanceVariableNames: 'head'
|
---|
54 | classVariableNames: ''
|
---|
55 | poolDictionaries: ''
|
---|
56 | category: 'HowTo-SUnit'!
|
---|
57 |
|
---|
58 | !MyStack methodsFor: 'as yet unclassified' stamp: 'wm 10/26/2012 19:53:42.303'!
|
---|
59 | isEmpty
|
---|
60 | ^ self head isNil! !
|
---|
61 |
|
---|
62 | !MyStack methodsFor: 'as yet unclassified' stamp: 'wm 10/26/2012 20:08:25.072'!
|
---|
63 | pop
|
---|
64 | | result |
|
---|
65 | self isEmpty ifTrue: [EmptyStackError signal: 'Cannot pop from empty stack'].
|
---|
66 | result := self head item.
|
---|
67 | self head: self head next.
|
---|
68 | ^ result
|
---|
69 | ! !
|
---|
70 |
|
---|
71 | !MyStack methodsFor: 'as yet unclassified' stamp: 'wm 10/26/2012 20:03:06.602'!
|
---|
72 | push: anObject
|
---|
73 | self head: (MyElement newWith: anObject andNext: self head)! !
|
---|
74 |
|
---|
75 |
|
---|
76 | !MyStack methodsFor: 'accessing' stamp: 'wm 10/26/2012 19:54:23.282'!
|
---|
77 | head
|
---|
78 | "Answer the value of head"
|
---|
79 |
|
---|
80 | ^ head! !
|
---|
81 |
|
---|
82 | !MyStack methodsFor: 'accessing' stamp: 'wm 10/26/2012 19:54:23.295'!
|
---|
83 | head: anObject
|
---|
84 | "Set the value of head"
|
---|
85 |
|
---|
86 | head := anObject! !
|
---|
87 |
|
---|
88 | |
---|
89 |
|
---|
90 | TestCase subclass: #MyStackTest
|
---|
91 | instanceVariableNames: 'stack'
|
---|
92 | classVariableNames: ''
|
---|
93 | poolDictionaries: ''
|
---|
94 | category: 'HowTo-SUnit'!
|
---|
95 |
|
---|
96 | !MyStackTest methodsFor: 'accessing' stamp: 'wm 10/28/2012 17:36:19.845'!
|
---|
97 | stack
|
---|
98 | ^ stack! !
|
---|
99 |
|
---|
100 | !MyStackTest methodsFor: 'accessing' stamp: 'wm 10/28/2012 17:36:15.529'!
|
---|
101 | stack: anObject
|
---|
102 | stack := anObject! !
|
---|
103 |
|
---|
104 |
|
---|
105 | !MyStackTest methodsFor: 'testing' stamp: 'wm 10/28/2012 17:35:24.569'!
|
---|
106 | setUp
|
---|
107 | self stack: MyStack new! !
|
---|
108 |
|
---|
109 | !MyStackTest methodsFor: 'testing' stamp: 'wm 10/26/2012 19:52:56.721'!
|
---|
110 | testEmptyStackIsEmpty
|
---|
111 | self assert: MyStack new isEmpty ! !
|
---|
112 |
|
---|
113 | !MyStackTest methodsFor: 'testing' stamp: 'wm 10/26/2012 20:11:29.244'!
|
---|
114 | testNonEmptyStackIsNotEmpty
|
---|
115 | self stack push: Object new.
|
---|
116 | self deny: self stack isEmpty ! !
|
---|
117 |
|
---|
118 | !MyStackTest methodsFor: 'testing' stamp: 'wm 10/26/2012 19:55:08.976'!
|
---|
119 | testPopEmptyStackThrowsAnError
|
---|
120 | self should: [MyStack new pop] raise: EmptyStackError! !
|
---|
121 |
|
---|
122 | !MyStackTest methodsFor: 'testing' stamp: 'wm 10/26/2012 20:15:16.304'!
|
---|
123 | testPushPrependsElementPopRemovesIt
|
---|
124 | | o |
|
---|
125 | o := Object new.
|
---|
126 | self assert: (self stack push: o) pop equals: o.
|
---|
127 | self assert: self stack isEmpty ! !
|
---|