Update BasicInt and BasicLong. JDK-8195649-branch
authorsmarks
Thu, 29 Mar 2018 16:12:41 -0700
branchJDK-8195649-branch
changeset 56376 718b9df3e302
parent 56375 e0530ee8af48
child 56383 4872fc3de1b0
Update BasicInt and BasicLong.
test/jdk/java/util/Optional/Basic.java
test/jdk/java/util/Optional/BasicDouble.java
test/jdk/java/util/Optional/BasicInt.java
test/jdk/java/util/Optional/BasicLong.java
--- a/test/jdk/java/util/Optional/Basic.java	Thu Mar 29 14:40:53 2018 -0700
+++ b/test/jdk/java/util/Optional/Basic.java	Thu Mar 29 16:12:41 2018 -0700
@@ -22,6 +22,7 @@
  */
 
 /* @test
+ * @bug 8195649
  * @summary Basic functional test of Optional
  * @author Mike Duigou
  * @build ObscureException
@@ -39,7 +40,6 @@
 import static org.testng.Assert.*;
 import org.testng.annotations.Test;
 
-
 public class Basic {
 
     /**
--- a/test/jdk/java/util/Optional/BasicDouble.java	Thu Mar 29 14:40:53 2018 -0700
+++ b/test/jdk/java/util/Optional/BasicDouble.java	Thu Mar 29 16:12:41 2018 -0700
@@ -22,6 +22,7 @@
  */
 
 /* @test
+ * @bug 8195649
  * @summary Basic functional test of OptionalDouble
  * @author Mike Duigou
  * @build ObscureException
@@ -36,6 +37,7 @@
 import org.testng.annotations.Test;
 
 public class BasicDouble {
+    static final double DOUBLEVAL = Math.PI;
     static final double UNEXPECTED = 6.62607004E-34;
 
     /**
@@ -111,17 +113,17 @@
     }
 
     @Test(groups = "unit")
-    public void testOf() {
-        checkPresent(OptionalDouble.of(Math.PI), Math.PI);
+    public void testPresent() {
+        checkPresent(OptionalDouble.of(DOUBLEVAL), DOUBLEVAL);
     }
 
     @Test(groups = "unit")
     public void testStreamEmpty() {
-        assertEquals(OptionalDouble.empty().stream().toArray().length, 0);
+        assertEquals(OptionalDouble.empty().stream().toArray(), new double[] { });
     }
 
     @Test(groups = "unit")
     public void testStreamPresent() {
-        assertEquals(OptionalDouble.of(Math.PI).stream().toArray(), new double[] { Math.PI });
+        assertEquals(OptionalDouble.of(DOUBLEVAL).stream().toArray(), new double[] { DOUBLEVAL });
     }
 }
--- a/test/jdk/java/util/Optional/BasicInt.java	Thu Mar 29 14:40:53 2018 -0700
+++ b/test/jdk/java/util/Optional/BasicInt.java	Thu Mar 29 16:12:41 2018 -0700
@@ -22,6 +22,7 @@
  */
 
 /* @test
+ * @bug 8195649
  * @summary Basic functional test of OptionalInt
  * @author Mike Duigou
  * @build ObscureException
@@ -36,170 +37,95 @@
 import static org.testng.Assert.*;
 import org.testng.annotations.Test;
 
-
 public class BasicInt {
 
-    @Test(groups = "unit")
-    public void testEmpty() {
-        OptionalInt empty = OptionalInt.empty();
-        OptionalInt present = OptionalInt.of(1);
+    static final int INTVAL = 33_550_336;
+    static final int UNEXPECTED = 0xCAFEBABE;
+
+    /**
+     * Checks a block of assertions over an empty OptionalInt.
+     */
+    void checkEmpty(OptionalInt empty) {
+        assertTrue(empty.equals(OptionalInt.empty()));
+        assertTrue(OptionalInt.empty().equals(empty));
+        assertFalse(empty.equals(OptionalInt.of(UNEXPECTED)));
+        assertFalse(OptionalInt.of(UNEXPECTED).equals(empty));
+        assertFalse(empty.equals("unexpected"));
 
-        // empty
-        assertTrue(empty.equals(empty));
-        assertTrue(empty.equals(OptionalInt.empty()));
-        assertTrue(!empty.equals(present));
-        assertTrue(0 == empty.hashCode());
-        assertTrue(!empty.toString().isEmpty());
-        assertTrue(!empty.isPresent());
+        assertFalse(empty.isPresent());
+        assertEquals(empty.hashCode(), 0);
+        assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
+        assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
+
+        assertThrows(NoSuchElementException.class, () -> empty.getAsInt());
+        assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
+        assertThrows(ObscureException.class,       () -> empty.orElseThrow(ObscureException::new));
 
-        empty.ifPresent(v -> { fail(); });
+        var b = new AtomicBoolean();
+        empty.ifPresent(s -> b.set(true));
+        assertFalse(b.get());
+
+        var b1 = new AtomicBoolean(false);
+        var b2 = new AtomicBoolean(false);
+        empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
+        assertFalse(b1.get());
+        assertTrue(b2.get());
 
-        AtomicBoolean emptyCheck = new AtomicBoolean();
-        empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
-        assertTrue(emptyCheck.get());
+        assertEquals(empty.toString(), "OptionalInt.empty");
+    }
+
+    /**
+     * Checks a block of assertions over an OptionalInt that is expected to
+     * have a particular value present.
+     */
+    void checkPresent(OptionalInt opt, int expected) {
+        assertFalse(opt.equals(OptionalInt.empty()));
+        assertFalse(OptionalInt.empty().equals(opt));
+        assertTrue(opt.equals(OptionalInt.of(expected)));
+        assertTrue(OptionalInt.of(expected).equals(opt));
+        assertFalse(opt.equals(OptionalInt.of(UNEXPECTED)));
+        assertFalse(OptionalInt.of(UNEXPECTED).equals(opt));
+        assertFalse(opt.equals("unexpected"));
 
-        try {
-            empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
-            fail();
-        } catch (ObscureException expected) {
-        } catch (AssertionError e) {
-            throw e;
-        } catch (Throwable t) {
-            fail();
-        }
+        assertTrue(opt.isPresent());
+        assertEquals(opt.hashCode(), Integer.hashCode(expected));
+        assertEquals(opt.orElse(UNEXPECTED), expected);
+        assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);
+
+        assertEquals(opt.getAsInt(), expected);
+        assertEquals(opt.orElseThrow(), expected);
+        assertEquals(opt.orElseThrow(ObscureException::new), expected);
 
-        assertEquals(2, empty.orElse(2));
-        assertEquals(2, empty.orElseGet(()-> 2));
+        var b = new AtomicBoolean(false);
+        opt.ifPresent(s -> b.set(true));
+        assertTrue(b.get());
+
+        var b1 = new AtomicBoolean(false);
+        var b2 = new AtomicBoolean(false);
+        opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
+        assertTrue(b1.get());
+        assertFalse(b2.get());
+
+        assertEquals(opt.toString(), "OptionalInt[" + expected + "]");
     }
 
     @Test(groups = "unit")
-    public void testIfPresentAndOrElseAndNull() {
-        OptionalInt empty = OptionalInt.empty();
-        OptionalInt present = OptionalInt.of(1);
-
-        // No NPE
-        present.ifPresentOrElse(v -> {}, null);
-        empty.ifPresent(null);
-        empty.ifPresentOrElse(null, () -> {});
-
-        // NPE
-        try {
-            present.ifPresent(null);
-            fail();
-        } catch (NullPointerException ex) {}
-        try {
-            present.ifPresentOrElse(null, () -> {});
-            fail();
-        } catch (NullPointerException ex) {}
-        try {
-            empty.ifPresentOrElse(v -> {}, null);
-            fail();
-        } catch (NullPointerException ex) {}
-    }
-
-    @Test(expectedExceptions=NoSuchElementException.class)
-    public void testEmptyGet() {
-        OptionalInt empty = OptionalInt.empty();
-
-        int got = empty.getAsInt();
-    }
-
-    @Test(expectedExceptions=NullPointerException.class)
-    public void testEmptyOrElseGetNull() {
-        OptionalInt empty = OptionalInt.empty();
-
-        int got = empty.orElseGet(null);
-    }
-
-    @Test(expectedExceptions=NullPointerException.class)
-    public void testEmptyOrElseThrowNull() throws Throwable {
-        OptionalInt empty = OptionalInt.empty();
-
-        int got = empty.orElseThrow(null);
-    }
-
-    @Test(expectedExceptions=ObscureException.class)
-    public void testEmptyOrElseThrow() throws Exception {
-        OptionalInt empty = OptionalInt.empty();
-
-        int got = empty.orElseThrow(ObscureException::new);
-    }
-
-    @Test(expectedExceptions=NoSuchElementException.class)
-    public void testEmptyOrElseThrowNoArg() throws Exception {
-        OptionalInt empty = OptionalInt.empty();
-
-        int got = empty.orElseThrow();
+    public void testEmpty() {
+        checkEmpty(OptionalInt.empty());
     }
 
     @Test(groups = "unit")
     public void testPresent() {
-        OptionalInt empty = OptionalInt.empty();
-        OptionalInt present = OptionalInt.of(1);
-
-        // present
-        assertTrue(present.equals(present));
-        assertFalse(present.equals(OptionalInt.of(0)));
-        assertTrue(present.equals(OptionalInt.of(1)));
-        assertFalse(present.equals(empty));
-        assertTrue(Integer.hashCode(1) == present.hashCode());
-        assertFalse(present.toString().isEmpty());
-        assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString()));
-        assertTrue(-1 != present.toString().indexOf(Integer.toString(present.orElseThrow()).toString()));
-        assertEquals(1, present.getAsInt());
-        assertEquals(1, present.orElseThrow());
-
-        AtomicBoolean presentCheck = new AtomicBoolean();
-        present.ifPresent(v -> presentCheck.set(true));
-        assertTrue(presentCheck.get());
-        presentCheck.set(false);
-        present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
-        assertTrue(presentCheck.get());
-
-        try {
-            present.ifPresent(v -> { throw new ObscureException(); });
-            fail();
-        } catch (ObscureException expected) {
-        } catch (AssertionError e) {
-            throw e;
-        } catch (Throwable t) {
-            fail();
-        }
-        try {
-            present.ifPresentOrElse(v -> { throw new ObscureException(); }, () -> fail());
-            fail();
-        } catch (ObscureException expected) {
-        } catch (AssertionError e) {
-            throw e;
-        } catch (Throwable t) {
-            fail();
-        }
-
-        assertEquals(1, present.orElse(2));
-        assertEquals(1, present.orElseGet(null));
-        assertEquals(1, present.orElseGet(()-> 2));
-        assertEquals(1, present.orElseGet(()-> 3));
-        assertEquals(1, present.<RuntimeException>orElseThrow(null));
-        assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
+        checkPresent(OptionalInt.of(INTVAL), INTVAL);
     }
 
     @Test(groups = "unit")
-    public void testStream() {
-        {
-            IntStream s = OptionalInt.empty().stream();
-            assertFalse(s.isParallel());
-
-            int[] es = s.toArray();
-            assertEquals(es.length, 0);
-        }
+    public void testStreamEmpty() {
+        assertEquals(OptionalInt.empty().stream().toArray(), new int[] { });
+    }
 
-        {
-            IntStream s = OptionalInt.of(42).stream();
-            assertFalse(s.isParallel());
-
-            int[] es = OptionalInt.of(42).stream().toArray();
-            assertEquals(es.length, 1);
-            assertEquals(es[0], 42);
-        }
+    @Test(groups = "unit")
+    public void testStreamPresent() {
+        assertEquals(OptionalInt.of(INTVAL).stream().toArray(), new int[] { INTVAL });
     }
 }
--- a/test/jdk/java/util/Optional/BasicLong.java	Thu Mar 29 14:40:53 2018 -0700
+++ b/test/jdk/java/util/Optional/BasicLong.java	Thu Mar 29 16:12:41 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,6 +22,7 @@
  */
 
 /* @test
+ * @bug 8195649
  * @summary Basic functional test of OptionalLong
  * @author Mike Duigou
  * @build ObscureException
@@ -36,170 +37,94 @@
 import static org.testng.Assert.*;
 import org.testng.annotations.Test;
 
-
 public class BasicLong {
+    static final long LONGVAL = 2_305_843_008_139_952_128L;
+    static final long UNEXPECTED = 0xFEEDBEEFCAFEBABEL;
 
-    @Test(groups = "unit")
-    public void testEmpty() {
-        OptionalLong empty = OptionalLong.empty();
-        OptionalLong present = OptionalLong.of(1);
+    /**
+     * Checks a block of assertions over an empty OptionalLong.
+     */
+    void checkEmpty(OptionalLong empty) {
+        assertTrue(empty.equals(OptionalLong.empty()));
+        assertTrue(OptionalLong.empty().equals(empty));
+        assertFalse(empty.equals(OptionalLong.of(UNEXPECTED)));
+        assertFalse(OptionalLong.of(UNEXPECTED).equals(empty));
+        assertFalse(empty.equals("unexpected"));
 
-        // empty
-        assertTrue(empty.equals(empty));
-        assertTrue(empty.equals(OptionalLong.empty()));
-        assertTrue(!empty.equals(present));
-        assertTrue(0 == empty.hashCode());
-        assertTrue(!empty.toString().isEmpty());
-        assertTrue(!empty.isPresent());
+        assertFalse(empty.isPresent());
+        assertEquals(empty.hashCode(), 0);
+        assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
+        assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
+
+        assertThrows(NoSuchElementException.class, () -> empty.getAsLong());
+        assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
+        assertThrows(ObscureException.class,       () -> empty.orElseThrow(ObscureException::new));
+
+        var b = new AtomicBoolean();
+        empty.ifPresent(s -> b.set(true));
+        assertFalse(b.get());
+
+        var b1 = new AtomicBoolean(false);
+        var b2 = new AtomicBoolean(false);
+        empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
+        assertFalse(b1.get());
+        assertTrue(b2.get());
 
-        empty.ifPresent(v -> { fail(); });
+        assertEquals(empty.toString(), "OptionalLong.empty");
+    }
 
-        AtomicBoolean emptyCheck = new AtomicBoolean();
-        empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
-        assertTrue(emptyCheck.get());
+    /**
+     * Checks a block of assertions over an OptionalLong that is expected to
+     * have a particular value present.
+     */
+    void checkPresent(OptionalLong opt, long expected) {
+        assertFalse(opt.equals(OptionalLong.empty()));
+        assertFalse(OptionalLong.empty().equals(opt));
+        assertTrue(opt.equals(OptionalLong.of(expected)));
+        assertTrue(OptionalLong.of(expected).equals(opt));
+        assertFalse(opt.equals(OptionalLong.of(UNEXPECTED)));
+        assertFalse(OptionalLong.of(UNEXPECTED).equals(opt));
+        assertFalse(opt.equals("unexpected"));
 
-        try {
-            empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
-            fail();
-        } catch (ObscureException expected) {
-        } catch (AssertionError e) {
-            throw e;
-        } catch (Throwable t) {
-            fail();
-        }
+        assertTrue(opt.isPresent());
+        assertEquals(opt.hashCode(), Long.hashCode(expected));
+        assertEquals(opt.orElse(UNEXPECTED), expected);
+        assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);
+
+        assertEquals(opt.getAsLong(), expected);
+        assertEquals(opt.orElseThrow(), expected);
+        assertEquals(opt.orElseThrow(ObscureException::new), expected);
 
-        assertEquals(2, empty.orElse(2));
-        assertEquals(2, empty.orElseGet(()-> 2));
+        var b = new AtomicBoolean(false);
+        opt.ifPresent(s -> b.set(true));
+        assertTrue(b.get());
+
+        var b1 = new AtomicBoolean(false);
+        var b2 = new AtomicBoolean(false);
+        opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
+        assertTrue(b1.get());
+        assertFalse(b2.get());
+
+        assertEquals(opt.toString(), "OptionalLong[" + expected + "]");
     }
 
     @Test(groups = "unit")
-    public void testIfPresentAndOrElseAndNull() {
-        OptionalLong empty = OptionalLong.empty();
-        OptionalLong present = OptionalLong.of(1);
-
-        // No NPE
-        present.ifPresentOrElse(v -> {}, null);
-        empty.ifPresent(null);
-        empty.ifPresentOrElse(null, () -> {});
-
-        // NPE
-        try {
-            present.ifPresent(null);
-            fail();
-        } catch (NullPointerException ex) {}
-        try {
-            present.ifPresentOrElse(null, () -> {});
-            fail();
-        } catch (NullPointerException ex) {}
-        try {
-            empty.ifPresentOrElse(v -> {}, null);
-            fail();
-        } catch (NullPointerException ex) {}
-    }
-
-    @Test(expectedExceptions=NoSuchElementException.class)
-    public void testEmptyGet() {
-        OptionalLong empty = OptionalLong.empty();
-
-        long got = empty.getAsLong();
-    }
-
-    @Test(expectedExceptions=NullPointerException.class)
-    public void testEmptyOrElseGetNull() {
-        OptionalLong empty = OptionalLong.empty();
-
-        long got = empty.orElseGet(null);
-    }
-
-    @Test(expectedExceptions=NullPointerException.class)
-    public void testEmptyOrElseThrowNull() throws Throwable {
-        OptionalLong empty = OptionalLong.empty();
-
-        long got = empty.orElseThrow(null);
-    }
-
-    @Test(expectedExceptions=ObscureException.class)
-    public void testEmptyOrElseThrow() throws Exception {
-        OptionalLong empty = OptionalLong.empty();
-
-        long got = empty.orElseThrow(ObscureException::new);
-    }
-
-    @Test(expectedExceptions=NoSuchElementException.class)
-    public void testEmptyOrElseThrowNoArg() throws Exception {
-        OptionalLong empty = OptionalLong.empty();
-
-        long got = empty.orElseThrow();
+    public void testEmpty() {
+        checkEmpty(OptionalLong.empty());
     }
 
     @Test(groups = "unit")
     public void testPresent() {
-        OptionalLong empty = OptionalLong.empty();
-        OptionalLong present = OptionalLong.of(1L);
-
-        // present
-        assertTrue(present.equals(present));
-        assertFalse(present.equals(OptionalLong.of(0L)));
-        assertTrue(present.equals(OptionalLong.of(1L)));
-        assertFalse(present.equals(empty));
-        assertTrue(Long.hashCode(1) == present.hashCode());
-        assertFalse(present.toString().isEmpty());
-        assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
-        assertTrue(-1 != present.toString().indexOf(Long.toString(present.orElseThrow()).toString()));
-        assertEquals(1L, present.getAsLong());
-        assertEquals(1L, present.orElseThrow());
-
-        AtomicBoolean presentCheck = new AtomicBoolean();
-        present.ifPresent(v -> presentCheck.set(true));
-        assertTrue(presentCheck.get());
-        presentCheck.set(false);
-        present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
-        assertTrue(presentCheck.get());
-
-        try {
-            present.ifPresent(v -> { throw new ObscureException(); });
-            fail();
-        } catch (ObscureException expected) {
-        } catch (AssertionError e) {
-            throw e;
-        } catch (Throwable t) {
-            fail();
-        }
-        try {
-            present.ifPresentOrElse(v -> {
-                throw new ObscureException();
-            }, () -> fail());
-            fail();
-        } catch (ObscureException expected) {
-        } catch (AssertionError e) {
-            throw e;
-        } catch (Throwable t) {
-            fail();
-        }
-
-        assertEquals(1, present.orElse(2));
-        assertEquals(1, present.orElseGet(null));
-        assertEquals(1, present.orElseGet(()-> 2));
-        assertEquals(1, present.orElseGet(()-> 3));
-        assertEquals(1, present.<RuntimeException>orElseThrow(null));
-        assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
+        checkPresent(OptionalLong.of(LONGVAL), LONGVAL);
     }
 
     @Test(groups = "unit")
-    public void testStream() {
-        {
-            LongStream s = OptionalLong.empty().stream();
-
-            long[] es = s.toArray();
-            assertEquals(es.length, 0);
-        }
+    public void testStreamEmpty() {
+        assertEquals(OptionalLong.empty().stream().toArray(), new long[] { });
+    }
 
-        {
-            LongStream s = OptionalLong.of(42L).stream();
-
-            long[] es = s.toArray();
-            assertEquals(es.length, 1);
-            assertEquals(es[0], 42L);
-        }
+    @Test(groups = "unit")
+    public void testStreamPresent() {
+        assertEquals(OptionalLong.of(LONGVAL).stream().toArray(), new long[] { LONGVAL });
     }
 }