def test_mismatch_type_mod(self) -> None: with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([pt.Return(pt.Mod(pt.Int8(2), pt.Int32(2)))]), ) with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([pt.Return(pt.Mod(pt.Int32(2), pt.Int8(2)))]), ) with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int64_t, pt.Block([pt.Return(pt.Mod(pt.Int16(2), pt.Int64(2)))]), ) with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int64_t, pt.Block([pt.Return(pt.Mod(pt.Int64(2), pt.Int16(2)))]), )
def test_mismatch_type_deref(self) -> None: with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([pt.Return(pt.Deref(pt.Int64(123)))]), ) with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (ptr, ), (), pt.Block([ pt.DefineVar(v32, pt.Deref(pt.Var(ptr))), ]))
def test_return_call_nothing_for_int32_t(self) -> None: with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "nothing", (), (), pt.Block([pt.Return(())])).add_func( "call_nothing", (), pt.Int32_t, pt.Block([pt.Return(pt.Call("nothing", []))]), )
def test_assign_undeclared_variable(self) -> None: with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([ pt.Assign(pt.Var(x), pt.Int32(500)), pt.Return(pt.Int32(2)), ]), )
def test_declare_wrong_type(self) -> None: with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([ pt.DefineVar(z, pt.Int32(2)), pt.Return(pt.Int32(2)), ]), ) with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([ pt.DefineVar(x, pt.Int8(2)), pt.Return(pt.Int32(2)), ]), )
def test_redeclared_variable(self) -> None: with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([ pt.DefineVar(x, pt.Int32(2)), pt.DefineVar(x, pt.Int32(3)), pt.Return(pt.Int32(2)), ]), )
from typing import cast, Callable import petra as pt import unittest from ctypes import CFUNCTYPE, c_int8, c_int16, c_int32, c_int64 program = pt.Program("module") # Int8_t functions. # TODO(adbenson): refactor this to take in arguments for better testing a = pt.Symbol(pt.Int8_t, "a") b = pt.Symbol(pt.Int8_t, "b") c = pt.Symbol(pt.Int8_t, "c") d = pt.Symbol(pt.Int8_t, "d") e = pt.Symbol(pt.Int8_t, "e") f = pt.Symbol(pt.Int8_t, "f") g = pt.Symbol(pt.Int8_t, "g") h = pt.Symbol(pt.Int8_t, "h") i = pt.Symbol(pt.Int8_t, "i") program.add_func( "add_i8", (), pt.Int8_t, pt.Block([ pt.DefineVar(a, pt.Add(pt.Int8(-11), pt.Int8(-4))), pt.DefineVar(b, pt.Add(pt.Int8(-11), pt.Int8(0))), pt.DefineVar(c, pt.Add(pt.Int8(-11), pt.Int8(7))),