Higher Order Blog

home

Fibers: a solution

15 Aug 2008

Here I will post a solution to the Fibers exercise... Post your solutions as comments. Your code should at least pass the following simple test:
require 'fiber_stack'
require 'test/unit'

class FiberStackTest < Test::Unit::TestCase
      
  def test_simple
    fs = FiberStack.new
    assert fs.empty?
    fs.push 'a'
    fs.push 'b'
    assert_equal 2, fs.size
    
    assert_equal 'b', fs.pop
    assert_equal 1, fs.size
    assert_equal 'a', fs.pop
    assert fs.empty?
    assert_equal 0, fs.size
    
    assert_nil fs.pop
    assert fs.empty?
    assert_equal 0, fs.size
    
    fs.push 'c'
    assert_equal false, fs.empty?
    assert_equal 1, fs.size
    
    assert_equal 'c', fs.pop
    assert fs.empty?
    assert_equal 0, fs.size
  end
end

require 'test/unit/ui/console/testrunner'
Test::Unit::UI::Console::TestRunner.run(FiberStackTest)
with the following result:
krukow:~/Projects/private/Fiber/lib$ ruby run_stack.rb 
Loaded suite FiberStackTest
Started
.
Finished in 0.002572 seconds.

1 tests, 15 assertions, 0 failures, 0 errors
krukow:~/Projects/private/Fiber/lib$
Have fun ;-)