Search in sources :

Example 1 with OptionalInt

use of java8.util.OptionalInt in project streamsupport by stefan-zobel.

the class FindAnyOpTest method exerciseIntStream.

void exerciseIntStream(TestData.OfInt data, Function<IntStream, IntStream> fs) {
    OptionalInt or = withData(data).terminal(fs, s -> s.findAny()).equalator(INT_VALID_ANSWER).exercise();
    if (or.isPresent()) {
        int r = or.getAsInt();
        PrimitiveIterator.OfInt it = fs.apply(data.stream()).iterator();
        boolean contained = false;
        while (!contained && it.hasNext()) {
            contained = r == it.nextInt();
        }
        assertTrue(contained);
    } else {
        assertFalse(fs.apply(data.stream()).iterator().hasNext());
    }
}
Also used : PrimitiveIterator(java8.util.PrimitiveIterator) OptionalInt(java8.util.OptionalInt)

Example 2 with OptionalInt

use of java8.util.OptionalInt in project streamsupport by stefan-zobel.

the class IntReduceTest method testOps.

@Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
public void testOps(String name, TestData.OfInt data) {
    assertEquals(0, (int) exerciseTerminalOps(data, (IntStream s) -> s.filter(ipFalse), s -> s.reduce(0, irPlus)));
    OptionalInt seedless = exerciseTerminalOps(data, s -> s.reduce(irPlus));
    int folded = exerciseTerminalOps(data, s -> s.reduce(0, irPlus));
    assertEquals(folded, seedless.orElse(0));
    seedless = exerciseTerminalOps(data, s -> s.reduce(irMin));
    folded = exerciseTerminalOps(data, s -> s.reduce(Integer.MAX_VALUE, irMin));
    assertEquals(folded, seedless.orElse(Integer.MAX_VALUE));
    seedless = exerciseTerminalOps(data, s -> s.reduce(irMax));
    folded = exerciseTerminalOps(data, s -> s.reduce(Integer.MIN_VALUE, irMax));
    assertEquals(folded, seedless.orElse(Integer.MIN_VALUE));
    seedless = exerciseTerminalOps(data, (IntStream s) -> s.map(irDoubler), s -> s.reduce(irPlus));
    folded = exerciseTerminalOps(data, (IntStream s) -> s.map(irDoubler), s -> s.reduce(0, irPlus));
    assertEquals(folded, seedless.orElse(0));
    seedless = exerciseTerminalOps(data, (IntStream s) -> s.map(irDoubler), s -> s.reduce(irMin));
    folded = exerciseTerminalOps(data, (IntStream s) -> s.map(irDoubler), s -> s.reduce(Integer.MAX_VALUE, irMin));
    assertEquals(folded, seedless.orElse(Integer.MAX_VALUE));
    seedless = exerciseTerminalOps(data, (IntStream s) -> s.map(irDoubler), s -> s.reduce(irMax));
    folded = exerciseTerminalOps(data, (IntStream s) -> s.map(irDoubler), s -> s.reduce(Integer.MIN_VALUE, irMax));
    assertEquals(folded, seedless.orElse(Integer.MIN_VALUE));
}
Also used : LambdaTestHelpers(java8.util.stream.LambdaTestHelpers) J8Arrays(java8.util.J8Arrays) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) Test(org.testng.annotations.Test) OptionalInt(java8.util.OptionalInt) IntStreamTestDataProvider(java8.util.stream.IntStreamTestDataProvider) TestData(java8.util.stream.TestData) IntStreams(java8.util.stream.IntStreams) OptionalInt(java8.util.OptionalInt) IntStream(java8.util.stream.IntStream) Test(org.testng.annotations.Test)

Aggregations

OptionalInt (java8.util.OptionalInt)2 J8Arrays (java8.util.J8Arrays)1 PrimitiveIterator (java8.util.PrimitiveIterator)1 IntStream (java8.util.stream.IntStream)1 IntStreamTestDataProvider (java8.util.stream.IntStreamTestDataProvider)1 IntStreams (java8.util.stream.IntStreams)1 LambdaTestHelpers (java8.util.stream.LambdaTestHelpers)1 OpTestCase (java8.util.stream.OpTestCase)1 TestData (java8.util.stream.TestData)1 Test (org.testng.annotations.Test)1