Path: blob/master/test/jdk/java/lang/PrimitiveSumMinMaxTest.java
41145 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import org.testng.annotations.Test;2425import java.util.Comparator;26import java.util.function.BinaryOperator;27import java.util.function.IntBinaryOperator;2829import static org.testng.Assert.assertEquals;30import static org.testng.Assert.assertFalse;31import static org.testng.Assert.assertTrue;3233import java.util.function.DoubleBinaryOperator;34import java.util.function.LongBinaryOperator;3536/**37* @test38* @run testng PrimitiveSumMinMaxTest39* @summary test conversion of primitive wrapper sum, min, max, and compareTo methods to functional interfaces40*41* @author Brian Goetz42*/43@Test44public class PrimitiveSumMinMaxTest {4546public void testBooleanMethods() {47BinaryOperator<Boolean> and = Boolean::logicalAnd;48BinaryOperator<Boolean> or = Boolean::logicalOr;49BinaryOperator<Boolean> xor = Boolean::logicalXor;50Comparator<Boolean> cmp = Boolean::compare;5152assertTrue(and.apply(true, true));53assertFalse(and.apply(true, false));54assertFalse(and.apply(false, true));55assertFalse(and.apply(false, false));5657assertTrue(or.apply(true, true));58assertTrue(or.apply(true, false));59assertTrue(or.apply(false, true));60assertFalse(or.apply(false, false));6162assertFalse(xor.apply(true, true));63assertTrue(xor.apply(true, false));64assertTrue(xor.apply(false, true));65assertFalse(xor.apply(false, false));6667assertEquals(Boolean.TRUE.compareTo(Boolean.TRUE), cmp.compare(true, true));68assertEquals(Boolean.TRUE.compareTo(Boolean.FALSE), cmp.compare(true, false));69assertEquals(Boolean.FALSE.compareTo(Boolean.TRUE), cmp.compare(false, true));70assertEquals(Boolean.FALSE.compareTo(Boolean.FALSE), cmp.compare(false, false));71};7273public void testIntMethods() {74BinaryOperator<Integer> sum1 = Integer::sum;75IntBinaryOperator sum2 = Integer::sum;76BinaryOperator<Integer> max1 = Integer::max;77IntBinaryOperator max2 = Integer::max;78BinaryOperator<Integer> min1 = Integer::min;79IntBinaryOperator min2 = Integer::min;80Comparator<Integer> cmp = Integer::compare;8182int[] numbers = { -1, 0, 1, 100, Integer.MAX_VALUE, Integer.MIN_VALUE };83for (int i : numbers) {84for (int j : numbers) {85assertEquals(i+j, (int) sum1.apply(i, j));86assertEquals(i+j, sum2.applyAsInt(i, j));87assertEquals(Math.max(i,j), (int) max1.apply(i, j));88assertEquals(Math.max(i,j), max2.applyAsInt(i, j));89assertEquals(Math.min(i,j), (int) min1.apply(i, j));90assertEquals(Math.min(i,j), min2.applyAsInt(i, j));91assertEquals(((Integer) i).compareTo(j), cmp.compare(i, j));92}93}94}9596public void testLongMethods() {97BinaryOperator<Long> sum1 = Long::sum;98LongBinaryOperator sum2 = Long::sum;99BinaryOperator<Long> max1 = Long::max;100LongBinaryOperator max2 = Long::max;101BinaryOperator<Long> min1 = Long::min;102LongBinaryOperator min2 = Long::min;103Comparator<Long> cmp = Long::compare;104105long[] numbers = { -1, 0, 1, 100, Long.MAX_VALUE, Long.MIN_VALUE };106for (long i : numbers) {107for (long j : numbers) {108assertEquals(i+j, (long) sum1.apply(i, j));109assertEquals(i+j, sum2.applyAsLong(i, j));110assertEquals(Math.max(i,j), (long) max1.apply(i, j));111assertEquals(Math.max(i,j), max2.applyAsLong(i, j));112assertEquals(Math.min(i,j), (long) min1.apply(i, j));113assertEquals(Math.min(i,j), min2.applyAsLong(i, j));114assertEquals(((Long) i).compareTo(j), cmp.compare(i, j));115}116}117}118119public void testFloatMethods() {120BinaryOperator<Float> sum1 = Float::sum;121BinaryOperator<Float> max1 = Float::max;122BinaryOperator<Float> min1 = Float::min;123Comparator<Float> cmp = Float::compare;124125float[] numbers = { -1, 0, 1, 100, Float.MAX_VALUE, Float.MIN_VALUE };126for (float i : numbers) {127for (float j : numbers) {128assertEquals(i+j, (float) sum1.apply(i, j));129assertEquals(Math.max(i,j), (float) max1.apply(i, j));130assertEquals(Math.min(i,j), (float) min1.apply(i, j));131assertEquals(((Float) i).compareTo(j), cmp.compare(i, j));132}133}134}135136public void testDoubleMethods() {137BinaryOperator<Double> sum1 = Double::sum;138DoubleBinaryOperator sum2 = Double::sum;139BinaryOperator<Double> max1 = Double::max;140DoubleBinaryOperator max2 = Double::max;141BinaryOperator<Double> min1 = Double::min;142DoubleBinaryOperator min2 = Double::min;143Comparator<Double> cmp = Double::compare;144145double[] numbers = { -1, 0, 1, 100, Double.MAX_VALUE, Double.MIN_VALUE };146for (double i : numbers) {147for (double j : numbers) {148assertEquals(i+j, (double) sum1.apply(i, j));149assertEquals(i+j, sum2.applyAsDouble(i, j));150assertEquals(Math.max(i,j), (double) max1.apply(i, j));151assertEquals(Math.max(i,j), max2.applyAsDouble(i, j));152assertEquals(Math.min(i,j), (double) min1.apply(i, j));153assertEquals(Math.min(i,j), min2.applyAsDouble(i, j));154assertEquals(((Double) i).compareTo(j), cmp.compare(i, j));155}156}157}158159}160161162