Search in sources :

Example 1 with ArrayOfLongsSerDe

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

the class VarOptItemsSketchTest method checkMalformedPreamble.

@Test
public void checkMalformedPreamble() {
    final int k = 50;
    final VarOptItemsSketch<Long> sketch = getUnweightedLongsVIS(k, k);
    final byte[] sketchBytes = sketch.toByteArray(new ArrayOfLongsSerDe());
    final Memory srcMem = Memory.wrap(sketchBytes);
    // we'll use the same initial sketch a few times, so grab a copy of it
    final byte[] copyBytes = new byte[sketchBytes.length];
    final WritableMemory mem = WritableMemory.wrap(copyBytes);
    // may be null
    final Object memObj = mem.getArray();
    final long memAddr = mem.getCumulativeOffset(0L);
    // copy the bytes
    srcMem.copyTo(0, mem, 0, sketchBytes.length);
    assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_WARMUP_PRELONGS);
    // no items in R but max preLongs
    try {
        PreambleUtil.insertPreLongs(memObj, memAddr, Family.VAROPT.getMaxPreLongs());
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
        assertTrue(e.getMessage().startsWith("Possible Corruption: " + Family.VAROPT.getMaxPreLongs() + " preLongs but"));
    }
    // refresh the copy
    srcMem.copyTo(0, mem, 0, sketchBytes.length);
    assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_WARMUP_PRELONGS);
    // negative H region count
    try {
        PreambleUtil.insertHRegionItemCount(memObj, memAddr, -1);
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
        assertTrue(e.getMessage().equals("Possible Corruption: H region count cannot be negative: -1"));
    }
    // refresh the copy
    srcMem.copyTo(0, mem, 0, sketchBytes.length);
    assertEquals(PreambleUtil.extractHRegionItemCount(mem), k);
    // negative R region count
    try {
        PreambleUtil.insertRRegionItemCount(memObj, memAddr, -128);
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
        assertTrue(e.getMessage().equals("Possible Corruption: R region count cannot be negative: -128"));
    }
    // refresh the copy
    srcMem.copyTo(0, mem, 0, sketchBytes.length);
    assertEquals(PreambleUtil.extractRRegionItemCount(mem), 0);
    // invalid k < 2
    try {
        PreambleUtil.insertK(memObj, memAddr, 0);
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
        assertTrue(e.getMessage().equals("Possible Corruption: k must be at least 2: 0"));
    }
    // refresh the copy
    srcMem.copyTo(0, mem, 0, sketchBytes.length);
    assertEquals(PreambleUtil.extractK(mem), k);
    // invalid n < 0
    try {
        PreambleUtil.insertN(memObj, memAddr, -1024);
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
        assertTrue(e.getMessage().equals("Possible Corruption: n cannot be negative: -1024"));
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) ArrayOfLongsSerDe(com.yahoo.sketches.ArrayOfLongsSerDe) Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 2 with ArrayOfLongsSerDe

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

the class VarOptItemsSketchTest method checkBadPreLongs.

@Test
public void checkBadPreLongs() {
    final VarOptItemsSketch<Long> sketch = getUnweightedLongsVIS(32, 33);
    final byte[] bytes = sketch.toByteArray(new ArrayOfLongsSerDe());
    final WritableMemory mem = WritableMemory.wrap(bytes);
    // corrupt the preLongs count to 0
    mem.putByte(PREAMBLE_LONGS_BYTE, (byte) (Family.VAROPT.getMinPreLongs() - 1));
    try {
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
    // corrupt the preLongs count to 2
    mem.putByte(PREAMBLE_LONGS_BYTE, (byte) 2);
    try {
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
    // corrupt the preLongs count to be too large
    mem.putByte(PREAMBLE_LONGS_BYTE, (byte) (Family.VAROPT.getMaxPreLongs() + 1));
    try {
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) ArrayOfLongsSerDe(com.yahoo.sketches.ArrayOfLongsSerDe) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 3 with ArrayOfLongsSerDe

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

the class VarOptItemsSketchTest method checkEndOfWarmupSketchSerialization.

@Test
public void checkEndOfWarmupSketchSerialization() {
    final int k = 2048;
    final VarOptItemsSketch<Long> sketch = getUnweightedLongsVIS(k, k);
    final byte[] bytes = sketch.toByteArray(new ArrayOfLongsSerDe());
    final Memory mem = Memory.wrap(bytes);
    // ensure still only 2 preLongs
    assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_WARMUP_PRELONGS);
    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 4 with ArrayOfLongsSerDe

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

the class VarOptItemsSketchTest method checkUnderFullSketchSerialization.

@Test
public void checkUnderFullSketchSerialization() {
    final VarOptItemsSketch<Long> sketch = VarOptItemsSketch.newInstance(2048);
    for (long i = 0; i < 10; ++i) {
        sketch.update(i, 1.0);
    }
    assertEquals(sketch.getNumSamples(), 10);
    final byte[] bytes = sketch.toByteArray(new ArrayOfLongsSerDe());
    final Memory mem = Memory.wrap(bytes);
    // ensure correct number of preLongs
    assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_WARMUP_PRELONGS);
    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 5 with ArrayOfLongsSerDe

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

the class VarOptItemsSketchTest method checkBadFamily.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadFamily() {
    final VarOptItemsSketch<Long> sketch = getUnweightedLongsVIS(32, 16);
    final byte[] bytes = sketch.toByteArray(new ArrayOfLongsSerDe());
    final WritableMemory mem = WritableMemory.wrap(bytes);
    // corrupt the family ID
    mem.putByte(FAMILY_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)

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