use of org.apache.datasketches.memory.WritableHandle in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkResetAndStartingSubMultiple.
@Test
public void checkResetAndStartingSubMultiple() {
int k = 512;
int u = 4 * k;
try (WritableHandle h = makeNativeMemory(k)) {
WritableMemory mem = h.getWritable();
UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
// for internal checks
DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch) usk;
assertTrue(usk.isEmpty());
for (int i = 0; i < u; i++) {
usk.update(i);
}
assertFalse(usk.isEmpty());
assertTrue(sk1.getRetainedEntries(false) > k);
assertTrue(sk1.getThetaLong() < Long.MAX_VALUE);
sk1.reset();
assertTrue(usk.isEmpty());
assertEquals(sk1.getRetainedEntries(false), 0);
assertEquals(usk.getEstimate(), 0.0, 0.0);
assertEquals(sk1.getThetaLong(), Long.MAX_VALUE);
assertNotNull(sk1.getMemory());
assertFalse(sk1.isOrdered());
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.datasketches.memory.WritableHandle in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkEmptyAndP.
// Empty Tests
@Test
public void checkEmptyAndP() {
// virgin, p = 1.0
int k = 1024;
float p = (float) 1.0;
try (WritableHandle h = makeNativeMemory(k)) {
WritableMemory mem = h.getWritable();
UpdateSketch usk = UpdateSketch.builder().setP(p).setNominalEntries(k).build(mem);
// for internal checks
DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch) usk;
assertTrue(usk.isEmpty());
usk.update(1);
assertEquals(sk1.getRetainedEntries(true), 1);
assertFalse(usk.isEmpty());
// virgin, p = .001
p = (float) 0.001;
byte[] memArr2 = new byte[(int) mem.getCapacity()];
WritableMemory mem2 = WritableMemory.writableWrap(memArr2);
UpdateSketch usk2 = UpdateSketch.builder().setP(p).setNominalEntries(k).build(mem2);
sk1 = (DirectQuickSelectSketch) usk2;
assertTrue(usk2.isEmpty());
// will be rejected
usk2.update(1);
assertEquals(sk1.getRetainedEntries(true), 0);
assertFalse(usk2.isEmpty());
double est = usk2.getEstimate();
// println("Est: "+est);
// because curCount = 0
assertEquals(est, 0.0, 0.0);
// huge because theta is tiny!
double ub = usk2.getUpperBound(2);
// println("UB: "+ub);
assertTrue(ub > 0.0);
double lb = usk2.getLowerBound(2);
assertTrue(lb <= est);
// println("LB: "+lb);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.datasketches.memory.WritableHandle in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkBadSerVer.
// (expectedExceptions = SketchesArgumentException.class)
@Test
public void checkBadSerVer() {
int k = 512;
try (WritableHandle h = makeNativeMemory(k)) {
WritableMemory mem = h.getWritable();
UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
// for internal checks
DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch) usk;
assertTrue(usk.isEmpty());
for (int i = 0; i < k; i++) {
usk.update(i);
}
assertFalse(usk.isEmpty());
assertEquals(usk.getEstimate(), k, 0.0);
assertEquals(sk1.getRetainedEntries(false), k);
// corrupt the SerVer byte
mem.putByte(SER_VER_BYTE, (byte) 0);
Sketch.wrap(mem);
} catch (final Exception e) {
if (e instanceof SketchesArgumentException) {
} else {
throw new RuntimeException(e);
}
}
}
use of org.apache.datasketches.memory.WritableHandle in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkRebuild.
@Test
public void checkRebuild() {
int k = 512;
int u = 4 * k;
try (WritableHandle h = makeNativeMemory(k)) {
WritableMemory mem = h.getWritable();
UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
// for internal checks
DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch) usk;
assertTrue(usk.isEmpty());
for (int i = 0; i < u; i++) {
usk.update(i);
}
assertFalse(usk.isEmpty());
assertTrue(usk.getEstimate() > 0.0);
assertTrue(sk1.getRetainedEntries(false) > k);
sk1.rebuild();
assertEquals(sk1.getRetainedEntries(false), k);
assertEquals(sk1.getRetainedEntries(true), k);
sk1.rebuild();
assertEquals(sk1.getRetainedEntries(false), k);
assertEquals(sk1.getRetainedEntries(true), k);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.datasketches.memory.WritableHandle in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkExactModeMemoryArr.
@Test
public void checkExactModeMemoryArr() {
int k = 4096;
int u = 4096;
try (WritableHandle h = makeNativeMemory(k)) {
WritableMemory mem = h.getWritable();
UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
// for internal checks
DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch) usk;
assertTrue(usk.isEmpty());
for (int i = 0; i < u; i++) {
usk.update(i);
}
assertEquals(usk.getEstimate(), u, 0.0);
assertEquals(sk1.getRetainedEntries(false), u);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
Aggregations