mduigou | 13cd88c | 2013-01-25 16:13:39 -0800 | [diff] [blame] | 1 | /* |
lana | f3d11f6 | 2013-12-26 12:04:16 -0800 | [diff] [blame] | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
mduigou | 13cd88c | 2013-01-25 16:13:39 -0800 | [diff] [blame] | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 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 | import org.testng.annotations.Test; |
| 25 | |
| 26 | import java.util.Comparator; |
| 27 | import java.util.function.BinaryOperator; |
| 28 | import java.util.function.IntBinaryOperator; |
| 29 | |
| 30 | import static org.testng.Assert.assertEquals; |
| 31 | import static org.testng.Assert.assertFalse; |
| 32 | import static org.testng.Assert.assertTrue; |
| 33 | |
| 34 | import java.util.function.DoubleBinaryOperator; |
| 35 | import java.util.function.LongBinaryOperator; |
| 36 | |
| 37 | /** |
| 38 | * @test |
| 39 | * @run testng PrimitiveSumMinMaxTest |
| 40 | * @summary test conversion of primitive wrapper sum, min, max, and compareTo methods to functional interfaces |
| 41 | * |
| 42 | * @author Brian Goetz |
| 43 | */ |
| 44 | @Test |
| 45 | public class PrimitiveSumMinMaxTest { |
| 46 | |
| 47 | public void testBooleanMethods() { |
| 48 | BinaryOperator<Boolean> and = Boolean::logicalAnd; |
| 49 | BinaryOperator<Boolean> or = Boolean::logicalOr; |
| 50 | BinaryOperator<Boolean> xor = Boolean::logicalXor; |
| 51 | Comparator<Boolean> cmp = Boolean::compare; |
| 52 | |
mduigou | eff9fcd | 2013-02-19 11:56:49 -0800 | [diff] [blame] | 53 | assertTrue(and.apply(true, true)); |
| 54 | assertFalse(and.apply(true, false)); |
| 55 | assertFalse(and.apply(false, true)); |
| 56 | assertFalse(and.apply(false, false)); |
mduigou | 13cd88c | 2013-01-25 16:13:39 -0800 | [diff] [blame] | 57 | |
mduigou | eff9fcd | 2013-02-19 11:56:49 -0800 | [diff] [blame] | 58 | assertTrue(or.apply(true, true)); |
| 59 | assertTrue(or.apply(true, false)); |
| 60 | assertTrue(or.apply(false, true)); |
| 61 | assertFalse(or.apply(false, false)); |
mduigou | 13cd88c | 2013-01-25 16:13:39 -0800 | [diff] [blame] | 62 | |
mduigou | eff9fcd | 2013-02-19 11:56:49 -0800 | [diff] [blame] | 63 | assertFalse(xor.apply(true, true)); |
| 64 | assertTrue(xor.apply(true, false)); |
| 65 | assertTrue(xor.apply(false, true)); |
| 66 | assertFalse(xor.apply(false, false)); |
mduigou | 13cd88c | 2013-01-25 16:13:39 -0800 | [diff] [blame] | 67 | |
| 68 | assertEquals(Boolean.TRUE.compareTo(Boolean.TRUE), cmp.compare(true, true)); |
| 69 | assertEquals(Boolean.TRUE.compareTo(Boolean.FALSE), cmp.compare(true, false)); |
| 70 | assertEquals(Boolean.FALSE.compareTo(Boolean.TRUE), cmp.compare(false, true)); |
| 71 | assertEquals(Boolean.FALSE.compareTo(Boolean.FALSE), cmp.compare(false, false)); |
| 72 | }; |
| 73 | |
| 74 | public void testIntMethods() { |
| 75 | BinaryOperator<Integer> sum1 = Integer::sum; |
| 76 | IntBinaryOperator sum2 = Integer::sum; |
| 77 | BinaryOperator<Integer> max1 = Integer::max; |
| 78 | IntBinaryOperator max2 = Integer::max; |
| 79 | BinaryOperator<Integer> min1 = Integer::min; |
| 80 | IntBinaryOperator min2 = Integer::min; |
| 81 | Comparator<Integer> cmp = Integer::compare; |
| 82 | |
| 83 | int[] numbers = { -1, 0, 1, 100, Integer.MAX_VALUE, Integer.MIN_VALUE }; |
| 84 | for (int i : numbers) { |
| 85 | for (int j : numbers) { |
mduigou | eff9fcd | 2013-02-19 11:56:49 -0800 | [diff] [blame] | 86 | assertEquals(i+j, (int) sum1.apply(i, j)); |
| 87 | assertEquals(i+j, sum2.applyAsInt(i, j)); |
| 88 | assertEquals(Math.max(i,j), (int) max1.apply(i, j)); |
| 89 | assertEquals(Math.max(i,j), max2.applyAsInt(i, j)); |
| 90 | assertEquals(Math.min(i,j), (int) min1.apply(i, j)); |
| 91 | assertEquals(Math.min(i,j), min2.applyAsInt(i, j)); |
mduigou | 13cd88c | 2013-01-25 16:13:39 -0800 | [diff] [blame] | 92 | assertEquals(((Integer) i).compareTo(j), cmp.compare(i, j)); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public void testLongMethods() { |
| 98 | BinaryOperator<Long> sum1 = Long::sum; |
| 99 | LongBinaryOperator sum2 = Long::sum; |
| 100 | BinaryOperator<Long> max1 = Long::max; |
| 101 | LongBinaryOperator max2 = Long::max; |
| 102 | BinaryOperator<Long> min1 = Long::min; |
| 103 | LongBinaryOperator min2 = Long::min; |
| 104 | Comparator<Long> cmp = Long::compare; |
| 105 | |
| 106 | long[] numbers = { -1, 0, 1, 100, Long.MAX_VALUE, Long.MIN_VALUE }; |
| 107 | for (long i : numbers) { |
| 108 | for (long j : numbers) { |
mduigou | eff9fcd | 2013-02-19 11:56:49 -0800 | [diff] [blame] | 109 | assertEquals(i+j, (long) sum1.apply(i, j)); |
| 110 | assertEquals(i+j, sum2.applyAsLong(i, j)); |
| 111 | assertEquals(Math.max(i,j), (long) max1.apply(i, j)); |
| 112 | assertEquals(Math.max(i,j), max2.applyAsLong(i, j)); |
| 113 | assertEquals(Math.min(i,j), (long) min1.apply(i, j)); |
| 114 | assertEquals(Math.min(i,j), min2.applyAsLong(i, j)); |
mduigou | 13cd88c | 2013-01-25 16:13:39 -0800 | [diff] [blame] | 115 | assertEquals(((Long) i).compareTo(j), cmp.compare(i, j)); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public void testFloatMethods() { |
| 121 | BinaryOperator<Float> sum1 = Float::sum; |
| 122 | BinaryOperator<Float> max1 = Float::max; |
| 123 | BinaryOperator<Float> min1 = Float::min; |
| 124 | Comparator<Float> cmp = Float::compare; |
| 125 | |
| 126 | float[] numbers = { -1, 0, 1, 100, Float.MAX_VALUE, Float.MIN_VALUE }; |
| 127 | for (float i : numbers) { |
| 128 | for (float j : numbers) { |
mduigou | eff9fcd | 2013-02-19 11:56:49 -0800 | [diff] [blame] | 129 | assertEquals(i+j, (float) sum1.apply(i, j)); |
| 130 | assertEquals(Math.max(i,j), (float) max1.apply(i, j)); |
| 131 | assertEquals(Math.min(i,j), (float) min1.apply(i, j)); |
mduigou | 13cd88c | 2013-01-25 16:13:39 -0800 | [diff] [blame] | 132 | assertEquals(((Float) i).compareTo(j), cmp.compare(i, j)); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | public void testDoubleMethods() { |
| 138 | BinaryOperator<Double> sum1 = Double::sum; |
| 139 | DoubleBinaryOperator sum2 = Double::sum; |
| 140 | BinaryOperator<Double> max1 = Double::max; |
| 141 | DoubleBinaryOperator max2 = Double::max; |
| 142 | BinaryOperator<Double> min1 = Double::min; |
| 143 | DoubleBinaryOperator min2 = Double::min; |
| 144 | Comparator<Double> cmp = Double::compare; |
| 145 | |
| 146 | double[] numbers = { -1, 0, 1, 100, Double.MAX_VALUE, Double.MIN_VALUE }; |
| 147 | for (double i : numbers) { |
| 148 | for (double j : numbers) { |
mduigou | eff9fcd | 2013-02-19 11:56:49 -0800 | [diff] [blame] | 149 | assertEquals(i+j, (double) sum1.apply(i, j)); |
| 150 | assertEquals(i+j, sum2.applyAsDouble(i, j)); |
| 151 | assertEquals(Math.max(i,j), (double) max1.apply(i, j)); |
| 152 | assertEquals(Math.max(i,j), max2.applyAsDouble(i, j)); |
| 153 | assertEquals(Math.min(i,j), (double) min1.apply(i, j)); |
| 154 | assertEquals(Math.min(i,j), min2.applyAsDouble(i, j)); |
mduigou | 13cd88c | 2013-01-25 16:13:39 -0800 | [diff] [blame] | 155 | assertEquals(((Double) i).compareTo(j), cmp.compare(i, j)); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | } |