Skip to content

Commit

Permalink
Create tests for lazy evaluation of OR, AND (#555)
Browse files Browse the repository at this point in the history
* add tests for laziness of the OR and AND ops;

* add costing spec tests for lazy OR, AND;

* add nested AND, OR sigma dsl tests;

* add more nested AND, OR sigma dsl tests;
  • Loading branch information
greenhat authored and aslesarenko committed Jul 29, 2019
1 parent eb8c267 commit 335101d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/test/scala/sigmastate/CostingSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,10 @@ class CostingSpecification extends SigmaTestingData {
cost shouldBe expectedCost
}

property("laziness of AND, OR costs") {
cost("{ val cond = getVar[Boolean](2).get; !(!cond && (1 / 0 == 1)) }")(
ContextVarAccess + constCost * 2 + logicCost * 3 + multiply + comparisonCost)
cost("{ val cond = getVar[Boolean](2).get; (cond || (1 / 0 == 1)) }")(
ContextVarAccess + constCost * 2 + logicCost + multiply + comparisonCost)
}
}
16 changes: 16 additions & 0 deletions src/test/scala/sigmastate/utxo/BasicOpsSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -621,4 +621,20 @@ class BasicOpsSpecification extends SigmaTestingCommons {
true
)
}

property("lazy OR") {
test("lazy OR", env, ext,
"true || ((1/0) == 1)",
null,
true
)
}

property("lazy AND") {
test("lazy AND", env, ext,
"(false && ((1/0) == 1)) == false",
null,
true
)
}
}
37 changes: 37 additions & 0 deletions src/test/scala/special/sigma/SigmaDslTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -993,4 +993,41 @@ class SigmaDslTest extends PropSpec
forAll { x: (Boolean, Boolean) => eq(x) }
}

property("lazy || and && boolean equivalence") {
checkEq(func[Boolean, Boolean](
"{ (x: Boolean) => x || (1 / 0 == 1) }"))
{ (x: Boolean) => x || (1 / 0 == 1) }(true)

checkEq(func[Boolean, Boolean](
"{ (x: Boolean) => x && (1 / 0 == 1) }"))
{ (x: Boolean) => x && (1 / 0 == 1) }(false)

// nested

checkEq(func[Boolean, Boolean](
"{ (x: Boolean) => x && (x || (1 / 0 == 1)) }"))
{ (x: Boolean) => x && (x || (1 / 0 == 1)) }(true)
checkEq(func[Boolean, Boolean](
"{ (x: Boolean) => x && (x && (x || (1 / 0 == 1))) }"))
{ (x: Boolean) => x && (x && (x || (1 / 0 == 1))) }(true)
checkEq(func[Boolean, Boolean](
"{ (x: Boolean) => x && (x && (x && (x || (1 / 0 == 1)))) }"))
{ (x: Boolean) => x && (x && (x && (x || (1 / 0 == 1)))) }(true)
checkEq(func[Boolean, Boolean](
"{ (x: Boolean) => !(!x && (1 / 0 == 1)) && (x || (1 / 0 == 1)) }"))
{ (x: Boolean) => !(!x && (1 / 0 == 1)) && (x || (1 / 0 == 1)) }(true)

checkEq(func[Boolean, Boolean](
"{ (x: Boolean) => (x || (1 / 0 == 1)) && x }"))
{ (x: Boolean) => (x || (1 / 0 == 1)) && x }(true)
checkEq(func[Boolean, Boolean](
"{ (x: Boolean) => (x || (1 / 0 == 1)) && (x || (1 / 0 == 1)) }"))
{ (x: Boolean) => (x || (1 / 0 == 1)) && (x || (1 / 0 == 1)) }(true)
checkEq(func[Boolean, Boolean](
"{ (x: Boolean) => (!(!x && (1 / 0 == 1)) || (1 / 0 == 0)) && (x || (1 / 0 == 1)) }"))
{ (x: Boolean) => (!(!x && (1 / 0 == 1)) || (1 / 0 == 0)) && (x || (1 / 0 == 1)) }(true)
checkEq(func[Boolean, Boolean](
"{ (x: Boolean) => (!(!x && (1 / 0 == 1)) || (1 / 0 == 0)) && (!(!x && (1 / 0 == 1)) || (1 / 0 == 1)) }"))
{ (x: Boolean) => (!(!x && (1 / 0 == 1)) || (1 / 0 == 0)) && (!(!x && (1 / 0 == 1)) || (1 / 0 == 1)) }(true)
}
}

0 comments on commit 335101d

Please sign in to comment.