Skip to content

Commit

Permalink
using PredefinedFunc's in parser and typer tests;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Jan 16, 2019
1 parent 9d028b6 commit 1bce5f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
33 changes: 19 additions & 14 deletions src/test/scala/sigmastate/lang/SigmaParserTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import org.scalatest.{Matchers, PropSpec}
import sigmastate.SCollection.SByteArray
import sigmastate.Values._
import sigmastate._
import sigmastate.lang.SigmaPredef.PredefinedFuncRegistry
import sigmastate.lang.Terms._
import sigmastate.lang.syntax.ParserException
import sigmastate.serialization.OpCodes

class SigmaParserTest extends PropSpec with PropertyChecks with Matchers with LangTests {
import StdSigmaBuilder._

private val predefFuncRegistry = new PredefinedFuncRegistry(StdSigmaBuilder)
import predefFuncRegistry._

def parse(x: String): SValue = {
SigmaParser(x, TransformingSigmaBuilder) match {
case Parsed.Success(v, _) => v
Expand Down Expand Up @@ -503,8 +507,8 @@ class SigmaParserTest extends PropSpec with PropertyChecks with Matchers with La
}

property("fromBaseX string decoding") {
parse("""fromBase58("111")""") shouldBe Apply(Ident("fromBase58"), IndexedSeq(StringConstant("111")))
parse("""fromBase64("111")""") shouldBe Apply(Ident("fromBase64"), IndexedSeq(StringConstant("111")))
parse("""fromBase58("111")""") shouldBe Apply(FromBase58Func.symNoType, IndexedSeq(StringConstant("111")))
parse("""fromBase64("111")""") shouldBe Apply(FromBase64Func.symNoType, IndexedSeq(StringConstant("111")))
}

property("PK") {
Expand All @@ -513,24 +517,25 @@ class SigmaParserTest extends PropSpec with PropertyChecks with Matchers with La

property("deserialize") {
parse("""deserialize[GroupElement]("12345")""") shouldBe
Apply(ApplyTypes(Ident("deserialize"), Seq(SGroupElement)), IndexedSeq(StringConstant("12345")))
Apply(ApplyTypes(DeserializeFunc.symNoType, Seq(SGroupElement)), IndexedSeq(StringConstant("12345")))
parse("""deserialize[(GroupElement, Coll[(Int, Byte)])]("12345")""") shouldBe
Apply(ApplyTypes(Ident("deserialize"), Seq(STuple(SGroupElement, SCollection(STuple(SInt, SByte))))), IndexedSeq(StringConstant("12345")))
Apply(ApplyTypes(DeserializeFunc.symNoType, Seq(STuple(SGroupElement, SCollection(STuple(SInt, SByte))))), IndexedSeq(StringConstant("12345")))
}

property("ZKProof") {
parse("ZKProof { condition }") shouldBe Apply(Ident("ZKProof", SFunc(SSigmaProp, SBoolean)), IndexedSeq(Ident("condition")))
parse("ZKProof { condition }") shouldBe Apply(ZKProofFunc.sym, IndexedSeq(Ident("condition")))
parse("ZKProof { sigmaProp(HEIGHT > 1000) }") shouldBe
Apply(Ident("ZKProof", SFunc(SSigmaProp, SBoolean)),
IndexedSeq(Apply(Ident("sigmaProp"), IndexedSeq(GT(Ident("HEIGHT"), IntConstant(1000))))))
Apply(ZKProofFunc.sym,
IndexedSeq(Apply(SigmaPropFunc.symNoType, IndexedSeq(GT(Ident("HEIGHT"), IntConstant(1000))))))
}

property("invalid ZKProof (non block parameter)") {
an[ParserException] should be thrownBy parse("ZKProof HEIGHT > 1000 ")
}

property("sigmaProp") {
parse("sigmaProp(HEIGHT > 1000)") shouldBe Apply(Ident("sigmaProp"), IndexedSeq(GT(Ident("HEIGHT"), IntConstant(1000))))
parse("sigmaProp(HEIGHT > 1000)") shouldBe Apply(SigmaPropFunc.symNoType,
IndexedSeq(GT(Ident("HEIGHT"), IntConstant(1000))))
}

property("SBigInt.toBytes") {
Expand Down Expand Up @@ -558,7 +563,7 @@ class SigmaParserTest extends PropSpec with PropertyChecks with Matchers with La

property("byteArrayToLong") {
parse("byteArrayToLong(Coll[Byte](1.toByte))") shouldBe
Apply(Ident("byteArrayToLong"), Vector(
Apply(ByteArrayToLongFunc.symNoType, Vector(
Apply(
ApplyTypes(Ident("Coll", NoType), Vector(SByte)),
Vector(Select(IntConstant(1), "toByte", None)))
Expand All @@ -567,7 +572,7 @@ class SigmaParserTest extends PropSpec with PropertyChecks with Matchers with La

property("decodePoint") {
parse("decodePoint(Coll[Byte](1.toByte))") shouldBe
Apply(Ident("decodePoint"), Vector(
Apply(DecodePointFunc.symNoType, Vector(
Apply(
ApplyTypes(Ident("Coll", NoType), Vector(SByte)),
Vector(Select(IntConstant(1), "toByte", None)))
Expand All @@ -576,7 +581,7 @@ class SigmaParserTest extends PropSpec with PropertyChecks with Matchers with La

property("xorOf") {
parse("xorOf(Coll[Boolean](true, false))") shouldBe
Apply(Ident("xorOf"), Vector(
Apply(XorOfFunc.symNoType, Vector(
Apply(
ApplyTypes(Ident("Coll", NoType), Vector(SBoolean)),
Vector(TrueLeaf, FalseLeaf))
Expand Down Expand Up @@ -724,7 +729,7 @@ class SigmaParserTest extends PropSpec with PropertyChecks with Matchers with La
| { (b, i) => (b + i).toLong },
| { (b, s, i) => (b + s + i).toLong }
| )""".stripMargin) shouldBe Apply(
ApplyTypes(Ident("outerJoin", NoType), Vector(SByte, SShort, SInt, SLong)),
ApplyTypes(OuterJoinFunc.symNoType, Vector(SByte, SShort, SInt, SLong)),
Vector(
Apply(
ApplyTypes(Ident("Coll", NoType), Vector(STuple(SByte, SShort))),
Expand Down Expand Up @@ -763,7 +768,7 @@ class SigmaParserTest extends PropSpec with PropertyChecks with Matchers with La

property("substConstants") {
parse("substConstants[Long](Coll[Byte](1.toByte), Coll[Int](1), Coll[Long](1L))") shouldBe Apply(
ApplyTypes(Ident("substConstants", NoType), Vector(SLong)),
ApplyTypes(SubstConstantsFunc.symNoType, Vector(SLong)),
Vector(
Apply(ApplyTypes(Ident("Coll", NoType), Vector(SByte)), Vector(Select(IntConstant(1), "toByte", None))),
Apply(ApplyTypes(Ident("Coll", NoType), Vector(SInt)), Vector(IntConstant(1))),
Expand All @@ -774,7 +779,7 @@ class SigmaParserTest extends PropSpec with PropertyChecks with Matchers with La

property("executeFromVar") {
parse("executeFromVar[Boolean](1)") shouldBe Apply(
ApplyTypes(Ident("executeFromVar", NoType), Vector(SBoolean)), Vector(IntConstant(1))
ApplyTypes(ExecuteFromVarFunc.symNoType, Vector(SBoolean)), Vector(IntConstant(1))
)
}

Expand Down
5 changes: 4 additions & 1 deletion src/test/scala/sigmastate/lang/SigmaTyperTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import sigmastate.utxo.{Append, ExtractCreationInfo, SizeOf}

class SigmaTyperTest extends PropSpec with PropertyChecks with Matchers with LangTests with ValueGenerators {

private val predefFuncRegistry = new PredefinedFuncRegistry(StdSigmaBuilder)
import predefFuncRegistry._

def typecheck(env: ScriptEnv, x: String, expected: SValue = null): SType = {
try {
val builder = TransformingSigmaBuilder
Expand Down Expand Up @@ -90,7 +93,7 @@ class SigmaTyperTest extends PropSpec with PropertyChecks with Matchers with Lan
}

property("predefined functions") {
typecheck(env, "allOf") shouldBe SFunc(SCollection[SBoolean.type], SBoolean)
typecheck(env, "allOf") shouldBe AllOfFunc.declaration.tpe
typecheck(env, "allOf(Coll(c1, c2))") shouldBe SBoolean
typecheck(env, "getVar[Byte](10).get") shouldBe SByte
typecheck(env, "getVar[Coll[Byte]](10).get") shouldBe SByteArray
Expand Down

0 comments on commit 1bce5f1

Please sign in to comment.