mootoh (owner)

Revisions

  • 60aa19 Sun Nov 01 01:38:23 -0700 2009
gist: 223438 Download_button fork
public
Public Clone URL: git://gist.github.com/223438.git
Embed All Files: show embed
stack.rb #
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
class Stack
   def initialize(n = 8)
      @s = Array.new(n, nil)
      @top = -1
   end
                                                                    
   def empty?
      @top == -1
   end
                                                                    
   def push(x)
      raise 'overflow' if @top == @s.size-1
      @top += 1
      @s[@top] = x
   end
end
                                                                    
require 'test/unit'
                                                                    
class StackTest < Test::Unit::TestCase
   def setup
      @stack = Stack.new
   end
                                                                    
   def test_empty
      assert(@stack.empty?)
                                                                    
      @stack.push 3
                                                                    
      assert(! @stack.empty?)
   end
                                                                    
   def test_push
      8.times {|x| @stack.push x}
      assert_raise(RuntimeError) {
         @stack.push 'illegal'
      }
   end
end