20 * or visit www.oracle.com if you need additional information or have any |
20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 /* @test |
24 /* @test |
25 * @bug 8195649 |
|
26 * @summary Basic functional test of OptionalDouble |
25 * @summary Basic functional test of OptionalDouble |
27 * @author Mike Duigou |
26 * @author Mike Duigou |
28 * @build ObscureException |
|
29 * @run testng BasicDouble |
27 * @run testng BasicDouble |
30 */ |
28 */ |
31 |
29 |
32 import java.util.NoSuchElementException; |
30 import java.util.NoSuchElementException; |
33 import java.util.OptionalDouble; |
31 import java.util.OptionalDouble; |
34 import java.util.concurrent.atomic.AtomicBoolean; |
32 import java.util.concurrent.atomic.AtomicBoolean; |
|
33 import java.util.stream.DoubleStream; |
35 |
34 |
36 import static org.testng.Assert.*; |
35 import static org.testng.Assert.*; |
37 import org.testng.annotations.Test; |
36 import org.testng.annotations.Test; |
38 |
37 |
|
38 |
39 public class BasicDouble { |
39 public class BasicDouble { |
40 static final double DOUBLEVAL = Math.PI; |
40 |
41 static final double UNEXPECTED = 6.62607004E-34; |
41 @Test(groups = "unit") |
42 |
42 public void testEmpty() { |
43 /** |
43 OptionalDouble empty = OptionalDouble.empty(); |
44 * Checks a block of assertions over an empty OptionalDouble. |
44 OptionalDouble present = OptionalDouble.of(1.0); |
45 */ |
45 |
46 void checkEmpty(OptionalDouble empty) { |
46 // empty |
|
47 assertTrue(empty.equals(empty)); |
47 assertTrue(empty.equals(OptionalDouble.empty())); |
48 assertTrue(empty.equals(OptionalDouble.empty())); |
48 assertTrue(OptionalDouble.empty().equals(empty)); |
49 assertTrue(!empty.equals(present)); |
49 assertFalse(empty.equals(OptionalDouble.of(UNEXPECTED))); |
50 assertTrue(0 == empty.hashCode()); |
50 assertFalse(OptionalDouble.of(UNEXPECTED).equals(empty)); |
51 assertTrue(!empty.toString().isEmpty()); |
51 assertFalse(empty.equals("unexpected")); |
52 assertTrue(!empty.isPresent()); |
52 |
53 |
53 assertFalse(empty.isPresent()); |
54 empty.ifPresent(v -> { fail(); }); |
54 assertEquals(empty.hashCode(), 0); |
55 |
55 assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED); |
56 AtomicBoolean emptyCheck = new AtomicBoolean(); |
56 assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED); |
57 empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true)); |
57 |
58 assertTrue(emptyCheck.get()); |
58 assertThrows(NoSuchElementException.class, () -> empty.getAsDouble()); |
59 |
59 assertThrows(NoSuchElementException.class, () -> empty.orElseThrow()); |
60 try { |
60 assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new)); |
61 empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); }); |
61 |
62 fail(); |
62 var b = new AtomicBoolean(); |
63 } catch (ObscureException expected) { |
63 empty.ifPresent(s -> b.set(true)); |
64 } catch (AssertionError e) { |
64 assertFalse(b.get()); |
65 throw e; |
65 |
66 } catch (Throwable t) { |
66 var b1 = new AtomicBoolean(false); |
67 fail(); |
67 var b2 = new AtomicBoolean(false); |
68 } |
68 empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); |
69 |
69 assertFalse(b1.get()); |
70 assertEquals(2.0, empty.orElse(2.0)); |
70 assertTrue(b2.get()); |
71 assertEquals(2.0, empty.orElseGet(()-> 2.0)); |
71 |
72 } |
72 assertEquals(empty.toString(), "OptionalDouble.empty"); |
73 |
73 } |
74 @Test(groups = "unit") |
74 |
75 public void testIfPresentAndOrElseAndNull() { |
75 /** |
76 OptionalDouble empty = OptionalDouble.empty(); |
76 * Checks a block of assertions over an OptionalDouble that is expected to |
77 OptionalDouble present = OptionalDouble.of(1.0); |
77 * have a particular value present. |
78 |
78 */ |
79 // No NPE |
79 void checkPresent(OptionalDouble opt, double expected) { |
80 present.ifPresentOrElse(v -> {}, null); |
80 assertFalse(opt.equals(OptionalDouble.empty())); |
81 empty.ifPresent(null); |
81 assertFalse(OptionalDouble.empty().equals(opt)); |
82 empty.ifPresentOrElse(null, () -> {}); |
82 assertTrue(opt.equals(OptionalDouble.of(expected))); |
83 |
83 assertTrue(OptionalDouble.of(expected).equals(opt)); |
84 // NPE |
84 assertFalse(opt.equals(OptionalDouble.of(UNEXPECTED))); |
85 try { |
85 assertFalse(OptionalDouble.of(UNEXPECTED).equals(opt)); |
86 present.ifPresent(null); |
86 assertFalse(opt.equals("unexpected")); |
87 fail(); |
87 |
88 } catch (NullPointerException ex) {} |
88 assertTrue(opt.isPresent()); |
89 try { |
89 assertEquals(opt.hashCode(), Double.hashCode(expected)); |
90 present.ifPresentOrElse(null, () -> {}); |
90 assertEquals(opt.orElse(UNEXPECTED), expected); |
91 fail(); |
91 assertEquals(opt.orElseGet(() -> UNEXPECTED), expected); |
92 } catch (NullPointerException ex) {} |
92 |
93 try { |
93 assertEquals(opt.getAsDouble(), expected); |
94 empty.ifPresentOrElse(v -> {}, null); |
94 assertEquals(opt.orElseThrow(), expected); |
95 fail(); |
95 assertEquals(opt.orElseThrow(ObscureException::new), expected); |
96 } catch (NullPointerException ex) {} |
96 |
97 } |
97 var b = new AtomicBoolean(false); |
98 |
98 opt.ifPresent(s -> b.set(true)); |
99 @Test(expectedExceptions=NoSuchElementException.class) |
99 assertTrue(b.get()); |
100 public void testEmptyGet() { |
100 |
101 OptionalDouble empty = OptionalDouble.empty(); |
101 var b1 = new AtomicBoolean(false); |
102 |
102 var b2 = new AtomicBoolean(false); |
103 double got = empty.getAsDouble(); |
103 opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); |
104 } |
104 assertTrue(b1.get()); |
105 |
105 assertFalse(b2.get()); |
106 @Test(expectedExceptions=NullPointerException.class) |
106 |
107 public void testEmptyOrElseGetNull() { |
107 assertEquals(opt.toString(), "OptionalDouble[" + expected + "]"); |
108 OptionalDouble empty = OptionalDouble.empty(); |
108 } |
109 |
109 |
110 double got = empty.orElseGet(null); |
110 @Test(groups = "unit") |
111 } |
111 public void testEmpty() { |
112 |
112 checkEmpty(OptionalDouble.empty()); |
113 @Test(expectedExceptions=NullPointerException.class) |
|
114 public void testEmptyOrElseThrowNull() throws Throwable { |
|
115 OptionalDouble empty = OptionalDouble.empty(); |
|
116 |
|
117 double got = empty.orElseThrow(null); |
|
118 } |
|
119 |
|
120 @Test(expectedExceptions=ObscureException.class) |
|
121 public void testEmptyOrElseThrow() throws Exception { |
|
122 OptionalDouble empty = OptionalDouble.empty(); |
|
123 |
|
124 double got = empty.orElseThrow(ObscureException::new); |
|
125 } |
|
126 |
|
127 @Test(expectedExceptions=NoSuchElementException.class) |
|
128 public void testEmptyOrElseThrowNoArg() throws Exception { |
|
129 OptionalDouble empty = OptionalDouble.empty(); |
|
130 |
|
131 double got = empty.orElseThrow(); |
113 } |
132 } |
114 |
133 |
115 @Test(groups = "unit") |
134 @Test(groups = "unit") |
116 public void testPresent() { |
135 public void testPresent() { |
117 checkPresent(OptionalDouble.of(DOUBLEVAL), DOUBLEVAL); |
136 OptionalDouble empty = OptionalDouble.empty(); |
118 } |
137 OptionalDouble present = OptionalDouble.of(1.0); |
119 |
138 |
120 @Test(groups = "unit") |
139 // present |
121 public void testStreamEmpty() { |
140 assertTrue(present.equals(present)); |
122 assertEquals(OptionalDouble.empty().stream().toArray(), new double[] { }); |
141 assertFalse(present.equals(OptionalDouble.of(0.0))); |
123 } |
142 assertTrue(present.equals(OptionalDouble.of(1.0))); |
124 |
143 assertTrue(!present.equals(empty)); |
125 @Test(groups = "unit") |
144 assertTrue(Double.hashCode(1.0) == present.hashCode()); |
126 public void testStreamPresent() { |
145 assertFalse(present.toString().isEmpty()); |
127 assertEquals(OptionalDouble.of(DOUBLEVAL).stream().toArray(), new double[] { DOUBLEVAL }); |
146 assertTrue(-1 != present.toString().indexOf(Double.toString(present.getAsDouble()).toString())); |
|
147 assertTrue(-1 != present.toString().indexOf(Double.toString(present.orElseThrow()).toString())); |
|
148 assertEquals(1.0, present.getAsDouble()); |
|
149 assertEquals(1.0, present.orElseThrow()); |
|
150 |
|
151 AtomicBoolean presentCheck = new AtomicBoolean(); |
|
152 present.ifPresent(v -> presentCheck.set(true)); |
|
153 assertTrue(presentCheck.get()); |
|
154 presentCheck.set(false); |
|
155 present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail()); |
|
156 assertTrue(presentCheck.get()); |
|
157 |
|
158 try { |
|
159 present.ifPresent(v -> { throw new ObscureException(); }); |
|
160 fail(); |
|
161 } catch (ObscureException expected) { |
|
162 } catch (AssertionError e) { |
|
163 throw e; |
|
164 } catch (Throwable t) { |
|
165 fail(); |
|
166 } |
|
167 try { |
|
168 present.ifPresentOrElse(v -> { throw new ObscureException(); }, () -> fail()); |
|
169 fail(); |
|
170 } catch (ObscureException expected) { |
|
171 } catch (AssertionError e) { |
|
172 throw e; |
|
173 } catch (Throwable t) { |
|
174 fail(); |
|
175 } |
|
176 |
|
177 assertEquals(1.0, present.orElse(2.0)); |
|
178 assertEquals(1.0, present.orElseGet(null)); |
|
179 assertEquals(1.0, present.orElseGet(()-> 2.0)); |
|
180 assertEquals(1.0, present.orElseGet(()-> 3.0)); |
|
181 assertEquals(1.0, present.<RuntimeException>orElseThrow(null)); |
|
182 assertEquals(1.0, present.<RuntimeException>orElseThrow(ObscureException::new)); |
|
183 } |
|
184 |
|
185 @Test(groups = "unit") |
|
186 public void testStream() { |
|
187 { |
|
188 DoubleStream s = OptionalDouble.empty().stream(); |
|
189 assertFalse(s.isParallel()); |
|
190 |
|
191 double[] es = s.toArray(); |
|
192 assertEquals(es.length, 0); |
|
193 } |
|
194 |
|
195 { |
|
196 DoubleStream s = OptionalDouble.of(42.0).stream(); |
|
197 assertFalse(s.isParallel()); |
|
198 |
|
199 double[] es = s.toArray(); |
|
200 assertEquals(es.length, 1); |
|
201 assertEquals(es[0], 42.0); |
|
202 } |
|
203 } |
|
204 |
|
205 private static class ObscureException extends RuntimeException { |
|
206 |
128 } |
207 } |
129 } |
208 } |