-
Notifications
You must be signed in to change notification settings - Fork 14
/
Bound5Spec.scala
79 lines (65 loc) · 2.27 KB
/
Bound5Spec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package challenges.bound5
import cats.implicits.catsSyntaxTuple5Semigroupal
import challenges.bound5.Bound5Spec.{limit, quintuples}
import com.sageserpent.americium.Trials
import com.sageserpent.americium.java.CasesLimitStrategy
import org.scalatest.BeforeAndAfter
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.must.Matchers
import org.scalatest.matchers.should.Matchers.convertToAnyShouldWrapper
import java.util.concurrent.TimeUnit
import scala.concurrent.duration.FiniteDuration
object Bound5Spec {
private val api = Trials.api
private val limit: Short = 256
private val listsOfShortsWithLimitedSums = api
.integers(Short.MinValue, Short.MaxValue)
.map(_.toShort)
.lists
.filter(limit > _.sum)
private val quintuples =
(
listsOfShortsWithLimitedSums,
listsOfShortsWithLimitedSums,
listsOfShortsWithLimitedSums,
listsOfShortsWithLimitedSums,
listsOfShortsWithLimitedSums
) mapN Tuple5.apply
}
class Bound5Spec extends AnyFlatSpec with Matchers with BeforeAndAfter {
private var testCaseCount: Integer = 0
private val snoopOnShrinkage = () => {
var failedCaseCounter = 0
{ caze: Any =>
println(
s"$failedCaseCounter - Shrinkage has found a failing case: $caze after $testCaseCount trials including this one."
)
failedCaseCounter += 1
false
}
}
before { testCaseCount = 0 }
after { println(s"Examined $testCaseCount cases in total.") }
"the sum" should "be no more than five times the limit on the individual sums" in {
quintuples
.withStrategy(
caseSupplyCycle =>
CasesLimitStrategy.timed(
FiniteDuration(
1 + caseSupplyCycle.numberOfPreviousShrinkages(),
TimeUnit.SECONDS
)
),
shrinkageStop = snoopOnShrinkage
)
.supplyTo { quintuple =>
testCaseCount += 1
// Why does the original problem statement in Haskell insist on using a quintuple when it's much easier to use a sequence abstraction? Oh well...
val sum: Short = quintuple.productIterator
.asInstanceOf[Iterator[List[Short]]]
.flatten
.sum
sum.toInt should be <= 5 * limit // The Scalatest DSL isn't looking so clever here.
}
}
}