Skip to content

Commit

Permalink
Stack top and pop return nil if stack is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
pmatsinopoulos committed Feb 12, 2012
1 parent 5465485 commit c2e0776
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions models/stack.rb
Expand Up @@ -10,10 +10,12 @@ def push(element)
end

def pop
return nil if empty?
@stack.delete_at(@stack.size - 1)
end

def top
return nil if empty?
@stack[@stack.size - 1]
end

Expand Down
10 changes: 10 additions & 0 deletions test/unit/stack_test.rb
Expand Up @@ -25,6 +25,11 @@ def test_pop
assert_equal number_of_elements_after - 1, @stack.size
end

def test_pop_returns_nil_on_empty_stack
s = Stack.new
assert_nil s.pop
end

def test_top
number_of_elements_before = @stack.size
@stack.push(1)
Expand All @@ -33,6 +38,11 @@ def test_top
assert_equal 1, @stack.top
end

def test_top_returns_nil_on_empty_stack
s = Stack.new
assert_nil s.top
end

def test_size
assert @stack.empty?
@stack.push(1)
Expand Down

0 comments on commit c2e0776

Please sign in to comment.