source/projects/shoggoth/test/python/ichor/test_interpreter.py

73 lines
1.6 KiB
Python
Raw Normal View History

2022-03-29 07:29:18 +00:00
"""
Tests coverign the VM interpreter
"""
2022-05-31 15:41:10 +00:00
from .fixtures import * # noqa
2022-07-16 01:37:34 +00:00
from ichor import (
FALSE,
InterpreterError,
isa,
TRUE,
)
2022-06-01 05:17:32 +00:00
import pytest
2022-03-29 07:29:18 +00:00
def test_return(vm):
2022-07-16 01:33:32 +00:00
assert vm.run([isa.RETURN(0)], stack=[TRUE, FALSE]) == []
assert vm.run([isa.RETURN(1)], stack=[TRUE, FALSE]) == [TRUE]
assert vm.run([isa.RETURN(2)], stack=[TRUE, FALSE]) == [TRUE, FALSE]
2022-03-29 07:29:18 +00:00
def test_dup(vm):
2022-07-16 01:33:32 +00:00
assert vm.run([isa.DUP(1), isa.RETURN(3)], stack=[FALSE, TRUE]) == [FALSE, TRUE, TRUE]
assert vm.run([isa.DUP(2), isa.RETURN(4)], stack=[FALSE, TRUE]) == [FALSE, TRUE, FALSE, TRUE]
2022-03-29 07:29:18 +00:00
def test_rot(vm):
2022-03-29 07:29:18 +00:00
assert vm.run([
2022-07-16 01:33:32 +00:00
isa.ROT(2),
isa.RETURN(2)
2022-06-28 04:35:21 +00:00
], stack=[FALSE, TRUE]) == [TRUE, FALSE]
2022-03-29 07:29:18 +00:00
assert vm.run([
2022-07-16 01:33:32 +00:00
isa.ROT(2),
isa.RETURN(5)
], stack=[TRUE, TRUE, TRUE, FALSE, TRUE]) == [TRUE, TRUE, TRUE, TRUE, FALSE]
assert vm.run([
isa.ROT(3),
isa.RETURN(3)
2022-06-28 04:35:21 +00:00
], stack=[FALSE, TRUE, FALSE]) == [FALSE, FALSE, TRUE]
2022-03-29 07:29:18 +00:00
def test_drop(vm):
2022-03-29 07:29:18 +00:00
assert vm.run([
2022-07-16 01:33:32 +00:00
isa.DROP(1),
isa.RETURN(1)
2022-06-28 04:35:21 +00:00
], stack=[TRUE, FALSE]) == [TRUE]
2022-03-29 07:29:18 +00:00
def test_dup_too_many(vm):
with pytest.raises(InterpreterError):
2022-07-16 01:33:32 +00:00
vm.run([isa.DUP(1)])
with pytest.raises(InterpreterError):
2022-07-16 01:33:32 +00:00
vm.run([isa.DUP(2)], stack=[FALSE])
def test_rot_too_many(vm):
with pytest.raises(InterpreterError):
2022-07-16 01:33:32 +00:00
vm.run([isa.ROT(1)])
with pytest.raises(InterpreterError):
2022-07-16 01:33:32 +00:00
vm.run([isa.ROT(2)], stack=[FALSE])
def test_drop_too_many(vm):
with pytest.raises(InterpreterError):
2022-07-16 01:33:32 +00:00
vm.run([isa.DROP(1)])
with pytest.raises(InterpreterError):
2022-07-16 01:33:32 +00:00
vm.run([isa.DROP(2)], stack=[FALSE])