From 9aa3003022a180be0cf530093de424e3cd34a786 Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Wed, 4 Sep 2002 18:42:58 +0000 Subject: [PATCH] reduced NUMTESTS to 10 git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@839 51baf565-9d33-0410-a72c-fc3788e3496d --- .../pmd/stat/StatisticalRuleTest.java | 226 +++++++++--------- 1 file changed, 113 insertions(+), 113 deletions(-) diff --git a/pmd/regress/test/net/sourceforge/pmd/stat/StatisticalRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/stat/StatisticalRuleTest.java index d65b114a9f..a0b1d49cb8 100644 --- a/pmd/regress/test/net/sourceforge/pmd/stat/StatisticalRuleTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/stat/StatisticalRuleTest.java @@ -13,30 +13,30 @@ import junit.framework.TestCase; /** * This class tests the Statistical Rules in PMD. - * + * * The idea is, that we fill up 999 datapoints into * the Stat Rule, and then throw random parameters * at it. - * + * * The three parameters which are checked are: * sigma - # Sigmas over the mean. * topscore - Only the top 5 or so items. * minimum - Only things of score 10 or better - * + * * When more than one parameter is lumped together, then - * we expect the one which would return the fewest to + * we expect the one which would return the fewest to * determine what gets sent back. - * + * * So, we throw each collection of parameters, where each * one is a different order into the system. We check the * results off of what the smallest value should be. - * + * * If you are going to work with StatisticalRule any, please * bump the "NUM_TESTS" number up to something like 128. That * way you are more likely to identify problems. It is set low * now to make building and running tests easier (when we aren't * touching the file.) - * + * * Note also, that when verifying the Sigma, I wasn't quite able * to determine how many results it would return (it would vary * from -2 to 2 of what I expected.) That is what the delta @@ -58,15 +58,15 @@ public class StatisticalRuleTest public static final double NO_SIGMA = -1.0; public static final int MIN_TOPSCORE = 0; public static final int NO_TOPSCORE = -1; - - + + public static final double MEAN = 499.5; public static final double SIGMA = 288.6750; - public static final int NUM_TESTS = 512; - + public static final int NUM_TESTS = 10; + public static final double DELTA = 0.005; - - public StatisticalRuleTest(String name) + + public StatisticalRuleTest(String name) { super( name ); this.testName = name; @@ -92,72 +92,72 @@ public class StatisticalRuleTest { 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", + + 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. */ public double randomSigma() { - return random.nextDouble() * 1.0; + return random.nextDouble() * 1.0; } - + /** * This returns a Random value for Sigma which value * is greater than the parameter. */ public double randomSigma(int minimum) { double minSigma = ((999 - minimum) - MEAN)/SIGMA; - + if ((minSigma <= 0) || (minSigma > 2)) return randomSigma(); - + return minSigma + (random.nextDouble() * (2 - minSigma)); } - + /** * This returns the expected number of results when * the Sigma rating is the smallest. */ public int expectedSigma(double sigma) { long expectedMin = Math.round(MEAN + (sigma * SIGMA)); - - if ((999 - expectedMin) < 0) return 0; + + if ((999 - expectedMin) < 0) return 0; return 999 - (int) expectedMin; } - + /** * This generates a random minimum value for testing. */ public double randomMinimum() { return random.nextDouble() * 999; - } - + } + /** * This generates a random minimum value for which fewer * results would be returned. */ public double randomMinimum(int minimum) { double diffTarget = 1.0 * (999 - minimum); - return (random.nextDouble() * minimum) + diffTarget; + return (random.nextDouble() * minimum) + diffTarget; } - + /** * This returns the expected number of reports. - * + * * If the Minimum comes in at 521.569 then we expect * 522, 523, ... 999 will pass. */ @@ -165,30 +165,30 @@ public class StatisticalRuleTest Double d = new Double( minimum ); return 999 - d.intValue(); } - + public void testExpectedMinimum() { for (int i = 0; i < 999; i++) { assertEquals("Integer Min", 999 - i, expectedMinimum( i * 1.0 )); assertEquals("Double Min", 999 - i, expectedMinimum( (i * 1.0) + 0.5 )); - } + } } /** * This returns a random value for Top Score. */ public int randomTopScore() { - return random.nextInt(999); + return random.nextInt(999); } - + /** * This will return a random value for the Top Score * which will return more than the minimum provided. */ public int randomTopScore(double target) { if (target < 0) return 0; - + return random.nextInt( (new Double(target)).intValue() ); } - + /** * This will return the expected number of results * with the given Top Score. @@ -232,161 +232,161 @@ public class StatisticalRuleTest public void testS() throws Throwable { verifyResults( MAX_SIGMA, NO_MINIMUM, NO_TOPSCORE, 0, 2 ); - + for (int i = 0; i < NUM_TESTS; i++) { double sigma = randomSigma(); - verifyResults( sigma, -1.0, -1, expectedSigma( sigma ), 2); - } - } - + verifyResults( sigma, -1.0, -1, expectedSigma( sigma ), 2); + } + } + public void testT() throws Throwable { verifyResults( NO_SIGMA, NO_MINIMUM, MIN_TOPSCORE, 0, 0 ); - + for (int i = 0; i < NUM_TESTS; i++) { int topScore = randomTopScore(); - verifyResults( -1.0, -1.0, topScore, expectedTopScore(topScore), 0); - } + verifyResults( -1.0, -1.0, topScore, expectedTopScore(topScore), 0); + } } - + public void testM() throws Throwable { verifyResults( NO_SIGMA, MAX_MINIMUM, NO_TOPSCORE, 0, 0 ); - + for (int i = 0; i < NUM_TESTS; i++) { - double minimum = randomMinimum(); + double minimum = randomMinimum(); verifyResults( -1.0, minimum, -1, expectedMinimum( minimum ), 0 ); } } - + public void testST() throws Throwable - { + { verifyResults( randomSigma(), NO_MINIMUM, MIN_TOPSCORE, 0, 0 ); - + for (int i = 0; i < NUM_TESTS; i++) { double sigma = randomSigma(); int topScore = randomTopScore( expectedSigma( sigma )); - - verifyResults( sigma, NO_MINIMUM, topScore, expectedTopScore( topScore ), 0); + + verifyResults( sigma, NO_MINIMUM, topScore, expectedTopScore( topScore ), 0); } } public void testTS() throws Throwable - { + { verifyResults( MAX_SIGMA, NO_MINIMUM, randomTopScore(), 0, 0 ); - + for (int i = 0; i < NUM_TESTS; i++) { int topScore = randomTopScore(); double sigma = randomSigma( expectedTopScore(topScore) ); - - verifyResults( sigma, -1.0, topScore, expectedSigma( sigma ), 2); + + verifyResults( sigma, -1.0, topScore, expectedSigma( sigma ), 2); } } public void testSM() throws Throwable - { + { verifyResults( randomSigma(), MAX_MINIMUM, NO_TOPSCORE, 0, 0); for (int i = 0; i < NUM_TESTS; i++) { double sigma = randomSigma(); double minimum = randomMinimum( expectedSigma( sigma )); - - verifyResults( sigma, minimum, -1, expectedMinimum( minimum ), 0); + + verifyResults( sigma, minimum, -1, expectedMinimum( minimum ), 0); } - + } public void testMS() throws Throwable - { + { verifyResults(MAX_SIGMA, randomMinimum(), NO_TOPSCORE, 0, 0); for (int i = 0; i < NUM_TESTS; i++) { double minimum = randomMinimum(); double sigma = randomSigma( expectedMinimum( minimum )); - - verifyResults( sigma, minimum, -1, expectedSigma( sigma ), 2 ); + + verifyResults( sigma, minimum, -1, expectedSigma( sigma ), 2 ); } } public void testTM() throws Throwable - { + { verifyResults(NO_SIGMA, MAX_MINIMUM, randomTopScore(), 0, 0); for (int i = 0; i < NUM_TESTS; i++) { int topScore = randomTopScore(); double minimum = randomMinimum( expectedTopScore( topScore )); - - verifyResults( NO_SIGMA, minimum, topScore, expectedMinimum( minimum ), 0 ); + + verifyResults( NO_SIGMA, minimum, topScore, expectedMinimum( minimum ), 0 ); } } public void testMT() throws Throwable - { + { verifyResults(NO_SIGMA, randomMinimum(), MIN_TOPSCORE, 0, 0); for (int i = 0; i < NUM_TESTS; i++) { double minimum = randomMinimum(); int topScore = randomTopScore( expectedMinimum( minimum )); - - verifyResults( NO_SIGMA, minimum, topScore, expectedTopScore(topScore), 0); + + verifyResults( NO_SIGMA, minimum, topScore, expectedTopScore(topScore), 0); } } - + public void testSTM() throws Throwable - { + { double sigma = randomSigma(); verifyResults(sigma, MAX_MINIMUM, randomTopScore( expectedSigma( sigma )), 0, 0); - + for (int i = 0; i < NUM_TESTS; i++) { sigma = randomSigma(); int topScore = randomTopScore( expectedSigma( sigma )); double minimum = randomMinimum( expectedTopScore( topScore )); - + verifyResults( sigma, minimum, topScore, expectedMinimum( minimum ), 0 ); } } public void testSMT() throws Throwable { - double sigma = randomSigma(); - verifyResults(sigma, randomMinimum(expectedSigma( sigma )), MIN_TOPSCORE, + double sigma = randomSigma(); + verifyResults(sigma, randomMinimum(expectedSigma( sigma )), MIN_TOPSCORE, 0, 0 ); - + for (int i = 0; i < NUM_TESTS; i++) { sigma = randomSigma(); double minimum = randomMinimum( expectedSigma( sigma )); int topScore = randomTopScore( expectedMinimum( minimum )); - - verifyResults( sigma, minimum, topScore, + + verifyResults( sigma, minimum, topScore, expectedTopScore( topScore ), 0 ); } } - + public void testTSM() throws Throwable { int topScore = randomTopScore(); verifyResults(randomSigma( expectedTopScore( topScore )), MAX_MINIMUM, topScore, 0, 0); - + for (int i = 0; i < NUM_TESTS; i++) { topScore = randomTopScore(); double sigma = randomSigma( expectedTopScore( topScore )); double minimum = randomMinimum( expectedSigma( sigma )); - + verifyResults( sigma, minimum, topScore, - expectedMinimum( minimum ), 0 ); - } + expectedMinimum( minimum ), 0 ); + } } - + public void testTMS() throws Throwable - { + { int topScore = randomTopScore(); - verifyResults( MAX_SIGMA, randomMinimum( expectedTopScore( topScore )), + verifyResults( MAX_SIGMA, randomMinimum( expectedTopScore( topScore )), topScore, 0, 0 ); - + for (int i = 0; i < NUM_TESTS; i++) { topScore = randomTopScore(); double minimum = randomMinimum( expectedTopScore( topScore )); double sigma = randomSigma( expectedMinimum( minimum )); - + verifyResults( sigma, minimum, topScore, - expectedSigma( sigma ), 2 ); + expectedSigma( sigma ), 2 ); } } @@ -396,31 +396,31 @@ public class StatisticalRuleTest * Usually DELTA is only used for Sigma, as we really can't * calculate it exactly. */ - - public void verifyResults( double sigma, double minimum, - int topScore, int expected, int delta ) + + public void verifyResults( double sigma, double minimum, + int topScore, int expected, int delta ) { try { setUp(); if (sigma >= 0) { - IUT.addProperty("sigma", Double.toString(sigma)); - } - + IUT.addProperty("sigma", Double.toString(sigma)); + } + if (minimum >= 0) { IUT.addProperty("minimum", Double.toString(minimum)); } - + if (topScore >= 0) { IUT.addProperty("topscore", Integer.toString(topScore)); } - + Report report = makeReport( IUT ); if (delta == 0) { assertEquals("Unexpected number of results: sigma= " + - Double.toString(sigma) + " min= " + + Double.toString(sigma) + " min= " + Double.toString(minimum) + " topscore= " + - Integer.toString(topScore), - expected, report.size()); + Integer.toString(topScore), + expected, report.size()); } else { String assertStr = "Unexpected number of results: sigma= " + Double.toString(sigma) + " min= " + @@ -436,20 +436,20 @@ public class StatisticalRuleTest } catch (AssertionFailedError afe) { System.err.println("******** " + testName + " ***********"); if (sigma != NO_SIGMA) { - System.err.println("SIGMA: " + Double.toString( sigma ) + - " EXPECT: " + Integer.toString(expectedSigma(sigma))); - } + System.err.println("SIGMA: " + Double.toString( sigma ) + + " EXPECT: " + Integer.toString(expectedSigma(sigma))); + } if (minimum != NO_MINIMUM) { - System.err.println("MIN: " + Double.toString( minimum ) + - " EXPECT: " + Integer.toString(expectedMinimum(minimum))); - } + System.err.println("MIN: " + Double.toString( minimum ) + + " EXPECT: " + Integer.toString(expectedMinimum(minimum))); + } if (topScore != NO_TOPSCORE) { - System.err.println("TOP: " + Integer.toString( topScore ) + - " EXPECT: " + Integer.toString(expectedTopScore(topScore))); - } - + System.err.println("TOP: " + Integer.toString( topScore ) + + " EXPECT: " + Integer.toString(expectedTopScore(topScore))); + } + throw afe; } @@ -463,7 +463,7 @@ public class StatisticalRuleTest ctx.setSourceCodeFilename(testName); IUT.apply( list, ctx ); - + return report; } }