diff --git a/junit/src/test/scala/scala/collection/parallel/mutable/ParArrayTest.scala b/junit/src/test/scala/scala/collection/parallel/mutable/ParArrayTest.scala new file mode 100644 index 00000000..458951ae --- /dev/null +++ b/junit/src/test/scala/scala/collection/parallel/mutable/ParArrayTest.scala @@ -0,0 +1,130 @@ +/* + * Scala (https://www.scala-lang.org) + * + * Copyright EPFL and Lightbend, Inc. + * + * Licensed under Apache License 2.0 + * (http://www.apache.org/licenses/LICENSE-2.0). + * + * See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + */ + +package scala.collection.parallel.mutable + +import org.junit.Test +import org.junit.Assert._ + +class ParArrayTest extends scala.collection.concurrent.ctries_old.Spec { + + @Test + def `create new parallel array with a bad initial capacity`: Unit = { + evaluating { new ParArray(-5) }.shouldProduce[IllegalArgumentException]() + /** + * this currently passes, but do we want it to? + * does it have meaning to have an empty parallel array? + */ + new ParArray(0) + () + } + + @Test + def `compare identical ParArrays`: Unit = { + assert(new ParArray(5) == new ParArray(5)) + assert(ParArray(1,2,3,4,5) == ParArray(1,2,3,4,5)) + } + + /** + * this test needs attention. how is equality defined on ParArrays? + * Well, the same way it is for normal collections, I guess. For normal arrays its reference equality. + * I do not think it should be that way in the case of ParArray-s. I'll check this with Martin. + */ + @Test + def `compare non-identical ParArrays`: Unit = { + assert(ParArray(1,2,3,4,5) != ParArray(1,2,3,4), + "compared PA's that I expect to not be identical, but they were!") + } + + @Test + def `"creation via PA object [String]`: Unit = { + val paFromApply: ParArray[String] = ParArray("x", "1", "true", "etrijwejiorwer") + val paFromHandoff: ParArray[String] = ParArray.handoff(Array("x", "1", "true", "etrijwejiorwer")) + val paFromCopy: ParArray[String] = ParArray.createFromCopy(Array("x", "1", "true", "etrijwejiorwer")) + assert( paFromApply == paFromCopy ) + assert( paFromApply == paFromCopy ) + } + +// // handoffs dont work for primitive types... +// test("creation via PA object [Boolean]"){ +// val paFromApply: ParArray[Boolean] = ParArray(true, false, true, false) +// val paFromCopy: ParArray[Boolean] = ParArray.createFromCopy(Array(true, false, true, false)) +// assert( paFromApply == paFromCopy ) +// } +// +// // handoffs dont work for primitive types... +// test("creation via PA object [Int]"){ +// val paFromApply: ParArray[Int] = ParArray(1, 2, 4, 3) +// val paFromCopy: ParArray[Int] = ParArray.createFromCopy(Array(1, 2, 4, 3)) +// assert( paFromApply == paFromCopy ) +// } + + /** + * This fails because handoff is really doing a copy. + * TODO: look at handoff + */ + @Test + def `Handoff Is Really A Handoff`: Unit = { + val arrayToHandOff = Array("a", "x", "y", "z") + val paFromHandoff: ParArray[String] = ParArray.handoff(arrayToHandOff) + arrayToHandOff(0) = "w" + assert(paFromHandoff(0) == "w") + } + + @Test + def `simple reduce`: Unit = { + assert( ParArray(1,2,3,4,5).reduce(_+_) == 15 ) + } + + @Test + def `simple count`: Unit = { + assert( ParArray[Int]().count(_ > 7) == 0 ) + assert( ParArray(1,2,3).count(_ > 7) == 0 ) + assert( ParArray(1,2,3).count(_ <= 3) == 3 ) + assert( ParArray(1,2,3,4,5,6,7,8,9,10).count(_ > 7 ) == 3 ) + } + + @Test + def `simple forall`: Unit = { + assert( ParArray[Int]().forall(_ > 7) == true ) + assert( ParArray(1,2,3).forall(_ > 3) == false ) + assert( ParArray(1,2,3).forall(_ <= 3) == true ) + assert( ParArray(1,2,3,4,5,6,7,8,9,10).forall(_ > 0) == true ) + assert( ParArray(1,2,3,4,5,6,7,8,9,10).forall(_ < 5) == false ) + } + + /** + */ + @Test + def `simple foreach`: Unit = { + val buf = new java.util.concurrent.ArrayBlockingQueue[Int](10000) + ParArray((1 to 10000):_*).foreach(buf add _) + (1 to 10000).foreach(i => assert( buf contains i, "buf should have contained:" + i )) + } + + @Test + def `simple exists`: Unit = { + assert( ParArray[Int]().exists(_ => true) == false ) + assert( ParArray(1,2,3).forall(_ > 3) == false ) + assert( ParArray(1,2,3,4,5,6,7,8,9,10).exists(_ > 7) == true ) + } + + @Test + def `simple filter`: Unit = { + assert(ParArray(1,2,3,4,5).filter( _ < 4 ) == ParArray(1,2,3)) + } + + @Test + def `simple map test`: Unit = { + assert(ParArray(1,2,3,4,5).map( (_:Int) * 10 ) == ParArray(10,20,30,40,50)) + } +} diff --git a/scalacheck/src/test/scala/ParallelArrayCheck.scala b/scalacheck/src/test/scala/ParallelArrayCheck.scala index 02e0c004..62674811 100644 --- a/scalacheck/src/test/scala/ParallelArrayCheck.scala +++ b/scalacheck/src/test/scala/ParallelArrayCheck.scala @@ -29,8 +29,6 @@ abstract class ParallelArrayCheck[T](tp: String) extends ParallelSeqCheck[T]("Pa type CollType = ParArray[T] - def isCheckingViews = false - def hasStrictOrder = true def tasksupport: TaskSupport diff --git a/scalacheck/src/test/scala/ParallelArrayTest.scala b/scalacheck/src/test/scala/ParallelArrayTest.scala deleted file mode 100644 index ef54dbb4..00000000 --- a/scalacheck/src/test/scala/ParallelArrayTest.scala +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Scala (https://www.scala-lang.org) - * - * Copyright EPFL and Lightbend, Inc. - * - * Licensed under Apache License 2.0 - * (http://www.apache.org/licenses/LICENSE-2.0). - * - * See the NOTICE file distributed with this work for - * additional information regarding copyright ownership. - */ - -// package test.scala.collection.parallel.mutable - -// import org.scalatest.FunSuite -// import collection.parallel.mutable.ParallelArray - -// /** -// * Notes: -// */ -// class ParallelArrayTest extends FunSuite { - -// test("create new parallel array with a bad initial capacity"){ -// intercept[IllegalArgumentException]{ -// new ParallelArray(-5) -// } - -// /** -// * this currently passes, but do we want it to? -// * does it have meaning to have an empty parallel array? -// */ -// new ParallelArray(0) -// () -// } - -// test("compare identical ParallelArrays"){ -// assert(new ParallelArray(5) === new ParallelArray(5)) -// assert(ParallelArray(1,2,3,4,5) === ParallelArray(1,2,3,4,5)) -// } - -// /** -// * this test needs attention. how is equality defined on ParallelArrays? -// * Well, the same way it is for normal collections, I guess. For normal arrays its reference equality. -// * I do not think it should be that way in the case of ParallelArray-s. I'll check this with Martin. -// */ -// test("compare non-identical ParallelArrays"){ -// assert(ParallelArray(1,2,3,4,5) != ParallelArray(1,2,3,4), -// "compared PA's that I expect to not be identical, but they were!") -// } - -// test("creation via PA object [String]"){ -// val paFromApply: ParallelArray[String] = ParallelArray("x", "1", "true", "etrijwejiorwer") -// val paFromHandoff: ParallelArray[String] = ParallelArray.handoff(Array("x", "1", "true", "etrijwejiorwer")) -// val paFromCopy: ParallelArray[String] = ParallelArray.createFromCopy(Array("x", "1", "true", "etrijwejiorwer")) -// assert( paFromApply === paFromCopy ) -// assert( paFromApply === paFromCopy ) -// } - -// // // handoffs dont work for primitive types... -// // test("creation via PA object [Boolean]"){ -// // val paFromApply: ParallelArray[Boolean] = ParallelArray(true, false, true, false) -// // val paFromCopy: ParallelArray[Boolean] = ParallelArray.createFromCopy(Array(true, false, true, false)) -// // assert( paFromApply === paFromCopy ) -// // } -// // -// // // handoffs dont work for primitive types... -// // test("creation via PA object [Int]"){ -// // val paFromApply: ParallelArray[Int] = ParallelArray(1, 2, 4, 3) -// // val paFromCopy: ParallelArray[Int] = ParallelArray.createFromCopy(Array(1, 2, 4, 3)) -// // assert( paFromApply === paFromCopy ) -// // } - -// /** -// * This fails because handoff is really doing a copy. -// * TODO: look at handoff -// */ -// test("Handoff Is Really A Handoff"){ -// val arrayToHandOff = Array("a", "x", "y", "z") -// val paFromHandoff: ParallelArray[String] = ParallelArray.handoff(arrayToHandOff) -// arrayToHandOff(0) = "w" -// assert(paFromHandoff(0) === "w") -// } - -// test("simple reduce"){ -// assert( ParallelArray(1,2,3,4,5).reduce(_+_) === 15 ) -// } - -// test("simple count"){ -// assert( ParallelArray[Int]().count(_ > 7) === 0 ) -// assert( ParallelArray(1,2,3).count(_ > 7) === 0 ) -// assert( ParallelArray(1,2,3).count(_ <= 3) === 3 ) -// assert( ParallelArray(1,2,3,4,5,6,7,8,9,10).count(_ > 7 ) === 3 ) -// } - -// test("simple forall"){ -// assert( ParallelArray[Int]().forall(_ > 7) === true ) -// assert( ParallelArray(1,2,3).forall(_ > 3) === false ) -// assert( ParallelArray(1,2,3).forall(_ <= 3) === true ) -// assert( ParallelArray(1,2,3,4,5,6,7,8,9,10).forall(_ > 0) === true ) -// assert( ParallelArray(1,2,3,4,5,6,7,8,9,10).forall(_ < 5) === false ) -// } - -// /** -// */ -// test("simple foreach"){ -// val buf = new java.util.concurrent.ArrayBlockingQueue[Int](10000) -// ParallelArray((1 to 10000):_*).foreach(buf add _) -// (1 to 10000).foreach(i => assert( buf contains i, "buf should have contained:" + i )) -// } - -// test("simple exists"){ -// assert( ParallelArray[Int]().exists(_ => true) === false ) -// assert( ParallelArray(1,2,3).forall(_ > 3) === false ) -// assert( ParallelArray(1,2,3,4,5,6,7,8,9,10).exists(_ > 7) === true ) -// } - -// test("simple filter"){ -// assert(ParallelArray(1,2,3,4,5).filter( _ < 4 ) === ParallelArray(1,2,3)) -// } - -// test("simple map test"){ -// assert(ParallelArray(1,2,3,4,5).map( (_:Int) * 10 ) === ParallelArray(10,20,30,40,50)) -// } -// } diff --git a/scalacheck/src/test/scala/ParallelArrayViewCheck.scala b/scalacheck/src/test/scala/ParallelArrayViewCheck.scala deleted file mode 100644 index 814a3eef..00000000 --- a/scalacheck/src/test/scala/ParallelArrayViewCheck.scala +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Scala (https://www.scala-lang.org) - * - * Copyright EPFL and Lightbend, Inc. - * - * Licensed under Apache License 2.0 - * (http://www.apache.org/licenses/LICENSE-2.0). - * - * See the NOTICE file distributed with this work for - * additional information regarding copyright ownership. - */ - -// package scala.collection.parallel -// package mutable - -// import org.scalacheck._ -// import org.scalacheck.Gen -// import org.scalacheck.Gen._ -// import org.scalacheck.Prop._ -// import org.scalacheck.Properties -// import org.scalacheck.Arbitrary._ - -// import scala.collection.TraversableView -// import scala.collection.mutable.ArrayBuffer -// import scala.collection.parallel.ops._ -// import scala.collection.mutable.ArraySeq - - -// abstract class ParallelArrayViewCheck[T](tp: String) -// extends ParallelSeqCheck[T]("ParallelSeqView[" + tp + ", ParallelArray[" + tp + "]]") { -// // ForkJoinTasks.defaultForkJoinPool.setMaximumPoolSize(Runtime.getRuntime.availableProcessors * 2) -// // ForkJoinTasks.defaultForkJoinPool.setParallelism(Runtime.getRuntime.availableProcessors * 2) - -// type CollType = ParallelSeqView[T, ParallelArray[T], ArraySeq[T]] - -// def isCheckingViews = true - -// def instances(vals: Seq[Gen[T]]): Gen[Seq[T]] = sized { sz => -// val a = new ArrayBuffer[T](sz) -// val gen = vals(rnd.nextInt(vals.size)) -// for (i <- 0 until sz) a += sample(gen) -// a -// } - -// def fromSeq(a: Seq[T]) = { -// val pa = new ParallelArray[T](a.size) -// var i = 0 -// for (elem <- a) { -// pa(i) = elem -// i += 1 -// } -// pa.view -// } - -// property("forces must be equal") = forAllNoShrink(collectionPairs) { case (s, coll) => -// val smodif = (s ++ s).reverse.take(s.length).reverse.zip(s).drop(s.length / 2) -// val cmodif = (coll ++ s).reverse.take(s.length).reverse.zip(s).drop(s.length / 2).force -// smodif == cmodif -// } - -// } - - -// object IntParallelArrayViewCheck extends ParallelArrayViewCheck[Int]("Int") with IntSeqOperators with IntValues { -// override def instances(vals: Seq[Gen[Int]]) = oneOf(super.instances(vals), sized { sz => -// (0 until sz).toArray.toSeq -// }, sized { sz => -// (-sz until 0).toArray.toSeq -// }) -// } - - -// abstract class ParallelArrayViewComposedCheck[T](tp: String) -// extends ParallelSeqCheck[T]("ParallelSeqView[" + tp + "], ParallelArray[" + tp + "].++.patch.reverse.take.reverse") { -// ForkJoinTasks.defaultForkJoinPool.setMaximumPoolSize(Runtime.getRuntime.availableProcessors * 2) -// ForkJoinTasks.defaultForkJoinPool.setParallelism(Runtime.getRuntime.availableProcessors * 2) - -// type CollType = collection.parallel.ParallelSeq[T] - -// def isCheckingViews = true - -// def instances(vals: Seq[Gen[T]]): Gen[Seq[T]] = sized { sz => -// val a = new ArrayBuffer[T](sz) -// val gen = vals(rnd.nextInt(vals.size)) -// for (i <- 0 until sz) a += sample(gen) -// a -// } - -// def fromSeq(a: Seq[T]) = { -// val pa = new ParallelArray[T](a.size) -// var i = 0 -// for (elem <- a) { -// pa(i) = elem -// i += 1 -// } -// val modified = (pa.view ++ a).patch(0, a, a.length).reverse -// val original = modified.take(modified.length / 2).reverse -// original -// } - -// } - - -// object IntParallelArrayViewComposedCheck extends ParallelArrayViewComposedCheck[Int]("Int") with IntSeqOperators with IntValues { -// override def instances(vals: Seq[Gen[Int]]) = oneOf(super.instances(vals), sized { sz => -// (0 until sz).toArray.toSeq -// }, sized { sz => -// (-sz until 0).toArray.toSeq -// }) -// } diff --git a/scalacheck/src/test/scala/ParallelCtrieCheck.scala b/scalacheck/src/test/scala/ParallelCtrieCheck.scala index 06499471..f6aae85e 100644 --- a/scalacheck/src/test/scala/ParallelCtrieCheck.scala +++ b/scalacheck/src/test/scala/ParallelCtrieCheck.scala @@ -29,8 +29,6 @@ abstract class ParallelConcurrentTrieMapCheck[K, V](tp: String) extends Parallel type CollType = ParTrieMap[K, V] - def isCheckingViews = false - def hasStrictOrder = false def tasksupport: TaskSupport diff --git a/scalacheck/src/test/scala/ParallelHashMapCheck.scala b/scalacheck/src/test/scala/ParallelHashMapCheck.scala index e670387d..91ac1ac9 100644 --- a/scalacheck/src/test/scala/ParallelHashMapCheck.scala +++ b/scalacheck/src/test/scala/ParallelHashMapCheck.scala @@ -29,8 +29,6 @@ abstract class ParallelHashMapCheck[K, V](tp: String) extends ParallelMapCheck[K type CollType = ParHashMap[K, V] - def isCheckingViews = false - def hasStrictOrder = false def tasksupport: TaskSupport diff --git a/scalacheck/src/test/scala/ParallelHashSetCheck.scala b/scalacheck/src/test/scala/ParallelHashSetCheck.scala index dc54fab1..480d1514 100644 --- a/scalacheck/src/test/scala/ParallelHashSetCheck.scala +++ b/scalacheck/src/test/scala/ParallelHashSetCheck.scala @@ -29,8 +29,6 @@ abstract class ParallelHashSetCheck[T](tp: String) extends ParallelSetCheck[T](" type CollType = ParHashSet[T] - def isCheckingViews = false - def hasStrictOrder = false def tasksupport: TaskSupport diff --git a/scalacheck/src/test/scala/ParallelHashTrieCheck.scala b/scalacheck/src/test/scala/ParallelHashTrieCheck.scala index bebb0a97..cc3d43e3 100644 --- a/scalacheck/src/test/scala/ParallelHashTrieCheck.scala +++ b/scalacheck/src/test/scala/ParallelHashTrieCheck.scala @@ -29,8 +29,6 @@ abstract class ParallelHashMapCheck[K, V](tp: String) extends ParallelMapCheck[K type CollType = ParHashMap[K, V] - def isCheckingViews = false - def hasStrictOrder = false def tasksupport: TaskSupport @@ -81,8 +79,6 @@ abstract class ParallelHashSetCheck[T](tp: String) extends ParallelSetCheck[T](" type CollType = ParHashSet[T] - def isCheckingViews = false - def hasStrictOrder = false def tasksupport: TaskSupport diff --git a/scalacheck/src/test/scala/ParallelIterableCheck.scala b/scalacheck/src/test/scala/ParallelIterableCheck.scala index 8b4614e3..4102a127 100644 --- a/scalacheck/src/test/scala/ParallelIterableCheck.scala +++ b/scalacheck/src/test/scala/ParallelIterableCheck.scala @@ -28,7 +28,6 @@ abstract class ParallelIterableCheck[T](collName: String) extends Properties(col def values: Seq[Gen[T]] def ofSize(vals: Seq[Gen[T]], sz: Int): Iterable[T] def fromIterable(t: Iterable[T]): CollType - def isCheckingViews: Boolean def hasStrictOrder: Boolean @@ -253,7 +252,7 @@ abstract class ParallelIterableCheck[T](collName: String) extends Properties(col }).reduceLeft(_ && _) } - if (!isCheckingViews) property("partitions must be equal") = forAllNoShrink(collectionPairs) { case (t, coll) => + property("partitions must be equal") = forAllNoShrink(collectionPairs) { case (t, coll) => (for ((p, ind) <- partitionPredicates.zipWithIndex) yield { val tpart @ (tpart1, tpart2) = t.partition(p) val cpart @ (cpart1, cpart2) = coll.partition(p) diff --git a/scalacheck/src/test/scala/ParallelRangeCheck.scala b/scalacheck/src/test/scala/ParallelRangeCheck.scala index 39cdaa4e..1f1d8d7c 100644 --- a/scalacheck/src/test/scala/ParallelRangeCheck.scala +++ b/scalacheck/src/test/scala/ParallelRangeCheck.scala @@ -31,8 +31,6 @@ abstract class ParallelRangeCheck(val tasksupport: TaskSupport) extends Parallel def hasStrictOrder = true - def isCheckingViews = false - def ofSize(vals: Seq[Gen[Int]], sz: Int) = throw new UnsupportedOperationException override def instances(vals: Seq[Gen[Int]]): Gen[Seq[Int]] = sized { start => diff --git a/scalacheck/src/test/scala/ParallelSeqCheck.scala b/scalacheck/src/test/scala/ParallelSeqCheck.scala index e49d449c..008fce3e 100644 --- a/scalacheck/src/test/scala/ParallelSeqCheck.scala +++ b/scalacheck/src/test/scala/ParallelSeqCheck.scala @@ -230,7 +230,7 @@ abstract class ParallelSeqCheck[T](collName: String) extends ParallelIterableChe // This was failing because some corner cases weren't added to the patch method in ParSeqLike. // Curiously, this wasn't detected before. // - if (!isCheckingViews) property("patches must be equal") = forAll(collectionTripletsWith2Indices) { + property("patches must be equal") = forAll(collectionTripletsWith2Indices) { case (s, coll, pat, from, repl) => ("with seq" |: s.patch(from, pat, repl).sameElements(coll.patch(from, pat, repl))) && ("with par" |: s.patch(from, pat, repl).sameElements(coll.patch(from, fromSeq(pat), repl))) && @@ -238,7 +238,7 @@ abstract class ParallelSeqCheck[T](collName: String) extends ParallelIterableChe ("with one" |: (s.length == 0 || s.patch(from, List(s(0)), 1).sameElements(coll.patch(from, fromSeq(List(coll(0))), 1)))) } - if (!isCheckingViews) property("updates must be equal") = forAllNoShrink(collectionPairsWithLengths) { case (s, coll, len) => + property("updates must be equal") = forAllNoShrink(collectionPairsWithLengths) { case (s, coll, len) => val pos = if (len >= s.length) s.length - 1 else len if (s.length > 0) { val supd = s.updated(pos, s(0)) diff --git a/scalacheck/src/test/scala/ParallelVectorCheck.scala b/scalacheck/src/test/scala/ParallelVectorCheck.scala index ed60cf83..49feac28 100644 --- a/scalacheck/src/test/scala/ParallelVectorCheck.scala +++ b/scalacheck/src/test/scala/ParallelVectorCheck.scala @@ -34,8 +34,6 @@ abstract class ParallelVectorCheck[T](tp: String) extends collection.parallel.Pa type CollType = ParVector[T] - def isCheckingViews = false - def hasStrictOrder = true def tasksupport: TaskSupport