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)
|
2009-08-17 02:33:46 +02:00
|
|
|
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 }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|