1 /* 2 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 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 * @bug 8195649 26 * @summary Basic functional test of OptionalLong 27 * @author Mike Duigou 28 * @build ObscureException 29 * @run testng BasicLong 30 */ 31 32 import java.util.NoSuchElementException; 33 import java.util.OptionalLong; 34 import java.util.function.LongPredicate; 35 import java.util.concurrent.atomic.AtomicBoolean; 36 import java.util.List; 37 38 import static org.testng.Assert.*; 39 import org.testng.annotations.Test; 40 41 public class BasicLong { 42 static final long LONGVAL = 2_305_843_008_139_952_128L; 43 static final long UNEXPECTED = 0xFEEDBEEFCAFEBABEL; 44 45 /** 46 * Checks a block of assertions over an empty OptionalLong. 47 */ 48 void checkEmpty(OptionalLong empty) { 49 assertTrue(empty.equals(OptionalLong.empty())); 50 assertTrue(OptionalLong.empty().equals(empty)); 51 assertFalse(empty.equals(OptionalLong.of(UNEXPECTED))); 52 assertFalse(OptionalLong.of(UNEXPECTED).equals(empty)); 53 assertFalse(empty.equals("unexpected")); 54 55 assertFalse(empty.isPresent()); 56 assertTrue(empty.isEmpty()); 57 assertEquals(empty.hashCode(), 0); 58 assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED); 59 assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED); 60 61 assertThrows(NoSuchElementException.class, () -> empty.getAsLong()); 62 assertThrows(NoSuchElementException.class, () -> empty.orElseThrow()); 63 assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new)); 64 65 var b = new AtomicBoolean(); 66 empty.ifPresent(s -> b.set(true)); 67 assertFalse(b.get()); 68 69 var b1 = new AtomicBoolean(false); 70 var b2 = new AtomicBoolean(false); 71 empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); 72 assertFalse(b1.get()); 73 assertTrue(b2.get()); 74 75 assertEquals(empty.toString(), "OptionalLong.empty"); 76 } 77 78 /** 79 * Checks a block of assertions over an OptionalLong that is expected to 80 * have a particular value present. 81 */ 82 void checkPresent(OptionalLong opt, long expected) { 83 assertFalse(opt.equals(OptionalLong.empty())); 84 assertFalse(OptionalLong.empty().equals(opt)); 85 assertTrue(opt.equals(OptionalLong.of(expected))); 86 assertTrue(OptionalLong.of(expected).equals(opt)); 87 assertFalse(opt.equals(OptionalLong.of(UNEXPECTED))); 88 assertFalse(OptionalLong.of(UNEXPECTED).equals(opt)); 89 assertFalse(opt.equals("unexpected")); 90 91 assertTrue(opt.isPresent()); 92 assertEquals(opt.hashCode(), Long.hashCode(expected)); 93 assertEquals(opt.orElse(UNEXPECTED), expected); 94 assertEquals(opt.orElseGet(() -> UNEXPECTED), expected); 95 96 assertEquals(opt.getAsLong(), expected); 97 assertEquals(opt.orElseThrow(), expected); 98 assertEquals(opt.orElseThrow(ObscureException::new), expected); 99 100 var b = new AtomicBoolean(false); 101 opt.ifPresent(s -> b.set(true)); 102 assertTrue(b.get()); 103 104 var b1 = new AtomicBoolean(false); 105 var b2 = new AtomicBoolean(false); 106 opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); 107 assertTrue(b1.get()); 108 assertFalse(b2.get()); 109 110 assertEquals(opt.toString(), "OptionalLong[" + expected + "]"); 111 } 112 113 @Test(groups = "unit") 114 public void testEmpty() { 115 checkEmpty(OptionalLong.empty()); 116 } 117 118 @Test(groups = "unit") 119 public void testPresent() { 120 checkPresent(OptionalLong.of(LONGVAL), LONGVAL); 121 } 122 123 @Test(groups = "unit") 124 public void testStreamEmpty() { 125 assertEquals(OptionalLong.empty().stream().toArray(), new long[] { }); 126 } 127 128 @Test(groups = "unit") 129 public void testStreamPresent() { 130 assertEquals(OptionalLong.of(LONGVAL).stream().toArray(), new long[] { LONGVAL }); 131 } 132 133 @Test(groups = "unit") 134 public void testIsEmpty() { 135 OptionalLong empty = OptionalLong.empty(); 136 assertTrue(empty.isEmpty()); 137 OptionalLong present = OptionalLong.of(LONGVAL); 138 assertFalse(present.isEmpty()); 139 var longList = List.of(1L,2L,3L,4L,5L); 140 LongPredicate isPositiveNumber = x -> x > 0; 141 LongPredicate isNegativeNumber = x -> x < 0; 142 OptionalLong positiveNumber = longList.stream().mapToLong(Long::longValue).filter(isPositiveNumber).findAny(); 143 OptionalLong negativeNumber = longList.stream().mapToLong(Long::longValue).filter(isNegativeNumber).findAny(); 144 assertFalse(positiveNumber.isEmpty()); 145 assertTrue(negativeNumber.isEmpty()); 146 } 147 148 }