< prev index next >

test/jdk/java/util/Optional/BasicLong.java

Print this page

        

*** 29,39 **** --- 29,41 ---- * @run testng BasicLong */ import java.util.NoSuchElementException; import java.util.OptionalLong; + import java.util.function.LongPredicate; import java.util.concurrent.atomic.AtomicBoolean; + import java.util.List; import static org.testng.Assert.*; import org.testng.annotations.Test; public class BasicLong {
*** 49,58 **** --- 51,61 ---- assertFalse(empty.equals(OptionalLong.of(UNEXPECTED))); assertFalse(OptionalLong.of(UNEXPECTED).equals(empty)); assertFalse(empty.equals("unexpected")); assertFalse(empty.isPresent()); + assertTrue(empty.isEmpty()); assertEquals(empty.hashCode(), 0); assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED); assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED); assertThrows(NoSuchElementException.class, () -> empty.getAsLong());
*** 124,129 **** --- 127,148 ---- @Test(groups = "unit") public void testStreamPresent() { assertEquals(OptionalLong.of(LONGVAL).stream().toArray(), new long[] { LONGVAL }); } + + @Test(groups = "unit") + public void testIsEmpty() { + OptionalLong empty = OptionalLong.empty(); + assertTrue(empty.isEmpty()); + OptionalLong present = OptionalLong.of(LONGVAL); + assertFalse(present.isEmpty()); + var longList = List.of(1L,2L,3L,4L,5L); + LongPredicate isPositiveNumber = x -> x > 0; + LongPredicate isNegativeNumber = x -> x < 0; + OptionalLong positiveNumber = longList.stream().mapToLong(Long::longValue).filter(isPositiveNumber).findAny(); + OptionalLong negativeNumber = longList.stream().mapToLong(Long::longValue).filter(isNegativeNumber).findAny(); + assertFalse(positiveNumber.isEmpty()); + assertTrue(negativeNumber.isEmpty()); + } + }
< prev index next >