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.OptionalLong; 24import java.util.concurrent.atomic.AtomicLong; 25import java.util.function.LongConsumer; 26import java.util.function.LongSupplier; 27import java.util.function.Supplier; 28 29public class OptionalLongTest extends TestCase { 30 public void testEmpty_sameInstance() { 31 assertSame(OptionalLong.empty(), OptionalLong.empty()); 32 } 33 34 public void testGet() { 35 assertEquals(56, OptionalLong.of(56).getAsLong()); 36 37 try { 38 OptionalLong.empty().getAsLong(); 39 fail(); 40 } catch (NoSuchElementException nsee) { 41 } 42 } 43 44 public void testIsPresent() { 45 assertTrue(OptionalLong.of(56).isPresent()); 46 assertFalse(OptionalLong.empty().isPresent()); 47 } 48 49 public void testIfPresent() { 50 LongConsumer alwaysFails = value -> fail(); 51 OptionalLong.empty().ifPresent(alwaysFails); 52 53 final AtomicLong reference = new AtomicLong(); 54 LongConsumer recorder = (long value) -> reference.set(value); 55 OptionalLong.of(56).ifPresent(recorder); 56 assertEquals(56, reference.get()); 57 } 58 59 public void testOrElse() { 60 assertEquals(57, OptionalLong.empty().orElse(57)); 61 assertEquals(56, OptionalLong.of(56).orElse(57)); 62 } 63 64 public void testOrElseGet() { 65 LongSupplier alwaysFails = () -> { fail(); return 57; }; 66 assertEquals(56, OptionalLong.of(56).orElseGet(alwaysFails)); 67 68 LongSupplier supplies57 = () -> 57; 69 assertEquals(57, OptionalLong.empty().orElseGet(supplies57)); 70 } 71 72 public void testOrElseThrow() throws IOException { 73 final IOException bar = new IOException("bar"); 74 75 Supplier<IOException> barSupplier = () -> bar; 76 assertEquals(57, OptionalLong.of(57).orElseThrow(barSupplier)); 77 78 try { 79 OptionalLong.empty().orElseThrow(barSupplier); 80 fail(); 81 } catch (IOException expected) { 82 assertSame(bar, expected); 83 } 84 } 85 86 public void testEquals() { 87 assertEquals(OptionalLong.empty(), OptionalLong.empty()); 88 assertEquals(OptionalLong.of(56), OptionalLong.of(56)); 89 assertFalse(OptionalLong.empty().equals(OptionalLong.of(56))); 90 assertFalse(OptionalLong.of(57).equals(OptionalLong.of(56))); 91 } 92 93 public void testHashCode() { 94 assertEquals(Long.hashCode(57), OptionalLong.of(57).hashCode()); 95 } 96} 97