sbt/tasks/src/test/scala/TestRunnerSort.scala

43 lines
970 B
Scala
Raw Normal View History

2009-08-16 20:29:08 +02:00
import xsbt._
import org.scalacheck._
import Prop._
object TaskRunnerSortTest extends Properties("TaskRunnerSort")
{
specify("sort", (a: Array[Int], workers: Int) =>
(workers > 0) ==> {
val sorted = a.toArray
java.util.Arrays.sort(sorted)
("Workers: " + workers) |: ("Array: " + a.toList) |:
{
def result = TaskRunner( sort(a.toArray), workers)
checkResult(result.toList, sorted.toList)
2009-08-16 20:29:08 +02:00
}
}
)
final def sortDirect(a: RandomAccessSeq[Int]): RandomAccessSeq[Int] =
{
if(a.length < 2)
a
else
{
val pivot = a(0)
val (lt,gte) = a.projection.drop(1).partition(_ < pivot)
sortDirect(lt) ++ List(pivot) ++ sortDirect(gte)
}
}
final def sort(a: RandomAccessSeq[Int]): Task[RandomAccessSeq[Int]] =
{
if(a.length < 2)
Task(a)
else
{
Task(a) bind { a =>
val pivot = a(0)
val (lt,gte) = a.projection.drop(1).partition(_ < pivot)
(sort(lt), sort(gte)) map { (l,g) => l ++ List(pivot) ++ g }
}
}
}
}