require 'test/unit'
class JollyJumper
attr_reader :values
def initialize
@values = []
end
def insert_value(value)
@values << value
end
def print_values
@values.each{ |value| print value.to_s + ' ' }
end
def calculate
result = true
@values.each_index do |index|
first_value = (@values[index -1] - @values[index]).abs if @values[index-1] != nil
second_value = (@values[index +1] - @values[index]).abs if @values[index+1] != nil
result &= values.member?(first_value) if first_value != nil
result &= values.member?(second_value) if second_value != nil
end
result
end
endclass JollyJumperTest < Test::Unit::TestCase
def test_case1
jumper = JollyJumper.new
assert_not_equal(nil, jumper)
jumper.insert_value(4)
jumper.insert_value(1)
jumper.insert_value(4)
jumper.insert_value(2)
jumper.insert_value(3)
jumper.print_values()
assert_equal(true, jumper.calculate)
end
def test_case2
jumper = JollyJumper.new
assert_not_equal(nil, jumper)
jumper.insert_value(5)
jumper.insert_value(1)
jumper.insert_value(4)
jumper.insert_value(2)
jumper.insert_value(-1)
jumper.insert_value(6)
jumper.print_values()
assert_equal(false, jumper.calculate)
end
end