Search in sources :

Example 26 with ArrayOfLongsSerDe

use of com.yahoo.sketches.ArrayOfLongsSerDe in project sketches-core by DataSketches.

the class ItemsSketchTest method serializeLongDeserialize.

@Test
public void serializeLongDeserialize() {
    ItemsSketch<Long> sketch1 = new ItemsSketch<Long>(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);
}
Also used : ArrayOfLongsSerDe(com.yahoo.sketches.ArrayOfLongsSerDe) Test(org.testng.annotations.Test)

Example 27 with ArrayOfLongsSerDe

use of com.yahoo.sketches.ArrayOfLongsSerDe in project sketches-core by DataSketches.

the class ItemsSketchTest method tryBadMem.

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
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) ArrayOfLongsSerDe(com.yahoo.sketches.ArrayOfLongsSerDe)

Aggregations

ArrayOfLongsSerDe (com.yahoo.sketches.ArrayOfLongsSerDe)27 Test (org.testng.annotations.Test)24 WritableMemory (com.yahoo.memory.WritableMemory)21 Memory (com.yahoo.memory.Memory)11 SketchesArgumentException (com.yahoo.sketches.SketchesArgumentException)4