Skip to content
Permalink
Browse files
Added tests, cleaned up a few unused methods, improved some Javadocs.
  • Loading branch information
leerho committed Apr 15, 2022
1 parent 945a835 commit 261c372d40bb12edda6fba2df951908474c8451c
Showing 12 changed files with 130 additions and 66 deletions.
@@ -24,7 +24,6 @@
import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryEmptyFlag;
import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryN;
import static org.apache.datasketches.kll.KllPreambleUtil.getMemorySingleItemFlag;
import static org.apache.datasketches.kll.KllSketch.Error.EMPTY_NO_DATA;
import static org.apache.datasketches.kll.KllSketch.Error.SINGLE_ITEM_IMPROPER_CALL;
import static org.apache.datasketches.kll.KllSketch.Error.kllSketchThrow;

@@ -72,15 +71,6 @@ public byte[] toByteArray() {
return itemsArr;
}

@Override
double getDoubleItemsArrayAt(final int index) {
if (isEmpty()) { kllSketchThrow(EMPTY_NO_DATA); }
if (isSingleItem()) { kllSketchThrow(SINGLE_ITEM_IMPROPER_CALL); }
final int offset =
DATA_START_ADR + (getLevelsArray().length - 1) * Integer.BYTES + (index + 2) * Double.BYTES;
return wmem.getDouble(offset);
}

@Override
double getDoubleSingleItem() {
if (!isSingleItem()) { kllSketchThrow(SINGLE_ITEM_IMPROPER_CALL); }
@@ -24,7 +24,6 @@
import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryEmptyFlag;
import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryN;
import static org.apache.datasketches.kll.KllPreambleUtil.getMemorySingleItemFlag;
import static org.apache.datasketches.kll.KllSketch.Error.EMPTY_NO_DATA;
import static org.apache.datasketches.kll.KllSketch.Error.SINGLE_ITEM_IMPROPER_CALL;
import static org.apache.datasketches.kll.KllSketch.Error.kllSketchThrow;

@@ -72,20 +71,9 @@ float[] getFloatItemsArray() {
return itemsArr;
}

@Override
float getFloatItemsArrayAt(final int index) {
if (isEmpty()) { kllSketchThrow(EMPTY_NO_DATA); }
if (isSingleItem()) { kllSketchThrow(SINGLE_ITEM_IMPROPER_CALL); }
final int offset =
DATA_START_ADR + (getLevelsArray().length - 1) * Integer.BYTES + (index + 2) * Float.BYTES;
return wmem.getFloat(offset);
}

@Override
float getFloatSingleItem() {
if (!isSingleItem()) {
kllSketchThrow(SINGLE_ITEM_IMPROPER_CALL);
}
if (!isSingleItem()) { kllSketchThrow(SINGLE_ITEM_IMPROPER_CALL); }
return wmem.getFloat(DATA_START_ADR_SINGLE_ITEM);
}

@@ -132,13 +132,6 @@ public byte[] toUpdatableByteArray() {
return itemsArr;
}

@Override
double getDoubleItemsArrayAt(final int index) {
final int offset =
DATA_START_ADR + getLevelsArray().length * Integer.BYTES + 2 * Double.BYTES + index * Double.BYTES;
return wmem.getDouble(offset);
}

@Override
double getDoubleSingleItem() { kllSketchThrow(MUST_NOT_CALL); return Double.NaN; }

@@ -135,13 +135,6 @@ float[] getFloatItemsArray() {
return itemsArr;
}

@Override
float getFloatItemsArrayAt(final int index) {
final int offset =
DATA_START_ADR + getLevelsArray().length * Integer.BYTES + (index + 2) * Float.BYTES;
return wmem.getFloat(offset);
}

@Override
float getFloatSingleItem() { kllSketchThrow(MUST_NOT_CALL); return Float.NaN; }

@@ -344,9 +344,6 @@ public void update(final double value) {
@Override //Artifact of inheritance
float[] getFloatItemsArray() { kllSketchThrow(MUST_NOT_CALL); return null; }

@Override //Artifact of inheritance
float getFloatItemsArrayAt(final int index) { kllSketchThrow(MUST_NOT_CALL); return Float.NaN; }

@Override //Artifact of inheritance
float getMaxFloatValue() { kllSketchThrow(MUST_NOT_CALL); return Float.NaN; }

@@ -344,9 +344,6 @@ public void update(final float value) {
@Override //Artifact of inheritance
double[] getDoubleItemsArray() { kllSketchThrow(MUST_NOT_CALL); return null; }

@Override //Artifact of inheritance
double getDoubleItemsArrayAt(final int index) { kllSketchThrow(MUST_NOT_CALL); return Double.NaN; }

@Override //Artifact of inheritance
double getMaxDoubleValue() { kllSketchThrow(MUST_NOT_CALL); return Double.NaN; }

@@ -125,9 +125,6 @@ else if (memVal.singleItem && !updatableMemFormat) {
@Override
double[] getDoubleItemsArray() { return doubleItems_; }

@Override
double getDoubleItemsArrayAt(final int index) { return doubleItems_[index]; }

@Override
double getDoubleSingleItem() { kllSketchThrow(MUST_NOT_CALL); return Double.NaN; }

@@ -128,9 +128,6 @@ else if (memVal.singleItem && !updatableMemFormat) {
@Override
float[] getFloatItemsArray() { return floatItems_; }

@Override
float getFloatItemsArrayAt(final int index) { return floatItems_[index]; }

@Override
float getFloatSingleItem() { kllSketchThrow(MUST_NOT_CALL); return Float.NaN; }

@@ -250,10 +250,10 @@ static void compressWhileUpdatingSketch(final KllSketch mine) {
mine.setLevelsArrayAt(level, mine.getLevelsArrayAt(level + 1) - 1); // the current level now contains one item
if (mine.sketchType == DOUBLES_SKETCH) {
mine.setDoubleItemsArrayAt(
mine.getLevelsArrayAt(level), mine.getDoubleItemsArrayAt(rawBeg)); // namely this leftover guy
mine.getLevelsArrayAt(level), mine.getDoubleItemsArray()[rawBeg]); // namely this leftover guy
} else {
mine.setFloatItemsArrayAt(
mine.getLevelsArrayAt(level), mine.getFloatItemsArrayAt(rawBeg)); // namely this leftover guy
mine.getLevelsArrayAt(level), mine.getFloatItemsArray()[rawBeg]); // namely this leftover guy
}

} else {
@@ -463,17 +463,13 @@ public byte[] toUpdatableByteArray() {
*/
abstract double[] getDoubleItemsArray();

abstract double getDoubleItemsArrayAt(int index);

abstract double getDoubleSingleItem();

/**
* @return full size of internal items array including garbage.
*/
abstract float[] getFloatItemsArray();

abstract float getFloatItemsArrayAt(int index);

abstract float getFloatSingleItem();

final int[] getLevelsArray() {
@@ -526,9 +522,11 @@ final boolean isCompactSingleItem() {

abstract boolean isLevelZeroSorted();

boolean isSingleItem() {
return getN() == 1;
}
/**
* First determine that this is a singleItem sketch before calling this.
* @return the value of the single item
*/
boolean isSingleItem() { return getN() == 1; }

abstract void setDoubleItemsArray(double[] floatItems);

@@ -529,16 +529,73 @@ public void checkRODirectCompact() {
assertEquals(sk3.getMaxValue(), 21.0F);
}

@Test
public void checkDirectCompactSingleItem() {
int k = 20;
KllDoublesSketch sk = KllDoublesSketch.newHeapInstance(k); //Heap
sk.update(1);
KllDoublesSketch sk2 = KllDoublesSketch.wrap(Memory.wrap(sk.toByteArray()));
assertEquals(sk2.getDoubleSingleItem(), 1.0);
sk.update(2);
sk2 = KllDoublesSketch.wrap(Memory.wrap(sk.toByteArray()));
assertEquals(sk2.getN(), 2);
try {
sk2.getDoubleSingleItem();
} catch (SketchesArgumentException e) { }
}

@Test
public void checkDirectCompactGetFloatItemsArray() {
int k = 20;
KllDoublesSketch sk = KllDoublesSketch.newHeapInstance(k);

KllDoublesSketch sk2 = KllDoublesSketch.wrap(Memory.wrap(sk.toByteArray()));
double[] itemsArr = sk2.getDoubleItemsArray();
for (int i = 0; i < 20; i++) { assertEquals(itemsArr[i], 0F); }

sk.update(1);
sk2 = KllDoublesSketch.wrap(Memory.wrap(sk.toByteArray()));
itemsArr = sk2.getDoubleItemsArray();
for (int i = 0; i < 19; i++) { assertEquals(itemsArr[i], 0F); }
assertEquals(itemsArr[19], 1F);

for (int i = 2; i <= 21; i++) { sk.update(i); }
sk2 = KllDoublesSketch.wrap(Memory.wrap(sk.toByteArray()));
itemsArr = sk2.getDoubleItemsArray();
assertEquals(itemsArr.length, 33);
assertEquals(itemsArr[22], 21);
//for (int i = 0; i < itemsArr.length; i++) {
// println(i + ": " + itemsArr[i]);
//}
}

@Test
public void checkMinAndMax() {
int k = 20;
KllDoublesSketch sk = KllDoublesSketch.newHeapInstance(k);
KllDoublesSketch sk2 = KllDoublesSketch.wrap(Memory.wrap(sk.toByteArray()));
assertTrue(Double.isNaN(sk2.getMaxValue()));
assertTrue(Double.isNaN(sk2.getMinValue()));
sk.update(1);
sk2 = KllDoublesSketch.wrap(Memory.wrap(sk.toByteArray()));
assertEquals(sk2.getMaxValue(),1.0F);
assertEquals(sk2.getMinValue(),1.0F);
for (int i = 2; i <= 21; i++) { sk.update(i); }
sk2 = KllDoublesSketch.wrap(Memory.wrap(sk.toByteArray()));
assertEquals(sk2.getMaxValue(),21.0F);
assertEquals(sk2.getMinValue(),1.0F);
}

@Test
public void printlnTest() {
println("PRINTING: " + this.getClass().getName());
}

/**
* @param s value to print
* @param o value to print
*/
static void println(final String s) {
//System.out.println(s); //disable here
static void println(final Object o) {
//System.out.println(o.toString()); //disable here
}

}
@@ -532,27 +532,84 @@ public void checkRODirectCompact() {
@Test
public void checkDirectCompactSingleItem() {
int k = 20;
KllFloatsSketch sk = KllFloatsSketch.newHeapInstance(k); //Heap
KllFloatsSketch sk = KllFloatsSketch.newHeapInstance(k);
sk.update(1);
KllFloatsSketch sk2 = KllFloatsSketch.wrap(Memory.wrap(sk.toByteArray()));
assertEquals(sk2.getFloatSingleItem(), 1.0F);
sk.update(2);
sk2 = KllFloatsSketch.wrap(Memory.wrap(sk.toByteArray()));
assertEquals(sk2.getN(), 2);
try {
sk2.getFloatSingleItem();
} catch (SketchesArgumentException e) { }
} catch (SketchesArgumentException e) { }
}

@Test
public void checkDirectCompactGetFloatItemsArray() {
int k = 20;
KllFloatsSketch sk = KllFloatsSketch.newHeapInstance(k);

KllFloatsSketch sk2 = KllFloatsSketch.wrap(Memory.wrap(sk.toByteArray()));
float[] itemsArr = sk2.getFloatItemsArray();
for (int i = 0; i < 20; i++) { assertEquals(itemsArr[i], 0F); }

sk.update(1);
sk2 = KllFloatsSketch.wrap(Memory.wrap(sk.toByteArray()));
itemsArr = sk2.getFloatItemsArray();
for (int i = 0; i < 19; i++) { assertEquals(itemsArr[i], 0F); }
assertEquals(itemsArr[19], 1F);

for (int i = 2; i <= 21; i++) { sk.update(i); }
sk2 = KllFloatsSketch.wrap(Memory.wrap(sk.toByteArray()));
itemsArr = sk2.getFloatItemsArray();
assertEquals(itemsArr.length, 33);
assertEquals(itemsArr[22], 21);
//for (int i = 0; i < itemsArr.length; i++) {
// println(i + ": " + itemsArr[i]);
//}
}

@Test
public void checkMinAndMax() {
int k = 20;
KllFloatsSketch sk = KllFloatsSketch.newHeapInstance(k);
KllFloatsSketch sk2 = KllFloatsSketch.wrap(Memory.wrap(sk.toByteArray()));
assertTrue(Float.isNaN(sk2.getMaxValue()));
assertTrue(Float.isNaN(sk2.getMinValue()));
sk.update(1);
sk2 = KllFloatsSketch.wrap(Memory.wrap(sk.toByteArray()));
assertEquals(sk2.getMaxValue(),1.0F);
assertEquals(sk2.getMinValue(),1.0F);
for (int i = 2; i <= 21; i++) { sk.update(i); }
sk2 = KllFloatsSketch.wrap(Memory.wrap(sk.toByteArray()));
assertEquals(sk2.getMaxValue(),21.0F);
assertEquals(sk2.getMinValue(),1.0F);
}

@Test
public void checkQuantile() {
int k = 20;
KllFloatsSketch sk = KllFloatsSketch.newHeapInstance(k);
for (int i = 1; i <= 100; i++) { sk.update(i); }
float med1 = sk.getQuantile(0.5);
println(med1);
KllFloatsSketch sk2 = KllFloatsSketch.wrap(Memory.wrap(sk.toByteArray()));
float med2 = sk2.getQuantile(0.5);
println(med2);

}


@Test
public void printlnTest() {
println("PRINTING: " + this.getClass().getName());
}

/**
* @param s value to print
* @param o value to print
*/
static void println(final String s) {
System.out.println(s); //disable here
static void println(final Object o) {
System.out.println(o.toString()); //disable here
}

}

0 comments on commit 261c372

Please sign in to comment.