< prev index next >

test/java/util/Optional/BasicDouble.java

Print this page
rev 11381 : 8071670: java.util.Optional: please add a way to specify if-else behavior
Reviewed-by: dfuchs


  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  */
  23 
  24 /* @test
  25  * @summary Basic functional test of OptionalDouble
  26  * @author Mike Duigou
  27  * @run testng BasicDouble
  28  */
  29 
  30 import java.util.NoSuchElementException;
  31 import java.util.OptionalDouble;

  32 import java.util.stream.DoubleStream;
  33 
  34 import static org.testng.Assert.*;
  35 import org.testng.annotations.Test;
  36 
  37 
  38 public class BasicDouble {
  39 
  40     @Test(groups = "unit")
  41     public void testEmpty() {
  42         OptionalDouble empty = OptionalDouble.empty();
  43         OptionalDouble present = OptionalDouble.of(1.0);
  44 
  45         // empty
  46         assertTrue(empty.equals(empty));
  47         assertTrue(empty.equals(OptionalDouble.empty()));
  48         assertTrue(!empty.equals(present));
  49         assertTrue(0 == empty.hashCode());
  50         assertTrue(!empty.toString().isEmpty());
  51         assertTrue(!empty.isPresent());

  52         empty.ifPresent(v -> { fail(); });















  53         assertEquals(2.0, empty.orElse(2.0));
  54         assertEquals(2.0, empty.orElseGet(()-> 2.0));
  55     }
  56 

























  57         @Test(expectedExceptions=NoSuchElementException.class)
  58         public void testEmptyGet() {
  59             OptionalDouble empty = OptionalDouble.empty();
  60 
  61             double got = empty.getAsDouble();
  62         }
  63 
  64         @Test(expectedExceptions=NullPointerException.class)
  65         public void testEmptyOrElseGetNull() {
  66             OptionalDouble empty = OptionalDouble.empty();
  67 
  68             double got = empty.orElseGet(null);
  69         }
  70 
  71         @Test(expectedExceptions=NullPointerException.class)
  72         public void testEmptyOrElseThrowNull() throws Throwable {
  73             OptionalDouble empty = OptionalDouble.empty();
  74 
  75             double got = empty.orElseThrow(null);
  76         }


  79         public void testEmptyOrElseThrow() throws Exception {
  80             OptionalDouble empty = OptionalDouble.empty();
  81 
  82             double got = empty.orElseThrow(ObscureException::new);
  83         }
  84 
  85         @Test(groups = "unit")
  86         public void testPresent() {
  87         OptionalDouble empty = OptionalDouble.empty();
  88         OptionalDouble present = OptionalDouble.of(1.0);
  89 
  90         // present
  91         assertTrue(present.equals(present));
  92         assertFalse(present.equals(OptionalDouble.of(0.0)));
  93         assertTrue(present.equals(OptionalDouble.of(1.0)));
  94         assertTrue(!present.equals(empty));
  95         assertTrue(Double.hashCode(1.0) == present.hashCode());
  96         assertFalse(present.toString().isEmpty());
  97         assertTrue(-1 != present.toString().indexOf(Double.toString(present.getAsDouble()).toString()));
  98         assertEquals(1.0, present.getAsDouble());








  99         try {
 100             present.ifPresent(v -> { throw new ObscureException(); });
 101             fail();
 102         } catch(ObscureException expected) {
 103 



 104         }










 105         assertEquals(1.0, present.orElse(2.0));
 106         assertEquals(1.0, present.orElseGet(null));
 107         assertEquals(1.0, present.orElseGet(()-> 2.0));
 108         assertEquals(1.0, present.orElseGet(()-> 3.0));
 109         assertEquals(1.0, present.<RuntimeException>orElseThrow(null));
 110         assertEquals(1.0, present.<RuntimeException>orElseThrow(ObscureException::new));
 111     }
 112 
 113     @Test(groups = "unit")
 114     public void testStream() {
 115         {
 116             DoubleStream s = OptionalDouble.empty().stream();
 117             assertFalse(s.isParallel());
 118 
 119             double[] es = s.toArray();
 120             assertEquals(es.length, 0);
 121         }
 122 
 123         {
 124             DoubleStream s = OptionalDouble.of(42.0).stream();


  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  */
  23 
  24 /* @test
  25  * @summary Basic functional test of OptionalDouble
  26  * @author Mike Duigou
  27  * @run testng BasicDouble
  28  */
  29 
  30 import java.util.NoSuchElementException;
  31 import java.util.OptionalDouble;
  32 import java.util.concurrent.atomic.AtomicBoolean;
  33 import java.util.stream.DoubleStream;
  34 
  35 import static org.testng.Assert.*;
  36 import org.testng.annotations.Test;
  37 
  38 
  39 public class BasicDouble {
  40 
  41     @Test(groups = "unit")
  42     public void testEmpty() {
  43         OptionalDouble empty = OptionalDouble.empty();
  44         OptionalDouble present = OptionalDouble.of(1.0);
  45 
  46         // empty
  47         assertTrue(empty.equals(empty));
  48         assertTrue(empty.equals(OptionalDouble.empty()));
  49         assertTrue(!empty.equals(present));
  50         assertTrue(0 == empty.hashCode());
  51         assertTrue(!empty.toString().isEmpty());
  52         assertTrue(!empty.isPresent());
  53 
  54         empty.ifPresent(v -> { fail(); });
  55 
  56         AtomicBoolean emptyCheck = new AtomicBoolean();
  57         empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
  58         assertTrue(emptyCheck.get());
  59 
  60         try {
  61             empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
  62             fail();
  63         } catch (ObscureException expected) {
  64         } catch (AssertionError e) {
  65             throw e;
  66         } catch (Throwable t) {
  67             fail();
  68         }
  69 
  70         assertEquals(2.0, empty.orElse(2.0));
  71         assertEquals(2.0, empty.orElseGet(()-> 2.0));
  72     }
  73 
  74     @Test(groups = "unit")
  75     public void testIfPresentAndOrElseAndNull() {
  76         OptionalDouble empty = OptionalDouble.empty();
  77         OptionalDouble present = OptionalDouble.of(1.0);
  78 
  79         // No NPE
  80         present.ifPresentOrElse(v -> {}, null);
  81         empty.ifPresent(null);
  82         empty.ifPresentOrElse(null, () -> {});
  83 
  84         // NPE
  85         try {
  86             present.ifPresent(null);
  87             fail();
  88         } catch (NullPointerException ex) {}
  89         try {
  90             present.ifPresentOrElse(null, () -> {});
  91             fail();
  92         } catch (NullPointerException ex) {}
  93         try {
  94             empty.ifPresentOrElse(v -> {}, null);
  95             fail();
  96         } catch (NullPointerException ex) {}
  97     }
  98 
  99     @Test(expectedExceptions=NoSuchElementException.class)
 100     public void testEmptyGet() {
 101         OptionalDouble empty = OptionalDouble.empty();
 102 
 103         double got = empty.getAsDouble();
 104     }
 105 
 106     @Test(expectedExceptions=NullPointerException.class)
 107     public void testEmptyOrElseGetNull() {
 108         OptionalDouble empty = OptionalDouble.empty();
 109 
 110         double got = empty.orElseGet(null);
 111     }
 112 
 113     @Test(expectedExceptions=NullPointerException.class)
 114     public void testEmptyOrElseThrowNull() throws Throwable {
 115         OptionalDouble empty = OptionalDouble.empty();
 116 
 117         double got = empty.orElseThrow(null);
 118     }


 121     public void testEmptyOrElseThrow() throws Exception {
 122         OptionalDouble empty = OptionalDouble.empty();
 123 
 124         double got = empty.orElseThrow(ObscureException::new);
 125     }
 126 
 127     @Test(groups = "unit")
 128     public void testPresent() {
 129         OptionalDouble empty = OptionalDouble.empty();
 130         OptionalDouble present = OptionalDouble.of(1.0);
 131 
 132         // present
 133         assertTrue(present.equals(present));
 134         assertFalse(present.equals(OptionalDouble.of(0.0)));
 135         assertTrue(present.equals(OptionalDouble.of(1.0)));
 136         assertTrue(!present.equals(empty));
 137         assertTrue(Double.hashCode(1.0) == present.hashCode());
 138         assertFalse(present.toString().isEmpty());
 139         assertTrue(-1 != present.toString().indexOf(Double.toString(present.getAsDouble()).toString()));
 140         assertEquals(1.0, present.getAsDouble());
 141 
 142         AtomicBoolean presentCheck = new AtomicBoolean();
 143         present.ifPresent(v -> presentCheck.set(true));
 144         assertTrue(presentCheck.get());
 145         presentCheck.set(false);
 146         present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
 147         assertTrue(presentCheck.get());
 148 
 149         try {
 150             present.ifPresent(v -> { throw new ObscureException(); });
 151             fail();
 152         } catch (ObscureException expected) {
 153         } catch (AssertionError e) {
 154             throw e;
 155         } catch (Throwable t) {
 156             fail();
 157         }
 158         try {
 159             present.ifPresentOrElse(v -> { throw new ObscureException(); }, () -> fail());
 160             fail();
 161         } catch (ObscureException expected) {
 162         } catch (AssertionError e) {
 163             throw e;
 164         } catch (Throwable t) {
 165             fail();
 166         }
 167 
 168         assertEquals(1.0, present.orElse(2.0));
 169         assertEquals(1.0, present.orElseGet(null));
 170         assertEquals(1.0, present.orElseGet(()-> 2.0));
 171         assertEquals(1.0, present.orElseGet(()-> 3.0));
 172         assertEquals(1.0, present.<RuntimeException>orElseThrow(null));
 173         assertEquals(1.0, present.<RuntimeException>orElseThrow(ObscureException::new));
 174     }
 175 
 176     @Test(groups = "unit")
 177     public void testStream() {
 178         {
 179             DoubleStream s = OptionalDouble.empty().stream();
 180             assertFalse(s.isParallel());
 181 
 182             double[] es = s.toArray();
 183             assertEquals(es.length, 0);
 184         }
 185 
 186         {
 187             DoubleStream s = OptionalDouble.of(42.0).stream();
< prev index next >