Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage, especially around caches. #12

Merged
merged 2 commits into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/main/scala/com/stripe/dagon/HMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ final class HMap[K[_], V[_]](protected val map: Map[K[_], V[_]]) {
def contains[T](id: K[T]): Boolean =
get(id).isDefined

def isEmpty: Boolean = map.isEmpty

def size: Int = map.size

def forallKeys(p: K[_] => Boolean): Boolean =
Expand Down
47 changes: 47 additions & 0 deletions core/src/test/scala/com/stripe/dagon/CacheTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.stripe.dagon

import org.scalacheck.Prop._
import org.scalacheck.{Arbitrary, Cogen, Properties}

abstract class CacheTests[K: Cogen: Arbitrary, V: Arbitrary](name: String) extends Properties(name) {

def buildMap(c: Cache[K, V], ks: Iterable[K], f: K => V): Map[K, V] =
ks.iterator.foldLeft(Map.empty[K, V]) {
(m, k) => m.updated(k, c.getOrElseUpdate(k, f(k)))
}

property("getOrElseUpdate") =
forAll { (f: K => V, k: K, v1: V, v2: V) =>
val c = Cache.empty[K, V]
var count = 0
val x = c.getOrElseUpdate(k, { count += 1; v1 })
val y = c.getOrElseUpdate(k, { count += 1; v2 })
x == v1 && y == v1 && count == 1
}

property("toMap") =
forAll { (f: K => V, ks: Set[K]) =>
val c = Cache.empty[K, V]
val m = buildMap(c, ks, f)
c.toMap == m
}

property("duplicate") =
forAll { (f: K => V, ks: Set[K]) =>
val c = Cache.empty[K, V]
val d = c.duplicate
buildMap(c, ks, f)
d.toMap.isEmpty
}

property("reset works") =
forAll { (f: K => V, ks: Set[K]) =>
val c = Cache.empty[K, V]
buildMap(c, ks, f)
val d = c.duplicate
c.reset()
c.toMap.isEmpty && d.toMap.size == ks.size
}
}

object CacheTestsSL extends CacheTests[String, Long]("CacheTests[String, Long]")
50 changes: 50 additions & 0 deletions core/src/test/scala/com/stripe/dagon/HCacheTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.stripe.dagon

import org.scalacheck.Prop._
import org.scalacheck.{Arbitrary, Cogen, Properties}

abstract class HCacheTests[K[_], V[_]](name: String)(implicit
ka: Arbitrary[K[Int]], kc: Cogen[K[Int]],
va: Arbitrary[V[Int]])
extends Properties(name) {

def buildHMap(c: HCache[K, V], ks: Iterable[K[Int]], f: K[Int] => V[Int]): HMap[K, V] =
ks.iterator.foldLeft(HMap.empty[K, V]) {
(m, k) => m.updated(k, c.getOrElseUpdate(k, f(k)))
}

property("getOrElseUpdate") =
forAll { (f: K[Int] => V[Int], k: K[Int], v1: V[Int], v2: V[Int]) =>
val c = HCache.empty[K, V]
var count = 0
val x = c.getOrElseUpdate(k, { count += 1; v1 })
val y = c.getOrElseUpdate(k, { count += 1; v2 })
x == v1 && y == v1 && count == 1
}

property("toHMap") =
forAll { (f: K[Int] => V[Int], ks: Set[K[Int]]) =>
val c = HCache.empty[K, V]
val m = buildHMap(c, ks, f)
c.toHMap == m
}

property("duplicate") =
forAll { (f: K[Int] => V[Int], ks: Set[K[Int]]) =>
val c = HCache.empty[K, V]
val d = c.duplicate
buildHMap(c, ks, f)
d.toHMap.isEmpty
}

property("reset works") =
forAll { (f: K[Int] => V[Int], ks: Set[K[Int]]) =>
val c = HCache.empty[K, V]
buildHMap(c, ks, f)
val d = c.duplicate
c.reset()
c.toHMap.isEmpty && d.toHMap.size == ks.size
}
}

object HCacheTestsLL extends HCacheTests[List, List]("HCacheTests[List, List]")
5 changes: 5 additions & 0 deletions core/src/test/scala/com/stripe/dagon/HMapTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,9 @@ object HMapTests extends Properties("HMap") {
property("HMap.from works") = forAll { (m: Map[K, V]) =>
HMap.from[Key, Value](m.asInstanceOf[Map[Key[_], Value[_]]]) == fromPairs(m)
}

property("heterogenous equality is false") =
forAll { (h: H) =>
h != null && h != 33
}
}