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

89 lines
1.9 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-06-01 01:25:18 +00:00
from ichor import *
2022-06-01 05:17:32 +00:00
import pytest
2022-03-29 07:29:18 +00:00
def test_true(vm):
2022-03-29 07:29:18 +00:00
assert vm.run([Opcode.TRUE(), Opcode.RETURN(1)]) == [True]
def test_false(vm):
2022-03-29 07:29:18 +00:00
assert vm.run([Opcode.FALSE(), Opcode.RETURN(1)]) == [False]
def test_return(vm):
2022-03-29 07:29:18 +00:00
assert vm.run([Opcode.FALSE(), Opcode.RETURN(0)]) == []
assert vm.run([Opcode.TRUE(), Opcode.FALSE(), Opcode.RETURN(1)]) == [False]
assert vm.run([Opcode.TRUE(), Opcode.FALSE(), Opcode.RETURN(2)]) == [False, True]
def test_dup(vm):
2022-03-29 07:29:18 +00:00
assert vm.run([
Opcode.TRUE(),
Opcode.FALSE(),
Opcode.DUP(1),
Opcode.RETURN(3)
]) == [False, False, True]
assert vm.run([
Opcode.TRUE(),
Opcode.FALSE(),
Opcode.DUP(2),
Opcode.RETURN(4)
]) == [False, True, False, True]
def test_rot(vm):
2022-03-29 07:29:18 +00:00
assert vm.run([
Opcode.TRUE(),
Opcode.FALSE(),
Opcode.ROT(2),
Opcode.RETURN(2)
]) == [True, False]
assert vm.run([
Opcode.FALSE(),
Opcode.TRUE(),
Opcode.FALSE(),
Opcode.ROT(3),
Opcode.RETURN(3)
]) == [False, False, True]
def test_drop(vm):
2022-03-29 07:29:18 +00:00
assert vm.run([
Opcode.TRUE(),
Opcode.FALSE(),
Opcode.DROP(1),
Opcode.RETURN(1)
]) == [True]
def test_dup_too_many(vm):
with pytest.raises(InterpreterError):
vm.run([Opcode.DUP(1)])
with pytest.raises(InterpreterError):
vm.run([Opcode.FALSE(), Opcode.DUP(2)])
def test_rot_too_many(vm):
with pytest.raises(InterpreterError):
vm.run([Opcode.ROT(1)])
with pytest.raises(InterpreterError):
vm.run([Opcode.TRUE(), Opcode.ROT(2)])
def test_drop_too_many(vm):
with pytest.raises(InterpreterError):
vm.run([Opcode.DROP(1)])
with pytest.raises(InterpreterError):
vm.run([Opcode.TRUE(), Opcode.DROP(2)])