-
Notifications
You must be signed in to change notification settings - Fork 0
/
crystal_and_amos.rb
99 lines (78 loc) · 1.73 KB
/
crystal_and_amos.rb
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
class Lifo
EmptyError = Class.new(Exception)
Node = Struct.new(:value, :next)
attr_accessor :size, :top
def initialize
self.size = 0
self.top = nil
end
def empty?
size.zero?
end
def push(element)
self.size = size + 1
self.top = Node.new(element, top)
end
def pop
raise EmptyError if empty?
self.size = size - 1
temp_value = top.value
self.top = top.next
temp_value
end
def peek
raise EmptyError if empty?
top.value
end
end
describe Lifo do
describe "that is new" do
it { is_expected.to be_empty }
specify { expect(subject.size).to be_zero }
specify do
expect { subject.push :element }.to change{ subject.size }.by(1)
end
specify do
subject.push :element
expect(subject).to_not be_empty
end
specify do
expect{ subject.pop }.to raise_error(Lifo::EmptyError)
end
specify do
expect{ subject.peek }.to raise_error(Lifo::EmptyError)
end
end
describe "that has one element" do
before do
subject.push :element
end
specify do
expect{ subject.pop }.to change{ subject.size }.by(-1)
end
specify do
expect(subject.pop).to eql(:element)
end
specify do
expect(subject.peek).to eql(:element)
end
end
describe "that has two elements" do
before do
subject.push :first_element
subject.push :second_element
end
specify do
expect(subject.pop).to eql(:second_element)
end
specify do
subject.pop
expect(subject.pop).to eql(:first_element)
end
end
end
__END__
push = add something to the stack
pop = take something off the stack
empty? = tells us if is empty
size = tells us how many items are in the stack