You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by le...@apache.org on 2019/08/26 01:06:03 UTC

[incubator-datasketches-java] 04/07: Update unit tests

This is an automated email from the ASF dual-hosted git repository.

leerho pushed a commit to branch BkwardCompat
in repository https://gitbox.apache.org/repos/asf/incubator-datasketches-java.git

commit 98801f01283d6d2ea8a44e387bc84bdcee2c3aa1
Author: Lee Rhodes <le...@users.noreply.github.com>
AuthorDate: Thu Aug 22 20:15:44 2019 -0700

    Update unit tests
---
 .../yahoo/sketches/theta/DirectCompactSketch.java  |  15 +--
 .../sketches/theta/HeapCompactOrderedSketch.java   |  25 ++--
 .../sketches/theta/PairwiseSetOperations.java      |  42 ++-----
 .../com/yahoo/sketches/theta/SingleItemSketch.java |   1 -
 .../yahoo/sketches/theta/CompactSketchTest.java    |  26 +++-
 .../sketches/theta/PairwiseSetOperationsTest.java  | 136 ++++++---------------
 6 files changed, 81 insertions(+), 164 deletions(-)

diff --git a/src/main/java/com/yahoo/sketches/theta/DirectCompactSketch.java b/src/main/java/com/yahoo/sketches/theta/DirectCompactSketch.java
index 24df841..f07dacb 100644
--- a/src/main/java/com/yahoo/sketches/theta/DirectCompactSketch.java
+++ b/src/main/java/com/yahoo/sketches/theta/DirectCompactSketch.java
@@ -39,14 +39,10 @@ abstract class DirectCompactSketch extends CompactSketch {
   }
 
   //Sketch
-
+  //overidden by EmptyCompactSketch and SingleItemSketch
   @Override
   public int getCurrentBytes(final boolean compact) { //compact is ignored here
     final int preLongs = getCurrentPreambleLongs(true);
-    final boolean empty = PreambleUtil.isEmpty(mem_);
-    if (preLongs == 1) {
-      return (empty) ? 8 : 16; //empty or singleItem
-    }
     //preLongs > 1
     final int curCount = extractCurCount(mem_);
     return (preLongs + curCount) << 3;
@@ -57,16 +53,11 @@ abstract class DirectCompactSketch extends CompactSketch {
     return new MemoryHashIterator(mem_, getRetainedEntries(), getThetaLong());
   }
 
+  //overidden by EmptyCompactSketch and SingleItemSketch
   @Override
   public int getRetainedEntries(final boolean valid) { //compact is always valid
-    final int preLongs = getCurrentPreambleLongs(true);
-    final boolean empty = PreambleUtil.isEmpty(mem_);
-    if (preLongs == 1) {
-      return (empty) ? 0 : 1;
-    }
     //preLongs > 1
-    final int curCount = extractCurCount(mem_);
-    return curCount;
+    return extractCurCount(mem_);
   }
 
   @Override
diff --git a/src/main/java/com/yahoo/sketches/theta/HeapCompactOrderedSketch.java b/src/main/java/com/yahoo/sketches/theta/HeapCompactOrderedSketch.java
index 61a062c..59521c0 100644
--- a/src/main/java/com/yahoo/sketches/theta/HeapCompactOrderedSketch.java
+++ b/src/main/java/com/yahoo/sketches/theta/HeapCompactOrderedSketch.java
@@ -63,24 +63,15 @@ final class HeapCompactOrderedSketch extends HeapCompactSketch {
 
     final int preLongs = extractPreLongs(srcMem);
     final boolean empty = PreambleUtil.isEmpty(srcMem); //checks for cap <= 8
-    int curCount = 0;
     long thetaLong = Long.MAX_VALUE;
-    long[] cache = new long[0];
-
-    if (preLongs == 1) {
-      if (!empty) { //singleItem
-        return new SingleItemSketch(srcMem.getLong(8), memSeedHash);
-      }
-      //else empty
-    } else { //preLongs > 1
-      curCount = extractCurCount(srcMem);
-      cache = new long[curCount];
-      if (preLongs == 2) {
-        srcMem.getLongArray(16, cache, 0, curCount);
-      } else { //preLongs == 3
-        srcMem.getLongArray(24, cache, 0, curCount);
-        thetaLong = extractThetaLong(srcMem);
-      }
+    //preLongs == 1 handled before this method, so preLongs > 1
+    final int curCount = extractCurCount(srcMem);
+    final long[] cache = new long[curCount];
+    if (preLongs == 2) {
+      srcMem.getLongArray(16, cache, 0, curCount);
+    } else { //preLongs == 3
+      srcMem.getLongArray(24, cache, 0, curCount);
+      thetaLong = extractThetaLong(srcMem);
     }
     return new HeapCompactOrderedSketch(cache, empty, memSeedHash, curCount, thetaLong);
   }
diff --git a/src/main/java/com/yahoo/sketches/theta/PairwiseSetOperations.java b/src/main/java/com/yahoo/sketches/theta/PairwiseSetOperations.java
index 6493693..245346a 100644
--- a/src/main/java/com/yahoo/sketches/theta/PairwiseSetOperations.java
+++ b/src/main/java/com/yahoo/sketches/theta/PairwiseSetOperations.java
@@ -110,44 +110,24 @@ public class PairwiseSetOperations {
     //Handle all corner cases with null or empty arguments
     //For backward compatibility, we must allow input empties with Theta < 1.0.
     final int swA, swB;
-    if ((skA == null) || (skA instanceof EmptyCompactSketch)) {
-      swA = 1;
-    } else {
-      checkOrdered(skA);
-      swA = skA.isEmpty() ? 2 : 3;
-    }
-    if ((skB == null) || (skB instanceof EmptyCompactSketch)) {
-      swB = 1;
-    } else {
-      checkOrdered(skB);
-      swB = skB.isEmpty() ? 2 : 3;
-    }
-    final int sw = (swA << 2) | swB;
+    swA = ((skA == null) || (skA instanceof EmptyCompactSketch)) ? 0 : 2;
+    swB = ((skB == null) || (skB instanceof EmptyCompactSketch)) ? 0 : 1;
+    final int sw = swA | swB;
     switch (sw) {
-      case 5:   //skA == null/ECS;  skB == null; return EmptyCompactSketch.
-      case 6:   //skA == null/ECS;  skB == empty; return EmptyCompactSketch. *
-      case 9: { //skA == empty; skB == null/ECS; return EmptyCompactSketch. *
+      case 0: { //skA == null/ECS;  skB == null/ECS; return EmptyCompactSketch.
         return EmptyCompactSketch.getInstance();
       }
-      case 7: {  //skA == null/ECS;  skB == valid; return skB
+      case 1: { //skA == null/ECS;  skB == valid; return skB
+        checkOrdered(skB);
         return maybeCutback(skB, k);
       }
-      case 10: { //skA == empty; skB == empty; return empty
-        seedHashesCheck(skA, skB);
-        return EmptyCompactSketch.getInstance();
-      }
-      case 11: { //skA == empty; skB == valid; return skB
-        seedHashesCheck(skA, skB);
-        return maybeCutback(skB, k);
-      }
-      case 13: { //skA == valid; skB == null/ECS; return skA
-        return maybeCutback(skA, k);
-      }
-      case 14: { //skA == valid; skB == empty; return skA
-        seedHashesCheck(skA, skB);
+      case 2: { //skA == valid; skB == null/ECS; return skA
+        checkOrdered(skA);
         return maybeCutback(skA, k);
       }
-      case 15: { //skA == valid; skB == valid; perform full union
+      case 3: { //skA == valid; skB == valid; perform full union
+        checkOrdered(skA);
+        checkOrdered(skB);
         seedHashesCheck(skA, skB);
         break;
       }
diff --git a/src/main/java/com/yahoo/sketches/theta/SingleItemSketch.java b/src/main/java/com/yahoo/sketches/theta/SingleItemSketch.java
index 536988c..f09239a 100644
--- a/src/main/java/com/yahoo/sketches/theta/SingleItemSketch.java
+++ b/src/main/java/com/yahoo/sketches/theta/SingleItemSketch.java
@@ -89,7 +89,6 @@ public final class SingleItemSketch extends CompactSketch {
     if (testPre0SeedHash(memPre0, DEFAULT_SEED_HASH)) {
       return new SingleItemSketch(mem.getLong(8));
     }
-    System.out.println("memPre0 : " + Long.toHexString(memPre0));
     final long def = ((DEFAULT_SEED_HASH << 48) | PRE0_LO6);
     throw new SketchesArgumentException("Input Memory does not match defualt Preamble. " + LS
         + "Memory Pre0   : " + Long.toHexString(memPre0) + LS
diff --git a/src/test/java/com/yahoo/sketches/theta/CompactSketchTest.java b/src/test/java/com/yahoo/sketches/theta/CompactSketchTest.java
index c7bfe19..ff9fe0f 100644
--- a/src/test/java/com/yahoo/sketches/theta/CompactSketchTest.java
+++ b/src/test/java/com/yahoo/sketches/theta/CompactSketchTest.java
@@ -174,6 +174,16 @@ public class CompactSketchTest {
     assertEquals(testSk.getCurrentPreambleLongs(true), refSk.getCurrentPreambleLongs(true));
   }
 
+  @Test
+  public void checkDirectSingleItemSketch() {
+    UpdateSketch sk = Sketches.updateSketchBuilder().build();
+    sk.update(1);
+    int bytes = sk.getCurrentBytes(true);
+    WritableMemory wmem = WritableMemory.allocate(bytes);
+    sk.compact(true, wmem);
+    Sketch csk2 = Sketch.heapify(wmem);
+    assertTrue(csk2 instanceof SingleItemSketch);
+  }
 
   @Test(expectedExceptions = SketchesArgumentException.class)
   public void checkMemTooSmall() {
@@ -220,10 +230,12 @@ public class CompactSketchTest {
   public void checkDirectCompactSingleItemSketch() {
     UpdateSketch sk = Sketches.updateSketchBuilder().build();
     CompactSketch csk = sk.compact(true, WritableMemory.allocate(16));
-    assertEquals(csk.getCurrentBytes(true), 8);
+    int bytes = csk.getCurrentBytes(true);
+    assertEquals(bytes, 8);
     sk.update(1);
     csk = sk.compact(true, WritableMemory.allocate(16));
-    assertEquals(csk.getCurrentBytes(true), 16);
+    bytes = csk.getCurrentBytes(true);
+    assertEquals(bytes, 16);
     assertTrue(csk == csk.compact());
     assertTrue(csk == csk.compact(true, null));
   }
@@ -250,6 +262,16 @@ public class CompactSketchTest {
   }
 
   @Test
+  public void checkGetCache() {
+    UpdateSketch sk = Sketches.updateSketchBuilder().setP((float).5).build();
+    sk.update(7);
+    int bytes = sk.getCurrentBytes(true);
+    CompactSketch csk = sk.compact(true, WritableMemory.allocate(bytes));
+    long[] cache = csk.getCache();
+    assertTrue(cache.length == 0);
+  }
+
+  @Test
   public void printlnTest() {
     println("PRINTING: "+this.getClass().getName());
   }
diff --git a/src/test/java/com/yahoo/sketches/theta/PairwiseSetOperationsTest.java b/src/test/java/com/yahoo/sketches/theta/PairwiseSetOperationsTest.java
index 7e3c77b..c30eaa6 100644
--- a/src/test/java/com/yahoo/sketches/theta/PairwiseSetOperationsTest.java
+++ b/src/test/java/com/yahoo/sketches/theta/PairwiseSetOperationsTest.java
@@ -279,118 +279,52 @@ public class PairwiseSetOperationsTest {
    CompactSketch cskBempty = uskB.compact();
    CompactSketch cskAnull = null;
    CompactSketch cskBnull = null;
+   uskA.update(1);
+   CompactSketch cskA1 = uskA.compact();
+
    Union union = SetOperation.builder().setNominalEntries(k).buildUnion();
    AnotB aNotB = SetOperation.builder().buildANotB();
    Intersection inter = SetOperation.builder().buildIntersection();
-   CompactSketch cskC, cskR;
-
-   //Null, Null
-   union.update(cskAnull);
-   union.update(cskBnull);
-   cskC = union.getResult();
-   cskR = PairwiseSetOperations.union(cskAnull, cskBnull, k);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
-   union.reset();
-
-   inter.update(cskAnull);
-   inter.update(cskBnull);
-   cskC = inter.getResult();
-   cskR = PairwiseSetOperations.intersect(cskAnull, cskBnull);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
-   inter.reset();
-
-   aNotB.update(cskAnull, cskBnull);
-   cskC = aNotB.getResult();
-   cskR = PairwiseSetOperations.aNotB(cskAnull, cskBnull);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
-
-   //Null, Empty
-   union.update(cskAnull);
-   union.update(cskBempty);
-   cskC = union.getResult();
-   cskR = PairwiseSetOperations.union(cskAnull, cskBempty, k);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
-   union.reset();
-
-   inter.update(cskAnull);
-   inter.update(cskBempty);
-   cskC = inter.getResult();
-   cskR = PairwiseSetOperations.intersect(cskAnull, cskBempty);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
-   inter.reset();
-
-   aNotB.update(cskAnull, cskBempty);
-   cskC = aNotB.getResult();
-   cskR = PairwiseSetOperations.aNotB(cskAnull, cskBempty);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
-
-   //Empty, Null
-   union.update(cskAempty);
-   union.update(cskBnull);
-   cskC = union.getResult();
-   cskR = PairwiseSetOperations.union(cskAempty, cskBnull, k);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
-   union.reset();
 
-   inter.update(cskAempty);
-   inter.update(cskBnull);
-   cskC = inter.getResult();
-   cskR = PairwiseSetOperations.intersect(cskAempty, cskBnull);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
-   inter.reset();
-
-   aNotB.update(cskAempty, cskBnull);
-   cskC = aNotB.getResult();
-   cskR = PairwiseSetOperations.aNotB(cskAempty, cskBnull);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
-
-   //Empty, Empty
-   union.update(cskAempty);
-   union.update(cskBempty);
-   cskC = union.getResult();
-   cskR = PairwiseSetOperations.union(cskAempty, cskBempty, k);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
-   union.reset();
-
-   inter.update(cskAempty);
-   inter.update(cskBempty);
-   cskC = inter.getResult();
-   cskR = PairwiseSetOperations.intersect(cskAempty, cskBempty);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
-   inter.reset();
+   checkSetOps(union, inter, aNotB, k, cskAnull, cskBnull);   //Null, Null
+   checkSetOps(union, inter, aNotB, k, cskAnull, cskBempty);  //Null, Empty
+   checkSetOps(union, inter, aNotB, k, cskAempty, cskBnull);  //Empty, Null
+   checkSetOps(union, inter, aNotB, k, cskAempty, cskBempty); //Empty, Empty
+   checkSetOps(union, inter, aNotB, k, cskA1, cskBempty);     //NotEmpty, Empty
+   checkSetOps(union, inter, aNotB, k, cskAempty, cskA1);     //Empty, NotEmpty
+ }
 
-   aNotB.update(cskAempty, cskBempty);
-   cskC = aNotB.getResult();
-   cskR = PairwiseSetOperations.aNotB(cskAempty, cskBempty);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
+ private static void checkSetOps(Union union, Intersection inter, AnotB aNotB, int k,
+     CompactSketch cskA, CompactSketch cskB) {
+   checkUnion(union, cskA, cskB, k);
+   checkIntersection(inter, cskA, cskB);
+   checkAnotB(aNotB, cskA, cskB);
 
-   //NotEmpty, Empty
-   uskA.update(1);
-   CompactSketch cskA1 = uskA.compact();
+ }
 
-   union.update(cskA1);
-   union.update(cskBempty);
-   cskC = union.getResult();
-   cskR = PairwiseSetOperations.union(cskA1, cskBempty, k);
-   assertEquals(!cskC.isEmpty(), !cskR.isEmpty());
+ private static void checkUnion(Union union, CompactSketch cskA, CompactSketch cskB, int k) {
+   union.update(cskA);
+   union.update(cskB);
+   CompactSketch cskU = union.getResult();
+   CompactSketch cskP = PairwiseSetOperations.union(cskA, cskB, k);
+   assertEquals(cskU.isEmpty(), cskP.isEmpty());
    union.reset();
+ }
 
-   inter.update(cskA1);
-   inter.update(cskBempty);
-   cskC = inter.getResult();
-   cskR = PairwiseSetOperations.intersect(cskA1, cskBempty);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
+ private static void checkIntersection(Intersection inter, CompactSketch cskA, CompactSketch cskB) {
+   inter.update(cskA);
+   inter.update(cskB);
+   CompactSketch cskI = inter.getResult();
+   CompactSketch cskP = PairwiseSetOperations.intersect(cskA, cskB);
+   assertEquals(cskI.isEmpty(), cskP.isEmpty());
    inter.reset();
+ }
 
-   aNotB.update(cskA1, cskBempty);
-   cskC = aNotB.getResult();
-   cskR = PairwiseSetOperations.aNotB(cskA1, cskBempty);
-   assertEquals(!cskC.isEmpty(), !cskR.isEmpty());
-
-   aNotB.update(cskBempty, cskA1);  //check the reverse
-   cskC = aNotB.getResult();
-   cskR = PairwiseSetOperations.aNotB(cskBempty, cskA1);
-   assertEquals(cskC.isEmpty(), cskR.isEmpty());
+ private static void checkAnotB(AnotB aNotB, CompactSketch cskA, CompactSketch cskB) {
+   aNotB.update(cskA, cskB);
+   CompactSketch cskD = aNotB.getResult();
+   CompactSketch cskP = PairwiseSetOperations.aNotB(cskA, cskB);
+   assertEquals(cskD.isEmpty(), cskP.isEmpty());
  }
 
   @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org