rpl.rb/spec/language_operations_spec.rb

245 lines
5.8 KiB
Ruby
Raw Normal View History

2021-11-10 16:44:57 +01:00
# coding: utf-8
# frozen_string_literal: true
require 'test/unit'
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
lang = Rpl::Language.new
lang.run '1 2 +'
assert_equal [{ value: 3, type: :numeric, base: 10 }],
lang.stack
2021-11-10 16:44:57 +01:00
lang = Rpl::Language.new
lang.run '1 "a" +'
2021-11-10 16:44:57 +01:00
assert_equal [{ value: '"1a"', type: :string }],
lang.stack
2021-11-10 16:44:57 +01:00
lang = Rpl::Language.new
lang.run '1 \'a\' +'
2021-11-10 16:44:57 +01:00
assert_equal [{ value: '"1a"', type: :string }],
lang.stack
2021-11-10 16:44:57 +01:00
lang = Rpl::Language.new
lang.run '\'a\' 1 +'
2021-11-10 16:44:57 +01:00
assert_equal [{ value: "'a1'", type: :name }],
lang.stack
2021-11-10 16:44:57 +01:00
lang = Rpl::Language.new
lang.run '\'a\' "b" +'
2021-11-10 16:44:57 +01:00
assert_equal [{ value: "'ab'", type: :name }],
lang.stack
2021-11-10 16:44:57 +01:00
lang = Rpl::Language.new
lang.run '\'a\' \'b\' +'
2021-11-10 16:44:57 +01:00
assert_equal [{ value: "'ab'", type: :name }],
lang.stack
2021-11-10 16:44:57 +01:00
lang = Rpl::Language.new
lang.run '"a" "b" +'
2021-11-10 16:44:57 +01:00
assert_equal [{ value: '"ab"', type: :string }],
lang.stack
2021-11-10 16:44:57 +01:00
lang = Rpl::Language.new
lang.run '"a" \'b\' +'
2021-11-10 16:44:57 +01:00
assert_equal [{ value: '"ab"', type: :string }],
lang.stack
2021-11-10 16:44:57 +01:00
lang = Rpl::Language.new
lang.run '"a" 1 +'
2021-11-10 16:44:57 +01:00
assert_equal [{ value: '"a1"', type: :string }],
lang.stack
2021-11-10 16:44:57 +01:00
end
2021-11-23 13:01:17 +01:00
def test_subtract
lang = Rpl::Language.new
lang.run '1 2 -'
assert_equal [{ value: -1, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
lang = Rpl::Language.new
lang.run '2 1 -'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
def test_negate
lang = Rpl::Language.new
lang.run '-1 chs'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
lang = Rpl::Language.new
lang.run '1 chs'
assert_equal [{ value: -1, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
def test_multiply
lang = Rpl::Language.new
lang.run '3 4 *'
assert_equal [{ value: 12, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
def test_divide
lang = Rpl::Language.new
lang.run '3.0 4 /'
assert_equal [{ value: 0.75, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
def test_inverse
lang = Rpl::Language.new
lang.run '4 inv'
assert_equal [{ value: 0.25, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
def test_power
lang = Rpl::Language.new
lang.run '3 4 ^'
assert_equal [{ value: 81, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
def test_sqrt
lang = Rpl::Language.new
lang.run '16 √'
assert_equal [{ value: 4, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
def test_sq
lang = Rpl::Language.new
lang.run '4 sq'
assert_equal [{ value: 16, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
def test_abs
lang = Rpl::Language.new
lang.run '-1 abs'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
lang = Rpl::Language.new
lang.run '1 abs'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
def test_dec
lang = Rpl::Language.new
lang.run '0x1 dec'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
def test_hex
lang = Rpl::Language.new
lang.run '1 hex'
assert_equal [{ value: 1, type: :numeric, base: 16 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
def test_bin
lang = Rpl::Language.new
lang.run '1 bin'
assert_equal [{ value: 1, type: :numeric, base: 2 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
def test_base
lang = Rpl::Language.new
lang.run '1 31 base'
assert_equal [{ value: 1, type: :numeric, base: 31 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
def test_sign
lang = Rpl::Language.new
lang.run '-10 sign'
assert_equal [{ value: -1, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
lang = Rpl::Language.new
lang.run '10 sign'
assert_equal [{ value: 1, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
lang = Rpl::Language.new
lang.run '0 sign'
assert_equal [{ value: 0, type: :numeric, base: 10 }],
lang.stack
2021-11-23 13:01:17 +01:00
end
2021-12-02 15:33:22 +01:00
def test_percent
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 }],
lang.stack
2021-12-02 15:33:22 +01:00
end
def test_inverse_percent
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 }],
lang.stack
2021-12-02 15:33:22 +01:00
end
def test_mod
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 }],
lang.stack
2021-12-02 15:33:22 +01:00
end
def test_fact
lang = Rpl::Language.new
lang.run '5 !'
2021-12-02 15:33:22 +01:00
assert_equal [{ value: 24, type: :numeric, base: 10 }],
lang.stack
2021-12-02 15:33:22 +01:00
end
def test_floor
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 }],
lang.stack
2021-12-02 15:33:22 +01:00
end
def test_ceil
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 }],
lang.stack
2021-12-02 15:33:22 +01:00
end
def test_min
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 }],
lang.stack
2021-12-02 15:33:22 +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 }],
lang.stack
2021-12-02 15:33:22 +01:00
end
def test_max
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 }],
lang.stack
2021-12-02 15:33:22 +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 }],
lang.stack
2021-12-02 15:33:22 +01:00
end
2021-11-10 16:44:57 +01:00
end