Search in sources :

Example 1 with IntConsumer

use of com.annimon.stream.function.IntConsumer in project Lightweight-Stream-API by aNNiMON.

the class RandomCompatTest method testRandomIntsSizedBounded.

@Test
public void testRandomIntsSizedBounded() {
    assertEquals(20, new RandomCompat().ints(20, 5, 42).peek(new IntConsumer() {

        @Override
        public void accept(int value) {
            if (value < 5 || value >= 42)
                fail();
        }
    }).count());
    new RandomCompat(100).ints(0, 1, 20).forEach(new IntConsumer() {

        @Override
        public void accept(int value) {
            fail();
        }
    });
}
Also used : IntConsumer(com.annimon.stream.function.IntConsumer) Test(org.junit.Test)

Example 2 with IntConsumer

use of com.annimon.stream.function.IntConsumer in project Lightweight-Stream-API by aNNiMON.

the class OptionalIntTest method testIfPresent.

@Test
public void testIfPresent() {
    OptionalInt.empty().ifPresent(new IntConsumer() {

        @Override
        public void accept(int value) {
            fail();
        }
    });
    OptionalInt.of(15).ifPresent(new IntConsumer() {

        @Override
        public void accept(int value) {
            assertEquals(15, value);
        }
    });
}
Also used : IntConsumer(com.annimon.stream.function.IntConsumer) Test(org.junit.Test)

Example 3 with IntConsumer

use of com.annimon.stream.function.IntConsumer in project Lightweight-Stream-API by aNNiMON.

the class SortedTest method testSorted.

@Test
public void testSorted() {
    assertTrue(IntStream.empty().sorted().count() == 0);
    assertTrue(IntStream.of(42).sorted().findFirst().getAsInt() == 42);
    final boolean[] wrongOrder = new boolean[] { false };
    IntStream.iterate(2, new IntUnaryOperator() {

        @Override
        public int applyAsInt(int operand) {
            return -operand + 1;
        }
    }).limit(1000).sorted().forEach(new IntConsumer() {

        int currentValue = Integer.MIN_VALUE;

        @Override
        public void accept(int value) {
            if (value < currentValue) {
                wrongOrder[0] = true;
            }
            currentValue = value;
        }
    });
    assertTrue(!wrongOrder[0]);
}
Also used : IntUnaryOperator(com.annimon.stream.function.IntUnaryOperator) IntConsumer(com.annimon.stream.function.IntConsumer) Test(org.junit.Test)

Example 4 with IntConsumer

use of com.annimon.stream.function.IntConsumer in project Lightweight-Stream-API by aNNiMON.

the class ForEachTest method testForEach.

@Test
public void testForEach() {
    IntStream.empty().forEach(new IntConsumer() {

        @Override
        public void accept(int value) {
            throw new IllegalStateException();
        }
    });
    IntStream.of(42).forEach(new IntConsumer() {

        @Override
        public void accept(int value) {
            assertTrue(value == 42);
        }
    });
    final int[] sum = new int[1];
    IntStream.rangeClosed(10, 20).forEach(new IntConsumer() {

        @Override
        public void accept(int value) {
            sum[0] += value;
        }
    });
    assertEquals(sum[0], 165);
}
Also used : IntConsumer(com.annimon.stream.function.IntConsumer) Test(org.junit.Test)

Example 5 with IntConsumer

use of com.annimon.stream.function.IntConsumer in project Lightweight-Stream-API by aNNiMON.

the class PeekTest method testPeek.

@Test
public void testPeek() {
    assertTrue(IntStream.empty().peek(new IntConsumer() {

        @Override
        public void accept(int value) {
            throw new IllegalStateException();
        }
    }).count() == 0);
    assertTrue(IntStream.generate(new IntSupplier() {

        int value = 2;

        @Override
        public int getAsInt() {
            int v = value;
            value *= 2;
            return v;
        }
    }).peek(new IntConsumer() {

        int curValue = 1;

        @Override
        public void accept(int value) {
            if (value != curValue * 2)
                throw new IllegalStateException();
            curValue = value;
        }
    }).limit(10).count() == 10);
}
Also used : IntSupplier(com.annimon.stream.function.IntSupplier) IntConsumer(com.annimon.stream.function.IntConsumer) Test(org.junit.Test)

Aggregations

IntConsumer (com.annimon.stream.function.IntConsumer)5 Test (org.junit.Test)5 IntSupplier (com.annimon.stream.function.IntSupplier)1 IntUnaryOperator (com.annimon.stream.function.IntUnaryOperator)1