Created
September 25, 2008 14:30
-
-
Save Sixeight/12828 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mini/test' | |
Mini::Test.autorun | |
class StackOverflow < StandardError; end | |
class EmptyStackError < StandardError; end | |
class Stack | |
def initialize(max = 1.0/0.0) | |
@stack = [] | |
@max = max | |
end | |
def empty? | |
@stack.empty? | |
end | |
def push(object) | |
if @stack.size >= @max | |
raise StackOverflow | |
else | |
@stack << object | |
end | |
end | |
def pop | |
if @stack.size == 0 | |
raise EmptyStackError | |
else | |
@stack.pop | |
end | |
end | |
end | |
class TestStack < Mini::Test::TestCase | |
def setup | |
@stack = Stack.new | |
end | |
def test_stack_empty? | |
assert @stack.empty? | |
end | |
def test_stack_not_empty | |
@stack.push 1 | |
refute @stack.empty? | |
end | |
def test_push_1_then_pop_1 | |
@stack.push 1 | |
assert_equal 1, @stack.pop | |
end | |
def test_faiulere_pop_when_stack_is_empty | |
assert_raises EmptyStackError do | |
@stack.pop | |
end | |
end | |
def test_failurer_push_when_stack_is_max__0 | |
@stack = Stack.new(0) | |
assert_raises StackOverflow do | |
@stack.push 1 | |
end | |
end | |
def test_failurer_push_when_stack_is_max__100 | |
@stack = Stack.new(100) | |
100.times {|i| @stack.push i } | |
assert_raises StackOverflow do | |
@stack.push 1 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment