Sha256: bf8e1f77f4fdc7498b653c08183c619d98852f8809ef2e2c6b29253d5766ea9a

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

require 'test_helper'
require 'test/unit'
require 'grizzled/stack'

include Grizzled

class StackTestDriver < Test::Unit::TestCase

  def test_correct_class
    assert_equal(Grizzled::Stack, Stack.new.class)
  end

  def test_underflow_no_exception
    stack = Stack.new
    assert_equal(nil, stack.pop)
  end

  def test_underflow_exception
    stack = Stack.new(false)
    assert_raise(StackUnderflowException) {stack.pop}
  end

  def test_push
    stack = Stack.new.push(5)
    assert_equal([5], stack.to_a)
    stack.push(10)
    assert_equal([10, 5], stack.to_a)
    stack.push(10)
    assert_equal([10, 10, 5], stack.to_a)
    stack.push([1, 2, 3])
    assert_equal([3, 2, 1, 10, 10, 5], stack.to_a)
  end

  def test_length
    stack = Stack.new.push(5)
    assert_equal(1, stack.length)

    stack.push(5)
    assert_equal(2, stack.length)

    stack.push((1..10).to_a)
    assert_equal(12, stack.length)

    stack.pop
    assert_equal(11, stack.length)

    stack.pop until stack.is_empty?
    assert_equal(0, stack.length)
  end

  def test_clear
    stack = Stack.new.push((1..10).to_a)
    assert_equal(10, stack.length)
    stack.clear
    assert_equal(0, stack.length)
  end

  def test_pop_all
    a = (1..10).to_a
    stack = Stack.new.push(a)
    assert_equal(10, stack.length)
    a2 = stack.pop_all
    assert_equal(0, stack.length)
    assert_equal(a2, a.reverse)
  end

  def test_immutable
    # Ensure that the to_a method doesn't return a stack that's
    stack = Stack.new.push(5).push(10)
    a = stack.to_a
    assert_equal([10, 5], a)
    a.pop
    assert_equal([10, 5], stack.to_a)
  end

  def test_enumerable
    stack = Stack.new.push([1, 2, 3])
    assert_equal([3, 2, 1], stack.to_a)
    a = []
    stack.each {|element| a << element}
    assert_equal(stack.to_a, a)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
grizzled-ruby-0.1.4 test/tc_stack.rb
grizzled-ruby-0.1.3 test/tc_stack.rb