Search in sources :

Example 1 with IntUnaryOperator

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

the class FilterTest method testFilter.

@Test
public void testFilter() {
    IntStream.rangeClosed(1, 10).filter(Functions.remainderInt(2)).custom(assertElements(arrayContaining(2, 4, 6, 8, 10)));
    assertTrue(IntStream.iterate(0, new IntUnaryOperator() {

        @Override
        public int applyAsInt(int operand) {
            return operand + 1;
        }
    }).filter(new IntPredicate() {

        @Override
        public boolean test(int value) {
            return value == 0;
        }
    }).findFirst().getAsInt() == 0);
}
Also used : IntPredicate(com.annimon.stream.function.IntPredicate) IntUnaryOperator(com.annimon.stream.function.IntUnaryOperator) Test(org.junit.Test)

Example 2 with IntUnaryOperator

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

the class OptionalIntTest method testMap.

@Test
public void testMap() {
    final IntUnaryOperator negatorFunction = new IntUnaryOperator() {

        @Override
        public int applyAsInt(int operand) {
            return -operand;
        }
    };
    OptionalInt result;
    result = OptionalInt.empty().map(negatorFunction);
    assertThat(result, isEmpty());
    result = OptionalInt.of(10).map(negatorFunction);
    assertThat(result, hasValue(-10));
}
Also used : IntUnaryOperator(com.annimon.stream.function.IntUnaryOperator) Test(org.junit.Test)

Example 3 with IntUnaryOperator

use of com.annimon.stream.function.IntUnaryOperator 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 IntUnaryOperator

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

the class IterateTest method testStreamIterateWithPredicate.

@Test
public void testStreamIterateWithPredicate() {
    IntPredicate condition = new IntPredicate() {

        @Override
        public boolean test(int value) {
            return value < 20;
        }
    };
    IntUnaryOperator increment = new IntUnaryOperator() {

        @Override
        public int applyAsInt(int t) {
            return t + 5;
        }
    };
    IntStream.iterate(0, condition, increment).custom(assertElements(arrayContaining(0, 5, 10, 15)));
}
Also used : IntPredicate(com.annimon.stream.function.IntPredicate) IntUnaryOperator(com.annimon.stream.function.IntUnaryOperator) Test(org.junit.Test)

Example 5 with IntUnaryOperator

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

the class IterateTest method testStreamIterate.

@Test
public void testStreamIterate() {
    IntUnaryOperator operator = new IntUnaryOperator() {

        @Override
        public int applyAsInt(int operand) {
            return operand + 1;
        }
    };
    assertTrue(IntStream.iterate(1, operator).limit(3).sum() == 6);
    assertTrue(IntStream.iterate(1, operator).iterator().hasNext());
}
Also used : IntUnaryOperator(com.annimon.stream.function.IntUnaryOperator) Test(org.junit.Test)

Aggregations

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