Skip to content

Commit 86b4811

Browse files
committed
fixes hashing of domains with named axioms with identical expressions and adds unit tests
1 parent 11e8e44 commit 86b4811

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

src/main/scala/viper/silver/utility/Caching.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,6 @@ object CacheHelper {
116116
CacheHelper.buildHash(node)
117117
}
118118

119-
implicit def domainFnOrdering: Ordering[DomainFunc] = Ordering.by(_.name)
120-
implicit def domainAxOrdering: Ordering[DomainAxiom] = Ordering.by(ax => FastPrettyPrinter.pretty(ax.exp))
119+
implicit def domainFnOrdering: Ordering[DomainFunc] = Ordering.by(FastPrettyPrinter.pretty(_))
120+
implicit def domainAxOrdering: Ordering[DomainAxiom] = Ordering.by(FastPrettyPrinter.pretty(_))
121121
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
//
5+
// Copyright (c) 2011-2022 ETH Zurich.
6+
7+
import org.scalatest.matchers.should.Matchers
8+
import org.scalatest.funsuite.AnyFunSuite
9+
import viper.silver.ast._
10+
import viper.silver.utility.CacheHelper
11+
12+
class EntityHashTests extends AnyFunSuite with Matchers {
13+
14+
val test_prefix = s"Test Entity Hash"
15+
16+
val domainName: String = "SomeDomain"
17+
val f0: DomainFunc = DomainFunc("f0", Seq(), Int)(domainName = domainName)
18+
val f1: DomainFunc = DomainFunc("f1", Seq(), Int)(domainName = domainName)
19+
val ax0: DomainAxiom = AnonymousDomainAxiom(TrueLit()())(domainName = domainName)
20+
val ax1: DomainAxiom = AnonymousDomainAxiom(TrueLit()())(domainName = domainName)
21+
val ax2: DomainAxiom = NamedDomainAxiom("ax2", TrueLit()())(domainName = domainName)
22+
val ax3: DomainAxiom = NamedDomainAxiom("ax3", TrueLit()())(domainName = domainName)
23+
24+
private def sameHash(d0: Domain, d1: Domain): Unit = {
25+
val d0Hash = CacheHelper.computeEntityHash("", d0)
26+
val d1Hash = CacheHelper.computeEntityHash("", d1)
27+
d0Hash should be (d1Hash)
28+
}
29+
30+
test(s"$test_prefix: domain hash does not depend on order of domain functions") {
31+
val d0: Domain = Domain(domainName, Seq(f0, f1), Seq(), Seq())()
32+
val d1: Domain = Domain(domainName, Seq(f1, f0), Seq(), Seq())()
33+
sameHash(d0, d1)
34+
}
35+
36+
test(s"$test_prefix: domain hash does not depend on order of unnamed axioms") {
37+
val d0: Domain = Domain(domainName, Seq(), Seq(ax0, ax1), Seq())()
38+
val d1: Domain = Domain(domainName, Seq(), Seq(ax1, ax0), Seq())()
39+
sameHash(d0, d1)
40+
}
41+
42+
test(s"$test_prefix: domain hash does not depend on order of named axioms") {
43+
val d0: Domain = Domain(domainName, Seq(), Seq(ax2, ax3), Seq())()
44+
val d1: Domain = Domain(domainName, Seq(), Seq(ax3, ax2), Seq())()
45+
sameHash(d0, d1)
46+
}
47+
48+
test(s"$test_prefix: domain hash does not depend on order of named and unnamed axioms") {
49+
val d0: Domain = Domain(domainName, Seq(), Seq(ax0, ax1, ax2, ax3), Seq())()
50+
val d1: Domain = Domain(domainName, Seq(), Seq(ax3, ax1, ax2, ax1), Seq())()
51+
sameHash(d0, d1)
52+
}
53+
54+
test(s"$test_prefix: domain hash does not depend on order of domain functions and named and unnamed axioms") {
55+
val d0: Domain = Domain(domainName, Seq(f0, f1), Seq(ax0, ax1, ax2, ax3), Seq())()
56+
val d1: Domain = Domain(domainName, Seq(f1, f0), Seq(ax3, ax1, ax2, ax1), Seq())()
57+
sameHash(d0, d1)
58+
}
59+
}

0 commit comments

Comments
 (0)