changeset 1071:df151f9c0071

7901227: Division by zero in ListStatistics.getVariance() with N=1 Summary: Fix the calculation and do more tests.
author shade
date Mon, 22 Dec 2014 17:47:35 +0300
parents 340244351917
children 21d5793de4c0
files jmh-core/src/main/java/org/openjdk/jmh/util/ListStatistics.java jmh-core/src/test/java/org/openjdk/jmh/util/TestListStatistics.java jmh-core/src/test/java/org/openjdk/jmh/util/TestMultisetStatistics.java
diffstat 3 files changed, 81 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/ListStatistics.java	Mon Dec 22 17:12:32 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/ListStatistics.java	Mon Dec 22 17:47:35 2014 +0300
@@ -67,7 +67,7 @@
 
     @Override
     public double getMax() {
-        if (getN() > 0) {
+        if (count > 0) {
             double m = Double.NEGATIVE_INFINITY;
             for (int i = 0; i < count; i++) {
                 m = Math.max(m, values[i]);
@@ -80,7 +80,7 @@
 
     @Override
     public double getMin() {
-        if (getN() > 0) {
+        if (count > 0) {
             double m = Double.POSITIVE_INFINITY;
             for (int i = 0; i < count; i++) {
                 m = Math.min(m, values[i]);
@@ -98,7 +98,7 @@
 
     @Override
     public double getSum() {
-        if (getN() > 0) {
+        if (count > 0) {
             double s = 0;
             for (int i = 0; i < count; i++) {
                 s += values[i];
@@ -128,13 +128,13 @@
 
     @Override
     public double getVariance() {
-        if (getN() > 0) {
+        if (count > 1) {
             double v = 0;
             double m = getMean();
             for (int i = 0; i < count; i++) {
                 v += Math.pow(values[i] - m, 2);
             }
-            return v / (getN() - 1);
+            return v / (count - 1);
         } else {
             return Double.NaN;
         }
--- a/jmh-core/src/test/java/org/openjdk/jmh/util/TestListStatistics.java	Mon Dec 22 17:12:32 2014 +0300
+++ b/jmh-core/src/test/java/org/openjdk/jmh/util/TestListStatistics.java	Mon Dec 22 17:47:35 2014 +0300
@@ -231,4 +231,41 @@
         Assert.assertEquals("compareTo at 0.9999", 0, s1.compareTo(s2, 0.9999));
     }
 
+    @Test
+    public strictfp void testEmpty() {
+        Statistics s = new ListStatistics();
+
+        Assert.assertEquals(0, s.getN());
+        Assert.assertEquals(Double.NaN, s.getSum());
+        Assert.assertEquals(Double.NaN, s.getMin());
+        Assert.assertEquals(Double.NaN, s.getMax());
+        Assert.assertEquals(Double.NaN, s.getMean());
+        Assert.assertEquals(Double.NaN, s.getMeanErrorAt(0.5));
+        Assert.assertEquals(Double.NaN, s.getVariance());
+        Assert.assertEquals(Double.NaN, s.getStandardDeviation());
+        Assert.assertEquals(Double.NaN, s.getConfidenceIntervalAt(0.50)[0]);
+        Assert.assertEquals(Double.NaN, s.getConfidenceIntervalAt(0.50)[1]);
+        Assert.assertEquals(Double.NaN, s.getPercentile(0));
+        Assert.assertEquals(Double.NaN, s.getPercentile(100));
+    }
+
+    @Test
+    public strictfp void testSingle() {
+        ListStatistics s = new ListStatistics();
+        s.addValue(42.0D);
+
+        Assert.assertEquals(1, s.getN());
+        Assert.assertEquals(42.0D, s.getSum());
+        Assert.assertEquals(42.0D, s.getMin());
+        Assert.assertEquals(42.0D, s.getMax());
+        Assert.assertEquals(42.0D, s.getMean());
+        Assert.assertEquals(Double.NaN, s.getMeanErrorAt(0.5));
+        Assert.assertEquals(Double.NaN, s.getVariance());
+        Assert.assertEquals(Double.NaN, s.getStandardDeviation());
+        Assert.assertEquals(Double.NaN, s.getConfidenceIntervalAt(0.50)[0]);
+        Assert.assertEquals(Double.NaN, s.getConfidenceIntervalAt(0.50)[1]);
+        Assert.assertEquals(42.0D, s.getPercentile(0));
+        Assert.assertEquals(42.0D, s.getPercentile(100));
+    }
+
 }
--- a/jmh-core/src/test/java/org/openjdk/jmh/util/TestMultisetStatistics.java	Mon Dec 22 17:12:32 2014 +0300
+++ b/jmh-core/src/test/java/org/openjdk/jmh/util/TestMultisetStatistics.java	Mon Dec 22 17:47:35 2014 +0300
@@ -224,4 +224,43 @@
         Assert.assertEquals("compareTo at 0.9999", 0, s1.compareTo(s2, 0.9999));
     }
 
+
+    @Test
+    public strictfp void testEmpty() {
+        Statistics s = new ListStatistics();
+
+        Assert.assertEquals(0, s.getN());
+        Assert.assertEquals(Double.NaN, s.getSum());
+        Assert.assertEquals(Double.NaN, s.getMin());
+        Assert.assertEquals(Double.NaN, s.getMax());
+        Assert.assertEquals(Double.NaN, s.getMean());
+        Assert.assertEquals(Double.NaN, s.getMeanErrorAt(0.5));
+        Assert.assertEquals(Double.NaN, s.getVariance());
+        Assert.assertEquals(Double.NaN, s.getStandardDeviation());
+        Assert.assertEquals(Double.NaN, s.getConfidenceIntervalAt(0.50)[0]);
+        Assert.assertEquals(Double.NaN, s.getConfidenceIntervalAt(0.50)[1]);
+        Assert.assertEquals(Double.NaN, s.getPercentile(0));
+        Assert.assertEquals(Double.NaN, s.getPercentile(100));
+    }
+
+    @Test
+    public strictfp void testSingle() {
+        MultisetStatistics s = new MultisetStatistics();
+        s.addValue(42.0D, 1);
+
+        Assert.assertEquals(1, s.getN());
+        Assert.assertEquals(42.0D, s.getSum());
+        Assert.assertEquals(42.0D, s.getMin());
+        Assert.assertEquals(42.0D, s.getMax());
+        Assert.assertEquals(42.0D, s.getMean());
+        Assert.assertEquals(Double.NaN, s.getMeanErrorAt(0.5));
+        Assert.assertEquals(Double.NaN, s.getVariance());
+        Assert.assertEquals(Double.NaN, s.getStandardDeviation());
+        Assert.assertEquals(Double.NaN, s.getConfidenceIntervalAt(0.50)[0]);
+        Assert.assertEquals(Double.NaN, s.getConfidenceIntervalAt(0.50)[1]);
+        Assert.assertEquals(42.0D, s.getPercentile(0));
+        Assert.assertEquals(42.0D, s.getPercentile(100));
+    }
+
+
 }