1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package libcore.java.util;
18
19import junit.framework.TestCase;
20
21import java.io.IOException;
22import java.util.NoSuchElementException;
23import java.util.Optional;
24import java.util.OptionalDouble;
25import java.util.concurrent.atomic.AtomicInteger;
26import java.util.concurrent.atomic.AtomicReference;
27import java.util.function.DoubleConsumer;
28import java.util.function.DoubleSupplier;
29import java.util.function.Supplier;
30
31public class OptionalDoubleTest extends TestCase {
32    public void testEmpty_sameInstance() {
33        assertSame(OptionalDouble.empty(), OptionalDouble.empty());
34    }
35
36    public void testGet() {
37        assertEquals(56.0, OptionalDouble.of(56.0).getAsDouble());
38
39        try {
40            OptionalDouble.empty().getAsDouble();
41            fail();
42        } catch (NoSuchElementException nsee) {
43        }
44    }
45
46    public void testIsPresent() {
47        assertTrue(OptionalDouble.of(56.0).isPresent());
48        assertFalse(OptionalDouble.empty().isPresent());
49    }
50
51    public void testIfPresent() {
52        DoubleConsumer alwaysFails = value -> fail();
53        OptionalDouble.empty().ifPresent(alwaysFails);
54
55        final AtomicReference<Double> reference = new AtomicReference<>();
56        DoubleConsumer recorder = value -> reference.set(value);
57        OptionalDouble.of(56.0).ifPresent(recorder);
58        assertEquals(56.0, reference.get().doubleValue());
59    }
60
61    public void testOrElse() {
62        assertEquals(57.0, OptionalDouble.empty().orElse(57.0));
63        assertEquals(56.0, OptionalDouble.of(56.0).orElse(57.0));
64    }
65
66    public void testOrElseGet() {
67        DoubleSupplier alwaysFails = () -> { fail(); return 57.0; };
68        assertEquals(56.0, OptionalDouble.of(56.0).orElseGet(alwaysFails));
69
70        DoubleSupplier supplies57 = () -> 57.0;
71        assertEquals(57.0, OptionalDouble.empty().orElseGet(supplies57));
72    }
73
74    public void testOrElseThrow() throws IOException {
75        final IOException bar = new IOException("bar");
76
77        Supplier<IOException> barSupplier = () -> bar;
78        assertEquals(57.0, OptionalDouble.of(57.0).orElseThrow(barSupplier));
79
80        try {
81            OptionalDouble.empty().orElseThrow(barSupplier);
82            fail();
83        } catch (IOException expected) {
84            assertSame(bar, expected);
85        }
86    }
87
88    public void testEquals() {
89        assertEquals(OptionalDouble.empty(), OptionalDouble.empty());
90        assertEquals(OptionalDouble.of(56.0), OptionalDouble.of(56.0));
91        assertFalse(OptionalDouble.empty().equals(OptionalDouble.of(56.0)));
92        assertFalse(OptionalDouble.of(57.0).equals(OptionalDouble.of(56.0)));
93    }
94
95    public void testHashCode() {
96        assertEquals(Double.hashCode(57.0), OptionalDouble.of(57.0).hashCode());
97    }
98}
99