Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Optional/BasicDouble.java
47182 views
/*1* Copyright (c) 2013, 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* @summary Basic functional test of OptionalDouble25* @author Mike Duigou26* @run testng BasicDouble27*/2829import java.util.NoSuchElementException;30import java.util.OptionalDouble;3132import static org.testng.Assert.*;33import org.testng.annotations.Test;343536public class BasicDouble {3738@Test(groups = "unit")39public void testEmpty() {40OptionalDouble empty = OptionalDouble.empty();41OptionalDouble present = OptionalDouble.of(1.0);4243// empty44assertTrue(empty.equals(empty));45assertTrue(empty.equals(OptionalDouble.empty()));46assertTrue(!empty.equals(present));47assertTrue(0 == empty.hashCode());48assertTrue(!empty.toString().isEmpty());49assertTrue(!empty.isPresent());50empty.ifPresent(v -> { fail(); });51assertEquals(2.0, empty.orElse(2.0));52assertEquals(2.0, empty.orElseGet(()-> 2.0));53}5455@Test(expectedExceptions=NoSuchElementException.class)56public void testEmptyGet() {57OptionalDouble empty = OptionalDouble.empty();5859double got = empty.getAsDouble();60}6162@Test(expectedExceptions=NullPointerException.class)63public void testEmptyOrElseGetNull() {64OptionalDouble empty = OptionalDouble.empty();6566double got = empty.orElseGet(null);67}6869@Test(expectedExceptions=NullPointerException.class)70public void testEmptyOrElseThrowNull() throws Throwable {71OptionalDouble empty = OptionalDouble.empty();7273double got = empty.orElseThrow(null);74}7576@Test(expectedExceptions=ObscureException.class)77public void testEmptyOrElseThrow() throws Exception {78OptionalDouble empty = OptionalDouble.empty();7980double got = empty.orElseThrow(ObscureException::new);81}8283@Test(groups = "unit")84public void testPresent() {85OptionalDouble empty = OptionalDouble.empty();86OptionalDouble present = OptionalDouble.of(1.0);8788// present89assertTrue(present.equals(present));90assertFalse(present.equals(OptionalDouble.of(0.0)));91assertTrue(present.equals(OptionalDouble.of(1.0)));92assertTrue(!present.equals(empty));93assertTrue(Double.hashCode(1.0) == present.hashCode());94assertFalse(present.toString().isEmpty());95assertTrue(-1 != present.toString().indexOf(Double.toString(present.getAsDouble()).toString()));96assertEquals(1.0, present.getAsDouble());97try {98present.ifPresent(v -> { throw new ObscureException(); });99fail();100} catch(ObscureException expected) {101102}103assertEquals(1.0, present.orElse(2.0));104assertEquals(1.0, present.orElseGet(null));105assertEquals(1.0, present.orElseGet(()-> 2.0));106assertEquals(1.0, present.orElseGet(()-> 3.0));107assertEquals(1.0, present.<RuntimeException>orElseThrow(null));108assertEquals(1.0, present.<RuntimeException>orElseThrow(ObscureException::new));109}110111private static class ObscureException extends RuntimeException {112113}114}115116117