From 42c8720e8b5af3eb428e1d9eeda31a22f4aa0a4b Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 18 Nov 2021 22:54:49 +0100 Subject: [PATCH] fix float rounding tests --- compiler/test/TestMemory.kt | 12 ++++++------ compiler/test/TestNumericLiteralValue.kt | 21 ++++++++++++++++----- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/compiler/test/TestMemory.kt b/compiler/test/TestMemory.kt index 5d4289217..8bf7bde4f 100644 --- a/compiler/test/TestMemory.kt +++ b/compiler/test/TestMemory.kt @@ -154,7 +154,7 @@ class TestMemory: FunSpec({ val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY) val subroutine = Subroutine("test", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY) val module = Module(mutableListOf(subroutine), Position.DUMMY, SourceCode.Generated("test")) - val program = Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder) + Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder) .addModule(module) target.isIOAddress(C64Target.machine) shouldBe false } @@ -166,7 +166,7 @@ class TestMemory: FunSpec({ val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY) val subroutine = Subroutine("test", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY) val module = Module(mutableListOf(subroutine), Position.DUMMY, SourceCode.Generated("test")) - val program = Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder) + Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder) .addModule(module) target.isIOAddress(C64Target.machine) shouldBe false } @@ -178,7 +178,7 @@ class TestMemory: FunSpec({ val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY) val subroutine = Subroutine("test", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY) val module = Module(mutableListOf(subroutine), Position.DUMMY, SourceCode.Generated("test")) - val program = Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder) + Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder) .addModule(module) target.isIOAddress(C64Target.machine) shouldBe true } @@ -190,7 +190,7 @@ class TestMemory: FunSpec({ val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY) val subroutine = Subroutine("test", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY) val module = Module(mutableListOf(subroutine), Position.DUMMY, SourceCode.Generated("test")) - val program = Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder) + Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder) .addModule(module) target.isIOAddress(C64Target.machine) shouldBe false } @@ -203,7 +203,7 @@ class TestMemory: FunSpec({ val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY) val subroutine = Subroutine("test", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY) val module = Module(mutableListOf(subroutine), Position.DUMMY, SourceCode.Generated("test")) - val program = Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder) + Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder) .addModule(module) target.isIOAddress(C64Target.machine) shouldBe false } @@ -216,7 +216,7 @@ class TestMemory: FunSpec({ val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY) val subroutine = Subroutine("test", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY) val module = Module(mutableListOf(subroutine), Position.DUMMY, SourceCode.Generated("test")) - val program = Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder) + Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder) .addModule(module) target.isIOAddress(C64Target.machine) shouldBe true } diff --git a/compiler/test/TestNumericLiteralValue.kt b/compiler/test/TestNumericLiteralValue.kt index 70f2bcc84..6e83333b4 100644 --- a/compiler/test/TestNumericLiteralValue.kt +++ b/compiler/test/TestNumericLiteralValue.kt @@ -1,9 +1,12 @@ package prog8tests +import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe +import io.kotest.matchers.string.shouldContain import prog8.ast.base.DataType +import prog8.ast.base.ExpressionError import prog8.ast.base.Position import prog8.ast.expressions.ArrayLiteralValue import prog8.ast.expressions.InferredTypes @@ -31,11 +34,19 @@ class TestNumericLiteralValue: FunSpec({ } test("test rounding") { - NumericLiteralValue(DataType.BYTE, -2.345, dummyPos).number shouldBe -2.0 - NumericLiteralValue(DataType.BYTE, -2.6, dummyPos).number shouldBe -3.0 - NumericLiteralValue(DataType.UWORD, 2222.345, dummyPos).number shouldBe 2222.0 - NumericLiteralValue(DataType.UWORD, 2222.6, dummyPos).number shouldBe 2223.0 - NumericLiteralValue(DataType.FLOAT, 123.456, dummyPos).number shouldBe 123.456 + shouldThrow { + NumericLiteralValue(DataType.BYTE, -2.345, dummyPos) + }.message shouldContain "refused silent rounding" + shouldThrow { + NumericLiteralValue(DataType.BYTE, -2.6, dummyPos) + }.message shouldContain "refused silent rounding" + shouldThrow { + NumericLiteralValue(DataType.UWORD, 2222.345, dummyPos) + }.message shouldContain "refused silent rounding" + NumericLiteralValue(DataType.UBYTE, 2.0, dummyPos).number shouldBe 2.0 + NumericLiteralValue(DataType.BYTE, -2.0, dummyPos).number shouldBe -2.0 + NumericLiteralValue(DataType.UWORD, 2222.0, dummyPos).number shouldBe 2222.0 + NumericLiteralValue(DataType.FLOAT, 123.456, dummyPos) } test("testEqualsAndNotEquals") {