@Test public void nullInput() { ArrayOfDoublesIntersection intersection = new ArrayOfDoublesSetOperationBuilder().buildIntersection(); intersection.update(null, null); ArrayOfDoublesCompactSketch result = intersection.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); Assert.assertEquals(result.getUpperBound(1), 0.0); Assert.assertEquals(result.getValues().length, 0); }
@Test public void empty() { ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder().build(); ArrayOfDoublesIntersection intersection = new ArrayOfDoublesSetOperationBuilder().buildIntersection(); intersection.update(sketch1, null); ArrayOfDoublesCompactSketch result = intersection.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); Assert.assertEquals(result.getUpperBound(1), 0.0); Assert.assertEquals(result.getValues().length, 0); }
@Test public void emptyFromQuickSelectSketch() { ArrayOfDoublesUpdatableSketch us = new ArrayOfDoublesUpdatableSketchBuilder().build(); ArrayOfDoublesCompactSketch sketch = us.compact(); Assert.assertTrue(sketch.isEmpty()); Assert.assertFalse(sketch.isEstimationMode()); Assert.assertEquals(sketch.getEstimate(), 0.0); Assert.assertEquals(sketch.getLowerBound(1), 0.0); Assert.assertEquals(sketch.getUpperBound(1), 0.0); Assert.assertEquals(sketch.getRetainedEntries(), 0); Assert.assertEquals(sketch.getThetaLong(), Long.MAX_VALUE); Assert.assertEquals(sketch.getTheta(), 1.0); Assert.assertNotNull(sketch.getValues()); Assert.assertEquals(sketch.getValues().length, 0); ArrayOfDoublesSketchIterator it = sketch.iterator(); while (it.next()) { Assert.fail("empty sketch expected"); } }
@Test public void notEmptyNoEntries() { ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder().setSamplingProbability(0.01f).build(); sketch1.update("a", new double[] {1}); // this happens to get rejected because of sampling with low probability ArrayOfDoublesIntersection intersection = new ArrayOfDoublesSetOperationBuilder().buildIntersection(); intersection.update(sketch1, null); ArrayOfDoublesCompactSketch result = intersection.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); Assert.assertEquals(result.getUpperBound(1), 0.0); Assert.assertEquals(result.getValues().length, 0); }
@Test public void emptyFromQuickSelectSketch() { ArrayOfDoublesUpdatableSketch us = new ArrayOfDoublesUpdatableSketchBuilder().build(WritableMemory.wrap(new byte[1000000])); ArrayOfDoublesCompactSketch sketch = us.compact(WritableMemory.wrap(new byte[1000000])); Assert.assertTrue(sketch.isEmpty()); Assert.assertFalse(sketch.isEstimationMode()); Assert.assertEquals(sketch.getEstimate(), 0.0); Assert.assertEquals(sketch.getLowerBound(1), 0.0); Assert.assertEquals(sketch.getUpperBound(1), 0.0); Assert.assertEquals(sketch.getRetainedEntries(), 0); Assert.assertEquals(sketch.getThetaLong(), Long.MAX_VALUE); Assert.assertEquals(sketch.getTheta(), 1.0); Assert.assertNotNull(sketch.getValues()); Assert.assertEquals(sketch.getValues().length, 0); ArrayOfDoublesSketchIterator it = sketch.iterator(); while (it.next()) { Assert.fail("empty sketch expected"); } }
ArrayOfDoublesCompactSketch result = aNotB.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); result = aNotB.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); result = aNotB.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); result = aNotB.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); result = aNotB.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0);
@Test public void exactModeFromQuickSelectSketch() { ArrayOfDoublesUpdatableSketch us = new ArrayOfDoublesUpdatableSketchBuilder().build(); us.update(1, new double[] {1.0}); us.update(2, new double[] {1.0}); us.update(3, new double[] {1.0}); us.update(1, new double[] {1.0}); us.update(2, new double[] {1.0}); us.update(3, new double[] {1.0}); ArrayOfDoublesCompactSketch sketch = us.compact(); Assert.assertFalse(sketch.isEmpty()); Assert.assertFalse(sketch.isEstimationMode()); Assert.assertEquals(sketch.getEstimate(), 3.0); Assert.assertEquals(sketch.getLowerBound(1), 3.0); Assert.assertEquals(sketch.getUpperBound(1), 3.0); Assert.assertEquals(sketch.getRetainedEntries(), 3); Assert.assertEquals(sketch.getThetaLong(), Long.MAX_VALUE); Assert.assertEquals(sketch.getTheta(), 1.0); double[][] values = sketch.getValues(); Assert.assertEquals(values.length, 3); for (double[] array: values) { Assert.assertEquals(array[0], 2.0); } }
@Test public void exactModeFromQuickSelectSketch() { ArrayOfDoublesUpdatableSketch us = new ArrayOfDoublesUpdatableSketchBuilder().build(WritableMemory.wrap(new byte[1000000])); us.update(1, new double[] {1.0}); us.update(2, new double[] {1.0}); us.update(3, new double[] {1.0}); us.update(1, new double[] {1.0}); us.update(2, new double[] {1.0}); us.update(3, new double[] {1.0}); ArrayOfDoublesCompactSketch sketch = us.compact(WritableMemory.wrap(new byte[1000000])); Assert.assertFalse(sketch.isEmpty()); Assert.assertFalse(sketch.isEstimationMode()); Assert.assertEquals(sketch.getEstimate(), 3.0); Assert.assertEquals(sketch.getLowerBound(1), 3.0); Assert.assertEquals(sketch.getUpperBound(1), 3.0); Assert.assertEquals(sketch.getRetainedEntries(), 3); Assert.assertEquals(sketch.getThetaLong(), Long.MAX_VALUE); Assert.assertEquals(sketch.getTheta(), 1.0); Assert.assertEquals(sketch.getSeedHash(), Util.computeSeedHash(DEFAULT_UPDATE_SEED)); double[][] values = sketch.getValues(); Assert.assertEquals(values.length, 3); for (double[] array: values) { Assert.assertEquals(array[0], 2.0); } }
@Test public void heapDisjointEstimationMode() { int key = 0; ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder().build(); for (int i = 0; i < 8192; i++) sketch1.update(key++, new double[] {1.0}); ArrayOfDoublesUpdatableSketch sketch2 = new ArrayOfDoublesUpdatableSketchBuilder().build(); for (int i = 0; i < 8192; i++) sketch2.update(key++, new double[] {1.0}); ArrayOfDoublesIntersection intersection = new ArrayOfDoublesSetOperationBuilder().buildIntersection(); intersection.update(sketch1, combiner); intersection.update(sketch2, combiner); ArrayOfDoublesCompactSketch result = intersection.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); Assert.assertEquals(result.getUpperBound(1), 0.0); Assert.assertEquals(result.getValues().length, 0); }
@Test public void aSameAsB() { ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().build(); sketch.update(1, new double[] {1.0}); sketch.update(2, new double[] {1.0}); sketch.update(3, new double[] {1.0}); sketch.update(4, new double[] {1.0}); sketch.update(5, new double[] {1.0}); ArrayOfDoublesAnotB aNotB = new ArrayOfDoublesSetOperationBuilder().buildAnotB(); aNotB.update(sketch, sketch); ArrayOfDoublesCompactSketch result = aNotB.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); Assert.assertEquals(result.getUpperBound(1), 0.0); }
@Test public void nullOrEmptyA() { ArrayOfDoublesUpdatableSketch sketchB = new ArrayOfDoublesUpdatableSketchBuilder().build(); sketchB.update(1, new double[] {1.0}); sketchB.update(2, new double[] {1.0}); sketchB.update(3, new double[] {1.0}); sketchB.update(4, new double[] {1.0}); sketchB.update(5, new double[] {1.0}); ArrayOfDoublesAnotB aNotB = new ArrayOfDoublesSetOperationBuilder().buildAnotB(); aNotB.update(null, sketchB); ArrayOfDoublesCompactSketch result = aNotB.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); Assert.assertEquals(result.getUpperBound(1), 0.0); ArrayOfDoublesSketch sketchA = new ArrayOfDoublesUpdatableSketchBuilder().build(); aNotB.update(sketchA, sketchB); result = aNotB.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); Assert.assertEquals(result.getUpperBound(1), 0.0); }
@Test public void heapExactWithEmpty() { ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder().build(); sketch1.update(1, new double[] {1}); sketch1.update(2, new double[] {1}); sketch1.update(3, new double[] {1}); ArrayOfDoublesUpdatableSketch sketch2 = new ArrayOfDoublesUpdatableSketchBuilder().build(); ArrayOfDoublesIntersection intersection = new ArrayOfDoublesSetOperationBuilder().buildIntersection(); intersection.update(sketch1, null); intersection.update(sketch2, null); ArrayOfDoublesCompactSketch result = intersection.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); Assert.assertEquals(result.getUpperBound(1), 0.0); }
ArrayOfDoublesCompactSketch result = intersection.getResult(); Assert.assertFalse(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 1); Assert.assertEquals(result.getEstimate(), 1.0); Assert.assertEquals(result.getLowerBound(1), 1.0); result = intersection.getResult(); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0);
ArrayOfDoublesCompactSketch result = aNotB.getResult(); Assert.assertFalse(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 5); Assert.assertEquals(result.getEstimate(), 5.0); Assert.assertEquals(result.getLowerBound(1), 5.0); result = aNotB.getResult(); Assert.assertFalse(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 5); Assert.assertEquals(result.getEstimate(), 5.0); Assert.assertEquals(result.getLowerBound(1), 5.0);
@Test public void directDisjointEstimationMode() { int key = 0; ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder(). build(WritableMemory.wrap(new byte[1000000])); for (int i = 0; i < 8192; i++) sketch1.update(key++, new double[] {1.0}); ArrayOfDoublesUpdatableSketch sketch2 = new ArrayOfDoublesUpdatableSketchBuilder(). build(WritableMemory.wrap(new byte[1000000])); for (int i = 0; i < 8192; i++) sketch2.update(key++, new double[] {1.0}); ArrayOfDoublesIntersection intersection = new ArrayOfDoublesSetOperationBuilder(). buildIntersection(WritableMemory.wrap(new byte[1000000])); intersection.update(sketch1, combiner); intersection.update(sketch2, combiner); ArrayOfDoublesCompactSketch result = intersection.getResult(WritableMemory.wrap(new byte[1000000])); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); Assert.assertEquals(result.getUpperBound(1), 0.0); Assert.assertEquals(result.getValues().length, 0); }
@Test public void directExactWithEmpty() { ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder() .build(WritableMemory.wrap(new byte[1000000])); sketch1.update(1, new double[] {1}); sketch1.update(2, new double[] {1}); sketch1.update(3, new double[] {1}); ArrayOfDoublesUpdatableSketch sketch2 = new ArrayOfDoublesUpdatableSketchBuilder() .build(WritableMemory.wrap(new byte[1000000])); ArrayOfDoublesIntersection intersection = new ArrayOfDoublesSetOperationBuilder(). buildIntersection(WritableMemory.wrap(new byte[1000000])); intersection.update(sketch1, null); intersection.update(sketch2, null); ArrayOfDoublesCompactSketch result = intersection.getResult(WritableMemory.wrap(new byte[1000000])); Assert.assertTrue(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 0); Assert.assertEquals(result.getEstimate(), 0.0); Assert.assertEquals(result.getLowerBound(1), 0.0); Assert.assertEquals(result.getUpperBound(1), 0.0); }
@Test public void heapEstimationModeFullOverlapTwoValuesAndDownsizing() { int key = 0; ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder().setNumberOfValues(2).build(); for (int i = 0; i < 8192; i++) { sketch1.update(key++, new double[] {1.0, 2.0}); } key = 0; // full overlap ArrayOfDoublesUpdatableSketch sketch2 = new ArrayOfDoublesUpdatableSketchBuilder().setNumberOfValues(2).build(); for (int i = 0; i < 8192; i++) { sketch2.update(key++, new double[] {1.0, 2.0}); } ArrayOfDoublesUnion union = new ArrayOfDoublesSetOperationBuilder().setNumberOfValues(2).setNominalEntries(1024).buildUnion(); union.update(sketch1); union.update(sketch2); ArrayOfDoublesCompactSketch result = union.getResult(); Assert.assertFalse(result.isEmpty()); Assert.assertTrue(result.isEstimationMode()); Assert.assertEquals(result.getEstimate(), 8192.0, 8192 * 0.01); Assert.assertEquals(result.getRetainedEntries(), 1024); // union was downsampled ArrayOfDoublesSketchIterator it = result.iterator(); double[] expected = {2, 4}; while (it.next()) { Assert.assertEquals(it.getValues(), expected, Arrays.toString(it.getValues()) + " != " + Arrays.toString(expected)); } }
@Test public void heapExactModeCustomSeed() { long seed = 1234567890; ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder().setSeed(seed).build(); sketch1.update(1, new double[] {1}); sketch1.update(1, new double[] {1}); sketch1.update(2, new double[] {1}); sketch1.update(2, new double[] {1}); ArrayOfDoublesUpdatableSketch sketch2 = new ArrayOfDoublesUpdatableSketchBuilder().setSeed(seed).build(); sketch2.update(2, new double[] {1}); sketch2.update(2, new double[] {1}); sketch2.update(3, new double[] {1}); sketch2.update(3, new double[] {1}); ArrayOfDoublesIntersection intersection = new ArrayOfDoublesSetOperationBuilder().setSeed(seed).buildIntersection(); intersection.update(sketch1, combiner); intersection.update(sketch2, combiner); ArrayOfDoublesCompactSketch result = intersection.getResult(); Assert.assertFalse(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 1); Assert.assertEquals(result.getEstimate(), 1.0); Assert.assertEquals(result.getLowerBound(1), 1.0); Assert.assertEquals(result.getUpperBound(1), 1.0); double[][] values = result.getValues(); for (int i = 0; i < values.length; i++) Assert.assertEquals(values[i][0], 4.0); }
@Test public void exactMode() { ArrayOfDoublesUpdatableSketch sketchA = new ArrayOfDoublesUpdatableSketchBuilder().build(); sketchA.update(1, new double[] {1}); sketchA.update(2, new double[] {1}); sketchA.update(3, new double[] {1}); sketchA.update(4, new double[] {1}); sketchA.update(5, new double[] {1}); ArrayOfDoublesUpdatableSketch sketchB = new ArrayOfDoublesUpdatableSketchBuilder().build(); sketchB.update(3, new double[] {1}); sketchB.update(4, new double[] {1}); sketchB.update(5, new double[] {1}); sketchB.update(6, new double[] {1}); sketchB.update(7, new double[] {1}); ArrayOfDoublesAnotB aNotB = new ArrayOfDoublesSetOperationBuilder().buildAnotB(); aNotB.update(sketchA, sketchB); ArrayOfDoublesCompactSketch result = aNotB.getResult(); Assert.assertFalse(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 2); Assert.assertEquals(result.getEstimate(), 2.0); Assert.assertEquals(result.getLowerBound(1), 2.0); Assert.assertEquals(result.getUpperBound(1), 2.0); ArrayOfDoublesSketchIterator it = result.iterator(); while (it.next()) { Assert.assertEquals(it.getValues(), new double[] {1}); } }
@Test public void exactModeCustomSeed() { long seed = 1234567890; ArrayOfDoublesUpdatableSketch sketchA = new ArrayOfDoublesUpdatableSketchBuilder().setSeed(seed).build(); sketchA.update(1, new double[] {1}); sketchA.update(2, new double[] {1}); sketchA.update(3, new double[] {1}); sketchA.update(4, new double[] {1}); sketchA.update(5, new double[] {1}); ArrayOfDoublesUpdatableSketch sketchB = new ArrayOfDoublesUpdatableSketchBuilder().setSeed(seed).build(); sketchB.update(3, new double[] {1}); sketchB.update(4, new double[] {1}); sketchB.update(5, new double[] {1}); sketchB.update(6, new double[] {1}); sketchB.update(7, new double[] {1}); ArrayOfDoublesAnotB aNotB = new ArrayOfDoublesSetOperationBuilder().setSeed(seed).buildAnotB(); aNotB.update(sketchA, sketchB); ArrayOfDoublesCompactSketch result = aNotB.getResult(); Assert.assertFalse(result.isEmpty()); Assert.assertEquals(result.getRetainedEntries(), 2); Assert.assertEquals(result.getEstimate(), 2.0); Assert.assertEquals(result.getLowerBound(1), 2.0); Assert.assertEquals(result.getUpperBound(1), 2.0); ArrayOfDoublesSketchIterator it = result.iterator(); while (it.next()) { Assert.assertEquals(it.getValues(), new double[] {1}); } }