/* * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import org.testng.annotations.Test; import java.util.Comparator; import java.util.function.BinaryOperator; import java.util.function.IntBinaryOperator; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; import java.util.function.DoubleBinaryOperator; import java.util.function.LongBinaryOperator; /** * @test * @run testng PrimitiveSumMinMaxTest * @summary test conversion of primitive wrapper sum, min, max, and compareTo methods to functional interfaces * * @author Brian Goetz */ @Test public class PrimitiveSumMinMaxTest { public void testBooleanMethods() { BinaryOperator and = Boolean::logicalAnd; BinaryOperator or = Boolean::logicalOr; BinaryOperator xor = Boolean::logicalXor; Comparator cmp = Boolean::compare; assertTrue(and.operate(true, true)); assertFalse(and.operate(true, false)); assertFalse(and.operate(false, true)); assertFalse(and.operate(false, false)); assertTrue(or.operate(true, true)); assertTrue(or.operate(true, false)); assertTrue(or.operate(false, true)); assertFalse(or.operate(false, false)); assertFalse(xor.operate(true, true)); assertTrue(xor.operate(true, false)); assertTrue(xor.operate(false, true)); assertFalse(xor.operate(false, false)); assertEquals(Boolean.TRUE.compareTo(Boolean.TRUE), cmp.compare(true, true)); assertEquals(Boolean.TRUE.compareTo(Boolean.FALSE), cmp.compare(true, false)); assertEquals(Boolean.FALSE.compareTo(Boolean.TRUE), cmp.compare(false, true)); assertEquals(Boolean.FALSE.compareTo(Boolean.FALSE), cmp.compare(false, false)); }; public void testByteMethods() { BinaryOperator sum1 = Byte::sum; BinaryOperator max1 = Byte::max; BinaryOperator min1 = Byte::min; Comparator cmp = Byte::compare; byte[] numbers = { -1, 0, 1, 100, Byte.MAX_VALUE, Byte.MIN_VALUE }; for (byte i : numbers) { for (byte j : numbers) { assertEquals((byte) (i+j), (byte) sum1.operate(i, j)); assertEquals(Math.max(i,j), (byte) max1.operate(i, j)); assertEquals(Math.min(i,j), (byte) min1.operate(i, j)); assertEquals(((Byte) i).compareTo(j), cmp.compare(i, j)); } } } public void testShortMethods() { BinaryOperator sum1 = Short::sum; BinaryOperator max1 = Short::max; BinaryOperator min1 = Short::min; Comparator cmp = Short::compare; short[] numbers = { -1, 0, 1, 100, Short.MAX_VALUE, Short.MIN_VALUE }; for (short i : numbers) { for (short j : numbers) { assertEquals((short) (i+j), (short) sum1.operate(i, j)); assertEquals(Math.max(i,j), (short) max1.operate(i, j)); assertEquals(Math.min(i,j), (short) min1.operate(i, j)); assertEquals(((Short) i).compareTo(j), cmp.compare(i, j)); } } } public void testIntMethods() { BinaryOperator sum1 = Integer::sum; IntBinaryOperator sum2 = Integer::sum; BinaryOperator max1 = Integer::max; IntBinaryOperator max2 = Integer::max; BinaryOperator min1 = Integer::min; IntBinaryOperator min2 = Integer::min; Comparator cmp = Integer::compare; int[] numbers = { -1, 0, 1, 100, Integer.MAX_VALUE, Integer.MIN_VALUE }; for (int i : numbers) { for (int j : numbers) { assertEquals(i+j, (int) sum1.operate(i, j)); assertEquals(i+j, sum2.operateAsInt(i, j)); assertEquals(Math.max(i,j), (int) max1.operate(i, j)); assertEquals(Math.max(i,j), max2.operateAsInt(i, j)); assertEquals(Math.min(i,j), (int) min1.operate(i, j)); assertEquals(Math.min(i,j), min2.operateAsInt(i, j)); assertEquals(((Integer) i).compareTo(j), cmp.compare(i, j)); } } } public void testLongMethods() { BinaryOperator sum1 = Long::sum; LongBinaryOperator sum2 = Long::sum; BinaryOperator max1 = Long::max; LongBinaryOperator max2 = Long::max; BinaryOperator min1 = Long::min; LongBinaryOperator min2 = Long::min; Comparator cmp = Long::compare; long[] numbers = { -1, 0, 1, 100, Long.MAX_VALUE, Long.MIN_VALUE }; for (long i : numbers) { for (long j : numbers) { assertEquals(i+j, (long) sum1.operate(i, j)); assertEquals(i+j, sum2.operateAsLong(i, j)); assertEquals(Math.max(i,j), (long) max1.operate(i, j)); assertEquals(Math.max(i,j), max2.operateAsLong(i, j)); assertEquals(Math.min(i,j), (long) min1.operate(i, j)); assertEquals(Math.min(i,j), min2.operateAsLong(i, j)); assertEquals(((Long) i).compareTo(j), cmp.compare(i, j)); } } } public void testFloatMethods() { BinaryOperator sum1 = Float::sum; BinaryOperator max1 = Float::max; BinaryOperator min1 = Float::min; Comparator cmp = Float::compare; float[] numbers = { -1, 0, 1, 100, Float.MAX_VALUE, Float.MIN_VALUE }; for (float i : numbers) { for (float j : numbers) { assertEquals(i+j, (float) sum1.operate(i, j)); assertEquals(Math.max(i,j), (float) max1.operate(i, j)); assertEquals(Math.min(i,j), (float) min1.operate(i, j)); assertEquals(((Float) i).compareTo(j), cmp.compare(i, j)); } } } public void testDoubleMethods() { BinaryOperator sum1 = Double::sum; DoubleBinaryOperator sum2 = Double::sum; BinaryOperator max1 = Double::max; DoubleBinaryOperator max2 = Double::max; BinaryOperator min1 = Double::min; DoubleBinaryOperator min2 = Double::min; Comparator cmp = Double::compare; double[] numbers = { -1, 0, 1, 100, Double.MAX_VALUE, Double.MIN_VALUE }; for (double i : numbers) { for (double j : numbers) { assertEquals(i+j, (double) sum1.operate(i, j)); assertEquals(i+j, sum2.operateAsDouble(i, j)); assertEquals(Math.max(i,j), (double) max1.operate(i, j)); assertEquals(Math.max(i,j), max2.operateAsDouble(i, j)); assertEquals(Math.min(i,j), (double) min1.operate(i, j)); assertEquals(Math.min(i,j), min2.operateAsDouble(i, j)); assertEquals(((Double) i).compareTo(j), cmp.compare(i, j)); } } } }