use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapAlphaSketchTest method checkBadPreambleLongs.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadPreambleLongs() {
int k = 512;
Sketch alpha = UpdateSketch.builder().setFamily(ALPHA).setNominalEntries(k).build();
byte[] byteArray = alpha.toByteArray();
WritableMemory mem = WritableMemory.writableWrap(byteArray);
// corrupt:
mem.putByte(PREAMBLE_LONGS_BYTE, (byte) 4);
Sketch.heapify(mem);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapAlphaSketchTest method checkAlphaIncompatibleWithMem.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkAlphaIncompatibleWithMem() {
WritableMemory mem = WritableMemory.writableWrap(new byte[(512 * 16) + 24]);
UpdateSketch.builder().setFamily(Family.ALPHA).setNominalEntries(512).build(mem);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapAlphaSketchTest method checkBadSerVer.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadSerVer() {
int k = 512;
int u = k;
long seed = DEFAULT_UPDATE_SEED;
UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setSeed(seed).setNominalEntries(k).build();
// for internal checks
HeapAlphaSketch sk1 = (HeapAlphaSketch) usk;
assertTrue(usk.isEmpty());
for (int i = 0; i < u; i++) {
sk1.update(i);
}
assertFalse(usk.isEmpty());
assertEquals(usk.getEstimate(), u, 0.0);
assertEquals(sk1.getRetainedEntries(false), u);
byte[] byteArray = usk.toByteArray();
WritableMemory mem = WritableMemory.writableWrap(byteArray);
// corrupt the SerVer byte
mem.putByte(SER_VER_BYTE, (byte) 0);
Sketch.heapify(mem, seed);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapAlphaSketchTest method checkIllegalSketchID_UpdateSketch.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkIllegalSketchID_UpdateSketch() {
int k = 512;
int u = k;
long seed = DEFAULT_UPDATE_SEED;
UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setSeed(seed).setNominalEntries(k).build();
// for internal checks
HeapAlphaSketch sk1 = (HeapAlphaSketch) usk;
assertTrue(usk.isEmpty());
for (int i = 0; i < u; i++) {
usk.update(i);
}
assertFalse(usk.isEmpty());
assertEquals(usk.getEstimate(), u, 0.0);
assertEquals(sk1.getRetainedEntries(false), u);
byte[] byteArray = usk.toByteArray();
WritableMemory mem = WritableMemory.writableWrap(byteArray);
// corrupt the Sketch ID byte
mem.putByte(FAMILY_BYTE, (byte) 0);
// try to heapify the corruped mem
Sketch.heapify(mem, seed);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapAlphaSketchTest method checkMemDeSerExceptions.
@Test
public void checkMemDeSerExceptions() {
int k = 1024;
UpdateSketch sk1 = UpdateSketch.builder().setFamily(ALPHA).setNominalEntries(k).build();
// forces preLongs to 3
sk1.update(1L);
byte[] bytearray1 = sk1.toByteArray();
WritableMemory mem = WritableMemory.writableWrap(bytearray1);
long pre0 = mem.getLong(0);
// Corrupt PreLongs
tryBadMem(mem, PREAMBLE_LONGS_BYTE, 2);
// restore
mem.putLong(0, pre0);
// Corrupt SerVer
tryBadMem(mem, SER_VER_BYTE, 2);
// restore
mem.putLong(0, pre0);
// Corrupt Family
tryBadMem(mem, FAMILY_BYTE, 2);
// restore
mem.putLong(0, pre0);
// Corrupt READ_ONLY to true
tryBadMem(mem, FLAGS_BYTE, 2);
// restore
mem.putLong(0, pre0);
final long origThetaLong = mem.getLong(THETA_LONG);
try {
// Corrupt the theta value
mem.putLong(THETA_LONG, Long.MAX_VALUE / 2);
HeapAlphaSketch.heapifyInstance(mem, DEFAULT_UPDATE_SEED);
fail();
} catch (SketchesArgumentException e) {
// expected
}
// restore theta
mem.putLong(THETA_LONG, origThetaLong);
byte[] byteArray2 = new byte[bytearray1.length - 1];
WritableMemory mem2 = WritableMemory.writableWrap(byteArray2);
mem.copyTo(0, mem2, 0, mem2.getCapacity());
try {
HeapAlphaSketch.heapifyInstance(mem2, DEFAULT_UPDATE_SEED);
fail();
} catch (SketchesArgumentException e) {
// expected
}
// force ResizeFactor.X1, and allocated capacity too small
insertLgResizeFactor(mem, ResizeFactor.X1.lg());
UpdateSketch usk = HeapAlphaSketch.heapifyInstance(mem, DEFAULT_UPDATE_SEED);
ResizeFactor rf = usk.getResizeFactor();
// ResizeFactor recovered to X2, which always works.
assertEquals(rf, ResizeFactor.X2);
}
Aggregations