-
Notifications
You must be signed in to change notification settings - Fork 2
/
Configuration.scala
401 lines (362 loc) · 18.1 KB
/
Configuration.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* Copyright 2001-2013 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalatest.prop
import org.scalactic.anyvals.{PosInt, PosZDouble, PosZInt}
import java.util.concurrent.atomic.AtomicReference
/**
* Trait providing methods and classes used to configure property checks provided by the
* the <code>forAll</code> methods of trait <code>GeneratorDrivenPropertyChecks</code> (for ScalaTest-style
* property checks) and the <code>check</code> methods of trait <code>Checkers</code> (for ScalaCheck-style property checks).
*
* @author Bill Venners
*/
trait Configuration {
/**
* Internal utility functions for configuration management.
*/
object PropertyCheckConfiguration {
/**
* Compute the new-style MaxDiscardedFactor based on the old-style MaxDiscarded.
*
* @param minSuccessful the provided minimum number of successful evaluations
* @param maxDiscarded the old-style provided maximum number of discards to allow
* @return the new-style ratio of maximum discards to successful evaluations
*/
private[scalatest] def calculateMaxDiscardedFactor(minSuccessful: Int, maxDiscarded: Int): Double =
(maxDiscarded.toDouble + 1.0) / minSuccessful.toDouble
/**
* Compute the actual number of discards allowed for this run.
*
* Note that this function appears to only be used in one place, in a test. It really should be
* merged with the version in [[Configuration]].
*
* @param maxDiscardedRatio the maximum number of discards, as a multiplier of the minimum number of
* successful evaluations
* @param minSuccessful the number of successful evaluations to require
* @return the maximum number of discards to allow for this run
*/
private[scalatest] def calculateMaxDiscarded(maxDiscardedRatio: Double, minSuccessful: Int): Double =
(maxDiscardedRatio * minSuccessful.toDouble) - 1.0
}
/**
* Describes the configuration to use when evaluating a property.
*
* @param minSuccessful the minimum number of successful property evaluations required for
* the property to pass; see [[MinSuccessful]]
* @param maxDiscardedFactor how many generated values may be discarded,
* as a multiple of the successful attempts, before the property check is considered to be
* [[org.scalatest.prop.PropertyCheckResult.Exhausted]]; see [[MaxDiscardedFactor]]
* @param minSize the minimum size parameter to provide to ScalaCheck, which it will use when
* generating objects for which size matters (such as strings or lists); see [[MinSize]]
* @param sizeRange the maximum size parameter to provide to ScalaCheck, which it will use when
* generating objects for which size matters (such as strings or lists); see [[SizeRange]]
* @param workers number of worker threads to use when evaluating a property; see [[Workers]]
*/
case class PropertyCheckConfiguration(minSuccessful: PosInt = PosInt(10),
maxDiscardedFactor: PosZDouble = PosZDouble(5.0),
minSize: PosZInt = Configuration.minSize.get(),
sizeRange: PosZInt = Configuration.sizeRange.get(),
workers: PosInt = PosInt(1))
import scala.language.implicitConversions
/**
* Abstract class defining a family of configuration parameters for property checks.
*
* <p>
* The subclasses of this abstract class are used to pass configuration information to
* the <code>forAll</code> methods of traits <code>PropertyChecks</code> (for ScalaTest-style
* property checks) and <code>Checkers</code>(for ScalaCheck-style property checks).
* </p>
*
* @author Bill Venners
*/
sealed abstract class PropertyCheckConfigParam extends Product with Serializable
/**
* A <code>PropertyCheckConfigParam</code> that specifies the minimum number of successful
* property evaluations required for the property to pass.
*
* Once this many evaluations have passed, the property will return
* [[PropertyCheckResult.Success]].
*
* @author Bill Venners
*/
case class MinSuccessful(value: PosInt) extends PropertyCheckConfigParam
/**
* A [[PropertyCheckConfigParam]] that specifies how many generated values may be discarded,
* as a multiple of the successful attempts, before the property check is considered to be
* [[org.scalatest.prop.PropertyCheckResult.Exhausted]].
*
*
* <p>
* In <code>GeneratorDrivenPropertyChecks</code>, a property evaluation is discarded if it throws
* <code>DiscardedEvaluationException</code>, which is produced by a <code>whenever</code> clause that
* evaluates to false. For example, consider this ScalaTest property check:
* </p>
*
* <pre class="stHighlighted">
* <span class="stLineComment">// forAll defined in <code>GeneratorDrivenPropertyChecks</code></span>
* forAll { (n: <span class="stType">Int</span>) =>
* whenever (n > <span class="stLiteral">0</span>) {
* doubleIt(n) should equal (n * <span class="stLiteral">2</span>)
* }
* }
* <br/><!-- -->
* </pre>
*
* <p>
* In the above code, whenever a non-positive <code>n</code> is passed, the property function will complete abruptly
* with <code>DiscardedEvaluationException</code>.
* </p>
*
* <p>
* Similarly, in <code>Checkers</code>, a property evaluation is discarded if the expression to the left
* of ScalaCheck's <code>==></code> operator is false. Here's an example:
* </p>
*
* <pre class="stHighlighted">
* <span class="stLineComment">// forAll defined in <code>Checkers</code></span>
* forAll { (n: <span class="stType">Int</span>) =>
* (n > <span class="stLiteral">0</span>) ==> doubleIt(n) == (n * <span class="stLiteral">2</span>)
* }
* <br/><!-- -->
* </pre>
*
* <p>
* For either kind of property check, <code>MaxDiscardedFactor</code> indicates the maximum fraction of
* total tests that may be discarded, relative to the number of successful tests. For example, if this
* is set to 4.0, and you are running 100 tests, it may discard up to 400 tries before considering the
* test to be [[org.scalatest.prop.PropertyCheckResult.Exhausted]].
* </p>
*
* @param value the permitted number of discarded tests, as a multiple of successful ones.
*/
case class MaxDiscardedFactor(value: PosZDouble) extends PropertyCheckConfigParam
/**
* A <code>PropertyCheckConfigParam</code> that specifies the minimum size parameter to
* provide to ScalaCheck, which it will use when generating objects for which size matters (such as
* strings or lists).
*
* @author Bill Venners
*/
case class MinSize(value: PosZInt) extends PropertyCheckConfigParam
/**
* A <code>PropertyCheckConfigParam</code> that (with minSize) specifies the maximum size parameter to
* provide to ScalaCheck, which it will use when generating objects for which size matters (such as
* strings or lists).
*
* <p>
* Note that the size range is added to minSize in order to calculate the maximum size passed to ScalaCheck.
* Using a range allows compile-time checking of a non-negative number being specified.
* </p>
*
* @author Bill Venners
*/
case class SizeRange(value: PosZInt) extends PropertyCheckConfigParam
/**
* A <code>PropertyCheckConfigParam</code> that specifies the number of worker threads
* to use when evaluating a property.
*
* Property evaluation runs on a single thread by default, but may run multiple
* threads if desired. If so, the evaluation will generally run faster. However,
* be careful not to use this if there is any risk of deadlocks, race conditions,
* or other hazards of multi-threaded code in evaluating this property or the
* code under test.
*
* @author Bill Venners
*/
case class Workers(value: PosInt) extends PropertyCheckConfigParam
/**
* Returns a <code>MinSuccessful</code> property check configuration parameter containing the passed value, which specifies the minimum number of successful
* property evaluations required for the property to pass.
*
*/
def minSuccessful(value: PosInt): MinSuccessful = new MinSuccessful(value)
/**
* Returns a <code>MaxDiscardedFactor</code> property check configuration parameter containing the passed value, which specifies the factor of discarded
* property evaluations allowed during property evaluation.
*
*/
def maxDiscardedFactor(value: PosZDouble): MaxDiscardedFactor = MaxDiscardedFactor(value)
/**
* Returns a <code>MinSize</code> property check configuration parameter containing the passed value, which specifies the minimum size parameter to
* provide to ScalaCheck, which it will use when generating objects for which size matters (such as
* strings or lists).
*
*/
def minSize(value: PosZInt): MinSize = new MinSize(value)
/**
* Returns a <code>SizeRange</code> property check configuration parameter containing the passed value, that (with minSize) specifies the maximum size parameter to
* provide to ScalaCheck, which it will use when generating objects for which size matters (such as
* strings or lists).
*
* <p>
* Note that the size range is added to minSize in order to calculate the maximum size passed to ScalaCheck.
* Using a range allows compile-time checking of a non-negative number being specified.
* </p>
*
* @author Bill Venners
*/
def sizeRange(value: PosZInt): SizeRange = SizeRange(value)
/**
* Returns a <code>Workers</code> property check configuration parameter containing the passed value, which specifies the number of worker threads
* to use when evaluating a property.
*
*/
def workers(value: PosInt): Workers = new Workers(value)
/**
* Given some optional [[PropertyCheckConfigParam]]s and a [[PropertyCheckConfiguration]], compute the resulting
* [[Configuration.Parameter]].
*
* This function deals with resolving the various forms of these configuration values, into a consistent form
* suitable for using in properties.
*
* Duplicate [[PropertyCheckConfigParam]] entries are not permitted in the `configParams` list.
*
* TODO: should this function be public? It feels like an internal implementation detail -- I think it should be private.
*
* @param configParams optionally, some parameters that differ from the provided `c`
* @param c a configuration object, describing how to run property evaluations
* @return a fully-set-up [[Configuration.Parameter]] object, ready to evaluate properties with.
*/
def getParameter(configParams: Seq[Configuration#PropertyCheckConfigParam], config: PropertyCheckConfiguration): Configuration.Parameter = {
var minSuccessful: Option[Int] = None
var maxDiscardedFactor: Option[Double] = None
var pminSize: Option[Int] = None
var psizeRange: Option[Int] = None
var pworkers: Option[Int] = None
var minSuccessfulTotalFound = 0
var maxDiscardedTotalFound = 0
var maxDiscardedFactorTotalFound = 0
var minSizeTotalFound = 0
var sizeRangeTotalFound = 0
var workersTotalFound = 0
for (configParam <- configParams) {
configParam match {
case param: MinSuccessful =>
minSuccessful = Some(param.value)
minSuccessfulTotalFound += 1
case param: MaxDiscardedFactor =>
maxDiscardedFactor = Some(param.value)
maxDiscardedFactorTotalFound += 1
case param: MinSize =>
pminSize = Some(param.value)
minSizeTotalFound += 1
case param: SizeRange =>
psizeRange = Some(param.value)
sizeRangeTotalFound += 1
case param: Workers =>
pworkers = Some(param.value)
workersTotalFound += 1
}
}
if (minSuccessfulTotalFound > 1)
throw new IllegalArgumentException("can pass at most one MinSuccessful config parameters, but " + minSuccessfulTotalFound + " were passed")
val maxDiscardedAndFactorTotalFound = maxDiscardedTotalFound + maxDiscardedFactorTotalFound
if (maxDiscardedAndFactorTotalFound > 1)
throw new IllegalArgumentException("can pass at most one MaxDiscarded or MaxDiscardedFactor config parameters, but " + maxDiscardedAndFactorTotalFound + " were passed")
if (minSizeTotalFound > 1)
throw new IllegalArgumentException("can pass at most one MinSize config parameters, but " + minSizeTotalFound + " were passed")
if (sizeRangeTotalFound > 1)
throw new IllegalArgumentException("can pass at most one SizeRange config parameters, but " + sizeRangeTotalFound + " were passed")
if (workersTotalFound > 1)
throw new IllegalArgumentException("can pass at most one Workers config parameters, but " + workersTotalFound + " were passed")
val minSuccessfulTests: Int = minSuccessful.getOrElse(config.minSuccessful)
val minSize: Int = pminSize.getOrElse(config.minSize)
val maxSize = psizeRange.getOrElse(config.sizeRange.value) + minSize
val maxDiscardRatio: Float = maxDiscardedFactor.getOrElse(config.maxDiscardedFactor.value).toFloat
val param =
Configuration.Parameter(
PosInt.from(minSuccessfulTests).getOrElse(config.minSuccessful),
PosZDouble.from(maxDiscardRatio).getOrElse(config.maxDiscardedFactor),
PosZInt.from(minSize).getOrElse(config.minSize),
PosZInt.from(maxSize - minSize).getOrElse(config.sizeRange),
PosInt.from(pworkers.getOrElse(config.workers)).getOrElse(config.workers)
)
if(
param.minSuccessful.value <= 0 ||
param.maxDiscardedFactor.value <= 0 ||
param.minSize.value < 0 ||
maxSize < param.minSize.value ||
param.workers.value <= 0
) throw new IllegalArgumentException("Invalid test parameters")
param
}
/**
* Implicit <code>PropertyCheckConfig</code> value providing default configuration values.
*/
implicit val generatorDrivenConfig: PropertyCheckConfiguration = PropertyCheckConfiguration()
}
/**
* Companion object that facilitates the importing of <code>Configuration</code> members as
* an alternative to mixing it in. One use case is to import <code>Configuration</code> members so you can use
* them in the Scala interpreter.
*/
object Configuration extends Configuration {
// TODO: I assume these are intended for poking default values in from the Runner? They are used in
// PropertyCheckConfiguration.
private[scalatest] lazy val minSize: AtomicReference[PosZInt] = new AtomicReference(PosZInt(0))
private[scalatest] lazy val sizeRange: AtomicReference[PosZInt] = new AtomicReference(PosZInt(100))
/**
* The parameters that define how a property evaluation should be executed.
*
* You typically will not build this directly. Instead, you should define a
* [[PropertyCheckConfiguration]] and/or some [[PropertyCheckConfigParam]]s, pass
* those in, and the system will resolve those to a coherent [[Parameter]].
*
* @param minSuccessful the minimum number of successful property evaluations required for
* the property to pass; see [[MinSuccessful]]
* @param maxDiscardedFactor how many generated values may be discarded,
* as a multiple of the successful attempts, before the property check is considered to be
* [[org.scalatest.prop.PropertyCheckResult.Exhausted]]; see [[MaxDiscardedFactor]]
* @param minSize the minimum size parameter to provide to ScalaCheck, which it will use when
* generating objects for which size matters (such as strings or lists); see [[MinSize]]
* @param sizeRange the maximum size parameter to provide to ScalaCheck, which it will use when
* generating objects for which size matters (such as strings or lists); see [[SizeRange]]
* @param workers number of worker threads to use when evaluating a property; see [[Workers]]
*/
case class Parameter(minSuccessful: PosInt = PosInt(10),
maxDiscardedFactor: PosZDouble = PosZDouble(5.0),
minSize: PosZInt = PosZInt(0),
sizeRange: PosZInt = PosZInt(100),
workers: PosInt = PosInt(1)) {
import org.scalactic.Requirements._
require(minSize.value + sizeRange.value >= 0)
lazy val maxSize: PosZInt = {
PosZInt.ensuringValid(minSize.value + sizeRange.value)
}
}
/**
* Compute the newer MaxDiscardedFactor from the older parameters.
*
* TODO: is this actually used anywhere? All calls I find seem to be using the version in
* [[PropertyCheckConfiguration]] instead.
*
* @param minSuccessful the minimum number of successful evaluations
* @param maxDiscarded the maximum number that may be discarded
* @return the newer-style ratio
*/
private[scalatest] def calculateMaxDiscardedFactor(minSuccessful: Int, maxDiscarded: Int): Double =
((maxDiscarded + 1): Double) / (minSuccessful: Double)
/**
* Compute the number of discarded evaluation attempts that will be considered to be exhausted. This is a
* multiple of two config parameters.
*
* @param maxDiscardedRatio the multiplier of acceptable discarded-to-successful evaluations
* @param minSuccessful the minimum number of successful evaluations
* @return how many discarded evaluations to allow in this run
*/
private[scalatest] def calculateMaxDiscarded(maxDiscardedRatio: Double, minSuccessful: Int): Double =
(maxDiscardedRatio * minSuccessful) - 1
}