2021-12-07 16:51:33 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
require_relative '../language'
|
|
|
|
|
|
|
|
class TestLanguageProgram < Test::Unit::TestCase
|
|
|
|
def test_sto
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '« 2 dup * » \'quatre\' sto'
|
|
|
|
assert_empty lang.stack
|
2021-12-08 12:46:57 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.run 'quatre'
|
2021-12-08 12:46:57 +01:00
|
|
|
assert_equal [{ value: 4, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-08 12:46:57 +01:00
|
|
|
end
|
|
|
|
|
2022-02-02 15:20:24 +01:00
|
|
|
def test_lsto
|
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run "« 2 'deux' lsto deux dup * » eval 'deux' rcl"
|
|
|
|
|
|
|
|
assert_empty lang.dictionary.local_vars_layers
|
|
|
|
assert_equal [{ value: 4, type: :numeric, base: 10 }],
|
|
|
|
lang.stack
|
|
|
|
|
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run "« 2 'deux' lsto « 3 'trois' lsto trois drop » eval deux dup * » eval 'deux' rcl 'trois' rcl"
|
|
|
|
|
|
|
|
assert_empty lang.dictionary.local_vars_layers
|
|
|
|
assert_equal [{ value: 4, type: :numeric, base: 10 }],
|
|
|
|
lang.stack
|
|
|
|
end
|
|
|
|
|
2021-12-08 12:46:57 +01:00
|
|
|
def test_rcl
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '« 2 dup * » \'quatre\' sto \'quatre\' rcl'
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: '2 dup *', type: :program }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-08 12:46:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_purge
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '« 2 dup * » \'quatre\' sto \'quatre\' purge'
|
2021-12-08 16:38:46 +01:00
|
|
|
assert_nil lang.dictionary.lookup( 'quatre' )
|
2021-12-08 12:46:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_vars
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '« 2 dup * » \'quatre\' sto 1 \'un\' sto vars'
|
2022-02-08 15:45:36 +01:00
|
|
|
assert_equal [{ value: [{ type: :name, value: 'quatre' },
|
|
|
|
{ type: :name, value: 'un' }], type: :list }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-08 12:46:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_clusr
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '« 2 dup * » \'quatre\' sto 1 \'un\' sto clusr'
|
|
|
|
assert_empty lang.dictionary.vars
|
2021-12-08 12:46:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sto_add
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 \'test\' sto \'test\' 3 sto+ \'test\' rcl'
|
|
|
|
assert_equal [{ value: 4, type: :numeric, base: 10 }],
|
|
|
|
lang.stack
|
|
|
|
|
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 \'test\' sto 3 \'test\' sto+ \'test\' rcl'
|
2021-12-08 12:46:57 +01:00
|
|
|
assert_equal [{ value: 4, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-08 12:46:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sto_subtract
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 \'test\' sto \'test\' 3 sto- \'test\' rcl'
|
|
|
|
assert_equal [{ value: -2, type: :numeric, base: 10 }],
|
|
|
|
lang.stack
|
|
|
|
|
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 \'test\' sto 3 \'test\' sto- \'test\' rcl'
|
2021-12-08 12:46:57 +01:00
|
|
|
assert_equal [{ value: -2, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-08 12:46:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sto_multiply
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '2 \'test\' sto \'test\' 3 sto* \'test\' rcl'
|
2021-12-08 12:46:57 +01:00
|
|
|
assert_equal [{ value: 6, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
|
|
|
|
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '2 \'test\' sto 3 \'test\' sto* \'test\' rcl'
|
|
|
|
assert_equal [{ value: 6, type: :numeric, base: 10 }],
|
|
|
|
lang.stack
|
2021-12-08 12:46:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sto_divide
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '3 \'test\' sto \'test\' 2.0 sto÷ \'test\' rcl'
|
|
|
|
assert_equal [{ value: 1.5, type: :numeric, base: 10 }],
|
|
|
|
lang.stack
|
|
|
|
|
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '3 \'test\' sto 2.0 \'test\' sto÷ \'test\' rcl'
|
|
|
|
assert_equal [{ value: 1.5, type: :numeric, base: 10 }],
|
|
|
|
lang.stack
|
2021-12-08 12:46:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sto_negate
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '3 \'test\' sto \'test\' sneg \'test\' rcl'
|
|
|
|
assert_equal [{ value: -3, type: :numeric, base: 10 }],
|
|
|
|
lang.stack
|
2021-12-08 12:46:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sto_inverse
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '2 \'test\' sto \'test\' sinv \'test\' rcl'
|
2021-12-08 12:46:57 +01:00
|
|
|
assert_equal [{ value: 0.5, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-07 16:51:33 +01:00
|
|
|
end
|
|
|
|
end
|