2017-10-04 19:01:06 +02:00
|
|
|
/*
|
|
|
|
|
* sbt
|
|
|
|
|
* Copyright 2011 - 2017, Lightbend, Inc.
|
|
|
|
|
* Copyright 2008 - 2010, Mark Harrah
|
|
|
|
|
* Licensed under BSD-3-Clause license (see LICENSE)
|
2010-06-10 14:19:15 +02:00
|
|
|
*/
|
2017-10-04 19:01:06 +02:00
|
|
|
|
2010-06-10 14:19:15 +02:00
|
|
|
package sbt
|
2009-08-16 20:29:08 +02:00
|
|
|
|
|
|
|
|
import org.scalacheck._
|
|
|
|
|
import Prop._
|
2009-11-12 00:41:39 +01:00
|
|
|
import TaskGen._
|
2009-08-16 20:29:08 +02:00
|
|
|
|
2014-05-07 17:52:23 +02:00
|
|
|
object TaskRunnerSortTest extends Properties("TaskRunnerSort") {
|
|
|
|
|
property("sort") = forAll(TaskListGen, MaxWorkersGen) { (list: List[Int], workers: Int) =>
|
|
|
|
|
val a = list.toArray
|
|
|
|
|
val sorted = a.toArray
|
|
|
|
|
java.util.Arrays.sort(sorted)
|
2017-04-21 09:14:31 +02:00
|
|
|
("Workers: " + workers) |: ("Array: " + a.toList) |: {
|
|
|
|
|
def result = tryRun(sort(a.toSeq), false, if (workers > 0) workers else 1)
|
|
|
|
|
checkResult(result.toList, sorted.toList)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
final def sortDirect(a: Seq[Int]): Seq[Int] = {
|
|
|
|
|
if (a.length < 2)
|
|
|
|
|
a
|
|
|
|
|
else {
|
|
|
|
|
val pivot = a(0)
|
|
|
|
|
val (lt, gte) = a.view.drop(1).partition(_ < pivot)
|
|
|
|
|
sortDirect(lt) ++ List(pivot) ++ sortDirect(gte)
|
|
|
|
|
}
|
2014-05-07 17:52:23 +02:00
|
|
|
}
|
2017-04-21 09:14:31 +02:00
|
|
|
final def sort(a: Seq[Int]): Task[Seq[Int]] = {
|
|
|
|
|
if (a.length < 200)
|
|
|
|
|
task(sortDirect(a))
|
|
|
|
|
else {
|
|
|
|
|
task(a) flatMap { a =>
|
2014-05-07 17:52:23 +02:00
|
|
|
val pivot = a(0)
|
|
|
|
|
val (lt, gte) = a.view.drop(1).partition(_ < pivot)
|
2017-04-21 09:14:31 +02:00
|
|
|
Test.t2(sort(lt), sort(gte)) map {
|
|
|
|
|
case (l, g) => l ++ List(pivot) ++ g
|
2014-05-07 17:52:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-21 09:14:31 +02:00
|
|
|
}
|
2015-09-11 11:00:34 +02:00
|
|
|
}
|