Search in sources :

Example 56 with WritableMemory

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

the class DirectQuickSelectSketchTest method checkResizeInBigMem.

//checks Alex's bug where lgArrLongs > lgNomLongs +1.
@Test
public void checkResizeInBigMem() {
    int k = 1 << 14;
    int u = 1 << 20;
    WritableMemory mem = WritableMemory.wrap(new byte[8 * k * 16 + 24]);
    UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(mem);
    for (int i = 0; i < u; i++) {
        sketch.update(i);
    }
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 57 with WritableMemory

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

the class ReservoirLongsUnionTest method checkBadPreLongs.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadPreLongs() {
    final ReservoirLongsUnion rlu = ReservoirLongsUnion.newInstance(1024);
    final WritableMemory mem = WritableMemory.wrap(rlu.toByteArray());
    // corrupt the preLongs count
    mem.putByte(PREAMBLE_LONGS_BYTE, (byte) 0);
    ReservoirLongsUnion.heapify(mem);
    fail();
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 58 with WritableMemory

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

the class ReservoirLongsUnionTest method checkBadFamily.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadFamily() {
    final ReservoirLongsUnion rlu = ReservoirLongsUnion.newInstance(1024);
    final WritableMemory mem = WritableMemory.wrap(rlu.toByteArray());
    // corrupt the family ID
    mem.putByte(FAMILY_BYTE, (byte) 0);
    ReservoirLongsUnion.heapify(mem);
    fail();
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 59 with WritableMemory

use of com.yahoo.memory.WritableMemory 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 60 with WritableMemory

use of com.yahoo.memory.WritableMemory 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)

Aggregations

WritableMemory (com.yahoo.memory.WritableMemory)264 Test (org.testng.annotations.Test)210 Memory (com.yahoo.memory.Memory)34 SketchesArgumentException (com.yahoo.sketches.SketchesArgumentException)15 ArrayOfLongsSerDe (com.yahoo.sketches.ArrayOfLongsSerDe)11 ArrayOfStringsSerDe (com.yahoo.sketches.ArrayOfStringsSerDe)6 WritableDirectHandle (com.yahoo.memory.WritableDirectHandle)5 MemoryRequestServer (com.yahoo.memory.MemoryRequestServer)2 ArrayOfDoublesSerDe (com.yahoo.sketches.ArrayOfDoublesSerDe)2 ArrayOfNumbersSerDe (com.yahoo.sketches.ArrayOfNumbersSerDe)2 PreambleUtil.extractTgtHllType (com.yahoo.sketches.hll.PreambleUtil.extractTgtHllType)2 ResizeFactor (com.yahoo.sketches.ResizeFactor)1 PreambleUtil.extractCurMode (com.yahoo.sketches.hll.PreambleUtil.extractCurMode)1 PreambleUtil.insertTgtHllType (com.yahoo.sketches.hll.PreambleUtil.insertTgtHllType)1 Util.checkIsCompactMemory (com.yahoo.sketches.quantiles.Util.checkIsCompactMemory)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ByteBuffer (java.nio.ByteBuffer)1