From ad3f0618f9c8e84a1b1ad7e3c3945bf6cb5a91d7 Mon Sep 17 00:00:00 2001 From: zenotme Date: Mon, 5 Jan 2026 21:32:54 +0800 Subject: [PATCH] Fix BinomialBoundsN may divide 0 --- .../thetacommon/BinomialBoundsN.java | 2 +- .../thetacommon/BinomialBoundsNTest.java | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/apache/datasketches/thetacommon/BinomialBoundsN.java b/src/main/java/org/apache/datasketches/thetacommon/BinomialBoundsN.java index 1f06d5702..767a43e92 100644 --- a/src/main/java/org/apache/datasketches/thetacommon/BinomialBoundsN.java +++ b/src/main/java/org/apache/datasketches/thetacommon/BinomialBoundsN.java @@ -272,7 +272,7 @@ static void checkArgs(final long numSamples, final double theta, final int numSD "numSDev must only be 1,2, or 3 and numSamples must >= 0: numSDev=" + numSDev + ", numSamples=" + numSamples); } - if ((theta < 0.0) || (theta > 1.0)) { + if ((theta <= 0.0) || (theta > 1.0)) { throw new SketchesArgumentException("0.0 < theta <= 1.0: " + theta); } } diff --git a/src/test/java/org/apache/datasketches/thetacommon/BinomialBoundsNTest.java b/src/test/java/org/apache/datasketches/thetacommon/BinomialBoundsNTest.java index c991432a9..8fa709074 100644 --- a/src/test/java/org/apache/datasketches/thetacommon/BinomialBoundsNTest.java +++ b/src/test/java/org/apache/datasketches/thetacommon/BinomialBoundsNTest.java @@ -25,6 +25,7 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; +import static org.testng.Assert.assertThrows; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.thetacommon.BinomialBoundsN; @@ -119,17 +120,17 @@ public static void checkBounds() { @Test public static void checkCheckArgs() { - try { - checkArgs(-1L, 1.0, 1); - checkArgs(10L, 0.0, 1); - checkArgs(10L, 1.01, 1); - checkArgs(10L, 1.0, 3); - checkArgs(10L, 1.0, 0); - checkArgs(10L, 1.0, 4); - fail("Expected SketchesArgumentException"); - } catch (final SketchesArgumentException e) { - //pass - } + assertThrows(SketchesArgumentException.class, + () -> checkArgs(-1L, 1.0, 1)); + assertThrows(SketchesArgumentException.class, + () -> checkArgs(10L, 0.0, 1)); + assertThrows(SketchesArgumentException.class, + () -> checkArgs(10L, 1.01, 1)); + checkArgs(10L, 1.0, 3); + assertThrows(SketchesArgumentException.class, + () -> checkArgs(10L, 1.0, 0)); + assertThrows(SketchesArgumentException.class, + () -> checkArgs(10L, 1.0, 4)); } @Test