fOOrth/tests/interpreter/data_stack_tests.rb

109 lines
2.2 KiB
Ruby
Raw Normal View History

2014-09-12 00:35:51 +02:00
# coding: utf-8
require_relative '../../lib/fOOrth/interpreter'
require 'minitest/autorun'
2014-12-20 01:17:23 +01:00
require 'minitest_visible'
2014-09-12 00:35:51 +02:00
#Test the monkey patches applied to the Object class.
class DataStackMapTester < MiniTest::Unit::TestCase
2014-09-12 00:35:51 +02:00
2014-12-20 01:17:23 +01:00
#Track mini-test progress.
MinitestVisible.track self, __FILE__
2014-09-12 00:35:51 +02:00
def test_data_stack_ops
vm = Thread.current[:vm]
2014-09-12 00:35:51 +02:00
refute(vm == nil)
vm.interpreter_reset
assert_equal(vm.data_stack, [])
vm.push(4)
assert_equal(vm.data_stack, [4])
vm.push(8)
assert_equal(vm.data_stack, [4, 8])
assert_equal(vm.peek, 8)
assert_equal(vm.peek(1), 8)
assert_equal(vm.peek(2), 4)
assert_equal(vm.pop, 8)
assert_equal(vm.data_stack, [4])
assert_equal(vm.pop, 4)
assert_equal(vm.data_stack, [])
vm.push(8)
assert_equal(vm.data_stack, [8])
vm.pushm([1,2,3])
assert_equal(vm.data_stack, [8, 1, 2, 3])
2014-09-12 00:35:51 +02:00
end
def test_boolean_stack_data
vm = Thread.current[:vm]
2014-09-12 00:35:51 +02:00
refute(vm == nil)
vm.interpreter_reset
vm.push(16)
assert(vm.pop?)
vm.push(true)
assert(vm.pop?)
vm.push('test')
assert(vm.pop?)
vm.push(0)
assert(vm.pop?)
2014-09-12 00:35:51 +02:00
vm.push(false)
refute(vm.pop?)
vm.push('')
assert(vm.pop?)
2014-09-12 00:35:51 +02:00
vm.push(nil)
refute(vm.pop?)
end
def test_pop_multiple
vm = Thread.current[:vm]
2014-09-12 00:35:51 +02:00
refute(vm == nil)
vm.interpreter_reset
vm.push(16)
vm.push(true)
vm.push('test')
vm.push(0)
vm.push('')
vm.push(nil)
assert_equal(vm.data_stack, [16, true, 'test', 0, '', nil])
assert_equal(vm.popm(3), [0, '', nil])
assert_equal(vm.popm(3), [16, true, 'test'])
assert_equal(vm.data_stack, [])
end
def test_poke
vm = Thread.current[:vm]
refute(vm == nil)
vm.interpreter_reset
vm.push(42)
vm.poke("hello")
assert_equal(vm.data_stack, ["hello"])
end
2014-09-22 06:04:16 +02:00
def test_swap_pop
vm = Thread.current[:vm]
2014-09-22 06:04:16 +02:00
refute(vm == nil)
vm.interpreter_reset
vm.push(16)
vm.push(99)
assert_equal(vm.swap_pop, 16)
assert_equal(vm.pop, 99)
assert_equal(vm.data_stack, [])
end
2014-09-12 00:35:51 +02:00
end