Path: blob/master/test/jdk/java/util/Optional/BasicDouble.java
41149 views
/*1* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 819564925* @summary Basic functional test of OptionalDouble26* @author Mike Duigou27* @build ObscureException28* @run testng BasicDouble29*/3031import java.util.NoSuchElementException;32import java.util.OptionalDouble;33import java.util.concurrent.atomic.AtomicBoolean;3435import static org.testng.Assert.*;36import org.testng.annotations.Test;3738public class BasicDouble {39static final double DOUBLEVAL = Math.PI;40static final double UNEXPECTED = 6.62607004E-34;4142/**43* Checks a block of assertions over an empty OptionalDouble.44*/45void checkEmpty(OptionalDouble empty) {46assertTrue(empty.equals(OptionalDouble.empty()));47assertTrue(OptionalDouble.empty().equals(empty));48assertFalse(empty.equals(OptionalDouble.of(UNEXPECTED)));49assertFalse(OptionalDouble.of(UNEXPECTED).equals(empty));50assertFalse(empty.equals("unexpected"));5152assertFalse(empty.isPresent());53assertTrue(empty.isEmpty());54assertEquals(empty.hashCode(), 0);55assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);56assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);5758assertThrows(NoSuchElementException.class, () -> empty.getAsDouble());59assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());60assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new));6162var b = new AtomicBoolean();63empty.ifPresent(s -> b.set(true));64assertFalse(b.get());6566var b1 = new AtomicBoolean(false);67var b2 = new AtomicBoolean(false);68empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));69assertFalse(b1.get());70assertTrue(b2.get());7172assertEquals(empty.toString(), "OptionalDouble.empty");73}7475/**76* Checks a block of assertions over an OptionalDouble that is expected to77* have a particular value present.78*/79void checkPresent(OptionalDouble opt, double expected) {80assertFalse(opt.equals(OptionalDouble.empty()));81assertFalse(OptionalDouble.empty().equals(opt));82assertTrue(opt.equals(OptionalDouble.of(expected)));83assertTrue(OptionalDouble.of(expected).equals(opt));84assertFalse(opt.equals(OptionalDouble.of(UNEXPECTED)));85assertFalse(OptionalDouble.of(UNEXPECTED).equals(opt));86assertFalse(opt.equals("unexpected"));8788assertTrue(opt.isPresent());89assertFalse(opt.isEmpty());90assertEquals(opt.hashCode(), Double.hashCode(expected));91assertEquals(opt.orElse(UNEXPECTED), expected);92assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);9394assertEquals(opt.getAsDouble(), expected);95assertEquals(opt.orElseThrow(), expected);96assertEquals(opt.orElseThrow(ObscureException::new), expected);9798var b = new AtomicBoolean(false);99opt.ifPresent(s -> b.set(true));100assertTrue(b.get());101102var b1 = new AtomicBoolean(false);103var b2 = new AtomicBoolean(false);104opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));105assertTrue(b1.get());106assertFalse(b2.get());107108assertEquals(opt.toString(), "OptionalDouble[" + expected + "]");109}110111@Test(groups = "unit")112public void testEmpty() {113checkEmpty(OptionalDouble.empty());114}115116@Test(groups = "unit")117public void testPresent() {118checkPresent(OptionalDouble.of(DOUBLEVAL), DOUBLEVAL);119}120121@Test(groups = "unit")122public void testStreamEmpty() {123assertEquals(OptionalDouble.empty().stream().toArray(), new double[] { });124}125126@Test(groups = "unit")127public void testStreamPresent() {128assertEquals(OptionalDouble.of(DOUBLEVAL).stream().toArray(), new double[] { DOUBLEVAL });129}130}131132133