use of com.yahoo.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.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, 2);
//restore
mem.putLong(0, pre0);
//Corrupt READ_ONLY to true
tryBadMem(mem, FLAGS_BYTE, 2);
//restore
mem.putLong(0, pre0);
try {
//Corrupt the theta value
mem.putDouble(16, 0.5);
HeapAlphaSketch.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 {
HeapAlphaSketch.heapifyInstance(mem2, DEFAULT_UPDATE_SEED);
fail();
} catch (SketchesArgumentException e) {
//expected
}
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkUpdateMemorySpecialCases.
@Test
public void checkUpdateMemorySpecialCases() {
//4096
int lgK = 12;
int k = 1 << lgK;
UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
CompactSketch usk1c = usk1.compact(true, null);
WritableMemory v3mem1 = WritableMemory.wrap(usk1c.toByteArray());
Memory v1mem1 = convertSerV3toSerV1(v3mem1);
//union memory
WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
union.update(v1mem1);
CompactSketch cOut = union.getResult(true, null);
assertEquals(cOut.getEstimate(), 0.0, 0.0);
Memory v2mem1 = convertSerV3toSerV2(v3mem1);
//union memory
uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
union.update(v2mem1);
cOut = union.getResult(true, null);
assertEquals(cOut.getEstimate(), 0.0, 0.0);
//union memory
uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
union.update(v3mem1);
cOut = union.getResult(true, null);
assertEquals(cOut.getEstimate(), 0.0, 0.0);
//union memory
uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
v3mem1 = null;
union.update(v3mem1);
cOut = union.getResult(true, null);
assertEquals(cOut.getEstimate(), 0.0, 0.0);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkUpdateMemorySpecialCases2.
@Test
public void checkUpdateMemorySpecialCases2() {
//4096
int lgK = 12;
int k = 1 << lgK;
int u = 2 * k;
UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
//force prelongs to 3
for (int i = 0; i < u; i++) usk1.update(i);
CompactSketch usk1c = usk1.compact(true, null);
WritableMemory v3mem1 = WritableMemory.wrap(usk1c.toByteArray());
//union memory
WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
union.update(v3mem1);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkEmptyUnionCompactResult.
@Test
public void checkEmptyUnionCompactResult() {
int k = 64;
//union memory
WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
WritableMemory mem = WritableMemory.wrap(new byte[Sketch.getMaxCompactSketchBytes(0)]);
//DirectCompactSketch
CompactSketch csk = union.getResult(false, mem);
assertTrue(csk.isEmpty());
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkMemBadSerVer.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkMemBadSerVer() {
//4096
int lgK = 12;
int k = 1 << lgK;
UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
CompactSketch usk1c = usk1.compact(true, null);
WritableMemory v3mem1 = WritableMemory.wrap(usk1c.toByteArray());
//corrupt SerVer
v3mem1.putByte(SER_VER_BYTE, (byte) 0);
//union memory
WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
union.update(v3mem1);
}
Aggregations