Skip to content

Commit

Permalink
Add tests for pattern alternatives
Browse files Browse the repository at this point in the history
Co-authored-by: SidonieBouthors <[email protected]>
Co-authored-by: Mai-LinhC <[email protected]>
  • Loading branch information
2 people authored and samuelchassot committed Jan 10, 2025
1 parent ed8082e commit f657ddb
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
object PatternAlternative1 {
sealed trait SignSet
case object None extends SignSet
case object Any extends SignSet
case object Neg extends SignSet
case object Zer extends SignSet
case object Pos extends SignSet
case object NegZer extends SignSet
case object NotZer extends SignSet
case object PosZer extends SignSet

def subsetOf(a: SignSet, b: SignSet): Boolean = (a, b) match {
case (None, _) => true
case (_, Any) => true
case (Neg, NegZer | NotZer) => true
case (Zer, NegZer | PosZer) => true
case (Pos, NotZer | PosZer) => true
case _ => false
}
}
33 changes: 33 additions & 0 deletions frontends/benchmarks/extraction/valid/PatternAlternative2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
object PatternAlternative2 {
sealed trait Tree
case class Node(left: Tree, right: Tree) extends Tree
case class IntLeaf(value: Int) extends Tree
case class StringLeaf(value: String) extends Tree
case class NoneLeaf() extends Tree

def containsNoneLeaf(tree: Tree): Boolean = {
tree match {
case Node(left, right) => containsNoneLeaf(left) || containsNoneLeaf(right)
case NoneLeaf() => true
case _ => false
}
}

def containsOnlyBinaryLeaves(tree: Tree): Boolean = {
tree match {
case Node(left, right) => containsOnlyBinaryLeaves(left) && containsOnlyBinaryLeaves(right)
case IntLeaf(v) => v == 0 || v == 1
case StringLeaf(v) => v == "0" || v == "1"
case _ => true
}
}

def hasBinaryLeaves(tree: Tree): Boolean = {
require(!containsNoneLeaf(tree) && containsOnlyBinaryLeaves(tree))
tree match {
case a @ Node(left: (IntLeaf | StringLeaf), right: (IntLeaf | StringLeaf)) => hasBinaryLeaves(left) && hasBinaryLeaves(right)
case b @ (IntLeaf(0 | 1) | StringLeaf("0" | "1")) => true
case _ => false
}
} ensuring { res => res }
}
33 changes: 33 additions & 0 deletions frontends/benchmarks/verification/valid/PatternAlternative.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
object PatternAlternative {
sealed trait Tree
case class Node(left: Tree, right: Tree) extends Tree
case class IntLeaf(value: Int) extends Tree
case class StringLeaf(value: String) extends Tree
case class NoneLeaf() extends Tree

def containsNoneLeaf(tree: Tree): Boolean = {
tree match {
case Node(left, right) => containsNoneLeaf(left) || containsNoneLeaf(right)
case NoneLeaf() => true
case _ => false
}
}

def containsOnlyBinaryLeaves(tree: Tree): Boolean = {
tree match {
case Node(left, right) => containsOnlyBinaryLeaves(left) && containsOnlyBinaryLeaves(right)
case IntLeaf(v) => v == 0 || v == 1
case StringLeaf(v) => v == "0" || v == "1"
case _ => true
}
}

def hasBinaryLeaves(tree: Tree): Boolean = {
require(!containsNoneLeaf(tree) && containsOnlyBinaryLeaves(tree))
tree match {
case a @ Node(left: (IntLeaf | StringLeaf), right: (IntLeaf | StringLeaf)) => hasBinaryLeaves(left) && hasBinaryLeaves(right)
case b @ (IntLeaf(0 | 1) | StringLeaf("0" | "1")) => true
case _ => false
}
} ensuring { res => res }
}

0 comments on commit f657ddb

Please sign in to comment.