blob: 1c44109c83f612dda0e06501ae201e1731d56b85 [file] [log] [blame]
mduigou13cd88c2013-01-25 16:13:39 -08001/*
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.
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
24import org.testng.annotations.Test;
25
26import java.util.Comparator;
27import java.util.function.BinaryOperator;
28import java.util.function.IntBinaryOperator;
29
30import static org.testng.Assert.assertEquals;
31import static org.testng.Assert.assertFalse;
32import static org.testng.Assert.assertTrue;
33
34import java.util.function.DoubleBinaryOperator;
35import 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
45public 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
53 assertTrue(and.operate(true, true));
54 assertFalse(and.operate(true, false));
55 assertFalse(and.operate(false, true));
56 assertFalse(and.operate(false, false));
57
58 assertTrue(or.operate(true, true));
59 assertTrue(or.operate(true, false));
60 assertTrue(or.operate(false, true));
61 assertFalse(or.operate(false, false));
62
63 assertFalse(xor.operate(true, true));
64 assertTrue(xor.operate(true, false));
65 assertTrue(xor.operate(false, true));
66 assertFalse(xor.operate(false, false));
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) {
86 assertEquals(i+j, (int) sum1.operate(i, j));
87 assertEquals(i+j, sum2.operateAsInt(i, j));
88 assertEquals(Math.max(i,j), (int) max1.operate(i, j));
89 assertEquals(Math.max(i,j), max2.operateAsInt(i, j));
90 assertEquals(Math.min(i,j), (int) min1.operate(i, j));
91 assertEquals(Math.min(i,j), min2.operateAsInt(i, j));
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) {
109 assertEquals(i+j, (long) sum1.operate(i, j));
110 assertEquals(i+j, sum2.operateAsLong(i, j));
111 assertEquals(Math.max(i,j), (long) max1.operate(i, j));
112 assertEquals(Math.max(i,j), max2.operateAsLong(i, j));
113 assertEquals(Math.min(i,j), (long) min1.operate(i, j));
114 assertEquals(Math.min(i,j), min2.operateAsLong(i, j));
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) {
129 assertEquals(i+j, (float) sum1.operate(i, j));
130 assertEquals(Math.max(i,j), (float) max1.operate(i, j));
131 assertEquals(Math.min(i,j), (float) min1.operate(i, j));
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) {
149 assertEquals(i+j, (double) sum1.operate(i, j));
150 assertEquals(i+j, sum2.operateAsDouble(i, j));
151 assertEquals(Math.max(i,j), (double) max1.operate(i, j));
152 assertEquals(Math.max(i,j), max2.operateAsDouble(i, j));
153 assertEquals(Math.min(i,j), (double) min1.operate(i, j));
154 assertEquals(Math.min(i,j), min2.operateAsDouble(i, j));
155 assertEquals(((Double) i).compareTo(j), cmp.compare(i, j));
156 }
157 }
158 }
159
160}