forked from ShehrozIrfan/ruby-rocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack-datastructure
More file actions
133 lines (110 loc) · 3.1 KB
/
stack-datastructure
File metadata and controls
133 lines (110 loc) · 3.1 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require 'forwardable'
class Stack
OverflowError = Class.new StandardError
UnderflowError = Class.new StandardError
extend Forwardable
def_delegators :@ary, :empty?, :size
attr_reader :max_size
def initialize(opts={})
opts = {max_size: 1000, initial_content: []}.merge(opts)
@ary = []
@max_size = opts[:max_size]
opts[:initial_content].each do |elem|
push elem
end
end
def push(item)
fail OverflowError if size >= max_size
@ary.push item
end
def pop
fail UnderflowError if empty?
@ary.pop
end
def peek
fail UnderflowError if empty?
@ary.last
end
end
if __FILE__ == $PROGRAM_NAME
require 'minitest/autorun'
describe Stack do
describe 'initialization' do
it 'is empty by default' do
Stack.new.must_be_empty
end
it 'has default max_size of 1000' do
Stack.new.max_size.must_equal 1000
end
it 'allows setting max_size manually' do
[10, 100, 123, 4562, 42].each do |size|
Stack.new(max_size: size).max_size.must_equal size
end
end
it 'is still empty when created with custom size' do
Stack.new(max_size: 3456).must_be_empty
end
it 'allows setting initial content' do
stack = Stack.new(initial_content: [1, 2, 3])
stack.pop.must_equal 3
stack.pop.must_equal 2
stack.pop.must_equal 1
end
it 'raises Stack::OverflowError when too much elements given' do
-> { Stack.new(max_size: 3, initial_content: [1, 2, 3, 4]) }
.must_raise Stack::OverflowError
end
end
describe '#push' do
it 'adds element on the top of the stack' do
stack = Stack.new
stack.push 1
stack.peek.must_equal 1
end
it 'raises Stack::OverflowError when reaches max stack size' do
stack = Stack.new(max_size: 5)
-> { 6.times { |i| stack.push i} }
.must_raise Stack::OverflowError
end
end
describe '#pop' do
it 'removes element from the top of the stack' do
stack = Stack.new
stack.push :a
stack.push :b
stack.size.must_equal 2
stack.pop
stack.size.must_equal 1
stack.pop
stack.size.must_equal 0
end
it 'returns removed element' do
stack = Stack.new
stack.push 1
stack.push 2
stack.pop.must_equal 2
stack.pop.must_equal 1
end
it 'raises Stack::UnderflowError when applied to empty stack' do
-> { Stack.new.pop }.must_raise Stack::UnderflowError
end
end
describe '#peek' do
it 'returns element on the top of the stack' do
Stack.new(initial_content: [1, 2, 3]).peek.must_equal 3
end
it 'does not modify the stack' do
stack = Stack.new
stack.push 0
stack.peek.must_equal 0
stack.push 1
stack.peek.must_equal 1
stack.pop
stack.peek.must_equal 0
end
it 'raises Stack::UnderflowError when applied to empty stack' do
-> { Stack.new.peek }.must_raise Stack::UnderflowError
end
end
end
end