Skip to content

Commit

Permalink
Tests for takeWhile/dropWhile/span.
Browse files Browse the repository at this point in the history
Also simplified implementation of span to just use splitAt.
  • Loading branch information
erikrozendaal committed Jan 7, 2012
1 parent 288874d commit e61075c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
5 changes: 1 addition & 4 deletions src/library/scala/collection/immutable/TreeMap.scala
Expand Up @@ -116,10 +116,7 @@ class TreeMap[A, +B] private (tree: RB.Tree[A, B])(implicit val ordering: Orderi
}
override def dropWhile(p: ((A, B)) => Boolean) = drop(countWhile(p))
override def takeWhile(p: ((A, B)) => Boolean) = take(countWhile(p))
override def span(p: ((A, B)) => Boolean) = {
val n = countWhile(p)
(take(n), drop(n))
}
override def span(p: ((A, B)) => Boolean) = splitAt(countWhile(p))

/** A factory to create empty maps of the same type of keys.
*/
Expand Down
5 changes: 1 addition & 4 deletions src/library/scala/collection/immutable/TreeSet.scala
Expand Up @@ -94,10 +94,7 @@ class TreeSet[A] private (tree: RB.Tree[A, Unit])(implicit val ordering: Orderin
}
override def dropWhile(p: A => Boolean) = drop(countWhile(p))
override def takeWhile(p: A => Boolean) = take(countWhile(p))
override def span(p: A => Boolean) = {
val n = countWhile(p)
(take(n), drop(n))
}
override def span(p: A => Boolean) = splitAt(countWhile(p))

@deprecated("use `ordering.lt` instead", "2.10")
def isSmaller(x: A, y: A) = compare(x,y) < 0
Expand Down
15 changes: 15 additions & 0 deletions test/files/scalacheck/treemap.scala
Expand Up @@ -96,6 +96,21 @@ object Test extends Properties("TreeMap") {
prefix == subject.take(n) && suffix == subject.drop(n)
}

property("takeWhile") = forAll { (subject: TreeMap[Int, String]) =>
val result = subject.takeWhile(_._1 < 0)
result.forall(_._1 < 0) && result == subject.take(result.size)
}

property("dropWhile") = forAll { (subject: TreeMap[Int, String]) =>
val result = subject.dropWhile(_._1 < 0)
result.forall(_._1 >= 0) && result == subject.takeRight(result.size)
}

property("span identity") = forAll { (subject: TreeMap[Int, String]) =>
val (prefix, suffix) = subject.span(_._1 < 0)
prefix.forall(_._1 < 0) && suffix.forall(_._1 >= 0) && subject == prefix ++ suffix
}

property("remove single") = forAll { (subject: TreeMap[Int, String]) => subject.nonEmpty ==> {
val key = oneOf(subject.keys.toSeq).sample.get
val removed = subject - key
Expand Down
15 changes: 15 additions & 0 deletions test/files/scalacheck/treeset.scala
Expand Up @@ -92,6 +92,21 @@ object Test extends Properties("TreeSet") {
prefix == subject.take(n) && suffix == subject.drop(n)
}

property("takeWhile") = forAll { (subject: TreeMap[Int, String]) =>
val result = subject.takeWhile(_._1 < 0)
result.forall(_._1 < 0) && result == subject.take(result.size)
}

property("dropWhile") = forAll { (subject: TreeMap[Int, String]) =>
val result = subject.dropWhile(_._1 < 0)
result.forall(_._1 >= 0) && result == subject.takeRight(result.size)
}

property("span identity") = forAll { (subject: TreeMap[Int, String]) =>
val (prefix, suffix) = subject.span(_._1 < 0)
prefix.forall(_._1 < 0) && suffix.forall(_._1 >= 0) && subject == prefix ++ suffix
}

property("remove single") = forAll { (subject: TreeSet[Int]) => subject.nonEmpty ==> {
val element = oneOf(subject.toSeq).sample.get
val removed = subject - element
Expand Down

0 comments on commit e61075c

Please sign in to comment.