8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 8014076 8025067 26 * @summary unit test for Arrays.ParallelPrefix(). 27 * @author Tristan Yan 28 * @run testng ParallelPrefix 29 */ 30 31 import java.util.Arrays; 32 import java.util.function.BinaryOperator; 33 import java.util.function.DoubleBinaryOperator; 34 import java.util.function.Function; 35 import java.util.function.IntBinaryOperator; 36 import java.util.function.LongBinaryOperator; 37 import java.util.stream.IntStream; 38 import java.util.stream.LongStream; 39 import static org.testng.Assert.*; 40 import org.testng.annotations.DataProvider; 41 import org.testng.annotations.Test; 42 43 public class ParallelPrefix { 44 //Array size less than MIN_PARTITION 45 private static final int SMALL_ARRAY_SIZE = 1 << 3; 46 47 //Array size equals MIN_PARTITION 48 private static final int THRESHOLD_ARRAY_SIZE = 1 << 4; 49 50 //Array size greater than MIN_PARTITION 51 private static final int MEDIUM_ARRAY_SIZE = 1 << 8; 52 53 //Array size much greater than MIN_PARTITION 54 private static final int LARGE_ARRAY_SIZE = 1 << 14; 55 56 private static final int[] ARRAY_SIZE_COLLECTION = new int[]{ 57 SMALL_ARRAY_SIZE, 58 THRESHOLD_ARRAY_SIZE, 59 MEDIUM_ARRAY_SIZE, 60 LARGE_ARRAY_SIZE 61 }; 62 63 @DataProvider(name = "intSet") 64 public static Object[][] intSet(){ 65 return genericData(size -> IntStream.range(0, size).toArray(), 66 new IntBinaryOperator[]{ 67 Integer::sum, 68 Integer::min}); 69 } 70 71 @DataProvider(name = "longSet") 72 public static Object[][] longSet(){ 73 return genericData(size -> LongStream.range(0, size).toArray(), 74 new LongBinaryOperator[]{ 75 Long::sum, 76 Long::min}); 77 } 78 79 @DataProvider(name = "doubleSet") 80 public static Object[][] doubleSet(){ 81 return genericData(size -> IntStream.range(0, size).mapToDouble(i -> (double)i).toArray(), 82 new DoubleBinaryOperator[]{ 83 Double::sum, 84 Double::min}); 85 } 86 87 @DataProvider(name = "stringSet") 88 public static Object[][] stringSet(){ 89 Function<Integer, String[]> stringsFunc = size -> 90 IntStream.range(0, size).mapToObj(Integer::toString).toArray(String[]::new); 91 BinaryOperator<String> concat = String::concat; 92 return genericData(stringsFunc, 93 (BinaryOperator<String>[]) new BinaryOperator[]{ 94 concat }); 95 } 96 97 private static <T, OPS> Object[][] genericData(Function<Integer, T> generateFunc, OPS[] ops) { 98 //test arrays which size is equals n-1, n, n+1, test random data 99 Object[][] data = new Object[ARRAY_SIZE_COLLECTION.length * 3 * ops.length][4]; 100 for(int n = 0; n < ARRAY_SIZE_COLLECTION.length; n++ ) { 101 for(int testValue = -1 ; testValue <= 1; testValue++) { 102 int array_size = ARRAY_SIZE_COLLECTION[n] + testValue; 103 for(int opsN = 0; opsN < ops.length; opsN++) { 104 int index = n * 3 * ops.length + (testValue + 1) * ops.length + opsN; 105 data[index][0] = generateFunc.apply(array_size); 106 data[index][1] = array_size / 3; 107 data[index][2] = 2 * array_size / 3; 108 data[index][3] = ops[opsN]; 109 } 110 } 111 } 112 return data; 113 } 114 115 @Test(dataProvider="intSet") 116 public void testParallelPrefixForInt(int[] data, int fromIndex, int toIndex, IntBinaryOperator op) { 117 int[] sequentialResult = data.clone(); 118 for (int index = fromIndex + 1; index < toIndex; index++) { 119 sequentialResult[index ] = op.applyAsInt(sequentialResult[index - 1], sequentialResult[index]); 120 } 121 122 int[] parallelResult = data.clone(); | 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 8014076 8025067 26 * @summary unit test for Arrays.ParallelPrefix(). 27 * @author Tristan Yan 28 * @modules java.management jdk.management 29 * @run testng/othervm -Xms256m -Xmx1024m ParallelPrefix 30 */ 31 32 import java.lang.management.ManagementFactory; 33 import java.util.Arrays; 34 import java.util.function.BinaryOperator; 35 import java.util.function.DoubleBinaryOperator; 36 import java.util.function.Function; 37 import java.util.function.IntBinaryOperator; 38 import java.util.function.LongBinaryOperator; 39 import java.util.stream.IntStream; 40 import java.util.stream.LongStream; 41 import com.sun.management.OperatingSystemMXBean; 42 import static org.testng.Assert.*; 43 import org.testng.annotations.DataProvider; 44 import org.testng.annotations.Test; 45 import org.testng.annotations.BeforeSuite; 46 47 public class ParallelPrefix { 48 //Array size less than MIN_PARTITION 49 private static final int SMALL_ARRAY_SIZE = 1 << 3; 50 51 //Array size equals MIN_PARTITION 52 private static final int THRESHOLD_ARRAY_SIZE = 1 << 4; 53 54 //Array size greater than MIN_PARTITION 55 private static final int MEDIUM_ARRAY_SIZE = 1 << 8; 56 57 //Array size much greater than MIN_PARTITION 58 private static final int LARGE_ARRAY_SIZE = 1 << 14; 59 60 private static int[] arraySizeCollection; 61 62 @BeforeSuite 63 public static void setup() { 64 java.lang.management.OperatingSystemMXBean bean = 65 ManagementFactory.getOperatingSystemMXBean(); 66 if (bean instanceof OperatingSystemMXBean) { 67 OperatingSystemMXBean os = (OperatingSystemMXBean)bean; 68 long physicalMemorySize = os.getTotalPhysicalMemorySize() / (1024 * 1024); 69 System.out.println("System memory size: " + physicalMemorySize + "M"); 70 // when we can get system memory size, and it's larger than 2G, 71 // then we enable large array size test below, 72 // else disable large array size test below. 73 if (physicalMemorySize > (2 * 1024)) { 74 arraySizeCollection = new int[]{ 75 SMALL_ARRAY_SIZE, 76 THRESHOLD_ARRAY_SIZE, 77 MEDIUM_ARRAY_SIZE, 78 LARGE_ARRAY_SIZE 79 }; 80 System.out.println("System memory is large enough, add large array size test"); 81 return; 82 } 83 } 84 arraySizeCollection = new int[]{ 85 SMALL_ARRAY_SIZE, 86 THRESHOLD_ARRAY_SIZE, 87 MEDIUM_ARRAY_SIZE 88 }; 89 System.out.println("System memory is not large enough, remove large array size test"); 90 } 91 92 @DataProvider(name = "intSet") 93 public static Object[][] intSet(){ 94 return genericData(size -> IntStream.range(0, size).toArray(), 95 new IntBinaryOperator[]{ 96 Integer::sum, 97 Integer::min}); 98 } 99 100 @DataProvider(name = "longSet") 101 public static Object[][] longSet(){ 102 return genericData(size -> LongStream.range(0, size).toArray(), 103 new LongBinaryOperator[]{ 104 Long::sum, 105 Long::min}); 106 } 107 108 @DataProvider(name = "doubleSet") 109 public static Object[][] doubleSet(){ 110 return genericData(size -> IntStream.range(0, size).mapToDouble(i -> (double)i).toArray(), 111 new DoubleBinaryOperator[]{ 112 Double::sum, 113 Double::min}); 114 } 115 116 @DataProvider(name = "stringSet") 117 public static Object[][] stringSet(){ 118 Function<Integer, String[]> stringsFunc = size -> 119 IntStream.range(0, size).mapToObj(Integer::toString).toArray(String[]::new); 120 BinaryOperator<String> concat = String::concat; 121 return genericData(stringsFunc, 122 (BinaryOperator<String>[]) new BinaryOperator[]{ 123 concat }); 124 } 125 126 private static <T, OPS> Object[][] genericData(Function<Integer, T> generateFunc, OPS[] ops) { 127 //test arrays which size is equals n-1, n, n+1, test random data 128 Object[][] data = new Object[arraySizeCollection.length * 3 * ops.length][4]; 129 for(int n = 0; n < arraySizeCollection.length; n++ ) { 130 for(int testValue = -1 ; testValue <= 1; testValue++) { 131 int array_size = arraySizeCollection[n] + testValue; 132 for(int opsN = 0; opsN < ops.length; opsN++) { 133 int index = n * 3 * ops.length + (testValue + 1) * ops.length + opsN; 134 data[index][0] = generateFunc.apply(array_size); 135 data[index][1] = array_size / 3; 136 data[index][2] = 2 * array_size / 3; 137 data[index][3] = ops[opsN]; 138 } 139 } 140 } 141 return data; 142 } 143 144 @Test(dataProvider="intSet") 145 public void testParallelPrefixForInt(int[] data, int fromIndex, int toIndex, IntBinaryOperator op) { 146 int[] sequentialResult = data.clone(); 147 for (int index = fromIndex + 1; index < toIndex; index++) { 148 sequentialResult[index ] = op.applyAsInt(sequentialResult[index - 1], sequentialResult[index]); 149 } 150 151 int[] parallelResult = data.clone(); |