changeset 1417:36a2ee9a075e

7901930: MultisetStatistics replies +Inf, -Inf when empty
author shade
date Tue, 28 Mar 2017 18:34:51 +0200
parents 704bfc869f77
children 4dce5a497417
files jmh-core/src/main/java/org/openjdk/jmh/util/MultisetStatistics.java jmh-core/src/test/java/org/openjdk/jmh/util/TestMultisetStatistics.java
diffstat 2 files changed, 27 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/MultisetStatistics.java	Thu Mar 16 20:59:37 2017 +0100
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/MultisetStatistics.java	Tue Mar 28 18:34:51 2017 +0200
@@ -41,20 +41,28 @@
 
     @Override
     public double getMax() {
-        double max = Double.NEGATIVE_INFINITY;
-        for (double d : values.keys()) {
-            max = Math.max(max, d);
+        if (!values.isEmpty()) {
+            double max = Double.NEGATIVE_INFINITY;
+            for (double d : values.keys()) {
+                max = Math.max(max, d);
+            }
+            return max;
+        } else {
+            return Double.NaN;
         }
-        return max;
     }
 
     @Override
     public double getMin() {
-        double min = Double.POSITIVE_INFINITY;
-        for (double d : values.keys()) {
-            min = Math.min(min, d);
+        if (!values.isEmpty()) {
+            double min = Double.POSITIVE_INFINITY;
+            for (double d : values.keys()) {
+                min = Math.min(min, d);
+            }
+            return min;
+        } else {
+            return Double.NaN;
         }
-        return min;
     }
 
     @Override
@@ -64,11 +72,15 @@
 
     @Override
     public double getSum() {
-        double sum = 0;
-        for (double d : values.keys()) {
-            sum += d*values.count(d);
+        if (!values.isEmpty()) {
+            double sum = 0;
+            for (double d : values.keys()) {
+                sum += d * values.count(d);
+            }
+            return sum;
+        } else {
+            return Double.NaN;
         }
-        return sum;
     }
 
     private double get(long index) {
--- a/jmh-core/src/test/java/org/openjdk/jmh/util/TestMultisetStatistics.java	Thu Mar 16 20:59:37 2017 +0100
+++ b/jmh-core/src/test/java/org/openjdk/jmh/util/TestMultisetStatistics.java	Tue Mar 28 18:34:51 2017 +0200
@@ -59,8 +59,8 @@
      */
     @Test
     public strictfp void testAdd_double() {
-        ListStatistics stats = new ListStatistics();
-        stats.addValue(VALUES[0]);
+        MultisetStatistics stats = new MultisetStatistics();
+        stats.addValue(VALUES[0], 1);
         assertEquals(1, stats.getN());
         assertEquals(VALUES[0], stats.getSum(), 0.0);
         assertEquals(VALUES[0], stats.getMax(), 0.0);
@@ -229,7 +229,7 @@
 
     @Test
     public strictfp void testEmpty() {
-        Statistics s = new ListStatistics();
+        Statistics s = new MultisetStatistics();
 
         Assert.assertEquals(0, s.getN());
         Assert.assertEquals(Double.NaN, s.getSum());