2022-02-21 00:32:29 +00:00
|
|
|
package prog8tests
|
|
|
|
|
|
|
|
import io.kotest.core.spec.style.FunSpec
|
|
|
|
import io.kotest.matchers.shouldBe
|
2022-03-10 21:38:16 +00:00
|
|
|
import prog8.code.core.DataType
|
|
|
|
import prog8.code.core.NumericDatatypes
|
|
|
|
import prog8.code.core.RegisterOrPair
|
2022-03-11 19:35:25 +00:00
|
|
|
import prog8.compiler.BuiltinFunctions
|
2022-02-21 00:32:29 +00:00
|
|
|
|
|
|
|
class TestBuiltinFunctions: FunSpec({
|
|
|
|
|
|
|
|
test("pure func with fixed type") {
|
2022-04-12 21:58:19 +00:00
|
|
|
val func = BuiltinFunctions.getValue("sgn")
|
|
|
|
func.name shouldBe "sgn"
|
2022-02-21 00:32:29 +00:00
|
|
|
func.parameters.size shouldBe 1
|
2022-04-12 21:58:19 +00:00
|
|
|
func.parameters[0].name shouldBe "value"
|
|
|
|
func.parameters[0].possibleDatatypes shouldBe NumericDatatypes
|
2022-02-21 00:32:29 +00:00
|
|
|
func.pure shouldBe true
|
|
|
|
func.hasReturn shouldBe true
|
2022-04-12 21:58:19 +00:00
|
|
|
func.returnType shouldBe DataType.BYTE
|
2022-02-21 00:32:29 +00:00
|
|
|
|
|
|
|
val conv = func.callConvention(listOf(DataType.UBYTE))
|
|
|
|
conv.params.size shouldBe 1
|
|
|
|
conv.params[0].dt shouldBe DataType.UBYTE
|
|
|
|
conv.params[0].reg shouldBe RegisterOrPair.A
|
|
|
|
conv.params[0].variable shouldBe false
|
2022-04-12 21:58:19 +00:00
|
|
|
conv.returns.dt shouldBe DataType.BYTE
|
2022-02-21 00:32:29 +00:00
|
|
|
conv.returns.floatFac1 shouldBe false
|
|
|
|
conv.returns.reg shouldBe RegisterOrPair.A
|
|
|
|
}
|
|
|
|
|
|
|
|
test("not-pure func with fixed type") {
|
|
|
|
val func = BuiltinFunctions.getValue("rnd")
|
|
|
|
func.name shouldBe "rnd"
|
|
|
|
func.parameters.size shouldBe 0
|
|
|
|
func.pure shouldBe false
|
|
|
|
func.hasReturn shouldBe true
|
|
|
|
func.returnType shouldBe DataType.UBYTE
|
|
|
|
|
|
|
|
val conv = func.callConvention(emptyList())
|
|
|
|
conv.params.size shouldBe 0
|
|
|
|
conv.returns.dt shouldBe DataType.UBYTE
|
|
|
|
conv.returns.floatFac1 shouldBe false
|
|
|
|
conv.returns.reg shouldBe RegisterOrPair.A
|
|
|
|
}
|
|
|
|
|
|
|
|
test("func without return type") {
|
|
|
|
val func = BuiltinFunctions.getValue("poke")
|
|
|
|
func.name shouldBe "poke"
|
|
|
|
func.parameters.size shouldBe 2
|
|
|
|
func.parameters[0].name shouldBe "address"
|
|
|
|
func.parameters[0].possibleDatatypes shouldBe arrayOf(DataType.UWORD)
|
|
|
|
func.parameters[1].name shouldBe "value"
|
|
|
|
func.parameters[1].possibleDatatypes shouldBe arrayOf(DataType.UBYTE)
|
|
|
|
func.pure shouldBe false
|
|
|
|
func.hasReturn shouldBe false
|
|
|
|
func.returnType shouldBe null
|
|
|
|
|
|
|
|
val conv = func.callConvention(listOf(DataType.UWORD, DataType.UBYTE))
|
|
|
|
conv.params.size shouldBe 2
|
|
|
|
conv.params[0].dt shouldBe DataType.UWORD
|
|
|
|
conv.params[0].reg shouldBe null
|
|
|
|
conv.params[0].variable shouldBe true
|
|
|
|
conv.params[1].dt shouldBe DataType.UBYTE
|
|
|
|
conv.params[1].reg shouldBe null
|
|
|
|
conv.params[1].variable shouldBe true
|
|
|
|
conv.returns.dt shouldBe null
|
|
|
|
conv.returns.floatFac1 shouldBe false
|
|
|
|
conv.returns.reg shouldBe null
|
|
|
|
}
|
|
|
|
|
|
|
|
test("func with variable return type") {
|
|
|
|
val func = BuiltinFunctions.getValue("abs")
|
|
|
|
func.name shouldBe "abs"
|
|
|
|
func.parameters.size shouldBe 1
|
|
|
|
func.parameters[0].name shouldBe "value"
|
|
|
|
func.parameters[0].possibleDatatypes.toSet() shouldBe NumericDatatypes.toSet()
|
|
|
|
func.pure shouldBe true
|
|
|
|
func.hasReturn shouldBe true
|
|
|
|
func.returnType shouldBe null
|
|
|
|
|
|
|
|
val conv = func.callConvention(listOf(DataType.UWORD))
|
|
|
|
conv.params.size shouldBe 1
|
|
|
|
conv.params[0].dt shouldBe DataType.UWORD
|
|
|
|
conv.params[0].reg shouldBe RegisterOrPair.AY
|
|
|
|
conv.params[0].variable shouldBe false
|
|
|
|
conv.returns.dt shouldBe DataType.UWORD
|
|
|
|
conv.returns.floatFac1 shouldBe false
|
|
|
|
conv.returns.reg shouldBe RegisterOrPair.AY
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|