use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class ForwardCompatibilityTest method checkSerVer1_32Bytes_tooSmall.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkSerVer1_32Bytes_tooSmall() {
byte[] byteArray = new byte[32];
WritableMemory mem = WritableMemory.wrap(byteArray);
//mdLongs
mem.putByte(0, (byte) 3);
//SerVer
mem.putByte(1, (byte) 1);
//SketchType = SetSketch
mem.putByte(2, (byte) 3);
//byte 3 lgNomLongs not used with SetSketch
//byte 4 lgArrLongs not used with SetSketch
//byte 5 lgRR not used with SetSketch
//byte 6: Flags: b0: BigEnd, b1: ReadOnly
mem.putByte(6, (byte) 2);
//byte 7 Not used
//curCount = 2
mem.putInt(8, 2);
mem.putLong(16, Long.MAX_VALUE);
Memory srcMem = Memory.wrap(byteArray);
Sketch.heapify(srcMem);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapQuickSelectSketchTest 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
HeapQuickSelectSketch sk1 = (HeapQuickSelectSketch) 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.wrap(byteArray);
//corrupt the Sketch ID byte
mem.putByte(FAMILY_BYTE, (byte) 0);
//try to heapify the corruped mem
Sketch.heapify(mem, seed);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapQuickSelectSketchTest method checkMemSerDeExceptions.
@Test
public void checkMemSerDeExceptions() {
int k = 1024;
UpdateSketch sk1 = UpdateSketch.builder().setFamily(QUICKSELECT).setNominalEntries(k).build();
//forces preLongs to 3
sk1.update(1L);
byte[] bytearray1 = sk1.toByteArray();
WritableMemory mem = WritableMemory.wrap(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, 1);
//restore
mem.putLong(0, pre0);
//Corrupt READ_ONLY to true
tryBadMem(mem, FLAGS_BYTE, 2);
//restore
mem.putLong(0, pre0);
//Corrupt, Family to Union
tryBadMem(mem, FAMILY_BYTE, 4);
try {
//Corrupt the theta value
mem.putDouble(16, 0.5);
HeapQuickSelectSketch.heapifyInstance(mem, DEFAULT_UPDATE_SEED);
fail();
} catch (SketchesArgumentException e) {
//expected
}
//restore theta
mem.putDouble(16, 1.0);
byte[] byteArray2 = new byte[bytearray1.length - 1];
WritableMemory mem2 = WritableMemory.wrap(byteArray2);
mem.copyTo(0, mem2, 0, mem2.getCapacity());
try {
HeapQuickSelectSketch.heapifyInstance(mem2, DEFAULT_UPDATE_SEED);
fail();
} catch (SketchesArgumentException e) {
//expected
}
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapQuickSelectSketchTest 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
HeapQuickSelectSketch sk1 = (HeapQuickSelectSketch) 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.wrap(byteArray);
//corrupt the SerVer byte
mem.putByte(SER_VER_BYTE, (byte) 0);
Sketch.heapify(mem, seed);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapQuickSelectSketchTest method checkHQStoCompactEmptyForms.
@Test
public void checkHQStoCompactEmptyForms() {
int k = 512;
UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setResizeFactor(X2).setNominalEntries(k).build();
println("lgArr: " + usk.getLgArrLongs());
//empty
usk.toString(false, true, 0, false);
boolean estimating = false;
assertEquals(usk.getClass().getSimpleName(), "HeapQuickSelectSketch");
double uskEst = usk.getEstimate();
double uskLB = usk.getLowerBound(2);
double uskUB = usk.getUpperBound(2);
int currentUSBytes = usk.getCurrentBytes(false);
// clumsy, but a function of RF and TCF
assertEquals(currentUSBytes, 32 * 8 + 24);
//compact form
int compBytes = usk.getCurrentBytes(true);
assertEquals(compBytes, 8);
assertEquals(usk.isEstimationMode(), estimating);
byte[] arr2 = new byte[compBytes];
WritableMemory mem2 = WritableMemory.wrap(arr2);
CompactSketch csk2 = usk.compact(false, mem2);
assertEquals(csk2.getEstimate(), uskEst);
assertEquals(csk2.getLowerBound(2), uskLB);
assertEquals(csk2.getUpperBound(2), uskUB);
assertEquals(csk2.isEmpty(), true);
assertEquals(csk2.isEstimationMode(), estimating);
assertEquals(csk2.getClass().getSimpleName(), "DirectCompactSketch");
CompactSketch csk3 = usk.compact(true, mem2);
csk3.toString(false, true, 0, false);
csk3.toString();
assertEquals(csk3.getEstimate(), uskEst);
assertEquals(csk3.getLowerBound(2), uskLB);
assertEquals(csk3.getUpperBound(2), uskUB);
assertEquals(csk3.isEmpty(), true);
assertEquals(csk3.isEstimationMode(), estimating);
assertEquals(csk3.getClass().getSimpleName(), "DirectCompactOrderedSketch");
}
Aggregations