1 /*
2  * Copyright (c) 2012, 2013, 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 package org.openjdk.tests.java.util.stream;
24 
25 import java.util.OptionalDouble;
26 import java.util.OptionalInt;
27 import java.util.OptionalLong;
28 import java.util.stream.*;
29 
30 import org.testng.annotations.Test;
31 
32 import static java.util.stream.LambdaTestHelpers.countTo;
33 
34 /**
35  * MinMaxTest
36  *
37  * @author Brian Goetz
38  */
39 @Test
40 public class MinMaxTest extends OpTestCase {
testMinMax()41     public void testMinMax() {
42         assertTrue(!countTo(0).stream().min(Integer::compare).isPresent());
43         assertTrue(!countTo(0).stream().max(Integer::compare).isPresent());
44         assertEquals(1, (int) countTo(1000).stream().min(Integer::compare).get());
45         assertEquals(1000, (int) countTo(1000).stream().max(Integer::compare).get());
46     }
47 
48     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testOps(String name, TestData.OfRef<Integer> data)49     public void testOps(String name, TestData.OfRef<Integer> data) {
50         exerciseTerminalOps(data, s -> s.min(Integer::compare));
51         exerciseTerminalOps(data, s -> s.max(Integer::compare));
52     }
53 
testIntMinMax()54     public void testIntMinMax() {
55         assertEquals(IntStream.empty().min(), OptionalInt.empty());
56         assertEquals(IntStream.empty().max(), OptionalInt.empty());
57         assertEquals(1, IntStream.range(1, 1001).min().getAsInt());
58         assertEquals(1000, IntStream.range(1, 1001).max().getAsInt());
59     }
60 
61     @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
testIntOps(String name, TestData.OfInt data)62     public void testIntOps(String name, TestData.OfInt data) {
63         exerciseTerminalOps(data, s -> s.min());
64         exerciseTerminalOps(data, s -> s.max());
65     }
66 
testLongMinMax()67     public void testLongMinMax() {
68         assertEquals(LongStream.empty().min(), OptionalLong.empty());
69         assertEquals(LongStream.empty().max(), OptionalLong.empty());
70         assertEquals(1, LongStream.range(1, 1001).min().getAsLong());
71         assertEquals(1000, LongStream.range(1, 1001).max().getAsLong());
72     }
73 
74     @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
testLongOps(String name, TestData.OfLong data)75     public void testLongOps(String name, TestData.OfLong data) {
76         exerciseTerminalOps(data, s -> s.min());
77         exerciseTerminalOps(data, s -> s.max());
78     }
79 
testDoubleMinMax()80     public void testDoubleMinMax() {
81         assertEquals(DoubleStream.empty().min(), OptionalDouble.empty());
82         assertEquals(DoubleStream.empty().max(), OptionalDouble.empty());
83         assertEquals(1.0, LongStream.range(1, 1001).asDoubleStream().min().getAsDouble());
84         assertEquals(1000.0, LongStream.range(1, 1001).asDoubleStream().max().getAsDouble());
85     }
86 
87     @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
testDoubleOps(String name, TestData.OfDouble data)88     public void testDoubleOps(String name, TestData.OfDouble data) {
89         exerciseTerminalOps(data, s -> s.min());
90         exerciseTerminalOps(data, s -> s.max());
91     }
92 }
93