From 0edbe9594c4f1ff8c70dc81eb38141a12878ad04 Mon Sep 17 00:00:00 2001 From: David Dixon-Peugh Date: Wed, 28 Aug 2002 21:20:41 +0000 Subject: [PATCH] Added test to check for Metrics on the Report. git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@816 51baf565-9d33-0410-a72c-fc3788e3496d --- .../pmd/stat/StatisticalRuleTest.java | 27 ++++++++++++++++++- .../sourceforge/pmd/stat/StatisticalRule.java | 18 ++++++------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/pmd/regress/test/net/sourceforge/pmd/stat/StatisticalRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/stat/StatisticalRuleTest.java index 4d912aa6cd..fc38de90c3 100644 --- a/pmd/regress/test/net/sourceforge/pmd/stat/StatisticalRuleTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/stat/StatisticalRuleTest.java @@ -1,5 +1,6 @@ package test.net.sourceforge.pmd.stat; +import java.util.Iterator; import java.util.List; import java.util.ArrayList; import java.util.Random; @@ -60,7 +61,7 @@ public class StatisticalRuleTest public static final double MEAN = 499.5; - public static final double SIGMA = 288.8194; + public static final double SIGMA = 288.6750; public static final int NUM_TESTS = 32; public StatisticalRuleTest(String name) @@ -81,6 +82,30 @@ public class StatisticalRuleTest } } + /** + * This test verifies that the Stat rule creates a Metric, + * with the proper values. + */ + public void testMetrics() throws Throwable + { + Report report = makeReport( IUT ); + Iterator metrics = report.metrics(); + + assertTrue( metrics.hasNext()); + Object o = metrics.next(); + + assertTrue( o instanceof Metric); + Metric m = (Metric) o; + + assertEquals("test.net.sourceforge.pmd.stat.MockStatisticalRule", + m.getMetricName()); + + assertEquals( 0.0, m.getLowValue(), 0.05 ); + assertEquals( 999.0, m.getHighValue(), 0.05 ); + assertEquals( MEAN, m.getAverage(), 0.05 ); + assertEquals( SIGMA, m.getStandardDeviation(), 0.05 ); + } + /** * This returns a Random value for Sigma which will * return some values. diff --git a/pmd/src/net/sourceforge/pmd/stat/StatisticalRule.java b/pmd/src/net/sourceforge/pmd/stat/StatisticalRule.java index 2f363ec700..7a974c927d 100644 --- a/pmd/src/net/sourceforge/pmd/stat/StatisticalRule.java +++ b/pmd/src/net/sourceforge/pmd/stat/StatisticalRule.java @@ -24,11 +24,14 @@ public abstract class StatisticalRule extends AbstractRule { private int count = 0; private double total = 0.0; - + private double totalSquared = 0.0; + public void addDataPoint( DataPoint point ) { count++; total += point.getScore(); + totalSquared += point.getScore() * point.getScore(); + dataPoints.add( point ); } @@ -61,8 +64,8 @@ public abstract class StatisticalRule extends AbstractRule { makeViolations(ctx, newPoints); - double low = ((DataPoint) newPoints.first()).getScore(); - double high = ((DataPoint) newPoints.last()).getScore(); + double low = ((DataPoint) dataPoints.first()).getScore(); + double high = ((DataPoint) dataPoints.last()).getScore(); ctx.getReport().addMetric( new Metric( this.getName(), low, high, getMean(), getStdDev())); @@ -77,13 +80,8 @@ public abstract class StatisticalRule extends AbstractRule { Iterator points = dataPoints.iterator(); - while (points.hasNext()) { - DataPoint point = (DataPoint) points.next(); - varTotal += ((point.getScore() - getMean()) * - (point.getScore() - getMean())); - } - - double variance = varTotal / count; + double variance = ((totalSquared / count) - + (getMean() * getMean() )); return Math.sqrt( variance ); }