use of com.yahoo.memory.Memory in project sketches-core by DataSketches.
the class ReservoirLongsUnionTest method checkInstantiation.
@Test
public void checkInstantiation() {
final int n = 100;
final int k = 25;
// create empty unions
ReservoirLongsUnion rlu = ReservoirLongsUnion.newInstance(k);
assertNull(rlu.getResult());
rlu.update(5);
assertNotNull(rlu.getResult());
// pass in a sketch, as both an object and memory
final ReservoirLongsSketch rls = ReservoirLongsSketch.newInstance(k);
for (int i = 0; i < n; ++i) {
rls.update(i);
}
rlu.reset();
assertEquals(rlu.getResult().getN(), 0);
rlu.update(rls);
assertEquals(rlu.getResult().getN(), rls.getN());
final byte[] sketchBytes = rls.toByteArray();
final Memory mem = Memory.wrap(sketchBytes);
rlu = ReservoirLongsUnion.newInstance(rls.getK());
rlu.update(mem);
assertNotNull(rlu.getResult());
println(rlu.toString());
}
use of com.yahoo.memory.Memory 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.Memory in project sketches-core by DataSketches.
the class VarOptItemsSketchTest method checkEndOfWarmupSketchSerialization.
@Test
public void checkEndOfWarmupSketchSerialization() {
final int k = 2048;
final VarOptItemsSketch<Long> sketch = getUnweightedLongsVIS(k, k);
final byte[] bytes = sketch.toByteArray(new ArrayOfLongsSerDe());
final Memory mem = Memory.wrap(bytes);
// ensure still only 2 preLongs
assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_WARMUP_PRELONGS);
final VarOptItemsSketch<Long> rebuilt = VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
checkIfEqual(rebuilt, sketch);
}
use of com.yahoo.memory.Memory in project sketches-core by DataSketches.
the class VarOptItemsSketchTest method checkUnderFullSketchSerialization.
@Test
public void checkUnderFullSketchSerialization() {
final VarOptItemsSketch<Long> sketch = VarOptItemsSketch.newInstance(2048);
for (long i = 0; i < 10; ++i) {
sketch.update(i, 1.0);
}
assertEquals(sketch.getNumSamples(), 10);
final byte[] bytes = sketch.toByteArray(new ArrayOfLongsSerDe());
final Memory mem = Memory.wrap(bytes);
// ensure correct number of preLongs
assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_WARMUP_PRELONGS);
final VarOptItemsSketch<Long> rebuilt = VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
checkIfEqual(rebuilt, sketch);
}
use of com.yahoo.memory.Memory in project sketches-core by DataSketches.
the class VarOptItemsSketchTest method checkBadMemory.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadMemory() {
byte[] bytes = new byte[4];
Memory mem = Memory.wrap(bytes);
try {
PreambleUtil.getAndCheckPreLongs(mem);
fail();
} catch (final SketchesArgumentException e) {
// expected
}
bytes = new byte[8];
// only 1 preLong worth of items in bytearray
bytes[0] = 2;
mem = Memory.wrap(bytes);
PreambleUtil.getAndCheckPreLongs(mem);
}
Aggregations