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