rpl.rb/spec/language_store_spec.rb

123 lines
3.7 KiB
Ruby
Raw Normal View History

2021-12-07 16:51:33 +01:00
# frozen_string_literal: true
2022-02-17 15:09:29 +01:00
require 'minitest/autorun'
2021-12-07 16:51:33 +01:00
2022-02-15 17:06:19 +01:00
require 'rpl'
2021-12-07 16:51:33 +01:00
2022-02-17 15:09:29 +01:00
class TestLanguageProgram < MiniTest::Test
2021-12-07 16:51:33 +01:00
def test_sto
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '« 2 dup * » \'quatre\' sto'
assert_empty interpreter.stack
interpreter.run 'quatre'
assert_equal [{ value: 4, type: :numeric, base: 10 }],
interpreter.stack
end
2022-02-02 15:20:24 +01:00
def test_lsto
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run "« 2 'deux' lsto deux dup * » eval 'deux' rcl"
2022-02-02 15:20:24 +01:00
assert_empty interpreter.dictionary.local_vars_layers
2022-02-02 15:20:24 +01:00
assert_equal [{ value: 4, type: :numeric, base: 10 }],
interpreter.stack
2022-02-02 15:20:24 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run "« 2 'deux' lsto « 3 'trois' lsto trois drop » eval deux dup * » eval 'deux' rcl 'trois' rcl"
2022-02-02 15:20:24 +01:00
assert_empty interpreter.dictionary.local_vars_layers
2022-02-02 15:20:24 +01:00
assert_equal [{ value: 4, type: :numeric, base: 10 }],
interpreter.stack
2022-02-02 15:20:24 +01:00
end
def test_rcl
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '« 2 dup * » \'quatre\' sto \'quatre\' rcl'
assert_equal [{ value: '2 dup *', type: :program }],
interpreter.stack
end
def test_purge
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '« 2 dup * » \'quatre\' sto \'quatre\' purge'
assert_nil interpreter.dictionary.lookup( 'quatre' )
end
def test_vars
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.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 }],
interpreter.stack
end
def test_clusr
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '« 2 dup * » \'quatre\' sto 1 \'un\' sto clusr'
assert_empty interpreter.dictionary.vars
end
def test_sto_add
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 \'test\' sto \'test\' 3 sto+ \'test\' rcl'
assert_equal [{ value: 4, type: :numeric, base: 10 }],
interpreter.stack
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 \'test\' sto 3 \'test\' sto+ \'test\' rcl'
assert_equal [{ value: 4, type: :numeric, base: 10 }],
interpreter.stack
end
def test_sto_subtract
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 \'test\' sto \'test\' 3 sto- \'test\' rcl'
assert_equal [{ value: -2, type: :numeric, base: 10 }],
interpreter.stack
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 \'test\' sto 3 \'test\' sto- \'test\' rcl'
assert_equal [{ value: -2, type: :numeric, base: 10 }],
interpreter.stack
end
def test_sto_multiply
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '2 \'test\' sto \'test\' 3 sto* \'test\' rcl'
assert_equal [{ value: 6, type: :numeric, base: 10 }],
interpreter.stack
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '2 \'test\' sto 3 \'test\' sto* \'test\' rcl'
assert_equal [{ value: 6, type: :numeric, base: 10 }],
interpreter.stack
end
def test_sto_divide
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '3 \'test\' sto \'test\' 2.0 sto÷ \'test\' rcl'
assert_equal [{ value: 1.5, type: :numeric, base: 10 }],
interpreter.stack
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '3 \'test\' sto 2.0 \'test\' sto÷ \'test\' rcl'
assert_equal [{ value: 1.5, type: :numeric, base: 10 }],
interpreter.stack
end
def test_sto_negate
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '3 \'test\' sto \'test\' sneg \'test\' rcl'
assert_equal [{ value: -3, type: :numeric, base: 10 }],
interpreter.stack
end
def test_sto_inverse
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '2 \'test\' sto \'test\' sinv \'test\' rcl'
assert_equal [{ value: 0.5, type: :numeric, base: 10 }],
interpreter.stack
2021-12-07 16:51:33 +01:00
end
end