2024-09-08 14:03:57 +00:00
|
|
|
package prog8tests.compiler
|
2022-02-10 23:21:40 +00:00
|
|
|
|
|
|
|
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.ExpressionError
|
|
|
|
import prog8.ast.expressions.ArrayLiteral
|
|
|
|
import prog8.ast.expressions.InferredTypes
|
|
|
|
import prog8.ast.expressions.NumericLiteral
|
|
|
|
import prog8.ast.expressions.StringLiteral
|
2024-10-08 22:54:17 +00:00
|
|
|
import prog8.ast.statements.AnonymousScope
|
2022-03-10 21:38:16 +00:00
|
|
|
import prog8.code.core.DataType
|
|
|
|
import prog8.code.core.Encoding
|
|
|
|
import prog8.code.core.Position
|
|
|
|
|
2022-02-10 23:21:40 +00:00
|
|
|
|
|
|
|
class TestNumericLiteral: FunSpec({
|
|
|
|
|
|
|
|
fun sameValueAndType(lv1: NumericLiteral, lv2: NumericLiteral): Boolean {
|
|
|
|
return lv1.type==lv2.type && lv1==lv2
|
|
|
|
}
|
|
|
|
|
|
|
|
val dummyPos = Position("test", 0, 0, 0)
|
|
|
|
|
|
|
|
test("testIdentity") {
|
|
|
|
val v = NumericLiteral(DataType.UWORD, 12345.0, dummyPos)
|
|
|
|
(v==v) shouldBe true
|
|
|
|
(v != v) shouldBe false
|
|
|
|
(v <= v) shouldBe true
|
|
|
|
(v >= v) shouldBe true
|
|
|
|
(v < v ) shouldBe false
|
|
|
|
(v > v ) shouldBe false
|
|
|
|
|
|
|
|
sameValueAndType(NumericLiteral(DataType.UWORD, 12345.0, dummyPos), NumericLiteral(DataType.UWORD, 12345.0, dummyPos)) shouldBe true
|
|
|
|
}
|
|
|
|
|
2023-12-15 21:05:57 +00:00
|
|
|
test("test truncating") {
|
2022-02-10 23:21:40 +00:00
|
|
|
shouldThrow<ExpressionError> {
|
|
|
|
NumericLiteral(DataType.BYTE, -2.345, dummyPos)
|
2023-12-15 21:05:57 +00:00
|
|
|
}.message shouldContain "refused truncating"
|
2022-02-10 23:21:40 +00:00
|
|
|
shouldThrow<ExpressionError> {
|
|
|
|
NumericLiteral(DataType.BYTE, -2.6, dummyPos)
|
2023-12-15 21:05:57 +00:00
|
|
|
}.message shouldContain "refused truncating"
|
2022-02-10 23:21:40 +00:00
|
|
|
shouldThrow<ExpressionError> {
|
|
|
|
NumericLiteral(DataType.UWORD, 2222.345, dummyPos)
|
2023-12-15 21:05:57 +00:00
|
|
|
}.message shouldContain "refused truncating"
|
2022-02-10 23:21:40 +00:00
|
|
|
NumericLiteral(DataType.UBYTE, 2.0, dummyPos).number shouldBe 2.0
|
|
|
|
NumericLiteral(DataType.BYTE, -2.0, dummyPos).number shouldBe -2.0
|
|
|
|
NumericLiteral(DataType.UWORD, 2222.0, dummyPos).number shouldBe 2222.0
|
|
|
|
NumericLiteral(DataType.FLOAT, 123.456, dummyPos)
|
|
|
|
}
|
|
|
|
|
|
|
|
test("testEqualsAndNotEquals") {
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) == NumericLiteral(DataType.UBYTE, 100.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) == NumericLiteral(DataType.UWORD, 100.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) == NumericLiteral(DataType.FLOAT, 100.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UWORD, 254.0, dummyPos) == NumericLiteral(DataType.UBYTE, 254.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UWORD, 12345.0, dummyPos) == NumericLiteral(DataType.UWORD, 12345.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UWORD, 12345.0, dummyPos) == NumericLiteral(DataType.FLOAT, 12345.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.FLOAT, 100.0, dummyPos) == NumericLiteral(DataType.UBYTE, 100.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.FLOAT, 22239.0, dummyPos) == NumericLiteral(DataType.UWORD, 22239.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.FLOAT, 9.99, dummyPos) == NumericLiteral(DataType.FLOAT, 9.99, dummyPos)) shouldBe true
|
|
|
|
|
|
|
|
sameValueAndType(NumericLiteral(DataType.UBYTE, 100.0, dummyPos), NumericLiteral(DataType.UBYTE, 100.0, dummyPos)) shouldBe true
|
|
|
|
sameValueAndType(NumericLiteral(DataType.UBYTE, 100.0, dummyPos), NumericLiteral(DataType.UWORD, 100.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.UBYTE, 100.0, dummyPos), NumericLiteral(DataType.FLOAT, 100.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.UWORD, 254.0, dummyPos), NumericLiteral(DataType.UBYTE, 254.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.UWORD, 12345.0, dummyPos), NumericLiteral(DataType.UWORD, 12345.0, dummyPos)) shouldBe true
|
|
|
|
sameValueAndType(NumericLiteral(DataType.UWORD, 12345.0, dummyPos), NumericLiteral(DataType.FLOAT, 12345.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.FLOAT, 100.0, dummyPos), NumericLiteral(DataType.UBYTE, 100.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.FLOAT, 22239.0, dummyPos), NumericLiteral(DataType.UWORD, 22239.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.FLOAT, 9.99, dummyPos), NumericLiteral(DataType.FLOAT, 9.99, dummyPos)) shouldBe true
|
|
|
|
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) != NumericLiteral(DataType.UBYTE, 101.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) != NumericLiteral(DataType.UWORD, 101.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) != NumericLiteral(DataType.FLOAT, 101.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UWORD, 245.0, dummyPos) != NumericLiteral(DataType.UBYTE, 246.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UWORD, 12345.0, dummyPos) != NumericLiteral(DataType.UWORD, 12346.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UWORD, 12345.0, dummyPos) != NumericLiteral(DataType.FLOAT, 12346.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.FLOAT, 9.99, dummyPos) != NumericLiteral(DataType.UBYTE, 9.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.FLOAT, 9.99, dummyPos) != NumericLiteral(DataType.UWORD, 9.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.FLOAT, 9.99, dummyPos) != NumericLiteral(DataType.FLOAT, 9.0, dummyPos)) shouldBe true
|
|
|
|
|
|
|
|
sameValueAndType(NumericLiteral(DataType.UBYTE, 100.0, dummyPos), NumericLiteral(DataType.UBYTE, 101.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.UBYTE, 100.0, dummyPos), NumericLiteral(DataType.UWORD, 101.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.UBYTE, 100.0, dummyPos), NumericLiteral(DataType.FLOAT, 101.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.UWORD, 245.0, dummyPos), NumericLiteral(DataType.UBYTE, 246.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.UWORD, 12345.0, dummyPos), NumericLiteral(DataType.UWORD, 12346.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.UWORD, 12345.0, dummyPos), NumericLiteral(DataType.FLOAT, 12346.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.FLOAT, 9.99, dummyPos), NumericLiteral(DataType.UBYTE, 9.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.FLOAT, 9.99, dummyPos), NumericLiteral(DataType.UWORD, 9.0, dummyPos)) shouldBe false
|
|
|
|
sameValueAndType(NumericLiteral(DataType.FLOAT, 9.99, dummyPos), NumericLiteral(DataType.FLOAT, 9.0, dummyPos)) shouldBe false
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
test("testEqualsRef") {
|
2024-08-21 16:23:30 +00:00
|
|
|
(StringLiteral.create("hello", Encoding.PETSCII, dummyPos) == StringLiteral.create("hello", Encoding.PETSCII, dummyPos)) shouldBe true
|
|
|
|
(StringLiteral.create("hello", Encoding.PETSCII, dummyPos) != StringLiteral.create("bye", Encoding.PETSCII, dummyPos)) shouldBe true
|
|
|
|
(StringLiteral.create("hello", Encoding.SCREENCODES, dummyPos) == StringLiteral.create("hello", Encoding.SCREENCODES, dummyPos)) shouldBe true
|
|
|
|
(StringLiteral.create("hello", Encoding.SCREENCODES, dummyPos) != StringLiteral.create("bye", Encoding.SCREENCODES, dummyPos)) shouldBe true
|
|
|
|
(StringLiteral.create("hello", Encoding.SCREENCODES, dummyPos) != StringLiteral.create("hello", Encoding.PETSCII, dummyPos)) shouldBe true
|
2022-02-10 23:21:40 +00:00
|
|
|
|
|
|
|
val lvOne = NumericLiteral(DataType.UBYTE, 1.0, dummyPos)
|
|
|
|
val lvTwo = NumericLiteral(DataType.UBYTE, 2.0, dummyPos)
|
|
|
|
val lvThree = NumericLiteral(DataType.UBYTE, 3.0, dummyPos)
|
|
|
|
val lvOneR = NumericLiteral(DataType.UBYTE, 1.0, dummyPos)
|
|
|
|
val lvTwoR = NumericLiteral(DataType.UBYTE, 2.0, dummyPos)
|
|
|
|
val lvThreeR = NumericLiteral(DataType.UBYTE, 3.0, dummyPos)
|
|
|
|
val lvFour= NumericLiteral(DataType.UBYTE, 4.0, dummyPos)
|
|
|
|
val lv1 = ArrayLiteral(InferredTypes.InferredType.known(DataType.ARRAY_UB), arrayOf(lvOne, lvTwo, lvThree), dummyPos)
|
|
|
|
val lv2 = ArrayLiteral(InferredTypes.InferredType.known(DataType.ARRAY_UB), arrayOf(lvOneR, lvTwoR, lvThreeR), dummyPos)
|
|
|
|
val lv3 = ArrayLiteral(InferredTypes.InferredType.known(DataType.ARRAY_UB), arrayOf(lvOneR, lvTwoR, lvFour), dummyPos)
|
|
|
|
lv1 shouldBe lv2
|
|
|
|
lv1 shouldNotBe lv3
|
|
|
|
}
|
|
|
|
|
|
|
|
test("testGreaterThan") {
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) > NumericLiteral(DataType.UBYTE, 99.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UWORD, 254.0, dummyPos) > NumericLiteral(DataType.UWORD, 253.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.FLOAT, 100.0, dummyPos) > NumericLiteral(DataType.FLOAT, 99.9, dummyPos)) shouldBe true
|
|
|
|
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) >= NumericLiteral(DataType.UBYTE, 100.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UWORD, 254.0, dummyPos) >= NumericLiteral(DataType.UWORD, 254.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.FLOAT, 100.0, dummyPos) >= NumericLiteral(DataType.FLOAT, 100.0, dummyPos)) shouldBe true
|
|
|
|
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) > NumericLiteral(DataType.UBYTE, 100.0, dummyPos)) shouldBe false
|
|
|
|
(NumericLiteral(DataType.UWORD, 254.0, dummyPos) > NumericLiteral(DataType.UWORD, 254.0, dummyPos)) shouldBe false
|
|
|
|
(NumericLiteral(DataType.FLOAT, 100.0, dummyPos) > NumericLiteral(DataType.FLOAT, 100.0, dummyPos)) shouldBe false
|
|
|
|
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) >= NumericLiteral(DataType.UBYTE, 101.0, dummyPos)) shouldBe false
|
|
|
|
(NumericLiteral(DataType.UWORD, 254.0, dummyPos) >= NumericLiteral(DataType.UWORD, 255.0, dummyPos)) shouldBe false
|
|
|
|
(NumericLiteral(DataType.FLOAT, 100.0, dummyPos) >= NumericLiteral(DataType.FLOAT, 100.1, dummyPos)) shouldBe false
|
|
|
|
}
|
|
|
|
|
|
|
|
test("testLessThan") {
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) < NumericLiteral(DataType.UBYTE, 101.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UWORD, 254.0, dummyPos) < NumericLiteral(DataType.UWORD, 255.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.FLOAT, 100.0, dummyPos) < NumericLiteral(DataType.FLOAT, 100.1, dummyPos)) shouldBe true
|
|
|
|
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) <= NumericLiteral(DataType.UBYTE, 100.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.UWORD, 254.0, dummyPos) <= NumericLiteral(DataType.UWORD, 254.0, dummyPos)) shouldBe true
|
|
|
|
(NumericLiteral(DataType.FLOAT, 100.0, dummyPos) <= NumericLiteral(DataType.FLOAT, 100.0, dummyPos)) shouldBe true
|
|
|
|
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) < NumericLiteral(DataType.UBYTE, 100.0, dummyPos)) shouldBe false
|
|
|
|
(NumericLiteral(DataType.UWORD, 254.0, dummyPos) < NumericLiteral(DataType.UWORD, 254.0, dummyPos)) shouldBe false
|
|
|
|
(NumericLiteral(DataType.FLOAT, 100.0, dummyPos) < NumericLiteral(DataType.FLOAT, 100.0, dummyPos)) shouldBe false
|
|
|
|
|
|
|
|
(NumericLiteral(DataType.UBYTE, 100.0, dummyPos) <= NumericLiteral(DataType.UBYTE, 99.0, dummyPos)) shouldBe false
|
|
|
|
(NumericLiteral(DataType.UWORD, 254.0, dummyPos) <= NumericLiteral(DataType.UWORD, 253.0, dummyPos)) shouldBe false
|
|
|
|
(NumericLiteral(DataType.FLOAT, 100.0, dummyPos) <= NumericLiteral(DataType.FLOAT, 99.9, dummyPos)) shouldBe false
|
|
|
|
}
|
|
|
|
|
2022-06-05 13:35:29 +00:00
|
|
|
test("optimalInteger") {
|
|
|
|
NumericLiteral.optimalInteger(10, Position.DUMMY).type shouldBe DataType.UBYTE
|
|
|
|
NumericLiteral.optimalInteger(10, Position.DUMMY).number shouldBe 10.0
|
|
|
|
NumericLiteral.optimalInteger(-10, Position.DUMMY).type shouldBe DataType.BYTE
|
|
|
|
NumericLiteral.optimalInteger(-10, Position.DUMMY).number shouldBe -10.0
|
|
|
|
NumericLiteral.optimalInteger(1000, Position.DUMMY).type shouldBe DataType.UWORD
|
|
|
|
NumericLiteral.optimalInteger(-1000, Position.DUMMY).number shouldBe -1000.0
|
|
|
|
NumericLiteral.optimalInteger(1000u, Position.DUMMY).type shouldBe DataType.UWORD
|
|
|
|
NumericLiteral.optimalInteger(1000u, Position.DUMMY).number shouldBe 1000.0
|
|
|
|
}
|
|
|
|
|
|
|
|
test("optimalNumeric") {
|
|
|
|
NumericLiteral.optimalNumeric(10, Position.DUMMY).type shouldBe DataType.UBYTE
|
|
|
|
NumericLiteral.optimalNumeric(10, Position.DUMMY).number shouldBe 10.0
|
|
|
|
NumericLiteral.optimalNumeric(-10, Position.DUMMY).type shouldBe DataType.BYTE
|
|
|
|
NumericLiteral.optimalNumeric(-10, Position.DUMMY).number shouldBe -10.0
|
|
|
|
NumericLiteral.optimalNumeric(1000, Position.DUMMY).type shouldBe DataType.UWORD
|
|
|
|
NumericLiteral.optimalNumeric(1000, Position.DUMMY).number shouldBe 1000.0
|
|
|
|
NumericLiteral.optimalNumeric(-1000, Position.DUMMY).type shouldBe DataType.WORD
|
|
|
|
NumericLiteral.optimalNumeric(-1000, Position.DUMMY).number shouldBe -1000.0
|
|
|
|
NumericLiteral.optimalNumeric(1.123, Position.DUMMY).type shouldBe DataType.FLOAT
|
|
|
|
NumericLiteral.optimalNumeric(1.123, Position.DUMMY).number shouldBe 1.123
|
|
|
|
NumericLiteral.optimalNumeric(1.0, Position.DUMMY).type shouldBe DataType.UBYTE
|
|
|
|
NumericLiteral.optimalNumeric(1.0, Position.DUMMY).number shouldBe 1.0
|
|
|
|
NumericLiteral.optimalNumeric(-1.0, Position.DUMMY).type shouldBe DataType.BYTE
|
|
|
|
NumericLiteral.optimalNumeric(-1.0, Position.DUMMY).number shouldBe -1.0
|
|
|
|
NumericLiteral.optimalNumeric(1234.0, Position.DUMMY).type shouldBe DataType.UWORD
|
|
|
|
NumericLiteral.optimalNumeric(1234.0, Position.DUMMY).number shouldBe 1234.0
|
|
|
|
NumericLiteral.optimalNumeric(-1234.0, Position.DUMMY).type shouldBe DataType.WORD
|
|
|
|
NumericLiteral.optimalNumeric(-1234.0, Position.DUMMY).number shouldBe -1234.0
|
|
|
|
}
|
2024-10-08 22:54:17 +00:00
|
|
|
|
|
|
|
test("cast can change value") {
|
|
|
|
fun num(dt: DataType, num: Double): NumericLiteral {
|
|
|
|
val n = NumericLiteral(dt, num, Position.DUMMY)
|
|
|
|
n.linkParents(AnonymousScope(mutableListOf(), Position.DUMMY))
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
val cast1 = num(DataType.UBYTE, 200.0).cast(DataType.BYTE, false)
|
|
|
|
cast1.isValid shouldBe true
|
|
|
|
cast1.valueOrZero().number shouldBe -56.0
|
|
|
|
val cast2 = num(DataType.BYTE, -50.0).cast(DataType.UBYTE, false)
|
|
|
|
cast2.isValid shouldBe true
|
|
|
|
cast2.valueOrZero().number shouldBe 206.0
|
|
|
|
val cast3 = num(DataType.UWORD, 55555.0).cast(DataType.WORD, false)
|
|
|
|
cast3.isValid shouldBe true
|
|
|
|
cast3.valueOrZero().number shouldBe -9981.0
|
|
|
|
val cast4 = num(DataType.WORD, -3333.0).cast(DataType.UWORD, false)
|
|
|
|
cast4.isValid shouldBe true
|
|
|
|
cast4.valueOrZero().number shouldBe 62203.0
|
|
|
|
}
|
|
|
|
|
|
|
|
test("convert cannot change value") {
|
|
|
|
fun num(dt: DataType, num: Double): NumericLiteral {
|
|
|
|
val n = NumericLiteral(dt, num, Position.DUMMY)
|
|
|
|
n.linkParents(AnonymousScope(mutableListOf(), Position.DUMMY))
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
num(DataType.UBYTE, 200.0).convertTypeKeepValue(DataType.BYTE).isValid shouldBe false
|
|
|
|
num(DataType.BYTE, -50.0).convertTypeKeepValue(DataType.UBYTE).isValid shouldBe false
|
|
|
|
num(DataType.UWORD, 55555.0).convertTypeKeepValue(DataType.WORD).isValid shouldBe false
|
|
|
|
num(DataType.WORD, -3333.0).convertTypeKeepValue(DataType.UWORD).isValid shouldBe false
|
|
|
|
|
|
|
|
num(DataType.UBYTE, 42.0).convertTypeKeepValue(DataType.BYTE).isValid shouldBe true
|
|
|
|
num(DataType.BYTE, 42.0).convertTypeKeepValue(DataType.UBYTE).isValid shouldBe true
|
|
|
|
num(DataType.UWORD, 12345.0).convertTypeKeepValue(DataType.WORD).isValid shouldBe true
|
|
|
|
num(DataType.WORD, 12345.0).convertTypeKeepValue(DataType.UWORD).isValid shouldBe true
|
|
|
|
}
|
2022-02-10 23:21:40 +00:00
|
|
|
})
|