2021-11-10 16:44:57 +01:00
|
|
|
# coding: utf-8
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'test/unit'
|
|
|
|
|
2021-12-07 16:46:33 +01:00
|
|
|
require_relative '../language'
|
2021-11-10 16:44:57 +01:00
|
|
|
|
2021-11-23 13:00:42 +01:00
|
|
|
class TesttLanguageOperations < Test::Unit::TestCase
|
|
|
|
def test_add
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 2 +'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 3, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-10 16:44:57 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 "a" +'
|
2021-11-10 16:44:57 +01:00
|
|
|
assert_equal [{ value: '"1a"', type: :string }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-10 16:44:57 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 \'a\' +'
|
2021-11-10 16:44:57 +01:00
|
|
|
assert_equal [{ value: '"1a"', type: :string }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-10 16:44:57 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '\'a\' 1 +'
|
2021-11-10 16:44:57 +01:00
|
|
|
assert_equal [{ value: "'a1'", type: :name }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-10 16:44:57 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '\'a\' "b" +'
|
2021-11-10 16:44:57 +01:00
|
|
|
assert_equal [{ value: "'ab'", type: :name }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-10 16:44:57 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '\'a\' \'b\' +'
|
2021-11-10 16:44:57 +01:00
|
|
|
assert_equal [{ value: "'ab'", type: :name }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-10 16:44:57 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '"a" "b" +'
|
2021-11-10 16:44:57 +01:00
|
|
|
assert_equal [{ value: '"ab"', type: :string }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-10 16:44:57 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '"a" \'b\' +'
|
2021-11-10 16:44:57 +01:00
|
|
|
assert_equal [{ value: '"ab"', type: :string }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-10 16:44:57 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '"a" 1 +'
|
2021-11-10 16:44:57 +01:00
|
|
|
assert_equal [{ value: '"a1"', type: :string }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-10 16:44:57 +01:00
|
|
|
end
|
2021-11-23 13:01:17 +01:00
|
|
|
|
|
|
|
def test_subtract
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 2 -'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: -1, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '2 1 -'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 1, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_negate
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '-1 chs'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 1, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 chs'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: -1, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_multiply
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '3 4 *'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 12, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_divide
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '3.0 4 /'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 0.75, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inverse
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '4 inv'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 0.25, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_power
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '3 4 ^'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 81, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sqrt
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '16 √'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 4, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sq
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '4 sq'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 16, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_abs
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '-1 abs'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 1, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 abs'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 1, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_dec
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '0x1 dec'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 1, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hex
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 hex'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 1, type: :numeric, base: 16 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_bin
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 bin'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 1, type: :numeric, base: 2 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_base
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 31 base'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 1, type: :numeric, base: 31 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sign
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '-10 sign'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: -1, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '10 sign'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 1, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '0 sign'
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 0, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-11-23 13:01:17 +01:00
|
|
|
end
|
2021-12-02 15:33:22 +01:00
|
|
|
|
|
|
|
def test_percent
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '2 33 %'
|
2021-12-02 15:33:22 +01:00
|
|
|
assert_equal [{ value: 0.66, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-02 15:33:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inverse_percent
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '2 0.66 %CH'
|
2021-12-02 15:33:22 +01:00
|
|
|
assert_equal [{ value: 33, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-02 15:33:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mod
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '9 4 mod'
|
2021-12-02 15:33:22 +01:00
|
|
|
assert_equal [{ value: 1, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-02 15:33:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fact
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '5 !'
|
2021-12-02 15:33:22 +01:00
|
|
|
assert_equal [{ value: 24, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-02 15:33:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_floor
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '5.23 floor'
|
2021-12-02 15:33:22 +01:00
|
|
|
assert_equal [{ value: 5, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-02 15:33:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ceil
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '5.23 ceil'
|
2021-12-02 15:33:22 +01:00
|
|
|
assert_equal [{ value: 6, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-02 15:33:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_min
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 2 min'
|
2021-12-02 15:33:22 +01:00
|
|
|
assert_equal [{ value: 1, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-02 15:33:22 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '2 1 min'
|
2021-12-02 15:33:22 +01:00
|
|
|
assert_equal [{ value: 1, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-02 15:33:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_max
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '1 2 max'
|
2021-12-02 15:33:22 +01:00
|
|
|
assert_equal [{ value: 2, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-02 15:33:22 +01:00
|
|
|
|
2021-12-08 16:08:49 +01:00
|
|
|
lang = Rpl::Language.new
|
|
|
|
lang.run '2 1 max'
|
2021-12-02 15:33:22 +01:00
|
|
|
assert_equal [{ value: 2, type: :numeric, base: 10 }],
|
2021-12-08 16:08:49 +01:00
|
|
|
lang.stack
|
2021-12-02 15:33:22 +01:00
|
|
|
end
|
2021-11-10 16:44:57 +01:00
|
|
|
end
|