@Override protected void testAggregation(Object expectedValue, Block block) { Page page = OperatorAssertion.appendSampleWeight(ImmutableList.of(new Page(block)), WEIGHT).get(0); assertApproximateAggregation( getFunction(), page.getChannelCount() - 1, getConfidence(), (Double) expectedValue, page); }
private void testCorrectnessOfErrorFunction(List<Number> inputList) throws Exception { int inRange = 0; int numberOfRuns = 1000; double sampleRatio = 1 / (double) WEIGHT; double actual = getExpectedValue(inputList); Random rand = new Random(1); for (int i = 0; i < numberOfRuns; i++) { // Compute Sampled Value using sampledList (numberOfRuns times) ImmutableList.Builder<Number> sampledList = ImmutableList.builder(); for (Number x : inputList) { if (rand.nextDouble() < sampleRatio) { sampledList.add(x); } } BlockBuilder builder = getType().createBlockBuilder(new BlockBuilderStatus()); for (Number sample : sampledList.build()) { if (getType() == BIGINT) { BIGINT.writeLong(builder, sample.longValue()); } else if (getType() == DOUBLE) { DOUBLE.writeDouble(builder, sample.doubleValue()); } else { throw new AssertionError("Can only handle longs and doubles"); } } Page page = new Page(builder.build()); page = OperatorAssertion.appendSampleWeight(ImmutableList.of(page), WEIGHT).get(0); Accumulator accumulator = getFunction() .bind( ImmutableList.of(0), Optional.<Integer>absent(), Optional.of(page.getChannelCount() - 1), getConfidence()) .createAccumulator(); accumulator.addInput(page); Block result = accumulator.evaluateFinal(); String approxValue = BlockAssertions.toValues(accumulator.getFinalType(), result).get(0).toString(); double approx = Double.parseDouble(approxValue.split(" ")[0]); double error = Double.parseDouble(approxValue.split(" ")[2]); // Check if actual answer lies within [approxAnswer - error, approxAnswer + error] if (Math.abs(approx - actual) <= error) { inRange++; } } BinomialDistribution binomial = new BinomialDistribution(numberOfRuns, getConfidence()); int lowerBound = binomial.inverseCumulativeProbability(0.01); int upperBound = binomial.inverseCumulativeProbability(0.99); assertTrue( lowerBound < inRange && inRange < upperBound, String.format( "%d out of %d passed. Expected [%d, %d]", inRange, numberOfRuns, lowerBound, upperBound)); }
@Test public void testLimitWithPageAlignment() throws Exception { List<Page> input = rowPagesBuilder(SINGLE_LONG) .addSequencePage(3, 1) .addSequencePage(2, 4) .addSequencePage(2, 6) .build(); OperatorFactory operatorFactory = new LimitOperatorFactory(0, ImmutableList.of(SINGLE_LONG), 5, Optional.<Integer>absent()); Operator operator = operatorFactory.createOperator(driverContext); List<Page> expected = rowPagesBuilder(SINGLE_LONG).addSequencePage(3, 1).addSequencePage(2, 4).build(); OperatorAssertion.assertOperatorEquals(operator, input, expected); }
@Test public void testSampledLimit() throws Exception { List<Page> input = rowPagesBuilder(SINGLE_LONG) .addSequencePage(2, 1) .addSequencePage(2, 4) .addSequencePage(2, 6) .build(); input = appendSampleWeight(input, 2); OperatorFactory operatorFactory = new LimitOperatorFactory( 0, ImmutableList.of(SINGLE_LONG), 5, Optional.of(input.get(0).getChannelCount() - 1)); Operator operator = operatorFactory.createOperator(driverContext); List<Page> expected = rowPagesBuilder(SINGLE_LONG, SINGLE_LONG).row(1, 2).row(2, 2).pageBreak().row(4, 1).build(); OperatorAssertion.assertOperatorEquals(operator, input, expected); }