diff options
author | Erik Rozendaal <erik@deler.org> | 2011-12-21 09:28:42 +0100 |
---|---|---|
committer | Erik Rozendaal <erik@deler.org> | 2011-12-28 13:12:35 +0100 |
commit | 6b950741c58938eab922908ac4fb809b7ca68c01 (patch) | |
tree | c55c01b782b74cd1019db1fd0a98ef07152cef2f /test/files/scalacheck/redblack.scala | |
parent | d2706db10c63851e549ef7ce4cbaff364c59fbc3 (diff) | |
download | scala-6b950741c58938eab922908ac4fb809b7ca68c01.tar.gz scala-6b950741c58938eab922908ac4fb809b7ca68c01.tar.bz2 scala-6b950741c58938eab922908ac4fb809b7ca68c01.zip |
Make sure the redblack test compiles and runs.
Diffstat (limited to 'test/files/scalacheck/redblack.scala')
-rw-r--r-- | test/files/scalacheck/redblack.scala | 76 |
1 files changed, 36 insertions, 40 deletions
diff --git a/test/files/scalacheck/redblack.scala b/test/files/scalacheck/redblack.scala index 1fcaa46f0e..011a5d0ca5 100644 --- a/test/files/scalacheck/redblack.scala +++ b/test/files/scalacheck/redblack.scala @@ -18,22 +18,18 @@ abstract class RedBlackTest extends Properties("RedBlack") { def minimumSize = 0 def maximumSize = 5 - object RedBlackTest extends scala.collection.immutable.RedBlack[String] { - def isSmaller(x: String, y: String) = x < y - } - - import RedBlackTest._ + import collection.immutable.RedBlack._ - def nodeAt[A](tree: Tree[A], n: Int): Option[(String, A)] = if (n < tree.iterator.size && n >= 0) + def nodeAt[A](tree: Tree[String, A], n: Int): Option[(String, A)] = if (n < tree.iterator.size && n >= 0) Some(tree.iterator.drop(n).next) else None - def treeContains[A](tree: Tree[A], key: String) = tree.iterator.map(_._1) contains key + def treeContains[A](tree: Tree[String, A], key: String) = tree.iterator.map(_._1) contains key - def mkTree(level: Int, parentIsBlack: Boolean = false, label: String = ""): Gen[Tree[Int]] = + def mkTree(level: Int, parentIsBlack: Boolean = false, label: String = ""): Gen[Tree[String, Int]] = if (level == 0) { - value(Empty) + value(Empty.empty) } else { for { oddOrEven <- choose(0, 2) @@ -56,10 +52,10 @@ abstract class RedBlackTest extends Properties("RedBlack") { } yield tree type ModifyParm - def genParm(tree: Tree[Int]): Gen[ModifyParm] - def modify(tree: Tree[Int], parm: ModifyParm): Tree[Int] + def genParm(tree: Tree[String, Int]): Gen[ModifyParm] + def modify(tree: Tree[String, Int], parm: ModifyParm): Tree[String, Int] - def genInput: Gen[(Tree[Int], ModifyParm, Tree[Int])] = for { + def genInput: Gen[(Tree[String, Int], ModifyParm, Tree[String, Int])] = for { tree <- genTree parm <- genParm(tree) } yield (tree, parm, modify(tree, parm)) @@ -68,30 +64,30 @@ abstract class RedBlackTest extends Properties("RedBlack") { trait RedBlackInvariants { self: RedBlackTest => - import RedBlackTest._ + import collection.immutable.RedBlack._ - def rootIsBlack[A](t: Tree[A]) = t.isBlack + def rootIsBlack[A](t: Tree[String, A]) = t.isBlack - def areAllLeavesBlack[A](t: Tree[A]): Boolean = t match { - case Empty => t.isBlack - case ne: NonEmpty[_] => List(ne.left, ne.right) forall areAllLeavesBlack + def areAllLeavesBlack[A](t: Tree[String, A]): Boolean = t match { + case Empty.Instance => t.isBlack + case ne: NonEmpty[_, _] => List(ne.left, ne.right) forall areAllLeavesBlack } - def areRedNodeChildrenBlack[A](t: Tree[A]): Boolean = t match { + def areRedNodeChildrenBlack[A](t: Tree[String, A]): Boolean = t match { case RedTree(_, _, left, right) => List(left, right) forall (t => t.isBlack && areRedNodeChildrenBlack(t)) case BlackTree(_, _, left, right) => List(left, right) forall areRedNodeChildrenBlack - case Empty => true + case Empty.Instance => true } - def blackNodesToLeaves[A](t: Tree[A]): List[Int] = t match { - case Empty => List(1) + def blackNodesToLeaves[A](t: Tree[String, A]): List[Int] = t match { + case Empty.Instance => List(1) case BlackTree(_, _, left, right) => List(left, right) flatMap blackNodesToLeaves map (_ + 1) case RedTree(_, _, left, right) => List(left, right) flatMap blackNodesToLeaves } - def areBlackNodesToLeavesEqual[A](t: Tree[A]): Boolean = t match { - case Empty => true - case ne: NonEmpty[_] => + def areBlackNodesToLeavesEqual[A](t: Tree[String, A]): Boolean = t match { + case Empty.Instance => true + case ne: NonEmpty[_, _] => ( blackNodesToLeaves(ne).distinct.size == 1 && areBlackNodesToLeavesEqual(ne.left) @@ -99,10 +95,10 @@ trait RedBlackInvariants { ) } - def orderIsPreserved[A](t: Tree[A]): Boolean = - t.iterator zip t.iterator.drop(1) forall { case (x, y) => isSmaller(x._1, y._1) } + def orderIsPreserved[A](t: Tree[String, A]): Boolean = + t.iterator zip t.iterator.drop(1) forall { case (x, y) => x._1 < y._1 } - def setup(invariant: Tree[Int] => Boolean) = forAll(genInput) { case (tree, parm, newTree) => + def setup(invariant: Tree[String, Int] => Boolean) = forAll(genInput) { case (tree, parm, newTree) => invariant(newTree) } @@ -114,13 +110,13 @@ trait RedBlackInvariants { } object TestInsert extends RedBlackTest with RedBlackInvariants { - import RedBlackTest._ + import collection.immutable.RedBlack._ override type ModifyParm = Int - override def genParm(tree: Tree[Int]): Gen[ModifyParm] = choose(0, tree.iterator.size + 1) - override def modify(tree: Tree[Int], parm: ModifyParm): Tree[Int] = tree update (generateKey(tree, parm), 0) + override def genParm(tree: Tree[String, Int]): Gen[ModifyParm] = choose(0, tree.iterator.size + 1) + override def modify(tree: Tree[String, Int], parm: ModifyParm): Tree[String, Int] = tree update (generateKey(tree, parm), 0) - def generateKey(tree: Tree[Int], parm: ModifyParm): String = nodeAt(tree, parm) match { + def generateKey(tree: Tree[String, Int], parm: ModifyParm): String = nodeAt(tree, parm) match { case Some((key, _)) => key.init.mkString + "MN" case None => nodeAt(tree, parm - 1) match { case Some((key, _)) => key.init.mkString + "RN" @@ -134,13 +130,13 @@ object TestInsert extends RedBlackTest with RedBlackInvariants { } object TestModify extends RedBlackTest { - import RedBlackTest._ + import collection.immutable.RedBlack._ def newValue = 1 override def minimumSize = 1 override type ModifyParm = Int - override def genParm(tree: Tree[Int]): Gen[ModifyParm] = choose(0, tree.iterator.size) - override def modify(tree: Tree[Int], parm: ModifyParm): Tree[Int] = nodeAt(tree, parm) map { + override def genParm(tree: Tree[String, Int]): Gen[ModifyParm] = choose(0, tree.iterator.size) + override def modify(tree: Tree[String, Int], parm: ModifyParm): Tree[String, Int] = nodeAt(tree, parm) map { case (key, _) => tree update (key, newValue) } getOrElse tree @@ -152,12 +148,12 @@ object TestModify extends RedBlackTest { } object TestDelete extends RedBlackTest with RedBlackInvariants { - import RedBlackTest._ + import collection.immutable.RedBlack._ override def minimumSize = 1 override type ModifyParm = Int - override def genParm(tree: Tree[Int]): Gen[ModifyParm] = choose(0, tree.iterator.size) - override def modify(tree: Tree[Int], parm: ModifyParm): Tree[Int] = nodeAt(tree, parm) map { + override def genParm(tree: Tree[String, Int]): Gen[ModifyParm] = choose(0, tree.iterator.size) + override def modify(tree: Tree[String, Int], parm: ModifyParm): Tree[String, Int] = nodeAt(tree, parm) map { case (key, _) => tree delete key } getOrElse tree @@ -169,17 +165,17 @@ object TestDelete extends RedBlackTest with RedBlackInvariants { } object TestRange extends RedBlackTest with RedBlackInvariants { - import RedBlackTest._ + import collection.immutable.RedBlack._ override type ModifyParm = (Option[Int], Option[Int]) - override def genParm(tree: Tree[Int]): Gen[ModifyParm] = for { + override def genParm(tree: Tree[String, Int]): Gen[ModifyParm] = for { from <- choose(0, tree.iterator.size) to <- choose(0, tree.iterator.size) suchThat (from <=) optionalFrom <- oneOf(Some(from), None, Some(from)) // Double Some(n) to get around a bug optionalTo <- oneOf(Some(to), None, Some(to)) // Double Some(n) to get around a bug } yield (optionalFrom, optionalTo) - override def modify(tree: Tree[Int], parm: ModifyParm): Tree[Int] = { + override def modify(tree: Tree[String, Int], parm: ModifyParm): Tree[String, Int] = { val from = parm._1 flatMap (nodeAt(tree, _) map (_._1)) val to = parm._2 flatMap (nodeAt(tree, _) map (_._1)) tree range (from, to) |