Search in sources :

Example 11 with ResizeFactor

use of org.apache.datasketches.ResizeFactor in project sketches-core by DataSketches.

the class HeapAlphaSketchTest method checkResetAndStartingSubMultiple.

@Test
public void checkResetAndStartingSubMultiple() {
    int k = 1024;
    int u = 4 * k;
    UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setResizeFactor(X8).setNominalEntries(k).build();
    // for internal checks
    HeapAlphaSketch sk1 = (HeapAlphaSketch) usk;
    assertTrue(usk.isEmpty());
    for (int i = 0; i < u; i++) {
        usk.update(i);
    }
    assertEquals(1 << sk1.getLgArrLongs(), 2 * k);
    sk1.reset();
    ResizeFactor rf = sk1.getResizeFactor();
    int subMul = Util.startingSubMultiple(11, rf.lg(), 5);
    assertEquals(sk1.getLgArrLongs(), subMul);
    UpdateSketch usk2 = UpdateSketch.builder().setFamily(fam_).setResizeFactor(ResizeFactor.X1).setNominalEntries(k).build();
    sk1 = (HeapAlphaSketch) usk2;
    for (int i = 0; i < u; i++) {
        usk2.update(i);
    }
    assertEquals(1 << sk1.getLgArrLongs(), 2 * k);
    sk1.reset();
    rf = sk1.getResizeFactor();
    subMul = Util.startingSubMultiple(11, rf.lg(), 5);
    assertEquals(sk1.getLgArrLongs(), subMul);
    assertNull(sk1.getMemory());
    assertFalse(sk1.isOrdered());
}
Also used : ResizeFactor(org.apache.datasketches.ResizeFactor) PreambleUtil.insertLgResizeFactor(org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor) Test(org.testng.annotations.Test)

Example 12 with ResizeFactor

use of org.apache.datasketches.ResizeFactor in project sketches-core by DataSketches.

the class HeapQuickSelectSketchTest method checkResetAndStartingSubMultiple.

@Test
public void checkResetAndStartingSubMultiple() {
    int k = 1024;
    int u = 4 * k;
    UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setResizeFactor(X8).setNominalEntries(k).build();
    // for internal checks
    HeapQuickSelectSketch sk1 = (HeapQuickSelectSketch) usk;
    assertTrue(usk.isEmpty());
    for (int i = 0; i < u; i++) {
        usk.update(i);
    }
    assertEquals(1 << sk1.getLgArrLongs(), 2 * k);
    sk1.reset();
    ResizeFactor rf = sk1.getResizeFactor();
    // messy
    int subMul = Util.startingSubMultiple(11, rf.lg(), 5);
    assertEquals(sk1.getLgArrLongs(), subMul);
    UpdateSketch usk2 = UpdateSketch.builder().setFamily(fam_).setResizeFactor(ResizeFactor.X1).setNominalEntries(k).build();
    sk1 = (HeapQuickSelectSketch) usk2;
    for (int i = 0; i < u; i++) {
        usk2.update(i);
    }
    assertEquals(1 << sk1.getLgArrLongs(), 2 * k);
    sk1.reset();
    rf = sk1.getResizeFactor();
    // messy
    subMul = Util.startingSubMultiple(11, rf.lg(), 5);
    assertEquals(sk1.getLgArrLongs(), subMul);
    assertNull(sk1.getMemory());
    assertFalse(sk1.isOrdered());
}
Also used : ResizeFactor(org.apache.datasketches.ResizeFactor) PreambleUtil.insertLgResizeFactor(org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor) Test(org.testng.annotations.Test)

Example 13 with ResizeFactor

use of org.apache.datasketches.ResizeFactor in project sketches-core by DataSketches.

the class ReservoirLongsSketchTest method checkBadConstructorArgs.

@Test
public void checkBadConstructorArgs() {
    final long[] data = new long[128];
    for (int i = 0; i < 128; ++i) {
        data[i] = i;
    }
    final ResizeFactor rf = ResizeFactor.X8;
    // no items
    try {
        ReservoirLongsSketch.getInstance(null, 128, rf, 128);
        fail();
    } catch (final SketchesException e) {
        assertTrue(e.getMessage().contains("null reservoir"));
    }
    // size too small
    try {
        ReservoirLongsSketch.getInstance(data, 128, rf, 1);
        fail();
    } catch (final SketchesException e) {
        assertTrue(e.getMessage().contains("size less than 2"));
    }
    // configured reservoir size smaller than items length
    try {
        ReservoirLongsSketch.getInstance(data, 128, rf, 64);
        fail();
    } catch (final SketchesException e) {
        assertTrue(e.getMessage().contains("max size less than array length"));
    }
    // too many items seen vs items length, full sketch
    try {
        ReservoirLongsSketch.getInstance(data, 512, rf, 256);
        fail();
    } catch (final SketchesException e) {
        assertTrue(e.getMessage().contains("too few samples"));
    }
    // too many items seen vs items length, under-full sketch
    try {
        ReservoirLongsSketch.getInstance(data, 256, rf, 256);
        fail();
    } catch (final SketchesException e) {
        assertTrue(e.getMessage().contains("too few samples"));
    }
}
Also used : ResizeFactor(org.apache.datasketches.ResizeFactor) SketchesException(org.apache.datasketches.SketchesException) Test(org.testng.annotations.Test)

Example 14 with ResizeFactor

use of org.apache.datasketches.ResizeFactor in project sketches-core by DataSketches.

the class DirectQuickSelectSketchTest method checkCorruptRFWithInsufficientArray.

@Test
public void checkCorruptRFWithInsufficientArray() {
    // lgNomLongs = 10
    int k = 1024;
    int bytes = Sketches.getMaxUpdateSketchBytes(k);
    byte[] arr = new byte[bytes];
    WritableMemory mem = WritableMemory.writableWrap(arr);
    // 3
    ResizeFactor rf = ResizeFactor.X8;
    UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).setResizeFactor(rf).build(mem);
    usk.update(0);
    // corrupt RF: X1
    insertLgResizeFactor(mem, 0);
    UpdateSketch dqss = DirectQuickSelectSketch.writableWrap(mem, DEFAULT_UPDATE_SEED);
    // force-promote to X2
    assertEquals(dqss.getResizeFactor(), ResizeFactor.X2);
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) ResizeFactor(org.apache.datasketches.ResizeFactor) PreambleUtil.insertLgResizeFactor(org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor) Test(org.testng.annotations.Test)

Example 15 with ResizeFactor

use of org.apache.datasketches.ResizeFactor in project sketches-core by DataSketches.

the class ReservoirItemsSketchTest method checkBadConstructorArgs.

@Test
public void checkBadConstructorArgs() {
    final ArrayList<String> data = new ArrayList<>(128);
    for (int i = 0; i < 128; ++i) {
        data.add(Integer.toString(i));
    }
    final ResizeFactor rf = ResizeFactor.X8;
    // no items
    try {
        ReservoirItemsSketch.<Byte>newInstance(null, 128, rf, 128);
        fail();
    } catch (final SketchesException e) {
        assertTrue(e.getMessage().contains("null reservoir"));
    }
    // size too small
    try {
        ReservoirItemsSketch.newInstance(data, 128, rf, 1);
        fail();
    } catch (final SketchesException e) {
        assertTrue(e.getMessage().contains("size less than 2"));
    }
    // configured reservoir size smaller than items length
    try {
        ReservoirItemsSketch.newInstance(data, 128, rf, 64);
        fail();
    } catch (final SketchesException e) {
        assertTrue(e.getMessage().contains("max size less than array length"));
    }
    // too many items seen vs items length, full sketch
    try {
        ReservoirItemsSketch.newInstance(data, 512, rf, 256);
        fail();
    } catch (final SketchesException e) {
        assertTrue(e.getMessage().contains("too few samples"));
    }
    // too many items seen vs items length, under-full sketch
    try {
        ReservoirItemsSketch.newInstance(data, 256, rf, 256);
        fail();
    } catch (final SketchesException e) {
        assertTrue(e.getMessage().contains("too few samples"));
    }
}
Also used : ArrayList(java.util.ArrayList) ResizeFactor(org.apache.datasketches.ResizeFactor) SketchesException(org.apache.datasketches.SketchesException) Test(org.testng.annotations.Test)

Aggregations

ResizeFactor (org.apache.datasketches.ResizeFactor)24 Test (org.testng.annotations.Test)13 PreambleUtil.insertLgResizeFactor (org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor)6 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)5 PreambleUtil.extractLgResizeFactor (org.apache.datasketches.theta.PreambleUtil.extractLgResizeFactor)5 Family (org.apache.datasketches.Family)4 ArrayList (java.util.ArrayList)3 WritableMemory (org.apache.datasketches.memory.WritableMemory)3 PreambleUtil.extractResizeFactor (org.apache.datasketches.sampling.PreambleUtil.extractResizeFactor)3 SketchesException (org.apache.datasketches.SketchesException)2 DefaultMemoryRequestServer (org.apache.datasketches.memory.DefaultMemoryRequestServer)2 MemoryRequestServer (org.apache.datasketches.memory.MemoryRequestServer)2 ArrayOfBooleansSerDe (org.apache.datasketches.ArrayOfBooleansSerDe)1