rpl.rb/spec/words_store_spec.rb

105 lines
3 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-26 18:53:39 +01:00
class TestLanguageStore < MiniTest::Test
include Types
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'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 4 )],
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-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 4 )],
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-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 4 )],
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'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplProgram, '« 2 dup * »' )],
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-28 11:40:47 +01:00
assert_equal [Types.new_object( RplList, [Types.new_object( RplName, 'quatre' ),
2022-03-01 21:54:39 +01:00
Types.new_object( RplName, 'un' )] )],
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 3 \'test\' sto+ \'test\' rcl'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 4 )],
interpreter.stack
end
def test_sto_subtract
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '1 \'test\' sto 3 \'test\' sto- \'test\' rcl'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, -2 )],
interpreter.stack
end
def test_sto_multiply
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '2 \'test\' sto 3 \'test\' sto* \'test\' rcl'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 6 )],
interpreter.stack
end
def test_sto_divide
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '3 \'test\' sto 2.0 \'test\' sto÷ \'test\' rcl'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 1.5 )],
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'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, -3 )],
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'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 0.5 )],
interpreter.stack
2021-12-07 16:51:33 +01:00
end
end