|
| 1 | +/* |
| 2 | + * Scala (https://www.scala-lang.org) |
| 3 | + * |
| 4 | + * Copyright EPFL and Lightbend, Inc. |
| 5 | + * |
| 6 | + * Licensed under Apache License 2.0 |
| 7 | + * (http://www.apache.org/licenses/LICENSE-2.0). |
| 8 | + * |
| 9 | + * See the NOTICE file distributed with this work for |
| 10 | + * additional information regarding copyright ownership. |
| 11 | + */ |
| 12 | + |
| 13 | +package scala.collection.parallel.mutable |
| 14 | + |
| 15 | +import org.junit.Test |
| 16 | +import org.junit.Assert._ |
| 17 | + |
| 18 | +class ParArrayTest extends scala.collection.concurrent.ctries_old.Spec { |
| 19 | + |
| 20 | + @Test |
| 21 | + def `create new parallel array with a bad initial capacity`: Unit = { |
| 22 | + evaluating { new ParArray(-5) }.shouldProduce[IllegalArgumentException]() |
| 23 | + /** |
| 24 | + * this currently passes, but do we want it to? |
| 25 | + * does it have meaning to have an empty parallel array? |
| 26 | + */ |
| 27 | + new ParArray(0) |
| 28 | + () |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + def `compare identical ParArrays`: Unit = { |
| 33 | + assert(new ParArray(5) == new ParArray(5)) |
| 34 | + assert(ParArray(1,2,3,4,5) == ParArray(1,2,3,4,5)) |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * this test needs attention. how is equality defined on ParArrays? |
| 39 | + * Well, the same way it is for normal collections, I guess. For normal arrays its reference equality. |
| 40 | + * I do not think it should be that way in the case of ParArray-s. I'll check this with Martin. |
| 41 | + */ |
| 42 | + @Test |
| 43 | + def `compare non-identical ParArrays`: Unit = { |
| 44 | + assert(ParArray(1,2,3,4,5) != ParArray(1,2,3,4), |
| 45 | + "compared PA's that I expect to not be identical, but they were!") |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + def `"creation via PA object [String]`: Unit = { |
| 50 | + val paFromApply: ParArray[String] = ParArray("x", "1", "true", "etrijwejiorwer") |
| 51 | + val paFromHandoff: ParArray[String] = ParArray.handoff(Array("x", "1", "true", "etrijwejiorwer")) |
| 52 | + val paFromCopy: ParArray[String] = ParArray.createFromCopy(Array("x", "1", "true", "etrijwejiorwer")) |
| 53 | + assert( paFromApply == paFromCopy ) |
| 54 | + assert( paFromApply == paFromCopy ) |
| 55 | + } |
| 56 | + |
| 57 | +// // handoffs dont work for primitive types... |
| 58 | +// test("creation via PA object [Boolean]"){ |
| 59 | +// val paFromApply: ParArray[Boolean] = ParArray(true, false, true, false) |
| 60 | +// val paFromCopy: ParArray[Boolean] = ParArray.createFromCopy(Array(true, false, true, false)) |
| 61 | +// assert( paFromApply == paFromCopy ) |
| 62 | +// } |
| 63 | +// |
| 64 | +// // handoffs dont work for primitive types... |
| 65 | +// test("creation via PA object [Int]"){ |
| 66 | +// val paFromApply: ParArray[Int] = ParArray(1, 2, 4, 3) |
| 67 | +// val paFromCopy: ParArray[Int] = ParArray.createFromCopy(Array(1, 2, 4, 3)) |
| 68 | +// assert( paFromApply == paFromCopy ) |
| 69 | +// } |
| 70 | + |
| 71 | + /** |
| 72 | + * This fails because handoff is really doing a copy. |
| 73 | + * TODO: look at handoff |
| 74 | + */ |
| 75 | + @Test |
| 76 | + def `Handoff Is Really A Handoff`: Unit = { |
| 77 | + val arrayToHandOff = Array("a", "x", "y", "z") |
| 78 | + val paFromHandoff: ParArray[String] = ParArray.handoff(arrayToHandOff) |
| 79 | + arrayToHandOff(0) = "w" |
| 80 | + assert(paFromHandoff(0) == "w") |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + def `simple reduce`: Unit = { |
| 85 | + assert( ParArray(1,2,3,4,5).reduce(_+_) == 15 ) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + def `simple count`: Unit = { |
| 90 | + assert( ParArray[Int]().count(_ > 7) == 0 ) |
| 91 | + assert( ParArray(1,2,3).count(_ > 7) == 0 ) |
| 92 | + assert( ParArray(1,2,3).count(_ <= 3) == 3 ) |
| 93 | + assert( ParArray(1,2,3,4,5,6,7,8,9,10).count(_ > 7 ) == 3 ) |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + def `simple forall`: Unit = { |
| 98 | + assert( ParArray[Int]().forall(_ > 7) == true ) |
| 99 | + assert( ParArray(1,2,3).forall(_ > 3) == false ) |
| 100 | + assert( ParArray(1,2,3).forall(_ <= 3) == true ) |
| 101 | + assert( ParArray(1,2,3,4,5,6,7,8,9,10).forall(_ > 0) == true ) |
| 102 | + assert( ParArray(1,2,3,4,5,6,7,8,9,10).forall(_ < 5) == false ) |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + */ |
| 107 | + @Test |
| 108 | + def `simple foreach`: Unit = { |
| 109 | + val buf = new java.util.concurrent.ArrayBlockingQueue[Int](10000) |
| 110 | + ParArray((1 to 10000):_*).foreach(buf add _) |
| 111 | + (1 to 10000).foreach(i => assert( buf contains i, "buf should have contained:" + i )) |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + def `simple exists`: Unit = { |
| 116 | + assert( ParArray[Int]().exists(_ => true) == false ) |
| 117 | + assert( ParArray(1,2,3).forall(_ > 3) == false ) |
| 118 | + assert( ParArray(1,2,3,4,5,6,7,8,9,10).exists(_ > 7) == true ) |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + def `simple filter`: Unit = { |
| 123 | + assert(ParArray(1,2,3,4,5).filter( _ < 4 ) == ParArray(1,2,3)) |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + def `simple map test`: Unit = { |
| 128 | + assert(ParArray(1,2,3,4,5).map( (_:Int) * 10 ) == ParArray(10,20,30,40,50)) |
| 129 | + } |
| 130 | +} |
0 commit comments