1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 import org.testng.annotations.Test;
  27 
  28 import java.util.Comparator;
  29 import java.util.function.BinaryOperator;
  30 import java.util.function.IntBinaryOperator;
  31 
  32 import static org.testng.Assert.assertEquals;
  33 import static org.testng.Assert.assertFalse;
  34 import static org.testng.Assert.assertTrue;
  35 
  36 import java.util.function.DoubleBinaryOperator;
  37 import java.util.function.LongBinaryOperator;
  38 
  39 /**
  40  * @test
  41  * @run testng PrimitiveSumMinMaxTest
  42  * @summary test conversion of primitive wrapper sum, min, max, and compareTo methods to functional interfaces
  43  *
  44  * @author Brian Goetz
  45  */
  46 @Test
  47 public class PrimitiveSumMinMaxTest {
  48 
  49     public void testBooleanMethods() {
  50         BinaryOperator<Boolean> and = Boolean::logicalAnd;
  51         BinaryOperator<Boolean> or = Boolean::logicalOr;
  52         BinaryOperator<Boolean> xor = Boolean::logicalXor;
  53         Comparator<Boolean> cmp = Boolean::compare;
  54 
  55         assertTrue(and.operate(true, true));
  56         assertFalse(and.operate(true, false));
  57         assertFalse(and.operate(false, true));
  58         assertFalse(and.operate(false, false));
  59 
  60         assertTrue(or.operate(true, true));
  61         assertTrue(or.operate(true, false));
  62         assertTrue(or.operate(false, true));
  63         assertFalse(or.operate(false, false));
  64 
  65         assertFalse(xor.operate(true, true));
  66         assertTrue(xor.operate(true, false));
  67         assertTrue(xor.operate(false, true));
  68         assertFalse(xor.operate(false, false));
  69 
  70         assertEquals(Boolean.TRUE.compareTo(Boolean.TRUE), cmp.compare(true, true));
  71         assertEquals(Boolean.TRUE.compareTo(Boolean.FALSE), cmp.compare(true, false));
  72         assertEquals(Boolean.FALSE.compareTo(Boolean.TRUE), cmp.compare(false, true));
  73         assertEquals(Boolean.FALSE.compareTo(Boolean.FALSE), cmp.compare(false, false));
  74     };
  75 
  76     public void testByteMethods() {
  77         BinaryOperator<Byte> sum1 = Byte::sum;
  78         BinaryOperator<Byte> max1 = Byte::max;
  79         BinaryOperator<Byte> min1 = Byte::min;
  80         Comparator<Byte> cmp = Byte::compare;
  81 
  82         byte[] numbers = { -1, 0, 1, 100, Byte.MAX_VALUE, Byte.MIN_VALUE };
  83         for (byte i : numbers) {
  84             for (byte j : numbers) {
  85                 assertEquals((byte) (i+j), (byte) sum1.operate(i, j));
  86                 assertEquals(Math.max(i,j), (byte) max1.operate(i, j));
  87                 assertEquals(Math.min(i,j), (byte) min1.operate(i, j));
  88                 assertEquals(((Byte) i).compareTo(j), cmp.compare(i, j));
  89             }
  90         }
  91     }
  92 
  93     public void testShortMethods() {
  94         BinaryOperator<Short> sum1 = Short::sum;
  95         BinaryOperator<Short> max1 = Short::max;
  96         BinaryOperator<Short> min1 = Short::min;
  97         Comparator<Short> cmp = Short::compare;
  98 
  99         short[] numbers = { -1, 0, 1, 100, Short.MAX_VALUE, Short.MIN_VALUE };
 100         for (short i : numbers) {
 101             for (short j : numbers) {
 102                 assertEquals((short) (i+j), (short) sum1.operate(i, j));
 103                 assertEquals(Math.max(i,j), (short) max1.operate(i, j));
 104                 assertEquals(Math.min(i,j), (short) min1.operate(i, j));
 105                 assertEquals(((Short) i).compareTo(j), cmp.compare(i, j));
 106             }
 107         }
 108     }
 109 
 110     public void testIntMethods() {
 111         BinaryOperator<Integer> sum1 = Integer::sum;
 112         IntBinaryOperator sum2 = Integer::sum;
 113         BinaryOperator<Integer> max1 = Integer::max;
 114         IntBinaryOperator max2 = Integer::max;
 115         BinaryOperator<Integer> min1 = Integer::min;
 116         IntBinaryOperator min2 = Integer::min;
 117         Comparator<Integer> cmp = Integer::compare;
 118 
 119         int[] numbers = { -1, 0, 1, 100, Integer.MAX_VALUE, Integer.MIN_VALUE };
 120         for (int i : numbers) {
 121             for (int j : numbers) {
 122                 assertEquals(i+j, (int) sum1.operate(i, j));
 123                 assertEquals(i+j, sum2.operateAsInt(i, j));
 124                 assertEquals(Math.max(i,j), (int) max1.operate(i, j));
 125                 assertEquals(Math.max(i,j), max2.operateAsInt(i, j));
 126                 assertEquals(Math.min(i,j), (int) min1.operate(i, j));
 127                 assertEquals(Math.min(i,j), min2.operateAsInt(i, j));
 128                 assertEquals(((Integer) i).compareTo(j), cmp.compare(i, j));
 129             }
 130         }
 131     }
 132 
 133     public void testLongMethods() {
 134         BinaryOperator<Long> sum1 = Long::sum;
 135         LongBinaryOperator sum2 = Long::sum;
 136         BinaryOperator<Long> max1 = Long::max;
 137         LongBinaryOperator max2 = Long::max;
 138         BinaryOperator<Long> min1 = Long::min;
 139         LongBinaryOperator min2 = Long::min;
 140         Comparator<Long> cmp = Long::compare;
 141 
 142         long[] numbers = { -1, 0, 1, 100, Long.MAX_VALUE, Long.MIN_VALUE };
 143         for (long i : numbers) {
 144             for (long j : numbers) {
 145                 assertEquals(i+j, (long) sum1.operate(i, j));
 146                 assertEquals(i+j, sum2.operateAsLong(i, j));
 147                 assertEquals(Math.max(i,j), (long) max1.operate(i, j));
 148                 assertEquals(Math.max(i,j), max2.operateAsLong(i, j));
 149                 assertEquals(Math.min(i,j), (long) min1.operate(i, j));
 150                 assertEquals(Math.min(i,j), min2.operateAsLong(i, j));
 151                 assertEquals(((Long) i).compareTo(j), cmp.compare(i, j));
 152             }
 153         }
 154     }
 155 
 156     public void testFloatMethods() {
 157         BinaryOperator<Float> sum1 = Float::sum;
 158         BinaryOperator<Float> max1 = Float::max;
 159         BinaryOperator<Float> min1 = Float::min;
 160         Comparator<Float> cmp = Float::compare;
 161 
 162         float[] numbers = { -1, 0, 1, 100, Float.MAX_VALUE, Float.MIN_VALUE };
 163         for (float i : numbers) {
 164             for (float j : numbers) {
 165                 assertEquals(i+j, (float) sum1.operate(i, j));
 166                 assertEquals(Math.max(i,j), (float) max1.operate(i, j));
 167                 assertEquals(Math.min(i,j), (float) min1.operate(i, j));
 168                 assertEquals(((Float) i).compareTo(j), cmp.compare(i, j));
 169             }
 170         }
 171     }
 172 
 173     public void testDoubleMethods() {
 174         BinaryOperator<Double> sum1 = Double::sum;
 175         DoubleBinaryOperator sum2 = Double::sum;
 176         BinaryOperator<Double> max1 = Double::max;
 177         DoubleBinaryOperator max2 = Double::max;
 178         BinaryOperator<Double> min1 = Double::min;
 179         DoubleBinaryOperator min2 = Double::min;
 180         Comparator<Double> cmp = Double::compare;
 181 
 182         double[] numbers = { -1, 0, 1, 100, Double.MAX_VALUE, Double.MIN_VALUE };
 183         for (double i : numbers) {
 184             for (double j : numbers) {
 185                 assertEquals(i+j, (double) sum1.operate(i, j));
 186                 assertEquals(i+j, sum2.operateAsDouble(i, j));
 187                 assertEquals(Math.max(i,j), (double) max1.operate(i, j));
 188                 assertEquals(Math.max(i,j), max2.operateAsDouble(i, j));
 189                 assertEquals(Math.min(i,j), (double) min1.operate(i, j));
 190                 assertEquals(Math.min(i,j), min2.operateAsDouble(i, j));
 191                 assertEquals(((Double) i).compareTo(j), cmp.compare(i, j));
 192             }
 193         }
 194     }
 195 
 196 }