Search in sources :

Example 21 with ArrayOfLongsSerDe

use of com.yahoo.sketches.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(com.yahoo.sketches.ArrayOfLongsSerDe) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 22 with ArrayOfLongsSerDe

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

the class VarOptItemsSketchTest method checkFullSketchSerialization.

@Test
public void checkFullSketchSerialization() {
    final VarOptItemsSketch<Long> sketch = VarOptItemsSketch.newInstance(32);
    for (long i = 0; i < 32; ++i) {
        sketch.update(i, 1.0);
    }
    sketch.update(100L, 100.0);
    sketch.update(101L, 101.0);
    assertEquals(sketch.getNumSamples(), 32);
    // first 2 entries should be heavy and in heap order (smallest at root)
    final VarOptItemsSamples<Long> samples = sketch.getSketchSamples();
    final Long[] data = samples.items();
    final double[] weights = samples.weights();
    assertEquals(weights[0], 100.0);
    assertEquals(weights[1], 101.0);
    assertEquals((long) data[0], 100L);
    assertEquals((long) data[1], 101L);
    final byte[] bytes = sketch.toByteArray(new ArrayOfLongsSerDe());
    final Memory mem = Memory.wrap(bytes);
    // ensure 3 preLongs
    assertEquals(PreambleUtil.extractPreLongs(mem), Family.VAROPT.getMaxPreLongs());
    final VarOptItemsSketch<Long> rebuilt = VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
    checkIfEqual(rebuilt, sketch);
}
Also used : ArrayOfLongsSerDe(com.yahoo.sketches.ArrayOfLongsSerDe) Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 23 with ArrayOfLongsSerDe

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

the class VarOptItemsSketchTest method checkBadSerVer.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadSerVer() {
    final VarOptItemsSketch<Long> sketch = getUnweightedLongsVIS(16, 16);
    final byte[] bytes = sketch.toByteArray(new ArrayOfLongsSerDe());
    final WritableMemory mem = WritableMemory.wrap(bytes);
    // corrupt the serialization version
    mem.putByte(SER_VER_BYTE, (byte) 0);
    VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
    fail();
}
Also used : ArrayOfLongsSerDe(com.yahoo.sketches.ArrayOfLongsSerDe) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 24 with ArrayOfLongsSerDe

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

the class VarOptItemsUnionTest method checkBadSerVer.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadSerVer() {
    final int k = 25;
    final int n = 30;
    final VarOptItemsUnion<Long> union = VarOptItemsUnion.newInstance(k);
    union.update(getUnweightedLongsVIS(k, n));
    final byte[] bytes = union.toByteArray(new ArrayOfLongsSerDe());
    final WritableMemory mem = WritableMemory.wrap(bytes);
    // corrupt the serialization version
    mem.putByte(SER_VER_BYTE, (byte) 0);
    VarOptItemsUnion.heapify(mem, new ArrayOfLongsSerDe());
    fail();
}
Also used : ArrayOfLongsSerDe(com.yahoo.sketches.ArrayOfLongsSerDe) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 25 with ArrayOfLongsSerDe

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

the class ItemsSketchTest method checkMemExceptions.

@Test
public void checkMemExceptions() {
    ItemsSketch<Long> sk1 = new ItemsSketch<Long>(1 << LG_MIN_MAP_SIZE);
    sk1.update(Long.valueOf(1), 1);
    ArrayOfLongsSerDe serDe = new ArrayOfLongsSerDe();
    byte[] byteArr = sk1.toByteArray(serDe);
    WritableMemory mem = WritableMemory.wrap(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(com.yahoo.sketches.ArrayOfLongsSerDe) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

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