rpl.rb/spec/words_stack_spec.rb

157 lines
4.1 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
2022-02-17 15:09:29 +01:00
require 'minitest/autorun'
2022-02-15 17:06:19 +01:00
require 'rpl'
class TestLanguageStack < Minitest::Test
2022-02-26 18:53:39 +01:00
include Types
def test_swap
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 swap'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 2 ),
Types.new_object( RplNumeric, 1 )],
interpreter.stack
end
def test_drop
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 drop'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 1 )],
interpreter.stack
end
def test_drop2
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 drop2'
assert_equal [],
interpreter.stack
end
def test_dropn
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 3 4 3 dropn'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 1 )],
interpreter.stack
end
def test_del
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 del'
assert_empty interpreter.stack
end
def test_rot
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 3 rot'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 2 ),
Types.new_object( RplNumeric, 3 ),
Types.new_object( RplNumeric, 1 )],
interpreter.stack
end
def test_dup
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 dup'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 1 ),
Types.new_object( RplNumeric, 2 ),
Types.new_object( RplNumeric, 2 )],
interpreter.stack
end
def test_dup2
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 dup2'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 1 ),
Types.new_object( RplNumeric, 2 ),
Types.new_object( RplNumeric, 1 ),
Types.new_object( RplNumeric, 2 )],
interpreter.stack
end
def test_dupn
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 3 4 3 dupn'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 1 ),
Types.new_object( RplNumeric, 2 ),
Types.new_object( RplNumeric, 3 ),
Types.new_object( RplNumeric, 4 ),
Types.new_object( RplNumeric, 2 ),
Types.new_object( RplNumeric, 3 ),
Types.new_object( RplNumeric, 4 )],
interpreter.stack
end
def test_pick
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 3 4 3 pick'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 1 ),
Types.new_object( RplNumeric, 2 ),
Types.new_object( RplNumeric, 3 ),
Types.new_object( RplNumeric, 4 ),
Types.new_object( RplNumeric, 2 )],
interpreter.stack
end
def test_depth
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! 'depth'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 0 )],
interpreter.stack
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 depth'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 1 ),
Types.new_object( RplNumeric, 2 ),
Types.new_object( RplNumeric, 2 )],
interpreter.stack
end
def test_roll
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 3 4 3 roll'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 1 ),
Types.new_object( RplNumeric, 3 ),
Types.new_object( RplNumeric, 4 ),
Types.new_object( RplNumeric, 2 )],
interpreter.stack
end
def test_rolld
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 4 3 2 rolld'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 1 ),
Types.new_object( RplNumeric, 2 ),
Types.new_object( RplNumeric, 3 ),
Types.new_object( RplNumeric, 4 )],
interpreter.stack
end
def test_over
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run! '1 2 3 4 over'
2022-02-28 11:40:47 +01:00
assert_equal [Types.new_object( RplNumeric, 1 ),
Types.new_object( RplNumeric, 2 ),
Types.new_object( RplNumeric, 3 ),
Types.new_object( RplNumeric, 4 ),
Types.new_object( RplNumeric, 3 )],
interpreter.stack
end
end