use of org.apache.datasketches.ArrayOfLongsSerDe in project sketches-core by DataSketches.
the class ItemsSketchTest method serializeLongDeserialize.
@Test
public void serializeLongDeserialize() {
ItemsSketch<Long> sketch1 = new ItemsSketch<>(1 << LG_MIN_MAP_SIZE);
sketch1.update(1L);
sketch1.update(2L);
sketch1.update(3L);
sketch1.update(4L);
String s = sketch1.toString();
println(s);
byte[] bytes = sketch1.toByteArray(new ArrayOfLongsSerDe());
ItemsSketch<Long> sketch2 = ItemsSketch.getInstance(Memory.wrap(bytes), new ArrayOfLongsSerDe());
sketch2.update(2L);
sketch2.update(3L);
sketch2.update(2L);
Assert.assertFalse(sketch2.isEmpty());
Assert.assertEquals(sketch2.getNumActiveItems(), 4);
Assert.assertEquals(sketch2.getStreamLength(), 7);
Assert.assertEquals(sketch2.getEstimate(1L), 1);
Assert.assertEquals(sketch2.getEstimate(2L), 3);
Assert.assertEquals(sketch2.getEstimate(3L), 2);
Assert.assertEquals(sketch2.getEstimate(4L), 1);
}
use of org.apache.datasketches.ArrayOfLongsSerDe in project sketches-core by DataSketches.
the class ItemsSketchTest method tryBadMem.
// Restricted methods
private static void tryBadMem(WritableMemory mem, int byteOffset, int byteValue) {
ArrayOfLongsSerDe serDe = new ArrayOfLongsSerDe();
try {
// Corrupt
mem.putByte(byteOffset, (byte) byteValue);
ItemsSketch.getInstance(mem, serDe);
fail();
} catch (SketchesArgumentException e) {
// expected
}
}
use of org.apache.datasketches.ArrayOfLongsSerDe in project sketches-core by DataSketches.
the class ItemsSketchTest method checkMemExceptions.
@Test
public void checkMemExceptions() {
ItemsSketch<Long> sk1 = new ItemsSketch<>(1 << LG_MIN_MAP_SIZE);
sk1.update(Long.valueOf(1), 1);
ArrayOfLongsSerDe serDe = new ArrayOfLongsSerDe();
byte[] byteArr = sk1.toByteArray(serDe);
WritableMemory mem = WritableMemory.writableWrap(byteArr);
// FrequentItemsSketch<Long> sk2 = FrequentItemsSketch.getInstance(mem, serDe);
// println(sk2.toString());
// The correct first 8 bytes.
long pre0 = mem.getLong(0);
// Now start corrupting
// Corrupt
tryBadMem(mem, PREAMBLE_LONGS_BYTE, 2);
// restore
mem.putLong(0, pre0);
// Corrupt
tryBadMem(mem, SER_VER_BYTE, 2);
// restore
mem.putLong(0, pre0);
// Corrupt
tryBadMem(mem, FAMILY_BYTE, 2);
// restore
mem.putLong(0, pre0);
// Corrupt to true
tryBadMem(mem, FLAGS_BYTE, 4);
// restore
mem.putLong(0, pre0);
}
use of org.apache.datasketches.ArrayOfLongsSerDe in project sketches-core by DataSketches.
the class ReservoirItemsSketchTest method checkBadSerVer.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadSerVer() {
final WritableMemory mem = getBasicSerializedLongsRIS();
// corrupt the serialization version
mem.putByte(SER_VER_BYTE, (byte) 0);
ReservoirItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
}
use of org.apache.datasketches.ArrayOfLongsSerDe in project sketches-core by DataSketches.
the class ReservoirItemsSketchTest method checkBadPreLongs.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadPreLongs() {
final WritableMemory mem = getBasicSerializedLongsRIS();
// corrupt the preLongs count
mem.putByte(PREAMBLE_LONGS_BYTE, (byte) 0);
ReservoirItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
}
Aggregations