add unit tests

This commit is contained in:
Irmen de Jong 2023-12-28 13:30:07 +01:00
parent 37fcde30d6
commit 44d82f9190
3 changed files with 61 additions and 1 deletions

View File

@ -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
}
})

View File

@ -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<x<10
}
}"""
val result=compileText(VMTarget(), optimize=false, src, writeAssembly=false)!!
val st = result.compilerAst.entrypoint.statements
st.size shouldBe 8
val comparison1 = (st[6] as Assignment).value as BinaryExpression
comparison1.operator shouldBe "and"
val left1 = comparison1.left as BinaryExpression
val right1 = comparison1.right as BinaryExpression
left1.operator shouldBe "=="
right1.operator shouldBe "=="
(left1.left as? IdentifierReference)?.nameInSource shouldBe listOf("x")
(left1.right as? IdentifierReference)?.nameInSource shouldBe listOf("y")
(right1.left as? IdentifierReference)?.nameInSource shouldBe listOf("y")
(right1.right as? IdentifierReference)?.nameInSource shouldBe listOf("z")
val comparison2 = (st[7] as Assignment).value as BinaryExpression
comparison2.operator shouldBe "and"
val left2 = comparison2.left as BinaryExpression
val right2 = comparison2.right as BinaryExpression
left2.operator shouldBe ">"
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
}
})

View File

@ -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)