/** * Tests that the the {@link CombineFn}, when applied to the provided input, produces the provided * output. Tests a variety of permutations of the input. */ public static <InputT, AccumT, OutputT> void testCombineFn( CombineFn<InputT, AccumT, OutputT> fn, List<InputT> input, final OutputT expected) { testCombineFn(fn, input, is(expected)); Collections.shuffle(input); testCombineFn(fn, input, is(expected)); }
@Test public void testBinaryCombineFnWithNulls() { testCombineFn(new NullCombiner(), Arrays.asList(3, 3, 5), 45); testCombineFn(new NullCombiner(), Arrays.asList(null, 3, 5), 30); testCombineFn(new NullCombiner(), Arrays.asList(3, 3, null), 18); testCombineFn(new NullCombiner(), Arrays.asList(null, 3, null), 12); testCombineFn(new NullCombiner(), Arrays.asList(null, null, null), 8); }
@Test public void testLotsOfDuplicates() { List<Integer> all = new ArrayList<>(); all.add(1); for (int i = 1; i < 300; i++) { all.add(2); } for (int i = 300; i < 1000; i++) { all.add(3); } testCombineFn(ApproximateQuantilesCombineFn.create(5), all, Arrays.asList(1, 2, 3, 3, 3)); }
@Test public void testLogDistribution() { List<Integer> all = new ArrayList<>(); for (int i = 1; i < 1000; i++) { all.add((int) Math.log(i)); } testCombineFn(ApproximateQuantilesCombineFn.create(5), all, Arrays.asList(0, 5, 6, 6, 6)); }
@Test public void testZipfianDistribution() { List<Integer> all = new ArrayList<>(); for (int i = 1; i < 1000; i++) { all.add(1000 / i); } testCombineFn(ApproximateQuantilesCombineFn.create(5), all, Arrays.asList(1, 1, 2, 4, 1000)); }
@Test public void testSingleton() { testCombineFn( ApproximateQuantilesCombineFn.create(5), Arrays.asList(389), Arrays.asList(389, 389, 389, 389, 389)); }
@Test public void testAlternateComparator() { List<String> inputs = Arrays.asList("aa", "aaa", "aaaa", "b", "ccccc", "dddd", "zz"); testCombineFn( ApproximateQuantilesCombineFn.create(3), inputs, Arrays.asList("aa", "b", "zz")); testCombineFn( ApproximateQuantilesCombineFn.create(3, new OrderByLength()), inputs, Arrays.asList("b", "aaa", "ccccc")); }
@Test public void testDuplicates() { int size = 101; List<Integer> all = new ArrayList<>(); for (int i = 0; i < 10; i++) { all.addAll(intRange(size)); } testCombineFn( ApproximateQuantilesCombineFn.create(5), all, Arrays.asList(0, 25, 50, 75, 100)); }
@Test public void testSimpleQuantiles() { testCombineFn( ApproximateQuantilesCombineFn.create(5), intRange(101), Arrays.asList(0, 25, 50, 75, 100)); }
private void runCombineFnTest(long elementCount, long uniqueCount, int sampleSize) { List<Double> input = LongStream.range(0, elementCount) .mapToObj(i -> 1.0 / (i % uniqueCount + 1)) .collect(Collectors.toList()); CombineFnTester.testCombineFn( new ApproximateUniqueCombineFn<>(sampleSize, DoubleCoder.of()), input, estimateIsWithinRangeFor(uniqueCount, sampleSize)); }
@Test public void testCombineFn() { CombineFnTester.testCombineFn( Sample.combineFn(limit), lines, allOf(Matchers.iterableWithSize(Math.min(lines.size(), limit)), everyItem(isIn(lines)))); } }
@Test public void testMeanFn() throws Exception { testCombineFn(Mean.of(), Lists.newArrayList(1, 2, 3, 4), 2.5); } }
@Test public void testSumIntegerFn() { testCombineFn(Sum.ofIntegers(), Lists.newArrayList(1, 2, 3, 4), 10); }
@Test public void testMaxIntegerFn() { testCombineFn(Max.ofIntegers(), Lists.newArrayList(1, 2, 3, 4), 4); }
@Test public void testMaxLongFn() { testCombineFn(Max.ofLongs(), Lists.newArrayList(1L, 2L, 3L, 4L), 4L); }
@Test public void testSumLongFn() { testCombineFn(Sum.ofLongs(), Lists.newArrayList(1L, 2L, 3L, 4L), 10L); }
@Test public void testSumDoubleFn() { testCombineFn(Sum.ofDoubles(), Lists.newArrayList(1.0, 2.0, 3.0, 4.0), 10.0); }
@Test public void testLargerQuantiles() { testCombineFn( ApproximateQuantilesCombineFn.create(50), intRange(10001), quantileMatcher(10001, 50, 20 /* tolerance */)); }
@Test public void testUnevenQuantiles() { testCombineFn( ApproximateQuantilesCombineFn.create(37), intRange(5000), quantileMatcher(5000, 37, 20 /* tolerance */)); }
@Test public void testTightEpsilon() { testCombineFn( ApproximateQuantilesCombineFn.<Integer>create(10).withEpsilon(0.01), intRange(10001), quantileMatcher(10001, 10, 5 /* tolerance */)); }