github-code-scanning[bot] commented on code in PR #470:
URL: https://github.com/apache/datasketches-java/pull/470#discussion_r1306270845


##########
src/test/java/org/apache/datasketches/quantiles/QuantilesSketchCrossLanguageTest.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.quantiles;
+
+import static org.apache.datasketches.common.TestUtil.CHECK_CPP_FILES;
+import static org.apache.datasketches.common.TestUtil.GENERATE_JAVA_FILES;
+import static org.apache.datasketches.common.TestUtil.cppPath;
+import static org.apache.datasketches.common.TestUtil.javaPath;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.Comparator;
+
+import org.apache.datasketches.common.ArrayOfStringsSerDe;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.quantilescommon.QuantilesDoublesSketchIterator;
+import org.apache.datasketches.quantilescommon.QuantilesGenericSketchIterator;
+import org.testng.annotations.Test;
+
+/**
+ * Serialize binary sketches to be tested by C++ code.
+ * Test deserialization of binary sketches serialized by C++ code.
+ */
+public class QuantilesSketchCrossLanguageTest {
+
+  @Test(groups = {GENERATE_JAVA_FILES})
+  public void generateDoublesSketch() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10_000, 100_000, 1_000_000};
+    for (final int n: nArr) {
+      final UpdateDoublesSketch sk = DoublesSketch.builder().build();
+      for (int i = 1; i <= n; i++) sk.update(i);
+      Files.newOutputStream(javaPath.resolve("quantiles_double_n" + n + 
"_java.sk")).write(sk.toByteArray());
+    }
+  }
+
+  @Test(groups = {GENERATE_JAVA_FILES})
+  public void generateItemsSketchWithStrings() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10_000, 100_000, 1_000_000};
+    for (final int n: nArr) {
+      final ItemsSketch<String> sk = ItemsSketch.getInstance(String.class, new 
Comparator<String>() {
+        @Override
+        public int compare(final String s1, final String s2) {
+          final Integer i1 = Integer.parseInt(s1);

Review Comment:
   ## Missing catch of NumberFormatException
   
   Potential uncaught 'java.lang.NumberFormatException'.
   
   [Show more 
details](https://github.com/apache/datasketches-java/security/code-scanning/630)



##########
src/test/java/org/apache/datasketches/quantiles/QuantilesSketchCrossLanguageTest.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.quantiles;
+
+import static org.apache.datasketches.common.TestUtil.CHECK_CPP_FILES;
+import static org.apache.datasketches.common.TestUtil.GENERATE_JAVA_FILES;
+import static org.apache.datasketches.common.TestUtil.cppPath;
+import static org.apache.datasketches.common.TestUtil.javaPath;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.Comparator;
+
+import org.apache.datasketches.common.ArrayOfStringsSerDe;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.quantilescommon.QuantilesDoublesSketchIterator;
+import org.apache.datasketches.quantilescommon.QuantilesGenericSketchIterator;
+import org.testng.annotations.Test;
+
+/**
+ * Serialize binary sketches to be tested by C++ code.
+ * Test deserialization of binary sketches serialized by C++ code.
+ */
+public class QuantilesSketchCrossLanguageTest {
+
+  @Test(groups = {GENERATE_JAVA_FILES})
+  public void generateDoublesSketch() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10_000, 100_000, 1_000_000};
+    for (final int n: nArr) {
+      final UpdateDoublesSketch sk = DoublesSketch.builder().build();
+      for (int i = 1; i <= n; i++) sk.update(i);
+      Files.newOutputStream(javaPath.resolve("quantiles_double_n" + n + 
"_java.sk")).write(sk.toByteArray());
+    }
+  }
+
+  @Test(groups = {GENERATE_JAVA_FILES})
+  public void generateItemsSketchWithStrings() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10_000, 100_000, 1_000_000};
+    for (final int n: nArr) {
+      final ItemsSketch<String> sk = ItemsSketch.getInstance(String.class, new 
Comparator<String>() {
+        @Override
+        public int compare(final String s1, final String s2) {
+          final Integer i1 = Integer.parseInt(s1);

Review Comment:
   ## Boxed variable is never null
   
   The variable 'i1' is only assigned values of primitive type and is never 
'null', but it is declared with the boxed type 'Integer'.
   
   [Show more 
details](https://github.com/apache/datasketches-java/security/code-scanning/626)



##########
src/test/java/org/apache/datasketches/kll/KllCrossLanguageTest.java:
##########
@@ -116,4 +102,92 @@
     assertEquals(sk.getMaxItem(), 1.0F);
   }
 
+  @Test(groups = {CHECK_CPP_FILES})
+  public void kllFloat() throws IOException {
+    final int[] nArr = {0, 10, 100, 1000, 10000, 100000, 1000000};
+    for (int n: nArr) {
+      final byte[] bytes = Files.readAllBytes(cppPath.resolve("kll_float_n" + 
n + "_cpp.sk"));
+      final KllFloatsSketch sketch = 
KllFloatsSketch.heapify(Memory.wrap(bytes));
+      assertEquals(sketch.getK(), 200);
+      assertTrue(n == 0 ? sketch.isEmpty() : !sketch.isEmpty());
+      assertTrue(n > 100 ? sketch.isEstimationMode() : 
!sketch.isEstimationMode());
+      assertEquals(sketch.getN(), n);
+      if (n > 0) {
+        assertEquals(sketch.getMinItem(), 1);
+        assertEquals(sketch.getMaxItem(), n);
+        long weight = 0;
+        QuantilesFloatsSketchIterator it = sketch.iterator();
+        while (it.next()) {
+          assertTrue(it.getQuantile() >= sketch.getMinItem());
+          assertTrue(it.getQuantile() <= sketch.getMaxItem());
+          weight += it.getWeight();
+        }
+        assertEquals(weight, n);
+      }
+    }
+  }
+
+  @Test(groups = {CHECK_CPP_FILES})
+  public void kllDouble() throws IOException {
+    final int[] nArr = {0, 10, 100, 1000, 10000, 100000, 1000000};
+    for (int n: nArr) {
+      final byte[] bytes = Files.readAllBytes(cppPath.resolve("kll_double_n" + 
n + "_cpp.sk"));
+      final KllDoublesSketch sketch = 
KllDoublesSketch.heapify(Memory.wrap(bytes));
+      assertEquals(sketch.getK(), 200);
+      assertTrue(n == 0 ? sketch.isEmpty() : !sketch.isEmpty());
+      assertTrue(n > 100 ? sketch.isEstimationMode() : 
!sketch.isEstimationMode());
+      assertEquals(sketch.getN(), n);
+      if (n > 0) {
+        assertEquals(sketch.getMinItem(), 1);
+        assertEquals(sketch.getMaxItem(), n);
+        long weight = 0;
+        QuantilesDoublesSketchIterator it = sketch.iterator();
+        while (it.next()) {
+          assertTrue(it.getQuantile() >= sketch.getMinItem());
+          assertTrue(it.getQuantile() <= sketch.getMaxItem());
+          weight += it.getWeight();
+        }
+        assertEquals(weight, n);
+      }
+    }
+  }
+
+  @Test(groups = {CHECK_CPP_FILES})
+  public void kllString() throws IOException {
+    // sketch contains numbers in strings to make meaningful assertions
+    Comparator<String> numericOrder = new Comparator<String>() {
+      @Override
+      public int compare(final String s1, final String s2) {
+        final Integer i1 = Integer.parseInt(s1);
+        final Integer i2 = Integer.parseInt(s2);

Review Comment:
   ## Missing catch of NumberFormatException
   
   Potential uncaught 'java.lang.NumberFormatException'.
   
   [Show more 
details](https://github.com/apache/datasketches-java/security/code-scanning/629)



##########
src/test/java/org/apache/datasketches/quantiles/QuantilesSketchCrossLanguageTest.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.quantiles;
+
+import static org.apache.datasketches.common.TestUtil.CHECK_CPP_FILES;
+import static org.apache.datasketches.common.TestUtil.GENERATE_JAVA_FILES;
+import static org.apache.datasketches.common.TestUtil.cppPath;
+import static org.apache.datasketches.common.TestUtil.javaPath;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.Comparator;
+
+import org.apache.datasketches.common.ArrayOfStringsSerDe;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.quantilescommon.QuantilesDoublesSketchIterator;
+import org.apache.datasketches.quantilescommon.QuantilesGenericSketchIterator;
+import org.testng.annotations.Test;
+
+/**
+ * Serialize binary sketches to be tested by C++ code.
+ * Test deserialization of binary sketches serialized by C++ code.
+ */
+public class QuantilesSketchCrossLanguageTest {
+
+  @Test(groups = {GENERATE_JAVA_FILES})
+  public void generateDoublesSketch() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10_000, 100_000, 1_000_000};
+    for (final int n: nArr) {
+      final UpdateDoublesSketch sk = DoublesSketch.builder().build();
+      for (int i = 1; i <= n; i++) sk.update(i);
+      Files.newOutputStream(javaPath.resolve("quantiles_double_n" + n + 
"_java.sk")).write(sk.toByteArray());
+    }
+  }
+
+  @Test(groups = {GENERATE_JAVA_FILES})
+  public void generateItemsSketchWithStrings() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10_000, 100_000, 1_000_000};
+    for (final int n: nArr) {
+      final ItemsSketch<String> sk = ItemsSketch.getInstance(String.class, new 
Comparator<String>() {
+        @Override
+        public int compare(final String s1, final String s2) {
+          final Integer i1 = Integer.parseInt(s1);
+          final Integer i2 = Integer.parseInt(s2);
+          return i1.compareTo(i2);
+        }
+      });
+      for (int i = 1; i <= n; i++) sk.update(Integer.toString(i));
+      if (n > 0) {
+        assertEquals(sk.getMinItem(), "1");
+        assertEquals(sk.getMaxItem(), Integer.toString(n));
+      }
+      Files.newOutputStream(javaPath.resolve("quantiles_string_n" + n + 
"_java.sk"))
+        .write(sk.toByteArray(new ArrayOfStringsSerDe()));
+    }
+  }
+
+  @Test(groups = {CHECK_CPP_FILES})
+  public void checkDoublesSketch() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10000, 100000, 1000000};
+    for (int n: nArr) {
+      final byte[] byteArr = 
Files.readAllBytes(cppPath.resolve("quantiles_double_n" + n + "_cpp.sk"));
+      final DoublesSketch sk = DoublesSketch.wrap(Memory.wrap(byteArr));
+      assertTrue(n == 0 ? sk.isEmpty() : !sk.isEmpty());
+      assertTrue(n > 128 ? sk.isEstimationMode() : !sk.isEstimationMode());
+      assertEquals(sk.getN(), n);
+      if (n > 0) {
+        assertEquals(sk.getMinItem(), 1);
+        assertEquals(sk.getMaxItem(), n);
+        QuantilesDoublesSketchIterator it = sk.iterator();
+        long weight = 0;
+        while(it.next()) {
+          assertTrue(it.getQuantile() >= sk.getMinItem());
+          assertTrue(it.getQuantile() <= sk.getMaxItem());
+          weight += it.getWeight();
+        }
+        assertEquals(weight, n);
+      }
+    }
+  }
+
+  @Test(groups = {CHECK_CPP_FILES})
+  public void checkItemsSketchWithStrings() throws IOException {
+    // sketch contains numbers in strings to make meaningful assertions
+    Comparator<String> numericOrder = new Comparator<String>() {
+      @Override
+      public int compare(final String s1, final String s2) {
+        final Integer i1 = Integer.parseInt(s1);

Review Comment:
   ## Boxed variable is never null
   
   The variable 'i1' is only assigned values of primitive type and is never 
'null', but it is declared with the boxed type 'Integer'.
   
   [Show more 
details](https://github.com/apache/datasketches-java/security/code-scanning/627)



##########
src/test/java/org/apache/datasketches/quantiles/QuantilesSketchCrossLanguageTest.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.quantiles;
+
+import static org.apache.datasketches.common.TestUtil.CHECK_CPP_FILES;
+import static org.apache.datasketches.common.TestUtil.GENERATE_JAVA_FILES;
+import static org.apache.datasketches.common.TestUtil.cppPath;
+import static org.apache.datasketches.common.TestUtil.javaPath;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.Comparator;
+
+import org.apache.datasketches.common.ArrayOfStringsSerDe;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.quantilescommon.QuantilesDoublesSketchIterator;
+import org.apache.datasketches.quantilescommon.QuantilesGenericSketchIterator;
+import org.testng.annotations.Test;
+
+/**
+ * Serialize binary sketches to be tested by C++ code.
+ * Test deserialization of binary sketches serialized by C++ code.
+ */
+public class QuantilesSketchCrossLanguageTest {
+
+  @Test(groups = {GENERATE_JAVA_FILES})
+  public void generateDoublesSketch() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10_000, 100_000, 1_000_000};
+    for (final int n: nArr) {
+      final UpdateDoublesSketch sk = DoublesSketch.builder().build();
+      for (int i = 1; i <= n; i++) sk.update(i);
+      Files.newOutputStream(javaPath.resolve("quantiles_double_n" + n + 
"_java.sk")).write(sk.toByteArray());
+    }
+  }
+
+  @Test(groups = {GENERATE_JAVA_FILES})
+  public void generateItemsSketchWithStrings() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10_000, 100_000, 1_000_000};
+    for (final int n: nArr) {
+      final ItemsSketch<String> sk = ItemsSketch.getInstance(String.class, new 
Comparator<String>() {
+        @Override
+        public int compare(final String s1, final String s2) {
+          final Integer i1 = Integer.parseInt(s1);
+          final Integer i2 = Integer.parseInt(s2);
+          return i1.compareTo(i2);
+        }
+      });
+      for (int i = 1; i <= n; i++) sk.update(Integer.toString(i));
+      if (n > 0) {
+        assertEquals(sk.getMinItem(), "1");
+        assertEquals(sk.getMaxItem(), Integer.toString(n));
+      }
+      Files.newOutputStream(javaPath.resolve("quantiles_string_n" + n + 
"_java.sk"))
+        .write(sk.toByteArray(new ArrayOfStringsSerDe()));
+    }
+  }
+
+  @Test(groups = {CHECK_CPP_FILES})
+  public void checkDoublesSketch() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10000, 100000, 1000000};
+    for (int n: nArr) {
+      final byte[] byteArr = 
Files.readAllBytes(cppPath.resolve("quantiles_double_n" + n + "_cpp.sk"));
+      final DoublesSketch sk = DoublesSketch.wrap(Memory.wrap(byteArr));
+      assertTrue(n == 0 ? sk.isEmpty() : !sk.isEmpty());
+      assertTrue(n > 128 ? sk.isEstimationMode() : !sk.isEstimationMode());
+      assertEquals(sk.getN(), n);
+      if (n > 0) {
+        assertEquals(sk.getMinItem(), 1);
+        assertEquals(sk.getMaxItem(), n);
+        QuantilesDoublesSketchIterator it = sk.iterator();
+        long weight = 0;
+        while(it.next()) {
+          assertTrue(it.getQuantile() >= sk.getMinItem());
+          assertTrue(it.getQuantile() <= sk.getMaxItem());
+          weight += it.getWeight();
+        }
+        assertEquals(weight, n);
+      }
+    }
+  }
+
+  @Test(groups = {CHECK_CPP_FILES})
+  public void checkItemsSketchWithStrings() throws IOException {
+    // sketch contains numbers in strings to make meaningful assertions
+    Comparator<String> numericOrder = new Comparator<String>() {
+      @Override
+      public int compare(final String s1, final String s2) {
+        final Integer i1 = Integer.parseInt(s1);
+        final Integer i2 = Integer.parseInt(s2);

Review Comment:
   ## Missing catch of NumberFormatException
   
   Potential uncaught 'java.lang.NumberFormatException'.
   
   [Show more 
details](https://github.com/apache/datasketches-java/security/code-scanning/633)



##########
src/test/java/org/apache/datasketches/kll/KllCrossLanguageTest.java:
##########
@@ -116,4 +102,92 @@
     assertEquals(sk.getMaxItem(), 1.0F);
   }
 
+  @Test(groups = {CHECK_CPP_FILES})
+  public void kllFloat() throws IOException {
+    final int[] nArr = {0, 10, 100, 1000, 10000, 100000, 1000000};
+    for (int n: nArr) {
+      final byte[] bytes = Files.readAllBytes(cppPath.resolve("kll_float_n" + 
n + "_cpp.sk"));
+      final KllFloatsSketch sketch = 
KllFloatsSketch.heapify(Memory.wrap(bytes));
+      assertEquals(sketch.getK(), 200);
+      assertTrue(n == 0 ? sketch.isEmpty() : !sketch.isEmpty());
+      assertTrue(n > 100 ? sketch.isEstimationMode() : 
!sketch.isEstimationMode());
+      assertEquals(sketch.getN(), n);
+      if (n > 0) {
+        assertEquals(sketch.getMinItem(), 1);
+        assertEquals(sketch.getMaxItem(), n);
+        long weight = 0;
+        QuantilesFloatsSketchIterator it = sketch.iterator();
+        while (it.next()) {
+          assertTrue(it.getQuantile() >= sketch.getMinItem());
+          assertTrue(it.getQuantile() <= sketch.getMaxItem());
+          weight += it.getWeight();
+        }
+        assertEquals(weight, n);
+      }
+    }
+  }
+
+  @Test(groups = {CHECK_CPP_FILES})
+  public void kllDouble() throws IOException {
+    final int[] nArr = {0, 10, 100, 1000, 10000, 100000, 1000000};
+    for (int n: nArr) {
+      final byte[] bytes = Files.readAllBytes(cppPath.resolve("kll_double_n" + 
n + "_cpp.sk"));
+      final KllDoublesSketch sketch = 
KllDoublesSketch.heapify(Memory.wrap(bytes));
+      assertEquals(sketch.getK(), 200);
+      assertTrue(n == 0 ? sketch.isEmpty() : !sketch.isEmpty());
+      assertTrue(n > 100 ? sketch.isEstimationMode() : 
!sketch.isEstimationMode());
+      assertEquals(sketch.getN(), n);
+      if (n > 0) {
+        assertEquals(sketch.getMinItem(), 1);
+        assertEquals(sketch.getMaxItem(), n);
+        long weight = 0;
+        QuantilesDoublesSketchIterator it = sketch.iterator();
+        while (it.next()) {
+          assertTrue(it.getQuantile() >= sketch.getMinItem());
+          assertTrue(it.getQuantile() <= sketch.getMaxItem());
+          weight += it.getWeight();
+        }
+        assertEquals(weight, n);
+      }
+    }
+  }
+
+  @Test(groups = {CHECK_CPP_FILES})
+  public void kllString() throws IOException {
+    // sketch contains numbers in strings to make meaningful assertions
+    Comparator<String> numericOrder = new Comparator<String>() {
+      @Override
+      public int compare(final String s1, final String s2) {
+        final Integer i1 = Integer.parseInt(s1);

Review Comment:
   ## Boxed variable is never null
   
   The variable 'i1' is only assigned values of primitive type and is never 
'null', but it is declared with the boxed type 'Integer'.
   
   [Show more 
details](https://github.com/apache/datasketches-java/security/code-scanning/625)



##########
src/test/java/org/apache/datasketches/kll/KllCrossLanguageTest.java:
##########
@@ -116,4 +102,92 @@
     assertEquals(sk.getMaxItem(), 1.0F);
   }
 
+  @Test(groups = {CHECK_CPP_FILES})
+  public void kllFloat() throws IOException {
+    final int[] nArr = {0, 10, 100, 1000, 10000, 100000, 1000000};
+    for (int n: nArr) {
+      final byte[] bytes = Files.readAllBytes(cppPath.resolve("kll_float_n" + 
n + "_cpp.sk"));
+      final KllFloatsSketch sketch = 
KllFloatsSketch.heapify(Memory.wrap(bytes));
+      assertEquals(sketch.getK(), 200);
+      assertTrue(n == 0 ? sketch.isEmpty() : !sketch.isEmpty());
+      assertTrue(n > 100 ? sketch.isEstimationMode() : 
!sketch.isEstimationMode());
+      assertEquals(sketch.getN(), n);
+      if (n > 0) {
+        assertEquals(sketch.getMinItem(), 1);
+        assertEquals(sketch.getMaxItem(), n);
+        long weight = 0;
+        QuantilesFloatsSketchIterator it = sketch.iterator();
+        while (it.next()) {
+          assertTrue(it.getQuantile() >= sketch.getMinItem());
+          assertTrue(it.getQuantile() <= sketch.getMaxItem());
+          weight += it.getWeight();
+        }
+        assertEquals(weight, n);
+      }
+    }
+  }
+
+  @Test(groups = {CHECK_CPP_FILES})
+  public void kllDouble() throws IOException {
+    final int[] nArr = {0, 10, 100, 1000, 10000, 100000, 1000000};
+    for (int n: nArr) {
+      final byte[] bytes = Files.readAllBytes(cppPath.resolve("kll_double_n" + 
n + "_cpp.sk"));
+      final KllDoublesSketch sketch = 
KllDoublesSketch.heapify(Memory.wrap(bytes));
+      assertEquals(sketch.getK(), 200);
+      assertTrue(n == 0 ? sketch.isEmpty() : !sketch.isEmpty());
+      assertTrue(n > 100 ? sketch.isEstimationMode() : 
!sketch.isEstimationMode());
+      assertEquals(sketch.getN(), n);
+      if (n > 0) {
+        assertEquals(sketch.getMinItem(), 1);
+        assertEquals(sketch.getMaxItem(), n);
+        long weight = 0;
+        QuantilesDoublesSketchIterator it = sketch.iterator();
+        while (it.next()) {
+          assertTrue(it.getQuantile() >= sketch.getMinItem());
+          assertTrue(it.getQuantile() <= sketch.getMaxItem());
+          weight += it.getWeight();
+        }
+        assertEquals(weight, n);
+      }
+    }
+  }
+
+  @Test(groups = {CHECK_CPP_FILES})
+  public void kllString() throws IOException {
+    // sketch contains numbers in strings to make meaningful assertions
+    Comparator<String> numericOrder = new Comparator<String>() {
+      @Override
+      public int compare(final String s1, final String s2) {
+        final Integer i1 = Integer.parseInt(s1);

Review Comment:
   ## Missing catch of NumberFormatException
   
   Potential uncaught 'java.lang.NumberFormatException'.
   
   [Show more 
details](https://github.com/apache/datasketches-java/security/code-scanning/628)



##########
src/test/java/org/apache/datasketches/quantiles/QuantilesSketchCrossLanguageTest.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.quantiles;
+
+import static org.apache.datasketches.common.TestUtil.CHECK_CPP_FILES;
+import static org.apache.datasketches.common.TestUtil.GENERATE_JAVA_FILES;
+import static org.apache.datasketches.common.TestUtil.cppPath;
+import static org.apache.datasketches.common.TestUtil.javaPath;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.Comparator;
+
+import org.apache.datasketches.common.ArrayOfStringsSerDe;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.quantilescommon.QuantilesDoublesSketchIterator;
+import org.apache.datasketches.quantilescommon.QuantilesGenericSketchIterator;
+import org.testng.annotations.Test;
+
+/**
+ * Serialize binary sketches to be tested by C++ code.
+ * Test deserialization of binary sketches serialized by C++ code.
+ */
+public class QuantilesSketchCrossLanguageTest {
+
+  @Test(groups = {GENERATE_JAVA_FILES})
+  public void generateDoublesSketch() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10_000, 100_000, 1_000_000};
+    for (final int n: nArr) {
+      final UpdateDoublesSketch sk = DoublesSketch.builder().build();
+      for (int i = 1; i <= n; i++) sk.update(i);
+      Files.newOutputStream(javaPath.resolve("quantiles_double_n" + n + 
"_java.sk")).write(sk.toByteArray());
+    }
+  }
+
+  @Test(groups = {GENERATE_JAVA_FILES})
+  public void generateItemsSketchWithStrings() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10_000, 100_000, 1_000_000};
+    for (final int n: nArr) {
+      final ItemsSketch<String> sk = ItemsSketch.getInstance(String.class, new 
Comparator<String>() {
+        @Override
+        public int compare(final String s1, final String s2) {
+          final Integer i1 = Integer.parseInt(s1);
+          final Integer i2 = Integer.parseInt(s2);

Review Comment:
   ## Missing catch of NumberFormatException
   
   Potential uncaught 'java.lang.NumberFormatException'.
   
   [Show more 
details](https://github.com/apache/datasketches-java/security/code-scanning/631)



##########
src/test/java/org/apache/datasketches/quantiles/QuantilesSketchCrossLanguageTest.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.quantiles;
+
+import static org.apache.datasketches.common.TestUtil.CHECK_CPP_FILES;
+import static org.apache.datasketches.common.TestUtil.GENERATE_JAVA_FILES;
+import static org.apache.datasketches.common.TestUtil.cppPath;
+import static org.apache.datasketches.common.TestUtil.javaPath;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.Comparator;
+
+import org.apache.datasketches.common.ArrayOfStringsSerDe;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.quantilescommon.QuantilesDoublesSketchIterator;
+import org.apache.datasketches.quantilescommon.QuantilesGenericSketchIterator;
+import org.testng.annotations.Test;
+
+/**
+ * Serialize binary sketches to be tested by C++ code.
+ * Test deserialization of binary sketches serialized by C++ code.
+ */
+public class QuantilesSketchCrossLanguageTest {
+
+  @Test(groups = {GENERATE_JAVA_FILES})
+  public void generateDoublesSketch() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10_000, 100_000, 1_000_000};
+    for (final int n: nArr) {
+      final UpdateDoublesSketch sk = DoublesSketch.builder().build();
+      for (int i = 1; i <= n; i++) sk.update(i);
+      Files.newOutputStream(javaPath.resolve("quantiles_double_n" + n + 
"_java.sk")).write(sk.toByteArray());
+    }
+  }
+
+  @Test(groups = {GENERATE_JAVA_FILES})
+  public void generateItemsSketchWithStrings() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10_000, 100_000, 1_000_000};
+    for (final int n: nArr) {
+      final ItemsSketch<String> sk = ItemsSketch.getInstance(String.class, new 
Comparator<String>() {
+        @Override
+        public int compare(final String s1, final String s2) {
+          final Integer i1 = Integer.parseInt(s1);
+          final Integer i2 = Integer.parseInt(s2);
+          return i1.compareTo(i2);
+        }
+      });
+      for (int i = 1; i <= n; i++) sk.update(Integer.toString(i));
+      if (n > 0) {
+        assertEquals(sk.getMinItem(), "1");
+        assertEquals(sk.getMaxItem(), Integer.toString(n));
+      }
+      Files.newOutputStream(javaPath.resolve("quantiles_string_n" + n + 
"_java.sk"))
+        .write(sk.toByteArray(new ArrayOfStringsSerDe()));
+    }
+  }
+
+  @Test(groups = {CHECK_CPP_FILES})
+  public void checkDoublesSketch() throws IOException {
+    final int[] nArr = {0, 1, 10, 100, 1000, 10000, 100000, 1000000};
+    for (int n: nArr) {
+      final byte[] byteArr = 
Files.readAllBytes(cppPath.resolve("quantiles_double_n" + n + "_cpp.sk"));
+      final DoublesSketch sk = DoublesSketch.wrap(Memory.wrap(byteArr));
+      assertTrue(n == 0 ? sk.isEmpty() : !sk.isEmpty());
+      assertTrue(n > 128 ? sk.isEstimationMode() : !sk.isEstimationMode());
+      assertEquals(sk.getN(), n);
+      if (n > 0) {
+        assertEquals(sk.getMinItem(), 1);
+        assertEquals(sk.getMaxItem(), n);
+        QuantilesDoublesSketchIterator it = sk.iterator();
+        long weight = 0;
+        while(it.next()) {
+          assertTrue(it.getQuantile() >= sk.getMinItem());
+          assertTrue(it.getQuantile() <= sk.getMaxItem());
+          weight += it.getWeight();
+        }
+        assertEquals(weight, n);
+      }
+    }
+  }
+
+  @Test(groups = {CHECK_CPP_FILES})
+  public void checkItemsSketchWithStrings() throws IOException {
+    // sketch contains numbers in strings to make meaningful assertions
+    Comparator<String> numericOrder = new Comparator<String>() {
+      @Override
+      public int compare(final String s1, final String s2) {
+        final Integer i1 = Integer.parseInt(s1);

Review Comment:
   ## Missing catch of NumberFormatException
   
   Potential uncaught 'java.lang.NumberFormatException'.
   
   [Show more 
details](https://github.com/apache/datasketches-java/security/code-scanning/632)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to