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
|
package scala.collection.parallel
import org.scalacheck._
import org.scalacheck.Gen
import org.scalacheck.Gen._
import org.scalacheck.Prop._
import org.scalacheck.Properties
import scala.collection._
import scala.collection.parallel._
abstract class ParallelIterableCheck[T](collName: String) extends Properties(collName) with Operators[T] {
type CollType <: ParIterable[T] with Sequentializable[T, Iterable[T]]
def values: Seq[Gen[T]]
def instances(valgens: Seq[Gen[T]]): Gen[Traversable[T]]
def fromTraversable(t: Traversable[T]): CollType
def isCheckingViews: Boolean
def hasStrictOrder: Boolean
def printDataStructureDebugInfo(cf: AnyRef) {
// can be overridden in subclasses
}
val rnd = new scala.util.Random
def sample(gen: Gen[T]): T = {
var s = gen.sample
while (s == None) s = gen.sample
s.get
}
def sampleValue: T = sample(values(rnd.nextInt(values.length)))
def collectionPairs = for (inst <- instances(values)) yield (inst, fromTraversable(inst))
def collectionPairsWithLengths = for (inst <- instances(values); s <- choose(0, inst.size))
yield (inst, fromTraversable(inst), s)
def collectionPairsWith2Indices = for (
inst <- instances(values);
f <- choose(0, inst.size);
s <- choose(0, inst.size))
yield (inst, fromTraversable(inst), f, s)
def collectionTriplets = for (inst <- instances(values);
updStart <- choose(0, inst.size); howMany <- choose(0, inst.size)) yield {
val modif = inst.toSeq.patch(updStart, inst.toSeq, howMany)
(inst, fromTraversable(inst), modif)
}
property("reductions must be equal for assoc. operators") = forAll(collectionPairs) { case (t, coll) =>
if (t.size != 0) {
val results = for ((op, ind) <- reduceOperators.zipWithIndex) yield {
("op index: " + ind) |: t.reduceLeft(op) == coll.reduce(op)
}
results.reduceLeft(_ && _)
} else "has size 0" |: true
}
property("counts must be equal") = forAll(collectionPairs) { case (t, coll) =>
val results = for ((pred, ind) <- countPredicates.zipWithIndex) yield {
val tc = t.count(pred)
val cc = coll.count(pred)
if (tc != cc) {
println("from: " + t)
println("and: " + coll.toList)
println(tc)
println(cc)
}
("op index: " + ind) |: tc == cc
}
results.reduceLeft(_ && _)
}
property("forall must be equal") = forAll(collectionPairs) { case (t, coll) =>
val results = for ((pred, ind) <- forallPredicates.zipWithIndex)
yield ("op index: " + ind) |: t.forall(pred) == coll.forall(pred)
results.reduceLeft(_ && _)
}
property("exists must be equal") = forAll(collectionPairs) { case (t, coll) =>
val results = for ((pred, ind) <- existsPredicates.zipWithIndex)
yield ("op index: " + ind) |: t.exists(pred) == coll.exists(pred)
results.reduceLeft(_ && _)
}
property("both must find or not find an element") = forAll(collectionPairs) { case (t, coll) =>
val results = for ((pred, ind) <- findPredicates.zipWithIndex) yield {
val ft = t.find(pred)
val fcoll = coll.find(pred)
("op index: " + ind) |: ((ft == None && fcoll == None) || (ft != None && fcoll != None))
}
results.reduceLeft(_ && _)
}
def areEqual(t1: Traversable[T], t2: Traversable[T]) = if (hasStrictOrder) {
t1 == t2
} else (t1, t2) match { // it is slightly delicate what `equal` means if the order is not strict
case (m1: Map[_, _], m2: Map[_, _]) => m1 == m2
case (i1: Iterable[_], i2: Iterable[_]) => i1.toSet == i2.toSet
case _ => t1 == t2
}
property("mappings must be equal") = forAll(collectionPairs) { case (t, coll) =>
val results = for ((f, ind) <- mapFunctions.zipWithIndex) yield {
val ms = t.map(f)
val mp = coll.map(f)
if (!areEqual(ms, mp)) {
println(t)
println(coll)
println("mapped to: ")
println(ms)
println(mp)
}
("op index: " + ind) |: areEqual(ms, mp)
}
results.reduceLeft(_ && _)
}
property("collects must be equal") = forAll(collectionPairs) { case (t, coll) =>
val results = for ((f, ind) <- partialMapFunctions.zipWithIndex) yield {
val ps = t.collect(f)
val pp = coll.collect(f)
if (!areEqual(ps, pp)) {
println(t)
println(coll)
println("collected to: ")
println(ps)
println(pp)
}
("op index: " + ind) |: areEqual(ps, pp)
}
results.reduceLeft(_ && _)
}
property("flatMaps must be equal") = forAll(collectionPairs) { case (t, coll) =>
(for ((f, ind) <- flatMapFunctions.zipWithIndex)
yield ("op index: " + ind) |: areEqual(t.flatMap(f), coll.flatMap(f))).reduceLeft(_ && _)
}
property("filters must be equal") = forAll(collectionPairs) { case (t, coll) =>
(for ((p, ind) <- filterPredicates.zipWithIndex) yield {
val tf = t.filter(p)
val cf = coll.filter(p)
if (tf != cf || cf != tf) {
println(t)
println(coll)
println("filtered to:")
println(cf)
println(tf)
println("tf == cf - " + (tf == cf))
println("cf == tf - " + (cf == tf))
printDataStructureDebugInfo(cf)
}
("op index: " + ind) |: tf == cf && cf == tf
}).reduceLeft(_ && _)
}
property("filterNots must be equal") = forAll(collectionPairs) { case (t, coll) =>
(for ((p, ind) <- filterNotPredicates.zipWithIndex)
yield ("op index: " + ind) |: t.filterNot(p) == coll.filterNot(p)).reduceLeft(_ && _)
}
if (!isCheckingViews) property("partitions must be equal") = forAll(collectionPairs) { case (t, coll) =>
(for ((p, ind) <- partitionPredicates.zipWithIndex) yield {
val tpart = t.partition(p)
val cpart = coll.partition(p)
if (tpart != cpart) {
println("from: " + t)
println("and: " + coll)
println(cpart)
println(tpart)
}
("op index: " + ind) |: tpart == cpart
}).reduceLeft(_ && _)
}
if (hasStrictOrder) property("takes must be equal") = forAll(collectionPairsWithLengths) { case (t, coll, n) =>
("take " + n + " elements") |: t.take(n) == coll.take(n)
}
if (hasStrictOrder) property("drops must be equal") = forAll(collectionPairsWithLengths) { case (t, coll, n) =>
("drop " + n + " elements") |: t.drop(n) == coll.drop(n)
}
if (hasStrictOrder) property("slices must be equal") = forAll(collectionPairsWith2Indices)
{ case (t, coll, fr, slicelength) =>
val from = if (fr < 0) 0 else fr
val until = if (from + slicelength > t.size) t.size else from + slicelength
val tsl = t.slice(from, until)
val collsl = coll.slice(from, until)
if (tsl != collsl) {
println("---------------------- " + from + ", " + until)
println("from: " + t)
println("and: " + coll)
println(tsl)
println(collsl)
println("as list: " + collsl.toList)
println(collsl.iterator.hasNext)
println(collsl.iterator.next)
println(collsl.iterator.hasNext)
println(collsl.iterator.next)
println(collsl.iterator.hasNext)
println(collsl.iterator.next)
println(collsl.iterator.hasNext)
}
("slice from " + from + " until " + until) |: tsl == collsl
}
if (hasStrictOrder) property("splits must be equal") = forAll(collectionPairsWithLengths) { case (t, coll, n) =>
val tspl = t.splitAt(n)
val cspl = coll.splitAt(n)
if (tspl != cspl) {
println("at: " + n)
println("from: " + t)
println("and: " + coll)
println(tspl)
println(cspl)
}
("splitAt " + n) |: tspl == cspl
}
if (hasStrictOrder) property("takeWhiles must be equal") = forAll(collectionPairs) { case (t, coll) =>
(for ((pred, ind) <- takeWhilePredicates.zipWithIndex) yield {
val tt = t.takeWhile(pred)
val ct = coll.takeWhile(pred)
if (tt != ct) {
println("from: " + t)
println("and: " + coll)
println("taking while...")
println(tt)
println(ct)
}
("operator " + ind) |: tt == ct
}).reduceLeft(_ && _)
}
if (hasStrictOrder) property("spans must be equal") = forAll(collectionPairs) { case (t, coll) =>
(for ((pred, ind) <- spanPredicates.zipWithIndex) yield {
val tsp = t.span(pred)
val csp = coll.span(pred)
if (tsp != csp) {
println("from: " + t)
println("and: " + coll)
println("span with predicate " + ind)
println(tsp)
println(csp)
println("---------------------------------")
println(coll.span(pred))
println("---------------------------------")
}
("operator " + ind) |: tsp == csp
}).reduceLeft(_ && _)
}
if (hasStrictOrder) property("dropWhiles must be equal") = forAll(collectionPairs) { case (t, coll) =>
(for ((pred, ind) <- dropWhilePredicates.zipWithIndex) yield {
("operator " + ind) |: t.dropWhile(pred) == coll.dropWhile(pred)
}).reduceLeft(_ && _)
}
property("folds must be equal for assoc. operators") = forAll(collectionPairs) { case (t, coll) =>
(for (((first, op), ind) <- foldArguments.zipWithIndex) yield {
val tres = t.foldLeft(first)(op)
val cres = coll.fold(first)(op)
if (cres != tres) {
println("from: " + t)
println("and: " + coll)
println("folds are: ")
println(tres)
println(cres)
}
("operator " + ind) |: tres == cres
}).reduceLeft(_ && _)
}
property("++s must be equal") = forAll(collectionTriplets) { case (t, coll, colltoadd) =>
val toadd = colltoadd
val tr = t ++ toadd.iterator
val cr = coll ++ toadd.iterator
if (areEqual(tr, cr)) {
println("from: " + t)
println("and: " + coll.iterator.toList)
println("adding: " + toadd)
println(tr.toList)
println(cr.iterator.toList)
}
("adding " |: areEqual(tr, cr)) &&
(for ((trav, ind) <- (addAllTraversables).zipWithIndex) yield {
val tadded = t ++ trav
val cadded = coll ++ collection.parallel.mutable.ParArray(trav.toSeq: _*)
if (!areEqual(tadded, cadded)) {
println("----------------------")
println("from: " + t)
println("and: " + coll)
println("adding: " + trav)
println(tadded)
println(cadded)
}
("traversable " + ind) |: areEqual(tadded, cadded)
}).reduceLeft(_ && _)
}
if (hasStrictOrder) property("copies to array must be equal") = forAll(collectionPairs) { case (t, coll) =>
val tarr = newArray(t.size)
val collarr = newArray(coll.size)
t.copyToArray(tarr, 0, t.size)
coll.copyToArray(collarr, 0, coll.size)
if (tarr.toSeq != collarr.toSeq) {
println("from: " + t)
println("and: " + coll)
println(tarr.toSeq)
println(collarr.toSeq)
}
tarr.toSeq == collarr.toSeq
}
}
|