prog8/tests/test_zp.py

84 lines
4.2 KiB
Python
Raw Normal View History

import pytest
2018-01-13 00:19:45 +00:00
from il65.compile import Zeropage, CompileError
2018-01-13 15:09:23 +00:00
from il65.plyparse import ZpOptions, VarDef
from il65.plylex import SourceRef
2018-01-13 00:19:45 +00:00
from il65.datatypes import DataType
def test_zp_names():
2018-01-13 15:09:23 +00:00
sref = SourceRef("test", 1, 1)
2018-02-02 21:42:09 +00:00
zp = Zeropage(ZpOptions.NOCLOBBER, False)
with pytest.raises(AssertionError):
2018-01-13 15:09:23 +00:00
zp.allocate(VarDef(name="", vartype="memory", datatype=DataType.BYTE, sourceref=sref))
zp.allocate(VarDef(name="", vartype="var", datatype=DataType.BYTE, sourceref=sref))
zp.allocate(VarDef(name="", vartype="var", datatype=DataType.BYTE, sourceref=sref))
zp.allocate(VarDef(name="varname", vartype="var", datatype=DataType.BYTE, sourceref=sref))
with pytest.raises(AssertionError):
zp.allocate(VarDef(name="varname", vartype="var", datatype=DataType.BYTE, sourceref=sref))
zp.allocate(VarDef(name="varname2", vartype="var", datatype=DataType.BYTE, sourceref=sref))
def test_zp_noclobber_allocation():
2018-01-13 15:09:23 +00:00
sref = SourceRef("test", 1, 1)
2018-02-02 21:42:09 +00:00
zp = Zeropage(ZpOptions.NOCLOBBER, True)
assert zp.available() == 9
2018-01-13 00:19:45 +00:00
with pytest.raises(CompileError):
2018-01-13 15:09:23 +00:00
# in regular zp there aren't 5 sequential bytes free
zp.allocate(VarDef(name="impossible", vartype="var", datatype=DataType.FLOAT, sourceref=sref))
for i in range(zp.available()):
2018-01-13 15:09:23 +00:00
loc = zp.allocate(VarDef(name="bvar"+str(i), vartype="var", datatype=DataType.BYTE, sourceref=sref))
2018-01-13 00:19:45 +00:00
assert loc > 0
assert zp.available() == 0
2018-01-13 00:19:45 +00:00
with pytest.raises(CompileError):
2018-01-13 15:09:23 +00:00
zp.allocate(VarDef(name="", vartype="var", datatype=DataType.BYTE, sourceref=sref))
2018-01-13 00:19:45 +00:00
with pytest.raises(CompileError):
2018-01-13 15:09:23 +00:00
zp.allocate(VarDef(name="", vartype="var", datatype=DataType.WORD, sourceref=sref))
2018-02-02 21:42:09 +00:00
def test_zp_float_enable():
sref = SourceRef("test", 1, 1)
zp = Zeropage(ZpOptions.CLOBBER, False)
with pytest.raises(TypeError):
zp.allocate(VarDef(name="", vartype="var", datatype=DataType.FLOAT, sourceref=sref))
zp = Zeropage(ZpOptions.CLOBBER, True)
zp.allocate(VarDef(name="", vartype="var", datatype=DataType.FLOAT, sourceref=sref))
def test_zp_clobber_allocation():
2018-01-13 15:09:23 +00:00
sref = SourceRef("test", 1, 1)
2018-02-02 21:42:09 +00:00
zp = Zeropage(ZpOptions.CLOBBER, True)
2017-12-31 02:19:06 +00:00
assert zp.available() == 239
2018-01-13 15:09:23 +00:00
loc = zp.allocate(VarDef(name="", vartype="var", datatype=DataType.FLOAT, sourceref=sref))
assert loc > 3 and loc not in zp.free
num, rest = divmod(zp.available(), 5)
2017-12-31 02:19:06 +00:00
for _ in range(num-3):
2018-01-13 15:09:23 +00:00
zp.allocate(VarDef(name="", vartype="var", datatype=DataType.FLOAT, sourceref=sref))
2017-12-31 02:19:06 +00:00
assert zp.available() == 19
2018-01-13 00:19:45 +00:00
with pytest.raises(CompileError):
2018-01-13 15:09:23 +00:00
# can't allocate because no more sequential bytes, only fragmented
zp.allocate(VarDef(name="", vartype="var", datatype=DataType.FLOAT, sourceref=sref))
2017-12-31 02:19:06 +00:00
for _ in range(14):
2018-01-13 15:09:23 +00:00
zp.allocate(VarDef(name="", vartype="var", datatype=DataType.BYTE, sourceref=sref))
zp.allocate(VarDef(name="", vartype="var", datatype=DataType.WORD, sourceref=sref))
zp.allocate(VarDef(name="", vartype="var", datatype=DataType.WORD, sourceref=sref))
2018-01-13 00:19:45 +00:00
with pytest.raises(CompileError):
2018-01-13 15:09:23 +00:00
zp.allocate(VarDef(name="", vartype="var", datatype=DataType.WORD, sourceref=sref))
assert zp.available() == 1
2018-01-13 15:09:23 +00:00
zp.allocate(VarDef(name="last", vartype="var", datatype=DataType.BYTE, sourceref=sref))
2018-01-13 00:19:45 +00:00
with pytest.raises(CompileError):
2018-01-13 15:09:23 +00:00
zp.allocate(VarDef(name="impossible", vartype="var", datatype=DataType.BYTE, sourceref=sref))
2018-01-13 00:19:45 +00:00
def test_zp_efficient_allocation():
# free = [0x04, 0x05, 0x06, 0x2a, 0x52, 0xf7, 0xf8, 0xf9, 0xfa]
2018-01-13 15:09:23 +00:00
sref = SourceRef("test", 1, 1)
2018-02-02 21:42:09 +00:00
zp = Zeropage(ZpOptions.NOCLOBBER, False)
2018-01-13 00:19:45 +00:00
assert zp.available() == 9
2018-01-13 15:09:23 +00:00
assert 0x2a == zp.allocate(VarDef(name="", vartype="var", datatype=DataType.BYTE, sourceref=sref))
assert 0x52 == zp.allocate(VarDef(name="", vartype="var", datatype=DataType.BYTE, sourceref=sref))
assert 0x04 == zp.allocate(VarDef(name="", vartype="var", datatype=DataType.WORD, sourceref=sref))
assert 0xf7 == zp.allocate(VarDef(name="", vartype="var", datatype=DataType.WORD, sourceref=sref))
assert 0x06 == zp.allocate(VarDef(name="", vartype="var", datatype=DataType.BYTE, sourceref=sref))
assert 0xf9 == zp.allocate(VarDef(name="", vartype="var", datatype=DataType.WORD, sourceref=sref))
2018-01-13 00:19:45 +00:00
assert zp.available() == 0