Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ static DoublesUnionImpl heapifyInstance(final DoublesSketch sketch) {
* @return a DoublesUnion object
*/
static DoublesUnionImpl heapifyInstance(final Memory srcMem) {
final long n = srcMem.getLong(PreambleUtil.N_LONG);
final int preLongs = srcMem.getByte(PreambleUtil.PREAMBLE_LONGS_BYTE) & 0xFF;
final int k = srcMem.getShort(PreambleUtil.K_SHORT) & 0xFFFF;
final HeapUpdateDoublesSketch sketch = (n == 0)
final HeapUpdateDoublesSketch sketch = (preLongs == 1)
? HeapUpdateDoublesSketch.newInstance(k)
: HeapUpdateDoublesSketch.heapifyInstance(srcMem);
final DoublesUnionImpl union = new DoublesUnionImpl(k);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,17 @@ public void isSameResourceDirect() {
Assert.assertFalse(union.isSameResource(mem2));
}

@SuppressWarnings("unused")
@Test
public void emptyUnionSerDeIssue195() {
DoublesUnion union = DoublesUnion.builder().build();
byte[] byteArr = union.toByteArray();
Memory mem = Memory.wrap(byteArr);
DoublesUnion union2 = DoublesUnionBuilder.heapify(mem);
Assert.assertEquals(mem.getCapacity(), 8L);
Assert.assertTrue(union2.isEmpty());
}

@Test
public void printlnTest() {
println("PRINTING: " + this.getClass().getName());
Expand Down
31 changes: 19 additions & 12 deletions src/test/java/com/yahoo/sketches/tuple/FilterTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.yahoo.sketches.tuple;

import java.util.Random;

import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.Random;

public class FilterTest {

private final int numberOfElements = 100;
private static final int numberOfElements = 100;
private static final Random random = new Random(1);//deterministic for this class

@Test
public void emptySketch() {
Expand Down Expand Up @@ -39,7 +40,8 @@ public void nullSketch() {

@Test
public void filledSketchShouldBehaveTheSame() {
UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build();
UpdatableSketch<Double, DoubleSummary> sketch =
new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build();

fillSketch(sketch, numberOfElements, 0.0);

Expand All @@ -56,7 +58,8 @@ public void filledSketchShouldBehaveTheSame() {

@Test
public void filledSketchShouldFilterOutElements() {
UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build();
UpdatableSketch<Double, DoubleSummary> sketch =
new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build();

fillSketch(sketch, numberOfElements, 0.0);
fillSketch(sketch, 2 * numberOfElements, 1.0);
Expand All @@ -74,7 +77,8 @@ public void filledSketchShouldFilterOutElements() {

@Test
public void filteringInEstimationMode() {
UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build();
UpdatableSketch<Double, DoubleSummary> sketch =
new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build();

int n = 10000;
fillSketch(sketch, n, 0.0);
Expand All @@ -93,7 +97,9 @@ public void filteringInEstimationMode() {

@Test
public void nonEmptySketchWithNoEntries() {
UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).setSamplingProbability(0.0001f).build();
UpdatableSketch<Double, DoubleSummary> sketch =
new UpdatableSketchBuilder<>(
new DoubleSummaryFactory()).setSamplingProbability(0.0001f).build();
sketch.update(0, 0.0);

Assert.assertFalse(sketch.isEmpty());
Expand All @@ -110,11 +116,12 @@ public void nonEmptySketchWithNoEntries() {
Assert.assertEquals(filteredSketch.getUpperBound(1), sketch.getUpperBound(1));
}

private static void fillSketch(UpdatableSketch<Double, DoubleSummary> sketch, int numberOfElements, Double sketchValue) {
Random random = new Random();
private static void fillSketch(UpdatableSketch<Double, DoubleSummary> sketch,
int numberOfElements, Double sketchValue) {


for (int cont = 0; cont < numberOfElements; cont++) {
sketch.update(random.nextLong(), sketchValue);
}
for (int cont = 0; cont < numberOfElements; cont++) {
sketch.update(random.nextLong(), sketchValue);
}
}
}