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