use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkResizeInBigMem.
//checks Alex's bug where lgArrLongs > lgNomLongs +1.
@Test
public void checkResizeInBigMem() {
int k = 1 << 14;
int u = 1 << 20;
WritableMemory mem = WritableMemory.wrap(new byte[8 * k * 16 + 24]);
UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(mem);
for (int i = 0; i < u; i++) {
sketch.update(i);
}
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class ReservoirLongsUnionTest method checkBadPreLongs.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadPreLongs() {
final ReservoirLongsUnion rlu = ReservoirLongsUnion.newInstance(1024);
final WritableMemory mem = WritableMemory.wrap(rlu.toByteArray());
// corrupt the preLongs count
mem.putByte(PREAMBLE_LONGS_BYTE, (byte) 0);
ReservoirLongsUnion.heapify(mem);
fail();
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class ReservoirLongsUnionTest method checkBadFamily.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadFamily() {
final ReservoirLongsUnion rlu = ReservoirLongsUnion.newInstance(1024);
final WritableMemory mem = WritableMemory.wrap(rlu.toByteArray());
// corrupt the family ID
mem.putByte(FAMILY_BYTE, (byte) 0);
ReservoirLongsUnion.heapify(mem);
fail();
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class VarOptItemsSketchTest method checkMalformedPreamble.
@Test
public void checkMalformedPreamble() {
final int k = 50;
final VarOptItemsSketch<Long> sketch = getUnweightedLongsVIS(k, k);
final byte[] sketchBytes = sketch.toByteArray(new ArrayOfLongsSerDe());
final Memory srcMem = Memory.wrap(sketchBytes);
// we'll use the same initial sketch a few times, so grab a copy of it
final byte[] copyBytes = new byte[sketchBytes.length];
final WritableMemory mem = WritableMemory.wrap(copyBytes);
// may be null
final Object memObj = mem.getArray();
final long memAddr = mem.getCumulativeOffset(0L);
// copy the bytes
srcMem.copyTo(0, mem, 0, sketchBytes.length);
assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_WARMUP_PRELONGS);
// no items in R but max preLongs
try {
PreambleUtil.insertPreLongs(memObj, memAddr, Family.VAROPT.getMaxPreLongs());
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().startsWith("Possible Corruption: " + Family.VAROPT.getMaxPreLongs() + " preLongs but"));
}
// refresh the copy
srcMem.copyTo(0, mem, 0, sketchBytes.length);
assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_WARMUP_PRELONGS);
// negative H region count
try {
PreambleUtil.insertHRegionItemCount(memObj, memAddr, -1);
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().equals("Possible Corruption: H region count cannot be negative: -1"));
}
// refresh the copy
srcMem.copyTo(0, mem, 0, sketchBytes.length);
assertEquals(PreambleUtil.extractHRegionItemCount(mem), k);
// negative R region count
try {
PreambleUtil.insertRRegionItemCount(memObj, memAddr, -128);
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().equals("Possible Corruption: R region count cannot be negative: -128"));
}
// refresh the copy
srcMem.copyTo(0, mem, 0, sketchBytes.length);
assertEquals(PreambleUtil.extractRRegionItemCount(mem), 0);
// invalid k < 2
try {
PreambleUtil.insertK(memObj, memAddr, 0);
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().equals("Possible Corruption: k must be at least 2: 0"));
}
// refresh the copy
srcMem.copyTo(0, mem, 0, sketchBytes.length);
assertEquals(PreambleUtil.extractK(mem), k);
// invalid n < 0
try {
PreambleUtil.insertN(memObj, memAddr, -1024);
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().equals("Possible Corruption: n cannot be negative: -1024"));
}
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class VarOptItemsSketchTest method checkBadPreLongs.
@Test
public void checkBadPreLongs() {
final VarOptItemsSketch<Long> sketch = getUnweightedLongsVIS(32, 33);
final byte[] bytes = sketch.toByteArray(new ArrayOfLongsSerDe());
final WritableMemory mem = WritableMemory.wrap(bytes);
// corrupt the preLongs count to 0
mem.putByte(PREAMBLE_LONGS_BYTE, (byte) (Family.VAROPT.getMinPreLongs() - 1));
try {
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
// expected
}
// corrupt the preLongs count to 2
mem.putByte(PREAMBLE_LONGS_BYTE, (byte) 2);
try {
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
// expected
}
// corrupt the preLongs count to be too large
mem.putByte(PREAMBLE_LONGS_BYTE, (byte) (Family.VAROPT.getMaxPreLongs() + 1));
try {
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
// expected
}
}
Aggregations