-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_interpreter.rb
71 lines (58 loc) · 1.97 KB
/
test_interpreter.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'test/unit'
require 'interpreter'
require 'ruby-debug'
class MultiStack
module Operations
module_function # like 'private', but creates singleton-like method calling
def add(a,b); puts "#{a} + #{b} = #{a+b}"; a + b end
def subtract(a,b); puts "#{a} - #{b} = #{a+b}"; a - b end
end
end
class Interpreter
# def solve
# end
def test_run
@sequence.reverse.each do |arg|
append_to_stack(arg)
end
end
end
class TestCaseInterpreter < Test::Unit::TestCase
def test_program_stacks_ints
interpreter = Interpreter.new(:sequence=>[1,2,3,4])
assert_equal [4,3,2,1], interpreter.stack[Fixnum]
end
def test_program_adds_ints
interpreter = Interpreter.new(:sequence=>[1,2,"add",3,4])
assert_equal [7,2,1], interpreter.stack[Fixnum]
end
def test_program_ignores_operations_when_insufficent_args
interpreter = Interpreter.new(:sequence=>[1,2,"add"])
assert_equal [2,1], interpreter.stack[Fixnum]
end
def test_program_should_fail_when_args_not_defined_in_operations_module
assert_raises NameError do
interpreter = Interpreter.new(:sequence=>[1,2,"not_a_valid_operation"])
end
end
def test_program_accepts_procs
interpreter = Interpreter.new(:sequence=>[1,2, Proc.new{3},4,5])
assert_equal [5,4,3,2,1], interpreter.stack[Fixnum]
end
# test utilities
def test_segment_method_works
interpreter = Interpreter.new(:sequence=>[1,2,"add",3,4])
assert_equal [1,2,"add"], interpreter.seg(0,2)
assert_equal [3,4], interpreter.seg(2,-1)
assert_equal [[1,2,"add"], [3,4]], interpreter.seg(2)
end
# test solution appearance
def test_program_solves
interpreter = Interpreter.new(:sequence=>[1,2, Proc.new{3},4,5])
assert_equal 1, interpreter.solve # default #solve is the last Fixnum
end
# test long-term evolutionary pressures
# a pressure to make it shorter-and-shorter
# VS
# a specific function to make it shorter and shorter
end