From 44d82f9190763aa5113a3dce4a7d4623018a1c25 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 28 Dec 2023 13:30:07 +0100 Subject: [PATCH] add unit tests --- compiler/test/TestOptimization.kt | 23 +++++++++++++ compiler/test/ast/TestVariousCompilerAst.kt | 38 +++++++++++++++++++++ docs/source/todo.rst | 1 - 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/compiler/test/TestOptimization.kt b/compiler/test/TestOptimization.kt index 6939ac85e..9898ad0ae 100644 --- a/compiler/test/TestOptimization.kt +++ b/compiler/test/TestOptimization.kt @@ -876,4 +876,27 @@ main { compileText(Cx16Target(), true, src, writeAssembly = false, errors = errors) shouldBe null errors.errors.single() shouldContain "undefined symbol" } + + test("var to const") { + val src=""" +main { + sub start() { + ubyte xx=10 ; to const + ubyte @shared yy=20 ; remain var + cx16.r0L = xx+yy + } +}""" + val errors = ErrorReporterForTests() + val result = compileText(Cx16Target(), true, src, writeAssembly = false, errors = errors)!! + val st = result.compilerAst.entrypoint.statements + st.size shouldBe 4 + val xxConst = st[0] as VarDecl + xxConst.type shouldBe VarDeclType.CONST + xxConst.name shouldBe "xx" + (xxConst.value as? NumericLiteral)?.number shouldBe 10.0 + (st[1] as VarDecl).type shouldBe VarDeclType.VAR + val expr = (st[3] as Assignment).value as BinaryExpression + (expr.left as? IdentifierReference)?.nameInSource shouldBe listOf("yy") + (expr.right as? NumericLiteral)?.number shouldBe 10.0 + } }) diff --git a/compiler/test/ast/TestVariousCompilerAst.kt b/compiler/test/ast/TestVariousCompilerAst.kt index f788a454d..d6a8681b1 100644 --- a/compiler/test/ast/TestVariousCompilerAst.kt +++ b/compiler/test/ast/TestVariousCompilerAst.kt @@ -455,5 +455,43 @@ main { }""" compileText(VMTarget(), optimize=false, src, writeAssembly=false) shouldNotBe null } + + test("chained comparison") { + val src=""" +main { + sub start() { + ubyte @shared x = 1 + ubyte @shared y = 2 + ubyte @shared z = 3 + x = x==y==z + y = 4" + right2.operator shouldBe "<" + (left2.left as? IdentifierReference)?.nameInSource shouldBe listOf("x") + (left2.right as? NumericLiteral)?.number shouldBe 4.0 + (right2.left as? IdentifierReference)?.nameInSource shouldBe listOf("x") + (right2.right as? NumericLiteral)?.number shouldBe 10.0 + } }) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 9a831f16e..350c27b69 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,7 +2,6 @@ TODO ==== -- add unit tests for chained comparisons and for var -> const optimization - add INFO error level and move some warnings to info - add switch to enable INFO error messages (default is WARN and up)