all tests use lang.run() instead of handcrafted stacks

This commit is contained in:
Gwenhael Le Moine 2021-12-08 16:08:49 +01:00
parent 490dfa5583
commit 1ddccc3eee
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
8 changed files with 282 additions and 440 deletions

View file

@ -7,7 +7,7 @@ require './lib/runner'
module Rpl module Rpl
class Language class Language
attr_reader :stack attr_reader :stack, :dictionary
def initialize( stack = [] ) def initialize( stack = [] )
@stack = stack @stack = stack

View file

@ -7,36 +7,30 @@ require_relative '../language'
class TestLanguageBranch < Test::Unit::TestCase class TestLanguageBranch < Test::Unit::TestCase
def test_ifte def test_ifte
stack, _dictionary = Rpl::Lang::Core.ifte( [{ type: :boolean, value: true }, lang = Rpl::Language.new
{ type: :program, value: '« 2 3 + »' }, lang.run 'true « 2 3 + » « 2 3 - » ifte'
{ type: :program, value: '« 2 3 - »' }],
Rpl::Lang::Dictionary.new )
assert_equal [{ value: 5, type: :numeric, base: 10 }], assert_equal [{ value: 5, type: :numeric, base: 10 }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.ifte( [{ type: :boolean, value: false }, lang = Rpl::Language.new
{ type: :program, value: '« 2 3 + »' }, lang.run 'false « 2 3 + » « 2 3 - » ifte'
{ type: :program, value: '« 2 3 - »' }],
Rpl::Lang::Dictionary.new )
assert_equal [{ value: -1, type: :numeric, base: 10 }], assert_equal [{ value: -1, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_ift def test_ift
stack, _dictionary = Rpl::Lang::Core.ift( [{ type: :boolean, value: true }, lang = Rpl::Language.new
{ type: :program, value: '« 2 3 + »' }], lang.run 'true « 2 3 + » ift'
Rpl::Lang::Dictionary.new )
assert_equal [{ value: 5, type: :numeric, base: 10 }], assert_equal [{ value: 5, type: :numeric, base: 10 }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.ift( [{ type: :boolean, value: false }, lang = Rpl::Language.new
{ type: :program, value: '« 2 3 + »' }], lang.run 'false « 2 3 + » ift'
Rpl::Lang::Dictionary.new )
assert_equal [], assert_equal [],
stack lang.stack
end end
end end

View file

@ -7,285 +7,238 @@ require_relative '../language'
class TesttLanguageOperations < Test::Unit::TestCase class TesttLanguageOperations < Test::Unit::TestCase
def test_add def test_add
stack, _dictionary = Rpl::Lang::Core.add [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }], lang.run '1 2 +'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 3, type: :numeric, base: 10 }], assert_equal [{ value: 3, type: :numeric, base: 10 }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: '"a"', type: :string }], lang.run '1 "a" +'
Rpl::Lang::Dictionary.new
assert_equal [{ value: '"1a"', type: :string }], assert_equal [{ value: '"1a"', type: :string }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: "'a'", type: :name }], lang.run '1 \'a\' +'
Rpl::Lang::Dictionary.new
assert_equal [{ value: '"1a"', type: :string }], assert_equal [{ value: '"1a"', type: :string }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: "'a'", type: :name }, lang = Rpl::Language.new
{ value: 1, type: :numeric, base: 10 }], lang.run '\'a\' 1 +'
Rpl::Lang::Dictionary.new
assert_equal [{ value: "'a1'", type: :name }], assert_equal [{ value: "'a1'", type: :name }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: "'a'", type: :name }, lang = Rpl::Language.new
{ value: '"b"', type: :string }], lang.run '\'a\' "b" +'
Rpl::Lang::Dictionary.new
assert_equal [{ value: "'ab'", type: :name }], assert_equal [{ value: "'ab'", type: :name }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: "'a'", type: :name }, lang = Rpl::Language.new
{ value: "'b'", type: :name }], lang.run '\'a\' \'b\' +'
Rpl::Lang::Dictionary.new
assert_equal [{ value: "'ab'", type: :name }], assert_equal [{ value: "'ab'", type: :name }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: '"a"', type: :string }, lang = Rpl::Language.new
{ value: '"b"', type: :string }], lang.run '"a" "b" +'
Rpl::Lang::Dictionary.new
assert_equal [{ value: '"ab"', type: :string }], assert_equal [{ value: '"ab"', type: :string }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: '"a"', type: :string }, lang = Rpl::Language.new
{ value: "'b'", type: :name }], lang.run '"a" \'b\' +'
Rpl::Lang::Dictionary.new
assert_equal [{ value: '"ab"', type: :string }], assert_equal [{ value: '"ab"', type: :string }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.add [{ value: '"a"', type: :string }, lang = Rpl::Language.new
{ value: 1, type: :numeric, base: 10 }], lang.run '"a" 1 +'
Rpl::Lang::Dictionary.new
assert_equal [{ value: '"a1"', type: :string }], assert_equal [{ value: '"a1"', type: :string }],
stack lang.stack
end end
def test_subtract def test_subtract
stack, _dictionary = Rpl::Lang::Core.subtract [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }], lang.run '1 2 -'
Rpl::Lang::Dictionary.new
assert_equal [{ value: -1, type: :numeric, base: 10 }], assert_equal [{ value: -1, type: :numeric, base: 10 }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.subtract [{ value: 2, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 1, type: :numeric, base: 10 }], lang.run '2 1 -'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_negate def test_negate
stack, _dictionary = Rpl::Lang::Core.negate [{ value: -1, type: :numeric, base: 10 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new lang.run '-1 chs'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.negate [{ value: 1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 chs'
assert_equal [{ value: -1, type: :numeric, base: 10 }], assert_equal [{ value: -1, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_multiply def test_multiply
stack, _dictionary = Rpl::Lang::Core.multiply [{ value: 3, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 4, type: :numeric, base: 10 }], lang.run '3 4 *'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 12, type: :numeric, base: 10 }], assert_equal [{ value: 12, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_divide def test_divide
stack, _dictionary = Rpl::Lang::Core.divide [{ value: 3.0, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 4, type: :numeric, base: 10 }], lang.run '3.0 4 /'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 0.75, type: :numeric, base: 10 }], assert_equal [{ value: 0.75, type: :numeric, base: 10 }],
stack lang.stack
# stack, _dictionary = Rpl::Lang::Core.divide [{ value: 3, type: :numeric, base: 10 },
# { value: 4, type: :numeric, base: 10 }]
# assert_equal [{ value: 0.75, type: :numeric, base: 10 }],
# stack
end end
def test_inverse def test_inverse
stack, _dictionary = Rpl::Lang::Core.inverse [{ value: 4, type: :numeric, base: 10 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new lang.run '4 inv'
assert_equal [{ value: 0.25, type: :numeric, base: 10 }], assert_equal [{ value: 0.25, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_power def test_power
stack, _dictionary = Rpl::Lang::Core.power [{ value: 3, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 4, type: :numeric, base: 10 }], lang.run '3 4 ^'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 81, type: :numeric, base: 10 }], assert_equal [{ value: 81, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_sqrt def test_sqrt
stack, _dictionary = Rpl::Lang::Core.sqrt [{ value: 16, type: :numeric, base: 10 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new lang.run '16 √'
assert_equal [{ value: 4, type: :numeric, base: 10 }], assert_equal [{ value: 4, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_sq def test_sq
stack, _dictionary = Rpl::Lang::Core.sq [{ value: 4, type: :numeric, base: 10 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new lang.run '4 sq'
assert_equal [{ value: 16, type: :numeric, base: 10 }], assert_equal [{ value: 16, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_abs def test_abs
stack, _dictionary = Rpl::Lang::Core.abs [{ value: -1, type: :numeric, base: 10 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new lang.run '-1 abs'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.abs [{ value: 1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '1 abs'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_dec def test_dec
stack, _dictionary = Rpl::Lang::Core.dec [{ value: 1, type: :numeric, base: 16 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new lang.run '0x1 dec'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_hex def test_hex
stack, _dictionary = Rpl::Lang::Core.hex [{ value: 1, type: :numeric, base: 10 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new lang.run '1 hex'
assert_equal [{ value: 1, type: :numeric, base: 16 }], assert_equal [{ value: 1, type: :numeric, base: 16 }],
stack lang.stack
end end
def test_bin def test_bin
stack, _dictionary = Rpl::Lang::Core.bin [{ value: 1, type: :numeric, base: 10 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new lang.run '1 bin'
assert_equal [{ value: 1, type: :numeric, base: 2 }], assert_equal [{ value: 1, type: :numeric, base: 2 }],
stack lang.stack
end end
def test_base def test_base
stack, _dictionary = Rpl::Lang::Core.base [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 31, type: :numeric, base: 10 }], lang.run '1 31 base'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 31 }], assert_equal [{ value: 1, type: :numeric, base: 31 }],
stack lang.stack
end end
def test_sign def test_sign
stack, _dictionary = Rpl::Lang::Core.sign [{ value: -10, type: :numeric, base: 10 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new lang.run '-10 sign'
assert_equal [{ value: -1, type: :numeric, base: 10 }], assert_equal [{ value: -1, type: :numeric, base: 10 }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.sign [{ value: 10, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '10 sign'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.sign [{ value: 0, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '0 sign'
assert_equal [{ value: 0, type: :numeric, base: 10 }], assert_equal [{ value: 0, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_percent def test_percent
stack, _dictionary = Rpl::Lang::Core.percent [{ value: 2, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 33, type: :numeric, base: 10 }], lang.run '2 33 %'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 0.66, type: :numeric, base: 10 }], assert_equal [{ value: 0.66, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_inverse_percent def test_inverse_percent
stack, _dictionary = Rpl::Lang::Core.inverse_percent [{ value: 2, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 0.66, type: :numeric, base: 10 }], lang.run '2 0.66 %CH'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 33, type: :numeric, base: 10 }], assert_equal [{ value: 33, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_mod def test_mod
stack, _dictionary = Rpl::Lang::Core.mod [{ value: 9, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 4, type: :numeric, base: 10 }], lang.run '9 4 mod'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_fact def test_fact
stack, _dictionary = Rpl::Lang::Core.fact [{ value: 5, type: :numeric, base: 10 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new lang.run '5 !'
assert_equal [{ value: 24, type: :numeric, base: 10 }], assert_equal [{ value: 24, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_floor def test_floor
stack, _dictionary = Rpl::Lang::Core.floor [{ value: 5.23, type: :numeric, base: 10 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new lang.run '5.23 floor'
assert_equal [{ value: 5, type: :numeric, base: 10 }], assert_equal [{ value: 5, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_ceil def test_ceil
stack, _dictionary = Rpl::Lang::Core.ceil [{ value: 5.23, type: :numeric, base: 10 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new lang.run '5.23 ceil'
assert_equal [{ value: 6, type: :numeric, base: 10 }], assert_equal [{ value: 6, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_min def test_min
stack, _dictionary = Rpl::Lang::Core.min [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }], lang.run '1 2 min'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.min [{ value: 2, type: :numeric, base: 10 },
{ value: 1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '2 1 min'
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_max def test_max
stack, _dictionary = Rpl::Lang::Core.max [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }], lang.run '1 2 max'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 2, type: :numeric, base: 10 }], assert_equal [{ value: 2, type: :numeric, base: 10 }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.max [{ value: 2, type: :numeric, base: 10 },
{ value: 1, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
lang = Rpl::Language.new
lang.run '2 1 max'
assert_equal [{ value: 2, type: :numeric, base: 10 }], assert_equal [{ value: 2, type: :numeric, base: 10 }],
stack lang.stack
end end
end end

View file

@ -6,24 +6,18 @@ require_relative '../language'
class TestLanguageProgram < Test::Unit::TestCase class TestLanguageProgram < Test::Unit::TestCase
def test_eval def test_eval
stack, _dictionary = Rpl::Lang::Core.eval( [{ value: '« 2 dup * dup »', type: :program }], Rpl::Lang::Dictionary.new ) lang = Rpl::Language.new
lang.run '« 2 dup * dup » eval'
assert_equal [{ value: 4, type: :numeric, base: 10 }, assert_equal [{ value: 4, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }], { value: 4, type: :numeric, base: 10 }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.eval( [{ value: 4, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: "'dup'", type: :name }], Rpl::Lang::Dictionary.new ) lang.run '4 \'dup\' eval'
assert_equal [{ value: 4, type: :numeric, base: 10 }, assert_equal [{ value: 4, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }], { value: 4, type: :numeric, base: 10 }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.eval( [{ value: 4, type: :numeric, base: 10 },
{ value: 'dup', type: :word }], Rpl::Lang::Dictionary.new )
assert_equal [{ value: 4, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }],
stack
end end
end end

View file

@ -7,88 +7,80 @@ require_relative '../language'
class TestLanguageStack < Test::Unit::TestCase class TestLanguageStack < Test::Unit::TestCase
def test_swap def test_swap
stack, _dictionary = Rpl::Lang::Core.swap [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }], lang.run '1 2 swap'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 2, type: :numeric, base: 10 }, assert_equal [{ value: 2, type: :numeric, base: 10 },
{ value: 1, type: :numeric, base: 10 }], { value: 1, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_drop def test_drop
stack, _dictionary = Rpl::Lang::Core.drop [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }], lang.run '1 2 drop'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_drop2 def test_drop2
stack, _dictionary = Rpl::Lang::Core.drop2 [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }], lang.run '1 2 drop2'
Rpl::Lang::Dictionary.new
assert_equal [], assert_equal [],
stack lang.stack
end end
def test_dropn def test_dropn
stack, _dictionary = Rpl::Lang::Core.dropn [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }, lang.run '1 2 3 4 3 dropn'
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 }], assert_equal [{ value: 1, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_del def test_del
stack, _dictionary = Rpl::Lang::Core.del [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }], lang.run '1 2 del'
Rpl::Lang::Dictionary.new
assert_equal [], assert_empty lang.stack
stack
end end
def test_rot def test_rot
stack, _dictionary = Rpl::Lang::Core.rot [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }, lang.run '1 2 3 rot'
{ value: 3, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
assert_equal [{ value: 2, type: :numeric, base: 10 }, assert_equal [{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 },
{ value: 1, type: :numeric, base: 10 }], { value: 1, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_dup def test_dup
stack, _dictionary = Rpl::Lang::Core.dup [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }], lang.run '1 2 dup'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }], { value: 2, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_dup2 def test_dup2
stack, _dictionary = Rpl::Lang::Core.dup2 [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }], lang.run '1 2 dup2'
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 },
{ value: 1, type: :numeric, base: 10 }, { value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }], { value: 2, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_dupn def test_dupn
stack, _dictionary = Rpl::Lang::Core.dupn [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }, lang.run '1 2 3 4 3 dupn'
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 },
@ -96,78 +88,68 @@ class TestLanguageStack < Test::Unit::TestCase
{ value: 2, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }], { value: 4, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_pick def test_pick
stack, _dictionary = Rpl::Lang::Core.pick [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }, lang.run '1 2 3 4 3 pick'
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }, { value: 4, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }], { value: 2, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_depth def test_depth
stack, _dictionary = Rpl::Lang::Core.depth [], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new lang.run 'depth'
assert_equal [{ value: 0, type: :numeric, base: 10 }],
stack assert_equal [{ value: 0, type: :numeric, base: 10 }],
lang.stack
lang = Rpl::Language.new
lang.run '1 2 depth'
stack, _dictionary = Rpl::Lang::Core.depth [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }], { value: 2, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_roll def test_roll
stack, _dictionary = Rpl::Lang::Core.roll [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }, lang.run '1 2 3 4 3 roll'
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }, { value: 4, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }], { value: 2, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_rolld def test_rolld
stack, _dictionary = Rpl::Lang::Core.rolld [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }, lang.run '1 2 4 3 2 rolld'
{ value: 4, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }], { value: 4, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_over def test_over
stack, _dictionary = Rpl::Lang::Core.over [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: 2, type: :numeric, base: 10 }, lang.run '1 2 3 4 over'
{ value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new
assert_equal [{ value: 1, type: :numeric, base: 10 }, assert_equal [{ value: 1, type: :numeric, base: 10 },
{ value: 2, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 },
{ value: 4, type: :numeric, base: 10 }, { value: 4, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 }], { value: 3, type: :numeric, base: 10 }],
stack lang.stack
end end
end end

View file

@ -6,178 +6,100 @@ require_relative '../language'
class TestLanguageProgram < Test::Unit::TestCase class TestLanguageProgram < Test::Unit::TestCase
def test_sto def test_sto
stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program }, lang = Rpl::Language.new
{ value: "'quatre'", type: :name }], lang.run '« 2 dup * » \'quatre\' sto'
Rpl::Lang::Dictionary.new ) assert_empty lang.stack
assert_equal [], stack
stack, _dictionary = Rpl::Lang::Core.eval( [{ value: 'quatre', type: :word }], lang.run 'quatre'
dictionary )
assert_equal [{ value: 4, type: :numeric, base: 10 }], assert_equal [{ value: 4, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_rcl def test_rcl
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program }, lang = Rpl::Language.new
{ value: "'quatre'", type: :name }], lang.run '« 2 dup * » \'quatre\' sto \'quatre\' rcl'
Rpl::Lang::Dictionary.new )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'quatre'", type: :name }],
dictionary )
assert_equal [{ value: '« 2 dup * »', type: :program }], assert_equal [{ value: '« 2 dup * »', type: :program }],
stack lang.stack
end end
def test_purge def test_purge
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program }, lang = Rpl::Language.new
{ value: "'quatre'", type: :name }], lang.run '« 2 dup * » \'quatre\' sto \'quatre\' purge'
Rpl::Lang::Dictionary.new ) assert_nil lang.dictionary['quatre']
_stack, dictionary = Rpl::Lang::Core.purge( [{ value: "'quatre'", type: :name }],
dictionary )
assert_equal nil,
dictionary['quatre']
end end
def test_vars def test_vars
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program }, lang = Rpl::Language.new
{ value: "'quatre'", type: :name }], lang.run '« 2 dup * » \'quatre\' sto 1 \'un\' sto vars'
Rpl::Lang::Dictionary.new )
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 1, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.vars( [],
dictionary )
assert_equal [{ value: ["'quatre'", "'un'"], type: :list }], assert_equal [{ value: ["'quatre'", "'un'"], type: :list }],
stack lang.stack
end end
def test_clusr def test_clusr
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program }, lang = Rpl::Language.new
{ value: "'quatre'", type: :name }], lang.run '« 2 dup * » \'quatre\' sto 1 \'un\' sto clusr'
Rpl::Lang::Dictionary.new ) assert_empty lang.dictionary.vars
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 1, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
dictionary )
_stack, dictionary = Rpl::Lang::Core.clusr( [],
dictionary )
assert_equal( {},
dictionary.vars )
end end
def test_sto_add def test_sto_add
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: "'un'", type: :name }], lang.run '1 \'test\' sto \'test\' 3 sto+ \'test\' rcl'
Rpl::Lang::Dictionary.new )
_stack, dictionary = Rpl::Lang::Core.sto_add( [{ value: "'un'", type: :name },
{ value: 3, type: :numeric, base: 10 }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
assert_equal [{ value: 4, type: :numeric, base: 10 }], assert_equal [{ value: 4, type: :numeric, base: 10 }],
stack lang.stack
_stack, dictionary = Rpl::Lang::Core.sto_add( [{ value: 3, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: "'un'", type: :name }], lang.run '1 \'test\' sto 3 \'test\' sto+ \'test\' rcl'
dictionary ) assert_equal [{ value: 4, type: :numeric, base: 10 }],
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], lang.stack
dictionary )
assert_equal [{ value: 7, type: :numeric, base: 10 }],
stack
end end
def test_sto_subtract def test_sto_subtract
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 1, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: "'un'", type: :name }], lang.run '1 \'test\' sto \'test\' 3 sto- \'test\' rcl'
Rpl::Lang::Dictionary.new )
_stack, dictionary = Rpl::Lang::Core.sto_subtract( [{ value: "'un'", type: :name },
{ value: 3, type: :numeric, base: 10 }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
assert_equal [{ value: -2, type: :numeric, base: 10 }], assert_equal [{ value: -2, type: :numeric, base: 10 }],
stack lang.stack
_stack, dictionary = Rpl::Lang::Core.sto_subtract( [{ value: 3, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: "'un'", type: :name }], lang.run '1 \'test\' sto 3 \'test\' sto- \'test\' rcl'
dictionary ) assert_equal [{ value: -2, type: :numeric, base: 10 }],
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], lang.stack
dictionary )
assert_equal [{ value: -5, type: :numeric, base: 10 }],
stack
end end
def test_sto_multiply def test_sto_multiply
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 2, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: "'un'", type: :name }], lang.run '2 \'test\' sto \'test\' 3 sto* \'test\' rcl'
Rpl::Lang::Dictionary.new )
_stack, dictionary = Rpl::Lang::Core.sto_multiply( [{ value: "'un'", type: :name },
{ value: 3, type: :numeric, base: 10 }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
assert_equal [{ value: 6, type: :numeric, base: 10 }], assert_equal [{ value: 6, type: :numeric, base: 10 }],
stack lang.stack
_stack, dictionary = Rpl::Lang::Core.sto_multiply( [{ value: 3, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: "'un'", type: :name }], lang.run '2 \'test\' sto 3 \'test\' sto* \'test\' rcl'
dictionary ) assert_equal [{ value: 6, type: :numeric, base: 10 }],
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], lang.stack
dictionary )
assert_equal [{ value: 18, type: :numeric, base: 10 }],
stack
end end
def test_sto_divide def test_sto_divide
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 2, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: "'un'", type: :name }], lang.run '3 \'test\' sto \'test\' 2.0 sto÷ \'test\' rcl'
Rpl::Lang::Dictionary.new ) assert_equal [{ value: 1.5, type: :numeric, base: 10 }],
lang.stack
_stack, dictionary = Rpl::Lang::Core.sto_divide( [{ value: "'un'", type: :name }, lang = Rpl::Language.new
{ value: 4.0, type: :numeric, base: 10 }], lang.run '3 \'test\' sto 2.0 \'test\' sto÷ \'test\' rcl'
dictionary ) assert_equal [{ value: 1.5, type: :numeric, base: 10 }],
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], lang.stack
dictionary )
assert_equal [{ value: 0.5, type: :numeric, base: 10 }],
stack
_stack, dictionary = Rpl::Lang::Core.sto_divide( [{ value: 5, type: :numeric, base: 10 },
{ value: "'un'", type: :name }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
assert_equal [{ value: 0.1, type: :numeric, base: 10 }],
stack
end end
def test_sto_negate def test_sto_negate
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 2, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: "'un'", type: :name }], lang.run '3 \'test\' sto \'test\' sneg \'test\' rcl'
Rpl::Lang::Dictionary.new ) assert_equal [{ value: -3, type: :numeric, base: 10 }],
lang.stack
_stack, dictionary = Rpl::Lang::Core.sto_negate( [{ value: "'un'", type: :name }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
assert_equal [{ value: -2, type: :numeric, base: 10 }],
stack
end end
def test_sto_inverse def test_sto_inverse
_stack, dictionary = Rpl::Lang::Core.sto( [{ value: 2, type: :numeric, base: 10 }, lang = Rpl::Language.new
{ value: "'un'", type: :name }], lang.run '2 \'test\' sto \'test\' sinv \'test\' rcl'
Rpl::Lang::Dictionary.new )
_stack, dictionary = Rpl::Lang::Core.sto_inverse( [{ value: "'un'", type: :name }],
dictionary )
stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }],
dictionary )
assert_equal [{ value: 0.5, type: :numeric, base: 10 }], assert_equal [{ value: 0.5, type: :numeric, base: 10 }],
stack lang.stack
end end
end end

View file

@ -7,69 +7,66 @@ require_relative '../language'
class TestLanguageString < Test::Unit::TestCase class TestLanguageString < Test::Unit::TestCase
def test_to_string def test_to_string
stack, _dictionary = Rpl::Lang::Core.to_string( [{ value: 2, type: :numeric, base: 10 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new ) lang.run '2 →str'
assert_equal [{ value: '2', type: :string }], assert_equal [{ value: '2', type: :string }],
stack lang.stack
end end
def test_from_string def test_from_string
stack, _dictionary = Rpl::Lang::Core.from_string( [{ value: '2', type: :string }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new ) lang.run '"2" str→'
assert_equal [{ value: 2, type: :numeric, base: 10 }], assert_equal [{ value: 2, type: :numeric, base: 10 }],
stack lang.stack
stack, _dictionary = Rpl::Lang::Core.from_string( [{ value: "« 2 dup * » 'carré' sto", type: :string }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new ) lang.run '"« dup * » \'carré\' sto" str→'
assert_equal [{ value: 2 dup * »', type: :program }, assert_equal [{ value: dup * »', type: :program },
{ value: "'carré'", type: :name }, { value: "'carré'", type: :name },
{ value: 'sto', type: :word }], { value: 'sto', type: :word }],
stack lang.stack
end end
def test_chr def test_chr
stack, _dictionary = Rpl::Lang::Core.chr( [{ value: 71, type: :numeric, base: 10 }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new ) lang.run '71 chr'
assert_equal [{ value: 'G', type: :string }], assert_equal [{ value: 'G', type: :string }],
stack lang.stack
end end
def test_num def test_num
stack, _dictionary = Rpl::Lang::Core.num( [{ value: 'G', type: :string }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new ) lang.run '"G" num'
assert_equal [{ value: 71, type: :numeric, base: 10 }], assert_equal [{ value: 71, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_size def test_size
stack, _dictionary = Rpl::Lang::Core.size( [{ value: 'test', type: :string }], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new ) lang.run '"test" size'
assert_equal [{ value: 4, type: :numeric, base: 10 }], assert_equal [{ value: 4, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_pos def test_pos
stack, _dictionary = Rpl::Lang::Core.pos( [{ value: 'test of POS', type: :string }, lang = Rpl::Language.new
{ value: 'of', type: :string }], lang.run '"test of POS" "of" pos'
Rpl::Lang::Dictionary.new )
assert_equal [{ value: 5, type: :numeric, base: 10 }], assert_equal [{ value: 5, type: :numeric, base: 10 }],
stack lang.stack
end end
def test_sub def test_sub
stack, _dictionary = Rpl::Lang::Core.sub( [{ value: 'test', type: :string }, lang = Rpl::Language.new
{ value: 1, type: :numeric, base: 10 }, lang.run '"my string to sub" 4 6 sub'
{ value: 2, type: :numeric, base: 10 }],
Rpl::Lang::Dictionary.new )
assert_equal [{ value: 'es', type: :string }], assert_equal [{ value: 'str', type: :string }],
stack lang.stack
end end
end end

View file

@ -8,28 +8,28 @@ require_relative '../language'
class TestLanguageTimeDate < Test::Unit::TestCase class TestLanguageTimeDate < Test::Unit::TestCase
def test_time def test_time
now = Time.now.to_s now = Time.now.to_s
stack, _dictionary = Rpl::Lang::Core.time( [], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new ) lang.run 'time'
assert_equal [{ value: now, type: :string }], assert_equal [{ value: now, type: :string }],
stack lang.stack
end end
def test_date def test_date
now = Date.today.to_s now = Date.today.to_s
stack, _dictionary = Rpl::Lang::Core.date( [], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new ) lang.run 'date'
assert_equal [{ value: now, type: :string }], assert_equal [{ value: now, type: :string }],
stack lang.stack
end end
def test_ticks def test_ticks
stack, _dictionary = Rpl::Lang::Core.ticks( [], lang = Rpl::Language.new
Rpl::Lang::Dictionary.new ) lang.run 'ticks'
# TODO: better test, but how? # TODO: better test, but how?
assert_equal :numeric, assert_equal :numeric,
stack[0][:type] lang.stack[0][:type]
end end
end end