Search in sources :

Example 1 with ArrayOfLongsSerDe

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);
}
Also used : ArrayOfLongsSerDe(org.apache.datasketches.ArrayOfLongsSerDe) Test(org.testng.annotations.Test)

Example 2 with ArrayOfLongsSerDe

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
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) ArrayOfLongsSerDe(org.apache.datasketches.ArrayOfLongsSerDe)

Example 3 with ArrayOfLongsSerDe

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);
}
Also used : ArrayOfLongsSerDe(org.apache.datasketches.ArrayOfLongsSerDe) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 4 with ArrayOfLongsSerDe

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();
}
Also used : ArrayOfLongsSerDe(org.apache.datasketches.ArrayOfLongsSerDe) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 5 with ArrayOfLongsSerDe

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();
}
Also used : ArrayOfLongsSerDe(org.apache.datasketches.ArrayOfLongsSerDe) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Aggregations

ArrayOfLongsSerDe (org.apache.datasketches.ArrayOfLongsSerDe)31 Test (org.testng.annotations.Test)28 WritableMemory (org.apache.datasketches.memory.WritableMemory)23 Memory (org.apache.datasketches.memory.Memory)14 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)4