use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class PreambleUtilTest method checkStreamErrors.
@Test
public void checkStreamErrors() {
WritableMemory wmem = WritableMemory.allocate(4 * 10);
putEmptyMerged(wmem, (byte) 12, defaultSeedHash);
try {
getSvStreamOffset(wmem);
fail();
} catch (SketchesArgumentException e) {
}
wmem.putByte(5, (byte) (7 << 2));
try {
getSvStreamOffset(wmem);
fail();
} catch (SketchesStateException e) {
}
wmem.putByte(5, (byte) 0);
try {
getWStreamOffset(wmem);
fail();
} catch (SketchesArgumentException e) {
}
wmem.putByte(5, (byte) (7 << 2));
try {
getWStreamOffset(wmem);
fail();
} catch (SketchesStateException e) {
}
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class PreambleUtilTest method checkEmptyMemory.
@Test
public void checkEmptyMemory() {
WritableMemory wmem = WritableMemory.allocate(4 * 10);
// legal Family
wmem.putByte(2, (byte) 16);
// select NONE
wmem.putByte(5, (byte) (1 << 2));
println(CpcSketch.toString(wmem, false));
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class LongsSketchTest method frequentItemsByteSerialTest.
@Test
public void frequentItemsByteSerialTest() {
// Empty Sketch
LongsSketch sketch = new LongsSketch(16);
byte[] bytearray0 = sketch.toByteArray();
WritableMemory mem0 = WritableMemory.writableWrap(bytearray0);
LongsSketch new_sketch0 = LongsSketch.getInstance(mem0);
String str0 = LongsSketch.toString(mem0);
println(str0);
String string0 = sketch.serializeToString();
String new_string0 = new_sketch0.serializeToString();
Assert.assertTrue(string0.equals(new_string0));
LongsSketch sketch2 = new LongsSketch(128);
sketch.update(10, 100);
sketch.update(10, 100);
sketch.update(15, 3443);
sketch.update(1000001, 1010230);
sketch.update(1000002, 1010230);
byte[] bytearray1 = sketch.toByteArray();
Memory mem1 = Memory.wrap(bytearray1);
LongsSketch new_sketch1 = LongsSketch.getInstance(mem1);
String str1 = LongsSketch.toString(bytearray1);
println(str1);
String string1 = sketch.serializeToString();
String new_string1 = new_sketch1.serializeToString();
Assert.assertTrue(string1.equals(new_string1));
Assert.assertTrue(new_sketch1.getMaximumMapCapacity() == sketch.getMaximumMapCapacity());
Assert.assertTrue(new_sketch1.getCurrentMapCapacity() == sketch.getCurrentMapCapacity());
sketch2.update(190, 12902390);
sketch2.update(191, 12902390);
sketch2.update(192, 12902390);
sketch2.update(193, 12902390);
sketch2.update(194, 12902390);
sketch2.update(195, 12902390);
sketch2.update(196, 12902390);
sketch2.update(197, 12902390);
sketch2.update(198, 12902390);
sketch2.update(199, 12902390);
sketch2.update(200, 12902390);
sketch2.update(201, 12902390);
sketch2.update(202, 12902390);
sketch2.update(203, 12902390);
sketch2.update(204, 12902390);
sketch2.update(205, 12902390);
sketch2.update(206, 12902390);
sketch2.update(207, 12902390);
sketch2.update(208, 12902390);
byte[] bytearray2 = sketch2.toByteArray();
Memory mem2 = Memory.wrap(bytearray2);
LongsSketch new_sketch2 = LongsSketch.getInstance(mem2);
String string2 = sketch2.serializeToString();
String new_string2 = new_sketch2.serializeToString();
Assert.assertTrue(string2.equals(new_string2));
Assert.assertTrue(new_sketch2.getMaximumMapCapacity() == sketch2.getMaximumMapCapacity());
Assert.assertTrue(new_sketch2.getCurrentMapCapacity() == sketch2.getCurrentMapCapacity());
Assert.assertTrue(new_sketch2.getStreamLength() == sketch2.getStreamLength());
LongsSketch merged_sketch = sketch.merge(sketch2);
byte[] bytearray = sketch.toByteArray();
Memory mem = Memory.wrap(bytearray);
LongsSketch new_sketch = LongsSketch.getInstance(mem);
String string = sketch.serializeToString();
String new_string = new_sketch.serializeToString();
Assert.assertTrue(string.equals(new_string));
Assert.assertTrue(new_sketch.getMaximumMapCapacity() == merged_sketch.getMaximumMapCapacity());
Assert.assertTrue(new_sketch.getCurrentMapCapacity() == merged_sketch.getCurrentMapCapacity());
Assert.assertTrue(new_sketch.getStreamLength() == merged_sketch.getStreamLength());
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class LongsSketchTest method checkGetInstanceMemory.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkGetInstanceMemory() {
WritableMemory mem = WritableMemory.writableWrap(new byte[4]);
LongsSketch.getInstance(mem);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DoublesUnionImplTest method checkWrapInstance.
@Test
public void checkWrapInstance() {
final int k = 128;
final int n = 1000;
final UpdateDoublesSketch sketch = DoublesSketch.builder().setK(k).build();
for (int i = 1; i <= n; i++) {
sketch.update(i);
}
final double skMedian = sketch.getQuantile(.5);
Assert.assertEquals(skMedian, 500, 10);
final byte[] byteArr = sketch.toByteArray(false);
final WritableMemory mem = WritableMemory.writableWrap(byteArr);
final DoublesUnion union = DoublesUnion.wrap(mem);
Assert.assertFalse(union.isEmpty());
assertTrue(union.isDirect());
final DoublesSketch sketch2 = union.getResult();
final double uMedian = sketch2.getQuantile(0.5);
Assert.assertEquals(skMedian, uMedian, 0.0);
// check serializing again
final byte[] bytesOut = union.toByteArray();
assertEquals(bytesOut.length, byteArr.length);
// wrapped, so should be exact
assertEquals(bytesOut, byteArr);
}
Aggregations