Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Created December 22, 2013 14:08
Show Gist options
  • Save akimboyko/8083147 to your computer and use it in GitHub Desktop.
Save akimboyko/8083147 to your computer and use it in GitHub Desktop.
Properties proved by ScalaCheck about BinaryTree from Reactive course week #005
package actorbintree
import scala.concurrent.duration._
import akka.actor.{Props, ActorSystem}
import akka.testkit._
import org.scalacheck.{Gen, Properties}
import org.scalacheck.Prop._
import actorbintree.BinaryTreeSet._
import actorbintree.BinaryTreeSet.Contains
import actorbintree.BinaryTreeSet.OperationFinished
import actorbintree.BinaryTreeSet.ContainsResult
import actorbintree.BinaryTreeSet.Insert
class BinaryTreeCheck extends Properties("BinaryTree") {
property("Add values to BinaryTree and retrieve them back") =
forAll { (values: Set[Int]) =>
var operationId = 0
new TestKit(ActorSystem("TestSys")) with ImplicitSender {
val topNode = system.actorOf(Props[BinaryTreeSet])
values.foreach(value => {
val firstContainsId = operationId
topNode ! Contains(testActor, id = firstContainsId, value)
operationId = operationId + 1
val insertId = operationId
topNode ! Insert(testActor, id = insertId, value)
operationId = operationId + 1
val secondContainsId = operationId
topNode ! Contains(testActor, id = secondContainsId, value)
expectMsg(ContainsResult(firstContainsId, false))
expectMsg(OperationFinished(insertId))
expectMsg(ContainsResult(secondContainsId, true))
operationId = operationId + 1
})
expectNoMsg(10.millis)
system.shutdown()
}
true
}
property("Add to and remove values from BinaryTree") =
forAll { (values: List[Int]) =>
var operationId = 0
new TestKit(ActorSystem("TestSys")) with ImplicitSender {
val topNode = system.actorOf(Props[BinaryTreeSet])
values.foreach(value => {
val insertId = operationId
topNode ! Insert(testActor, id = insertId, value)
operationId = operationId + 1
val firstContainsId = operationId
topNode ! Contains(testActor, id = firstContainsId, value)
operationId = operationId + 1
val removeId = operationId
topNode ! Remove(testActor, id = removeId, value)
operationId = operationId + 1
val secondContainsId = operationId
topNode ! Contains(testActor, id = secondContainsId, value)
expectMsg(OperationFinished(insertId))
expectMsg(ContainsResult(firstContainsId, true))
expectMsg(OperationFinished(removeId))
expectMsg(ContainsResult(secondContainsId, false))
operationId = operationId + 1
})
expectNoMsg(10.millis)
system.shutdown()
}
true
}
property("Add to and remove some of values from BinaryTree") =
forAll { (items: List[(Int, Boolean)]) =>
var operationId = 0
new TestKit(ActorSystem("TestSys")) with ImplicitSender {
val topNode = system.actorOf(Props[BinaryTreeSet])
items.foreach(item => {
val (value, toRemove) = item
val insertId = operationId
topNode ! Insert(testActor, id = insertId, value)
expectMsg(OperationFinished(insertId))
if (toRemove) {
operationId = operationId + 1
val removeId = operationId
topNode ! Remove(testActor, id = removeId, value)
expectMsg(OperationFinished(removeId))
}
if (value % 3 == 0) topNode ! GC
operationId = operationId + 1
})
topNode ! GC
items
.groupBy(_._1)
.map(group => (group._1, group._2.last._2))
.foreach(item => {
val (value, toRemove) = item
val containsId = operationId
topNode ! Contains(testActor, id = containsId, value)
expectMsg(ContainsResult(containsId, !toRemove))
operationId = operationId + 1
})
expectNoMsg(10.millis)
system.shutdown()
}
true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment