Skip to content
Permalink
Browse files
Update unit tests for KllDoubles and KllFloats
  • Loading branch information
leerho committed Feb 15, 2022
1 parent 4b993a9 commit 0f0f0757517db6cb0d8fa399b079e8d590d6f4e9
Showing 2 changed files with 125 additions and 0 deletions.
@@ -20,7 +20,12 @@
package org.apache.datasketches.kll;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import java.util.Objects;

import org.apache.datasketches.SketchesArgumentException;
import org.apache.datasketches.memory.WritableMemory;
import org.testng.annotations.Test;

/**
@@ -57,6 +62,63 @@ public void checkBounds() {
println("LB : " + lb);
}

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions1() {
KllDoublesSketch sk = new KllDoublesSketch();
WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
wmem.putByte(6, (byte)4); //corrupt M
KllDoublesSketch.heapify(wmem);
}

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions2() {
KllDoublesSketch sk = new KllDoublesSketch();
WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
wmem.putByte(0, (byte)1); //corrupt preamble ints, should be 2
KllDoublesSketch.heapify(wmem);
}

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions3() {
KllDoublesSketch sk = new KllDoublesSketch();
sk.update(1.0f);
sk.update(2.0f);
WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
wmem.putByte(0, (byte)1); //corrupt preamble ints, should be 5
KllDoublesSketch.heapify(wmem);
}

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions4() {
KllDoublesSketch sk = new KllDoublesSketch();
WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
wmem.putByte(1, (byte)0); //corrupt SerVer, should be 1 or 2
KllDoublesSketch.heapify(wmem);
}

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions5() {
KllDoublesSketch sk = new KllDoublesSketch();
WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
wmem.putByte(2, (byte)0); //corrupt FamilyID, should be 15
KllDoublesSketch.heapify(wmem);
}

@Test
public void checkMisc() {
KllDoublesSketch sk = new KllDoublesSketch(8, true);
assertTrue(Objects.isNull(sk.getQuantiles(10)));
sk.toString(true, true);
for (int i = 0; i < 20; i++) { sk.update(i); }
sk.toString(true, true);
sk.toByteArray();
final double[] items = sk.getItems();
assertEquals(items.length, 16);
final int[] levels = sk.getLevels();
assertEquals(levels.length, 3);
assertEquals(sk.getNumLevels(), 2);
}

//@Test //requires visual check
public void checkNumRetainedAboveLevelZero() {
final KllDoublesSketch sketch = new KllDoublesSketch(20);
@@ -20,6 +20,12 @@
package org.apache.datasketches.kll;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import java.util.Objects;

import org.apache.datasketches.SketchesArgumentException;
import org.apache.datasketches.memory.WritableMemory;

import org.testng.annotations.Test;

@@ -57,6 +63,63 @@ public void checkBounds() {
println("LB : " + lb);
}

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions1() {
KllFloatsSketch sk = new KllFloatsSketch();
WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
wmem.putByte(6, (byte)4); //corrupt M
KllFloatsSketch.heapify(wmem);
}

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions2() {
KllFloatsSketch sk = new KllFloatsSketch();
WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
wmem.putByte(0, (byte)1); //corrupt preamble ints, should be 2
KllFloatsSketch.heapify(wmem);
}

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions3() {
KllFloatsSketch sk = new KllFloatsSketch();
sk.update(1.0f);
sk.update(2.0f);
WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
wmem.putByte(0, (byte)1); //corrupt preamble ints, should be 5
KllFloatsSketch.heapify(wmem);
}

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions4() {
KllFloatsSketch sk = new KllFloatsSketch();
WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
wmem.putByte(1, (byte)0); //corrupt SerVer, should be 1 or 2
KllFloatsSketch.heapify(wmem);
}

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions5() {
KllFloatsSketch sk = new KllFloatsSketch();
WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
wmem.putByte(2, (byte)0); //corrupt FamilyID, should be 15
KllFloatsSketch.heapify(wmem);
}

@Test
public void checkMisc() {
KllFloatsSketch sk = new KllFloatsSketch(8, true);
assertTrue(Objects.isNull(sk.getQuantiles(10)));
sk.toString(true, true);
for (int i = 0; i < 20; i++) { sk.update(i); }
sk.toString(true, true);
sk.toByteArray();
final float[] items = sk.getItems();
assertEquals(items.length, 16);
final int[] levels = sk.getLevels();
assertEquals(levels.length, 3);
assertEquals(sk.getNumLevels(), 2);
}

//@Test //requires visual check
public void checkNumRetainedAboveLevelZero() {
final KllFloatsSketch sketch = new KllFloatsSketch(20);

0 comments on commit 0f0f075

Please sign in to comment.