Skip to content

Commit

Permalink
sequences: fix and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaKe committed Oct 18, 2023
1 parent b18b1aa commit 6d32db7
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/decider/Decider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ trait DefaultDeciderProvider extends VerifierComponent { this: Verifier =>
}
case None => {
pathConditions.setCurrentBranchCondition(t, None)
assume(t, new DebugExp("current branch condition"))
assume(t, new DebugExp("unknown branch condition"))
} // TODO ake
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/resources/PropertyInterpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ abstract class PropertyInterpreter {
case _ =>
val leftCond = buildPathCondition(left, info)
val rightCond = buildPathCondition(right, info)
new Pair(terms.Equals(leftCond.getFirst,rightCond.getFirst), ast.EqCmp(leftCond.getSecond, rightCond.getSecond)())
new Pair(terms.Equals(leftCond.getFirst, rightCond.getFirst), ast.EqCmp(leftCond.getSecond, rightCond.getSecond)())
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/scala/rules/Evaluator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ object evaluator extends EvaluationRules {

case _: ast.WildcardPerm =>
val (tVar, tConstraints) = v.decider.freshARP()
v.decider.assume(tConstraints, new DebugExp("Fresh ARP"))
v.decider.assume(tConstraints, new DebugExp(s"WildcardPerm Access: ${tConstraints.toString}"))
/* TODO: Only record wildcards in State.constrainableARPs that are used in exhale
* position. Currently, wildcards used in inhale position (only) may not be removed
* from State.constrainableARPs (potentially inefficient, but should be sound).
Expand Down Expand Up @@ -940,12 +940,13 @@ object evaluator extends EvaluationRules {
} else failure2}
} else failure1}}})

case ast.ExplicitSeq(es) =>
case seq@ast.ExplicitSeq(es) =>
evals2(s, es, Nil, _ => pve, v)((s1, tEs, v1) => {
val tSeq =
tEs.tail.foldLeft[SeqTerm](SeqSingleton(tEs.head))((tSeq, te) =>
SeqAppend(tSeq, SeqSingleton(te)))
v1.decider.assume(SeqLength(tSeq) === IntLiteral(es.size), new DebugExp("Init sequence length"))
val exp = ast.EqCmp(ast.SeqLength(seq)(), ast.IntLit(es.size)())()
v1.decider.assume(SeqLength(tSeq) === IntLiteral(es.size), new DebugExp(exp, s1.substituteVarsInExp(exp)))
Q(s1, tSeq, v1)})

/* Sets and multisets */
Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/state/State.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ final case class State(g: Store = Store(),

def substituteVarsInExp(e : ast.Exp): ast.Exp = {
val varMapping = g.values.map { case (localVar, term) => {
val newName = term.toString.replaceAll("@.. ", " ")
val newName = term match {
case Var(n, _) => n.name.substring(0, n.name.lastIndexOf("@"))
case _ => term.toString.replaceAll("@.. ", " ")
}
localVar.name -> LocalVar(newName, localVar.typ)(localVar.pos, localVar.info, localVar.errT)
} }
Sanitizer.replaceFreeVariablesInExpression(e, varMapping, Set())
Expand Down
22 changes: 22 additions & 0 deletions src/test/resources/viperDebugger/globalAssumptions.vpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
field f: Int

predicate P(x: Ref) { acc(x.f, wildcard) }

method test40(x: Ref, y: Ref)
requires acc(x.f, 1/2) && acc(y.f, 1/2)
{
fold P(x)
exhale acc(x.f, perm(x.f))
exhale acc(y.f, perm(y.f))
unfold P(x)
if (y == x) {
assert y.f == old(y.f)
}
}

method test50(x: Ref, y: Ref)
{
inhale acc(P(x), 1/2) && acc(P(y), 1/2)
//:: ExpectedOutput(exhale.failed:insufficient.permission)
exhale acc(P(x), wildcard) && acc(P(y), 1/2)
}
14 changes: 14 additions & 0 deletions src/test/resources/viperDebugger/predicateUnfoldingWithRef.vpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
field val : Int


predicate greater0(n : Ref, p : Perm)
{
none < p && p <= write && acc(n.val, p) && n.val >= 0
}

method my_unfold(n : Ref)
requires acc(n.val, 1/2) && greater0(n, 1/2)
ensures acc(n.val, 1/2) && n.val >= 0
{
assert unfolding greater0(n, 1/2) in n.val >= 0
}
18 changes: 18 additions & 0 deletions src/test/resources/viperDebugger/sequences.vpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@


method test_sequence()
{
var s : Seq[Int]
s := Seq(1,3,3)
var z : Int
z := s[1] + s[2]
assert z == 6
}

method test_sequence_append(s1 : Seq[Int], s2: Seq[Int])
requires |s1| > 0 && |s2| > 0
{
var s : Seq[Int]
s := s1 ++ s2
assert |s| > 1
}

0 comments on commit 6d32db7

Please sign in to comment.