test/jdk/java/util/Optional/BasicLong.java
author smarks
Thu, 29 Mar 2018 16:12:41 -0700
branchJDK-8195649-branch
changeset 56376 718b9df3e302
parent 56375 e0530ee8af48
child 56383 4872fc3de1b0
permissions -rw-r--r--
Update BasicInt and BasicLong.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     1
/*
56376
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
     2
 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     4
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     8
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    13
 * accompanied this code).
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    14
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    18
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    21
 * questions.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    22
 */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    23
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    24
/* @test
56376
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    25
 * @bug 8195649
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    26
 * @summary Basic functional test of OptionalLong
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    27
 * @author Mike Duigou
56375
e0530ee8af48 Move ObscureException to its own file. Rewrite BasicDouble.
smarks
parents: 48328
diff changeset
    28
 * @build ObscureException
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    29
 * @run testng BasicLong
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    30
 */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    31
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    32
import java.util.NoSuchElementException;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    33
import java.util.OptionalLong;
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    34
import java.util.concurrent.atomic.AtomicBoolean;
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
    35
import java.util.stream.LongStream;
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    36
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    37
import static org.testng.Assert.*;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    38
import org.testng.annotations.Test;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    39
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    40
public class BasicLong {
56376
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    41
    static final long LONGVAL = 2_305_843_008_139_952_128L;
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    42
    static final long UNEXPECTED = 0xFEEDBEEFCAFEBABEL;
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    43
56376
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    44
    /**
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    45
     * Checks a block of assertions over an empty OptionalLong.
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    46
     */
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    47
    void checkEmpty(OptionalLong empty) {
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    48
        assertTrue(empty.equals(OptionalLong.empty()));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    49
        assertTrue(OptionalLong.empty().equals(empty));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    50
        assertFalse(empty.equals(OptionalLong.of(UNEXPECTED)));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    51
        assertFalse(OptionalLong.of(UNEXPECTED).equals(empty));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    52
        assertFalse(empty.equals("unexpected"));
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    53
56376
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    54
        assertFalse(empty.isPresent());
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    55
        assertEquals(empty.hashCode(), 0);
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    56
        assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    57
        assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    58
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    59
        assertThrows(NoSuchElementException.class, () -> empty.getAsLong());
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    60
        assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    61
        assertThrows(ObscureException.class,       () -> empty.orElseThrow(ObscureException::new));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    62
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    63
        var b = new AtomicBoolean();
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    64
        empty.ifPresent(s -> b.set(true));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    65
        assertFalse(b.get());
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    66
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    67
        var b1 = new AtomicBoolean(false);
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    68
        var b2 = new AtomicBoolean(false);
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    69
        empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    70
        assertFalse(b1.get());
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    71
        assertTrue(b2.get());
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    72
56376
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    73
        assertEquals(empty.toString(), "OptionalLong.empty");
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    74
    }
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    75
56376
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    76
    /**
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    77
     * Checks a block of assertions over an OptionalLong that is expected to
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    78
     * have a particular value present.
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    79
     */
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    80
    void checkPresent(OptionalLong opt, long expected) {
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    81
        assertFalse(opt.equals(OptionalLong.empty()));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    82
        assertFalse(OptionalLong.empty().equals(opt));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    83
        assertTrue(opt.equals(OptionalLong.of(expected)));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    84
        assertTrue(OptionalLong.of(expected).equals(opt));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    85
        assertFalse(opt.equals(OptionalLong.of(UNEXPECTED)));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    86
        assertFalse(OptionalLong.of(UNEXPECTED).equals(opt));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    87
        assertFalse(opt.equals("unexpected"));
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    88
56376
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    89
        assertTrue(opt.isPresent());
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    90
        assertEquals(opt.hashCode(), Long.hashCode(expected));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    91
        assertEquals(opt.orElse(UNEXPECTED), expected);
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    92
        assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    93
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    94
        assertEquals(opt.getAsLong(), expected);
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    95
        assertEquals(opt.orElseThrow(), expected);
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    96
        assertEquals(opt.orElseThrow(ObscureException::new), expected);
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    97
56376
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    98
        var b = new AtomicBoolean(false);
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
    99
        opt.ifPresent(s -> b.set(true));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   100
        assertTrue(b.get());
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   101
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   102
        var b1 = new AtomicBoolean(false);
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   103
        var b2 = new AtomicBoolean(false);
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   104
        opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   105
        assertTrue(b1.get());
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   106
        assertFalse(b2.get());
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   107
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   108
        assertEquals(opt.toString(), "OptionalLong[" + expected + "]");
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   109
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   110
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   111
    @Test(groups = "unit")
56376
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   112
    public void testEmpty() {
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   113
        checkEmpty(OptionalLong.empty());
48328
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   114
    }
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   115
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   116
    @Test(groups = "unit")
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   117
    public void testPresent() {
56376
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   118
        checkPresent(OptionalLong.of(LONGVAL), LONGVAL);
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   119
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   120
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   121
    @Test(groups = "unit")
56376
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   122
    public void testStreamEmpty() {
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   123
        assertEquals(OptionalLong.empty().stream().toArray(), new long[] { });
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   124
    }
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   125
56376
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   126
    @Test(groups = "unit")
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   127
    public void testStreamPresent() {
718b9df3e302 Update BasicInt and BasicLong.
smarks
parents: 56375
diff changeset
   128
        assertEquals(OptionalLong.of(LONGVAL).stream().toArray(), new long[] { LONGVAL });
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   129
    }
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   130
}