Search in sources :

Example 1 with LinTrendAccumulator

use of com.hazelcast.jet.accumulator.LinTrendAccumulator in project hazelcast by hazelcast.

the class AggregateOperationsTest method when_linearTrend.

@Test
public void when_linearTrend() {
    // Given
    AggregateOperation1<Entry<Long, Long>, LinTrendAccumulator, Double> op = linearTrend(Entry::getKey, Entry::getValue);
    Supplier<LinTrendAccumulator> createFn = op.createFn();
    BiConsumer<? super LinTrendAccumulator, ? super Entry<Long, Long>> accFn = op.accumulateFn();
    BiConsumer<? super LinTrendAccumulator, ? super LinTrendAccumulator> combineFn = op.combineFn();
    BiConsumer<? super LinTrendAccumulator, ? super LinTrendAccumulator> deductFn = op.deductFn();
    Function<? super LinTrendAccumulator, ? extends Double> finishFn = op.finishFn();
    assertNotNull(createFn);
    assertNotNull(accFn);
    assertNotNull(combineFn);
    assertNotNull(deductFn);
    assertNotNull(finishFn);
    // When
    LinTrendAccumulator a1 = createFn.get();
    accFn.accept(a1, entry(1L, 3L));
    accFn.accept(a1, entry(2L, 5L));
    assertEquals(2.0, finishFn.apply(a1), Double.MIN_VALUE);
    LinTrendAccumulator a2 = createFn.get();
    accFn.accept(a2, entry(5L, 11L));
    accFn.accept(a2, entry(6L, 13L));
    assertEquals(2.0, finishFn.apply(a2), Double.MIN_VALUE);
    combineFn.accept(a1, a2);
    assertEquals(2.0, finishFn.apply(a1), Double.MIN_VALUE);
    deductFn.accept(a1, a2);
    assertEquals(2.0, finishFn.apply(a1), Double.MIN_VALUE);
    Double result = finishFn.apply(a1);
    assertEquals(Double.valueOf(2), result);
    // When
    LinTrendAccumulator acc = createFn.get();
    // Then
    assertTrue("NaN expected if nothing accumulated", Double.isNaN(finishFn.apply(acc)));
    // When
    accFn.accept(acc, entry(2L, 1L));
    // Then
    assertTrue("NaN expected if just single point accumulated", Double.isNaN(finishFn.apply(acc)));
    // When
    accFn.accept(acc, entry(2L, 1L));
    // Then
    assertTrue("NaN expected if all data points are equal", Double.isNaN(finishFn.apply(acc)));
    // When
    accFn.accept(acc, entry(2L, 2L));
    // Then
    assertTrue("NaN expected if all data points have same x value", Double.isNaN(finishFn.apply(acc)));
}
Also used : Entry(java.util.Map.Entry) LinTrendAccumulator(com.hazelcast.jet.accumulator.LinTrendAccumulator) AggregateOperations.averagingLong(com.hazelcast.jet.aggregate.AggregateOperations.averagingLong) AggregateOperations.summingLong(com.hazelcast.jet.aggregate.AggregateOperations.summingLong) AggregateOperations.averagingDouble(com.hazelcast.jet.aggregate.AggregateOperations.averagingDouble) AggregateOperations.summingDouble(com.hazelcast.jet.aggregate.AggregateOperations.summingDouble) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with LinTrendAccumulator

use of com.hazelcast.jet.accumulator.LinTrendAccumulator in project hazelcast-jet by hazelcast.

the class AggregateOperationsTest method when_linearTrend.

@Test
public void when_linearTrend() {
    // Given
    AggregateOperation1<Entry<Long, Long>, LinTrendAccumulator, Double> op = linearTrend(Entry::getKey, Entry::getValue);
    Supplier<LinTrendAccumulator> newFn = op.createFn();
    BiConsumer<? super LinTrendAccumulator, ? super Entry<Long, Long>> accFn = op.accumulateFn();
    BiConsumer<? super LinTrendAccumulator, ? super LinTrendAccumulator> combineFn = op.combineFn();
    BiConsumer<? super LinTrendAccumulator, ? super LinTrendAccumulator> deductFn = op.deductFn();
    Function<? super LinTrendAccumulator, Double> finishFn = op.finishFn();
    assertNotNull(deductFn);
    // When
    LinTrendAccumulator a1 = newFn.get();
    accFn.accept(a1, entry(1L, 3L));
    accFn.accept(a1, entry(2L, 5L));
    assertEquals(2.0, finishFn.apply(a1), Double.MIN_VALUE);
    LinTrendAccumulator a2 = newFn.get();
    accFn.accept(a2, entry(5L, 11L));
    accFn.accept(a2, entry(6L, 13L));
    assertEquals(2.0, finishFn.apply(a2), Double.MIN_VALUE);
    combineFn.accept(a1, a2);
    assertEquals(2.0, finishFn.apply(a1), Double.MIN_VALUE);
    deductFn.accept(a1, a2);
    assertEquals(2.0, finishFn.apply(a1), Double.MIN_VALUE);
    Double result = finishFn.apply(a1);
    assertEquals(Double.valueOf(2), result);
    // When
    LinTrendAccumulator acc = newFn.get();
    // Then
    assertTrue("NaN expected if nothing accumulated", Double.isNaN(finishFn.apply(acc)));
    // When
    accFn.accept(acc, entry(2L, 1L));
    // Then
    assertTrue("NaN expected if just single point accumulated", Double.isNaN(finishFn.apply(acc)));
    // When
    accFn.accept(acc, entry(2L, 1L));
    // Then
    assertTrue("NaN expected if all data points are equal", Double.isNaN(finishFn.apply(acc)));
    // When
    accFn.accept(acc, entry(2L, 2L));
    // Then
    assertTrue("NaN expected if all data points have same x value", Double.isNaN(finishFn.apply(acc)));
}
Also used : Entry(java.util.Map.Entry) LinTrendAccumulator(com.hazelcast.jet.accumulator.LinTrendAccumulator) AggregateOperations.averagingLong(com.hazelcast.jet.aggregate.AggregateOperations.averagingLong) AggregateOperations.summingLong(com.hazelcast.jet.aggregate.AggregateOperations.summingLong) AggregateOperations.averagingDouble(com.hazelcast.jet.aggregate.AggregateOperations.averagingDouble) AggregateOperations.summingDouble(com.hazelcast.jet.aggregate.AggregateOperations.summingDouble) Test(org.junit.Test)

Aggregations

LinTrendAccumulator (com.hazelcast.jet.accumulator.LinTrendAccumulator)2 AggregateOperations.averagingDouble (com.hazelcast.jet.aggregate.AggregateOperations.averagingDouble)2 AggregateOperations.averagingLong (com.hazelcast.jet.aggregate.AggregateOperations.averagingLong)2 AggregateOperations.summingDouble (com.hazelcast.jet.aggregate.AggregateOperations.summingDouble)2 AggregateOperations.summingLong (com.hazelcast.jet.aggregate.AggregateOperations.summingLong)2 Entry (java.util.Map.Entry)2 Test (org.junit.Test)2 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1