Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 98282 invoked from network); 20 Mar 2011 17:25:39 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 20 Mar 2011 17:25:39 -0000 Received: (qmail 17418 invoked by uid 500); 20 Mar 2011 17:25:38 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 17355 invoked by uid 500); 20 Mar 2011 17:25:38 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 17348 invoked by uid 99); 20 Mar 2011 17:25:38 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 20 Mar 2011 17:25:38 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 20 Mar 2011 17:25:11 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id A4F932388C18; Sun, 20 Mar 2011 17:24:22 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1083514 [9/15] - in /commons/proper/math/trunk/src/test/java/org/apache/commons/math: ./ analysis/integration/ analysis/interpolation/ analysis/polynomials/ analysis/solvers/ complex/ distribution/ fraction/ genetics/ geometry/ linear/ ode... Date: Sun, 20 Mar 2011 17:24:19 -0000 To: commits@commons.apache.org From: luc@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110320172422.A4F932388C18@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java?rev=1083514&r1=1083513&r2=1083514&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java Sun Mar 20 17:24:14 2011 @@ -21,7 +21,6 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; -import junit.framework.AssertionFailedError; import org.apache.commons.math.RetryTestCase; import org.apache.commons.math.TestUtils; @@ -48,6 +47,8 @@ import org.apache.commons.math.stat.infe import org.apache.commons.math.stat.inference.ChiSquareTestImpl; import org.apache.commons.math.util.FastMath; import org.apache.commons.math.exception.MathIllegalArgumentException; +import org.junit.Assert; +import org.junit.Test; /** * Test cases for the RandomData class. @@ -58,8 +59,7 @@ import org.apache.commons.math.exception public class RandomDataTest extends RetryTestCase { - public RandomDataTest(String name) { - super(name); + public RandomDataTest() { randomData = new RandomDataImpl(); } @@ -71,23 +71,26 @@ public class RandomDataTest extends Retr protected RandomDataImpl randomData = null; protected final ChiSquareTestImpl testStatistic = new ChiSquareTestImpl(); + @Test public void testNextIntExtremeValues() { int x = randomData.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE); int y = randomData.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE); - assertFalse(x == y); + Assert.assertFalse(x == y); } + @Test public void testNextLongExtremeValues() { long x = randomData.nextLong(Long.MIN_VALUE, Long.MAX_VALUE); long y = randomData.nextLong(Long.MIN_VALUE, Long.MAX_VALUE); - assertFalse(x == y); + Assert.assertFalse(x == y); } /** test dispersion and failure modes for nextInt() */ + @Test public void testNextInt() { try { randomData.nextInt(4, 3); - fail("MathIllegalArgumentException expected"); + Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } @@ -95,7 +98,7 @@ public class RandomDataTest extends Retr int value = 0; for (int i = 0; i < smallSampleSize; i++) { value = randomData.nextInt(0, 3); - assertTrue("nextInt range", (value >= 0) && (value <= 3)); + Assert.assertTrue("nextInt range", (value >= 0) && (value <= 3)); freq.addValue(value); } long[] observed = new long[4]; @@ -107,15 +110,16 @@ public class RandomDataTest extends Retr * Use ChiSquare dist with df = 4-1 = 3, alpha = .001 Change to 11.34 * for alpha = .01 */ - assertTrue("chi-square test -- will fail about 1 in 1000 times", + Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", testStatistic.chiSquare(expected, observed) < 16.27); } /** test dispersion and failure modes for nextLong() */ + @Test public void testNextLong() { try { randomData.nextLong(4, 3); - fail("MathIllegalArgumentException expected"); + Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } @@ -123,7 +127,7 @@ public class RandomDataTest extends Retr long value = 0; for (int i = 0; i < smallSampleSize; i++) { value = randomData.nextLong(0, 3); - assertTrue("nextInt range", (value >= 0) && (value <= 3)); + Assert.assertTrue("nextInt range", (value >= 0) && (value <= 3)); freq.addValue(value); } long[] observed = new long[4]; @@ -135,15 +139,16 @@ public class RandomDataTest extends Retr * Use ChiSquare dist with df = 4-1 = 3, alpha = .001 Change to 11.34 * for alpha = .01 */ - assertTrue("chi-square test -- will fail about 1 in 1000 times", + Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", testStatistic.chiSquare(expected, observed) < 16.27); } /** test dispersion and failure modes for nextSecureLong() */ + @Test public void testNextSecureLong() { try { randomData.nextSecureLong(4, 3); - fail("MathIllegalArgumentException expected"); + Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } @@ -151,7 +156,7 @@ public class RandomDataTest extends Retr long value = 0; for (int i = 0; i < smallSampleSize; i++) { value = randomData.nextSecureLong(0, 3); - assertTrue("nextInt range", (value >= 0) && (value <= 3)); + Assert.assertTrue("nextInt range", (value >= 0) && (value <= 3)); freq.addValue(value); } long[] observed = new long[4]; @@ -163,15 +168,16 @@ public class RandomDataTest extends Retr * Use ChiSquare dist with df = 4-1 = 3, alpha = .001 Change to 11.34 * for alpha = .01 */ - assertTrue("chi-square test -- will fail about 1 in 1000 times", + Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", testStatistic.chiSquare(expected, observed) < 16.27); } /** test dispersion and failure modes for nextSecureInt() */ + @Test public void testNextSecureInt() { try { randomData.nextSecureInt(4, 3); - fail("MathIllegalArgumentException expected"); + Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } @@ -179,7 +185,7 @@ public class RandomDataTest extends Retr int value = 0; for (int i = 0; i < smallSampleSize; i++) { value = randomData.nextSecureInt(0, 3); - assertTrue("nextInt range", (value >= 0) && (value <= 3)); + Assert.assertTrue("nextInt range", (value >= 0) && (value <= 3)); freq.addValue(value); } long[] observed = new long[4]; @@ -191,7 +197,7 @@ public class RandomDataTest extends Retr * Use ChiSquare dist with df = 4-1 = 3, alpha = .001 Change to 11.34 * for alpha = .01 */ - assertTrue("chi-square test -- will fail about 1 in 1000 times", + Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", testStatistic.chiSquare(expected, observed) < 16.27); } @@ -201,10 +207,11 @@ public class RandomDataTest extends Retr * fails when mean is non-positive TODO: replace with statistical test, * adding test stat to TestStatistic */ + @Test public void testNextPoisson() { try { randomData.nextPoisson(0); - fail("zero mean -- expecting MathIllegalArgumentException"); + Assert.fail("zero mean -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } @@ -217,22 +224,23 @@ public class RandomDataTest extends Retr long sumFreq = f.getSumFreq(); double cumPct = Double.valueOf(cumFreq).doubleValue() / Double.valueOf(sumFreq).doubleValue(); - assertEquals("cum Poisson(4)", cumPct, 0.7851, 0.2); + Assert.assertEquals("cum Poisson(4)", cumPct, 0.7851, 0.2); try { randomData.nextPoisson(-1); - fail("negative mean supplied -- MathIllegalArgumentException expected"); + Assert.fail("negative mean supplied -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextPoisson(0); - fail("0 mean supplied -- MathIllegalArgumentException expected"); + Assert.fail("0 mean supplied -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } } + @Test public void testNextPoissonConsistency() throws Exception { // Reseed randomGenerator to get fixed sequence @@ -352,10 +360,8 @@ public class RandomDataTest extends Retr // Use chisquare test to verify that generated values are poisson(mean)-distributed ChiSquareTest chiSquareTest = new ChiSquareTestImpl(); - try { // Fail if we can reject null hypothesis that distributions are the same - assertFalse(chiSquareTest.chiSquareTest(expected, observed, alpha)); - } catch (AssertionFailedError ex) { + if (chiSquareTest.chiSquareTest(expected, observed, alpha)) { StringBuilder msgBuffer = new StringBuilder(); DecimalFormat df = new DecimalFormat("#.##"); msgBuffer.append("Chisquare test failed for mean = "); @@ -381,46 +387,47 @@ public class RandomDataTest extends Retr msgBuffer.append("This test can fail randomly due to sampling error with probability "); msgBuffer.append(alpha); msgBuffer.append("."); - fail(msgBuffer.toString()); + Assert.fail(msgBuffer.toString()); } } /** test dispersion and failure modes for nextHex() */ + @Test public void testNextHex() { try { randomData.nextHexString(-1); - fail("negative length supplied -- MathIllegalArgumentException expected"); + Assert.fail("negative length supplied -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextHexString(0); - fail("zero length supplied -- MathIllegalArgumentException expected"); + Assert.fail("zero length supplied -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } String hexString = randomData.nextHexString(3); if (hexString.length() != 3) { - fail("incorrect length for generated string"); + Assert.fail("incorrect length for generated string"); } hexString = randomData.nextHexString(1); if (hexString.length() != 1) { - fail("incorrect length for generated string"); + Assert.fail("incorrect length for generated string"); } try { hexString = randomData.nextHexString(0); - fail("zero length requested -- expecting MathIllegalArgumentException"); + Assert.fail("zero length requested -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } if (hexString.length() != 1) { - fail("incorrect length for generated string"); + Assert.fail("incorrect length for generated string"); } Frequency f = new Frequency(); for (int i = 0; i < smallSampleSize; i++) { hexString = randomData.nextHexString(100); if (hexString.length() != 100) { - fail("incorrect length for generated string"); + Assert.fail("incorrect length for generated string"); } for (int j = 0; j < hexString.length(); j++) { f.addValue(hexString.substring(j, j + 1)); @@ -436,46 +443,47 @@ public class RandomDataTest extends Retr * Use ChiSquare dist with df = 16-1 = 15, alpha = .001 Change to 30.58 * for alpha = .01 */ - assertTrue("chi-square test -- will fail about 1 in 1000 times", + Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", testStatistic.chiSquare(expected, observed) < 37.70); } /** test dispersion and failure modes for nextHex() */ + @Test public void testNextSecureHex() { try { randomData.nextSecureHexString(-1); - fail("negative length -- MathIllegalArgumentException expected"); + Assert.fail("negative length -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextSecureHexString(0); - fail("zero length -- MathIllegalArgumentException expected"); + Assert.fail("zero length -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } String hexString = randomData.nextSecureHexString(3); if (hexString.length() != 3) { - fail("incorrect length for generated string"); + Assert.fail("incorrect length for generated string"); } hexString = randomData.nextSecureHexString(1); if (hexString.length() != 1) { - fail("incorrect length for generated string"); + Assert.fail("incorrect length for generated string"); } try { hexString = randomData.nextSecureHexString(0); - fail("zero length requested -- expecting MathIllegalArgumentException"); + Assert.fail("zero length requested -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } if (hexString.length() != 1) { - fail("incorrect length for generated string"); + Assert.fail("incorrect length for generated string"); } Frequency f = new Frequency(); for (int i = 0; i < smallSampleSize; i++) { hexString = randomData.nextSecureHexString(100); if (hexString.length() != 100) { - fail("incorrect length for generated string"); + Assert.fail("incorrect length for generated string"); } for (int j = 0; j < hexString.length(); j++) { f.addValue(hexString.substring(j, j + 1)); @@ -491,21 +499,22 @@ public class RandomDataTest extends Retr * Use ChiSquare dist with df = 16-1 = 15, alpha = .001 Change to 30.58 * for alpha = .01 */ - assertTrue("chi-square test -- will fail about 1 in 1000 times", + Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", testStatistic.chiSquare(expected, observed) < 37.70); } /** test failure modes and dispersion of nextUniform() */ + @Test public void testNextUniform() { try { randomData.nextUniform(4, 3); - fail("MathIllegalArgumentException expected"); + Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextUniform(3, 3); - fail("MathIllegalArgumentException expected"); + Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } @@ -518,7 +527,7 @@ public class RandomDataTest extends Retr for (int i = 0; i < 1000; i++) { result = randomData.nextUniform(lower, upper); if ((result == lower) || (result == upper)) { - fail("generated value equal to an endpoint: " + result); + Assert.fail("generated value equal to an endpoint: " + result); } if (result < midpoint) { observed[0]++; @@ -530,23 +539,25 @@ public class RandomDataTest extends Retr * Use ChiSquare dist with df = 2-1 = 1, alpha = .001 Change to 6.64 for * alpha = .01 */ - assertTrue("chi-square test -- will fail about 1 in 1000 times", + Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", testStatistic.chiSquare(expected, observed) < 10.83); } /** test exclusive endpoints of nextUniform **/ + @Test public void testNextUniformExclusiveEndpoints() { for (int i = 0; i < 1000; i++) { double u = randomData.nextUniform(0.99, 1); - assertTrue(u > 0.99 && u < 1); + Assert.assertTrue(u > 0.99 && u < 1); } } /** test failure modes and distribution of nextGaussian() */ + @Test public void testNextGaussian() { try { randomData.nextGaussian(0, 0); - fail("zero sigma -- MathIllegalArgumentException expected"); + Assert.fail("zero sigma -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } @@ -561,20 +572,21 @@ public class RandomDataTest extends Retr * t-test at .001-level TODO: replace with externalized t-test, with * test statistic defined in TestStatistic */ - assertTrue(FastMath.abs(xbar) / (s / FastMath.sqrt(n)) < 3.29); + Assert.assertTrue(FastMath.abs(xbar) / (s / FastMath.sqrt(n)) < 3.29); } /** test failure modes and distribution of nextExponential() */ + @Test public void testNextExponential() { try { randomData.nextExponential(-1); - fail("negative mean -- expecting MathIllegalArgumentException"); + Assert.fail("negative mean -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextExponential(0); - fail("zero mean -- expecting MathIllegalArgumentException"); + Assert.fail("zero mean -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } @@ -582,7 +594,7 @@ public class RandomDataTest extends Retr double v = 0; for (int i = 0; i < largeSampleSize; i++) { v = randomData.nextExponential(1); - assertTrue("exponential deviate postive", v > 0); + Assert.assertTrue("exponential deviate postive", v > 0); if (v < 2) cumFreq++; } @@ -591,41 +603,42 @@ public class RandomDataTest extends Retr * TestStatistic. Check below compares observed cumulative distribution * evaluated at 2 with exponential CDF */ - assertEquals("exponential cumulative distribution", (double) cumFreq + Assert.assertEquals("exponential cumulative distribution", (double) cumFreq / (double) largeSampleSize, 0.8646647167633873, .2); } /** test reseeding, algorithm/provider games */ + @Test public void testConfig() { randomData.reSeed(1000); double v = randomData.nextUniform(0, 1); randomData.reSeed(); - assertTrue("different seeds", Math + Assert.assertTrue("different seeds", Math .abs(v - randomData.nextUniform(0, 1)) > 10E-12); randomData.reSeed(1000); - assertEquals("same seeds", v, randomData.nextUniform(0, 1), 10E-12); + Assert.assertEquals("same seeds", v, randomData.nextUniform(0, 1), 10E-12); randomData.reSeedSecure(1000); String hex = randomData.nextSecureHexString(40); randomData.reSeedSecure(); - assertTrue("different seeds", !hex.equals(randomData + Assert.assertTrue("different seeds", !hex.equals(randomData .nextSecureHexString(40))); randomData.reSeedSecure(1000); - assertTrue("same seeds", !hex + Assert.assertTrue("same seeds", !hex .equals(randomData.nextSecureHexString(40))); /* * remove this test back soon, since it takes about 4 seconds * * try { randomData.setSecureAlgorithm("SHA1PRNG","SUN"); } catch - * (NoSuchProviderException ex) { ; } assertTrue("different seeds", + * (NoSuchProviderException ex) { ; } Assert.assertTrue("different seeds", * !hex.equals(randomData.nextSecureHexString(40))); try { * randomData.setSecureAlgorithm("NOSUCHTHING","SUN"); - * fail("expecting NoSuchAlgorithmException"); } catch + * Assert.fail("expecting NoSuchAlgorithmException"); } catch * (NoSuchProviderException ex) { ; } catch (NoSuchAlgorithmException * ex) { ; } * * try { randomData.setSecureAlgorithm("SHA1PRNG","NOSUCHPROVIDER"); - * fail("expecting NoSuchProviderException"); } catch + * Assert.fail("expecting NoSuchProviderException"); } catch * (NoSuchProviderException ex) { ; } */ @@ -645,6 +658,7 @@ public class RandomDataTest extends Retr } /** tests for nextSample() sampling from Collection */ + @Test public void testNextSample() { Object[][] c = { { "0", "1" }, { "0", "2" }, { "0", "3" }, { "0", "4" }, { "1", "2" }, { "1", "3" }, { "1", "4" }, @@ -674,7 +688,7 @@ public class RandomDataTest extends Retr * Use ChiSquare dist with df = 10-1 = 9, alpha = .001 Change to 21.67 * for alpha = .01 */ - assertTrue("chi-square test -- will fail about 1 in 1000 times", + Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", testStatistic.chiSquare(expected, observed) < 27.88); // Make sure sample of size = size of collection returns same collection @@ -683,13 +697,13 @@ public class RandomDataTest extends Retr Object[] one = randomData.nextSample(hs, 1); String oneString = (String) one[0]; if ((one.length != 1) || !oneString.equals("one")) { - fail("bad sample for set size = 1, sample size = 1"); + Assert.fail("bad sample for set size = 1, sample size = 1"); } // Make sure we fail for sample size > collection size try { one = randomData.nextSample(hs, 2); - fail("sample size > set size, expecting MathIllegalArgumentException"); + Assert.fail("sample size > set size, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } @@ -698,7 +712,7 @@ public class RandomDataTest extends Retr try { hs = new HashSet(); one = randomData.nextSample(hs, 0); - fail("n = k = 0, expecting MathIllegalArgumentException"); + Assert.fail("n = k = 0, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } @@ -716,11 +730,12 @@ public class RandomDataTest extends Retr return i; } } - fail("sample not found:{" + samp[0] + "," + samp[1] + "}"); + Assert.fail("sample not found:{" + samp[0] + "," + samp[1] + "}"); return -1; } /** tests for nextPermutation */ + @Test public void testNextPermutation() { int[][] p = { { 0, 1, 2 }, { 0, 2, 1 }, { 1, 0, 2 }, { 1, 2, 0 }, { 2, 0, 1 }, { 2, 1, 0 } }; @@ -736,18 +751,18 @@ public class RandomDataTest extends Retr * Use ChiSquare dist with df = 6-1 = 5, alpha = .001 Change to 15.09 * for alpha = .01 */ - assertTrue("chi-square test -- will fail about 1 in 1000 times", + Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", testStatistic.chiSquare(expected, observed) < 20.52); // Check size = 1 boundary case int[] perm = randomData.nextPermutation(1, 1); if ((perm.length != 1) || (perm[0] != 0)) { - fail("bad permutation for n = 1, sample k = 1"); + Assert.fail("bad permutation for n = 1, sample k = 1"); // Make sure we fail for k size > n try { perm = randomData.nextPermutation(2, 3); - fail("permutation k > n, expecting MathIllegalArgumentException"); + Assert.fail("permutation k > n, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } @@ -755,7 +770,7 @@ public class RandomDataTest extends Retr // Make sure we fail for n = 0 try { perm = randomData.nextPermutation(0, 0); - fail("permutation k = n = 0, expecting MathIllegalArgumentException"); + Assert.fail("permutation k = n = 0, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } @@ -763,7 +778,7 @@ public class RandomDataTest extends Retr // Make sure we fail for k < n < 0 try { perm = randomData.nextPermutation(-1, -3); - fail("permutation k < n < 0, expecting MathIllegalArgumentException"); + Assert.fail("permutation k < n < 0, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } @@ -773,7 +788,7 @@ public class RandomDataTest extends Retr // Disable until we have equals //public void testSerial() { - // assertEquals(randomData, TestUtils.serializeAndRecover(randomData)); + // Assert.assertEquals(randomData, TestUtils.serializeAndRecover(randomData)); //} private int findPerm(int[][] p, int[] samp) { @@ -788,10 +803,11 @@ public class RandomDataTest extends Retr return i; } } - fail("permutation not found"); + Assert.fail("permutation not found"); return -1; } + @Test public void testNextInversionDeviate() throws Exception { // Set the seed for the default random generator randomData.reSeed(100); @@ -809,10 +825,11 @@ public class RandomDataTest extends Retr */ for (int i = 0; i < 10; i++) { double value = randomData.nextInversionDeviate(betaDistribution); - assertEquals(betaDistribution.cumulativeProbability(value), quantiles[i], 10E-9); + Assert.assertEquals(betaDistribution.cumulativeProbability(value), quantiles[i], 10E-9); } } + @Test public void testNextBeta() throws Exception { double[] quartiles = TestUtils.getDistributionQuartiles(new BetaDistributionImpl(2,5)); long[] counts = new long[4]; @@ -824,6 +841,7 @@ public class RandomDataTest extends Retr TestUtils.assertChiSquareAccept(expected, counts, 0.001); } + @Test public void testNextCauchy() throws Exception { double[] quartiles = TestUtils.getDistributionQuartiles(new CauchyDistributionImpl(1.2, 2.1)); long[] counts = new long[4]; @@ -835,6 +853,7 @@ public class RandomDataTest extends Retr TestUtils.assertChiSquareAccept(expected, counts, 0.001); } + @Test public void testNextChiSquare() throws Exception { double[] quartiles = TestUtils.getDistributionQuartiles(new ChiSquaredDistributionImpl(12)); long[] counts = new long[4]; @@ -846,6 +865,7 @@ public class RandomDataTest extends Retr TestUtils.assertChiSquareAccept(expected, counts, 0.001); } + @Test public void testNextF() throws Exception { double[] quartiles = TestUtils.getDistributionQuartiles(new FDistributionImpl(12, 5)); long[] counts = new long[4]; @@ -857,6 +877,7 @@ public class RandomDataTest extends Retr TestUtils.assertChiSquareAccept(expected, counts, 0.001); } + @Test public void testNextGamma() throws Exception { double[] quartiles = TestUtils.getDistributionQuartiles(new GammaDistributionImpl(4, 2)); long[] counts = new long[4]; @@ -868,6 +889,7 @@ public class RandomDataTest extends Retr TestUtils.assertChiSquareAccept(expected, counts, 0.001); } + @Test public void testNextT() throws Exception { double[] quartiles = TestUtils.getDistributionQuartiles(new TDistributionImpl(10)); long[] counts = new long[4]; @@ -879,6 +901,7 @@ public class RandomDataTest extends Retr TestUtils.assertChiSquareAccept(expected, counts, 0.001); } + @Test public void testNextWeibull() throws Exception { double[] quartiles = TestUtils.getDistributionQuartiles(new WeibullDistributionImpl(1.2, 2.1)); long[] counts = new long[4]; @@ -890,8 +913,9 @@ public class RandomDataTest extends Retr TestUtils.assertChiSquareAccept(expected, counts, 0.001); } + @Test public void testNextBinomial() throws Exception { - BinomialDistributionTest testInstance = new BinomialDistributionTest(""); + BinomialDistributionTest testInstance = new BinomialDistributionTest(); int[] densityPoints = testInstance.makeDensityTestPoints(); double[] densityValues = testInstance.makeDensityTestValues(); int sampleSize = 1000; @@ -915,8 +939,9 @@ public class RandomDataTest extends Retr TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001); } + @Test public void testNextHypergeometric() throws Exception { - HypergeometricDistributionTest testInstance = new HypergeometricDistributionTest(""); + HypergeometricDistributionTest testInstance = new HypergeometricDistributionTest(); int[] densityPoints = testInstance.makeDensityTestPoints(); double[] densityValues = testInstance.makeDensityTestValues(); int sampleSize = 1000; @@ -940,8 +965,9 @@ public class RandomDataTest extends Retr TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001); } + @Test public void testNextPascal() throws Exception { - PascalDistributionTest testInstance = new PascalDistributionTest(""); + PascalDistributionTest testInstance = new PascalDistributionTest(); int[] densityPoints = testInstance.makeDensityTestPoints(); double[] densityValues = testInstance.makeDensityTestValues(); int sampleSize = 1000; @@ -964,8 +990,9 @@ public class RandomDataTest extends Retr TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001); } + @Test public void testNextZipf() throws Exception { - ZipfDistributionTest testInstance = new ZipfDistributionTest(""); + ZipfDistributionTest testInstance = new ZipfDistributionTest(); int[] densityPoints = testInstance.makeDensityTestPoints(); double[] densityValues = testInstance.makeDensityTestValues(); int sampleSize = 1000; Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/UniformRandomGeneratorTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/UniformRandomGeneratorTest.java?rev=1083514&r1=1083513&r2=1083514&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/UniformRandomGeneratorTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/UniformRandomGeneratorTest.java Sun Mar 20 17:24:14 2011 @@ -18,16 +18,13 @@ package org.apache.commons.math.random; import org.apache.commons.math.stat.StatUtils; +import org.junit.Assert; +import org.junit.Test; -import junit.framework.*; -public class UniformRandomGeneratorTest -extends TestCase { - - public UniformRandomGeneratorTest(String name) { - super(name); - } +public class UniformRandomGeneratorTest { + @Test public void testMeanAndStandardDeviation() { RandomGenerator rg = new JDKRandomGenerator(); rg.setSeed(17399225432l); @@ -36,8 +33,8 @@ extends TestCase { for (int i = 0; i < sample.length; ++i) { sample[i] = generator.nextNormalizedDouble(); } - assertEquals(0.0, StatUtils.mean(sample), 0.07); - assertEquals(1.0, StatUtils.variance(sample), 0.02); + Assert.assertEquals(0.0, StatUtils.mean(sample), 0.07); + Assert.assertEquals(1.0, StatUtils.variance(sample), 0.02); } } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/ValueServerTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/ValueServerTest.java?rev=1083514&r1=1083513&r2=1083514&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/ValueServerTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/ValueServerTest.java Sun Mar 20 17:24:14 2011 @@ -21,6 +21,9 @@ import java.net.URL; import org.apache.commons.math.RetryTestCase; import org.apache.commons.math.stat.descriptive.SummaryStatistics; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; /** * Test cases for the ValueServer class. @@ -32,11 +35,7 @@ public final class ValueServerTest exten private ValueServer vs = new ValueServer(); - public ValueServerTest(String name) { - super(name); - } - - @Override + @Before public void setUp() { vs.setMode(ValueServer.DIGEST_MODE); URL url = getClass().getResource("testData.txt"); @@ -48,20 +47,20 @@ public final class ValueServerTest exten * Note that there is a non-zero (but very small) probability that * these tests will fail even if the code is working as designed. */ + @Test public void testNextDigest() throws Exception{ double next = 0.0; double tolerance = 0.1; vs.computeDistribution(); - assertTrue("empirical distribution property", + Assert.assertTrue("empirical distribution property", vs.getEmpiricalDistribution() != null); SummaryStatistics stats = new SummaryStatistics(); for (int i = 1; i < 1000; i++) { next = vs.getNext(); stats.addValue(next); } - assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance); - assertEquals - ("std dev", 1.0173699343977738, stats.getStandardDeviation(), + Assert.assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance); + Assert.assertEquals("std dev", 1.0173699343977738, stats.getStandardDeviation(), tolerance); vs.computeDistribution(500); @@ -70,9 +69,8 @@ public final class ValueServerTest exten next = vs.getNext(); stats.addValue(next); } - assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance); - assertEquals - ("std dev", 1.0173699343977738, stats.getStandardDeviation(), + Assert.assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance); + Assert.assertEquals("std dev", 1.0173699343977738, stats.getStandardDeviation(), tolerance); } @@ -81,32 +79,35 @@ public final class ValueServerTest exten * Make sure exception thrown if digest getNext is attempted * before loading empiricalDistribution. */ + @Test public void testNextDigestFail() throws Exception { try { vs.getNext(); - fail("Expecting IllegalStateException"); + Assert.fail("Expecting IllegalStateException"); } catch (IllegalStateException ex) {} } + @Test public void testEmptyReplayFile() throws Exception { try { URL url = getClass().getResource("emptyFile.txt"); vs.setMode(ValueServer.REPLAY_MODE); vs.setValuesFileURL(url); vs.getNext(); - fail("an exception should have been thrown"); + Assert.fail("an exception should have been thrown"); } catch (EOFException eof) { // expected behavior } } + @Test public void testEmptyDigestFile() throws Exception { try { URL url = getClass().getResource("emptyFile.txt"); vs.setMode(ValueServer.DIGEST_MODE); vs.setValuesFileURL(url); vs.computeDistribution(); - fail("an exception should have been thrown"); + Assert.fail("an exception should have been thrown"); } catch (EOFException eof) { // expected behavior } @@ -117,6 +118,7 @@ public final class ValueServerTest exten * Check that the values 1,2,1001,1002 match data file values 1 and 2. * the sample data file. */ + @Test public void testReplay() throws Exception { double firstDataValue = 4.038625496201205; double secondDataValue = 3.6485326248346936; @@ -125,16 +127,16 @@ public final class ValueServerTest exten vs.setMode(ValueServer.REPLAY_MODE); vs.resetReplayFile(); compareValue = vs.getNext(); - assertEquals(compareValue,firstDataValue,tolerance); + Assert.assertEquals(compareValue,firstDataValue,tolerance); compareValue = vs.getNext(); - assertEquals(compareValue,secondDataValue,tolerance); + Assert.assertEquals(compareValue,secondDataValue,tolerance); for (int i = 3; i < 1001; i++) { compareValue = vs.getNext(); } compareValue = vs.getNext(); - assertEquals(compareValue,firstDataValue,tolerance); + Assert.assertEquals(compareValue,firstDataValue,tolerance); compareValue = vs.getNext(); - assertEquals(compareValue,secondDataValue,tolerance); + Assert.assertEquals(compareValue,secondDataValue,tolerance); vs.closeReplayFile(); // make sure no NPE vs.closeReplayFile(); @@ -143,26 +145,27 @@ public final class ValueServerTest exten /** * Test other ValueServer modes */ + @Test public void testModes() throws Exception { vs.setMode(ValueServer.CONSTANT_MODE); vs.setMu(0); - assertEquals("constant mode test",vs.getMu(),vs.getNext(),Double.MIN_VALUE); + Assert.assertEquals("constant mode test",vs.getMu(),vs.getNext(),Double.MIN_VALUE); vs.setMode(ValueServer.UNIFORM_MODE); vs.setMu(2); double val = vs.getNext(); - assertTrue(val > 0 && val < 4); + Assert.assertTrue(val > 0 && val < 4); vs.setSigma(1); vs.setMode(ValueServer.GAUSSIAN_MODE); val = vs.getNext(); - assertTrue("gaussian value close enough to mean", + Assert.assertTrue("gaussian value close enough to mean", val < vs.getMu() + 100*vs.getSigma()); vs.setMode(ValueServer.EXPONENTIAL_MODE); val = vs.getNext(); - assertTrue(val > 0); + Assert.assertTrue(val > 0); try { vs.setMode(1000); vs.getNext(); - fail("bad mode, expecting IllegalStateException"); + Assert.fail("bad mode, expecting IllegalStateException"); } catch (IllegalStateException ex) { // ignored } @@ -171,29 +174,31 @@ public final class ValueServerTest exten /** * Test fill */ + @Test public void testFill() throws Exception { vs.setMode(ValueServer.CONSTANT_MODE); vs.setMu(2); double[] val = new double[5]; vs.fill(val); for (int i = 0; i < 5; i++) { - assertEquals("fill test in place",2,val[i],Double.MIN_VALUE); + Assert.assertEquals("fill test in place",2,val[i],Double.MIN_VALUE); } double v2[] = vs.fill(3); for (int i = 0; i < 3; i++) { - assertEquals("fill test in place",2,v2[i],Double.MIN_VALUE); + Assert.assertEquals("fill test in place",2,v2[i],Double.MIN_VALUE); } } /** * Test getters to make Clover happy */ + @Test public void testProperties() throws Exception { vs.setMode(ValueServer.CONSTANT_MODE); - assertEquals("mode test",ValueServer.CONSTANT_MODE,vs.getMode()); + Assert.assertEquals("mode test",ValueServer.CONSTANT_MODE,vs.getMode()); vs.setValuesFileURL("http://www.apache.org"); URL url = vs.getValuesFileURL(); - assertEquals("valuesFileURL test","http://www.apache.org",url.toString()); + Assert.assertEquals("valuesFileURL test","http://www.apache.org",url.toString()); } } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/CertifiedDataTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/CertifiedDataTest.java?rev=1083514&r1=1083513&r2=1083514&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/CertifiedDataTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/CertifiedDataTest.java Sun Mar 20 17:24:14 2011 @@ -19,83 +19,78 @@ package org.apache.commons.math.stat; import java.io.BufferedReader; import java.io.InputStreamReader; -import junit.framework.TestCase; import org.apache.commons.math.stat.descriptive.DescriptiveStatistics; import org.apache.commons.math.stat.descriptive.SummaryStatistics; +import org.junit.Assert; +import org.junit.Test; /** * Certified data test cases. * @version $Revision$ $Date$ */ -public class CertifiedDataTest extends TestCase { +public class CertifiedDataTest { protected double mean = Double.NaN; protected double std = Double.NaN; /** - * Certified Data Test Constructor - * @param name - */ - public CertifiedDataTest(String name) { - super(name); - } - - /** * Test SummaryStatistics - implementations that do not store the data * and use single pass algorithms to compute statistics */ + @Test public void testSummaryStatistics() throws Exception { SummaryStatistics u = new SummaryStatistics(); loadStats("data/PiDigits.txt", u); - assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-13); - assertEquals("PiDigits: mean", mean, u.getMean(), 1E-13); + Assert.assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-13); + Assert.assertEquals("PiDigits: mean", mean, u.getMean(), 1E-13); loadStats("data/Mavro.txt", u); - assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14); - assertEquals("Mavro: mean", mean, u.getMean(), 1E-14); + Assert.assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14); + Assert.assertEquals("Mavro: mean", mean, u.getMean(), 1E-14); loadStats("data/Michelso.txt", u); - assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-13); - assertEquals("Michelso: mean", mean, u.getMean(), 1E-13); + Assert.assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-13); + Assert.assertEquals("Michelso: mean", mean, u.getMean(), 1E-13); loadStats("data/NumAcc1.txt", u); - assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14); - assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14); + Assert.assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14); + Assert.assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14); loadStats("data/NumAcc2.txt", u); - assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14); - assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14); + Assert.assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14); + Assert.assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14); } /** * Test DescriptiveStatistics - implementations that store full array of * values and execute multi-pass algorithms */ + @Test public void testDescriptiveStatistics() throws Exception { DescriptiveStatistics u = new DescriptiveStatistics(); loadStats("data/PiDigits.txt", u); - assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-14); - assertEquals("PiDigits: mean", mean, u.getMean(), 1E-14); + Assert.assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-14); + Assert.assertEquals("PiDigits: mean", mean, u.getMean(), 1E-14); loadStats("data/Mavro.txt", u); - assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14); - assertEquals("Mavro: mean", mean, u.getMean(), 1E-14); + Assert.assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14); + Assert.assertEquals("Mavro: mean", mean, u.getMean(), 1E-14); loadStats("data/Michelso.txt", u); - assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-14); - assertEquals("Michelso: mean", mean, u.getMean(), 1E-14); + Assert.assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-14); + Assert.assertEquals("Michelso: mean", mean, u.getMean(), 1E-14); loadStats("data/NumAcc1.txt", u); - assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14); - assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14); + Assert.assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14); + Assert.assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14); loadStats("data/NumAcc2.txt", u); - assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14); - assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14); + Assert.assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14); + Assert.assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14); } /** Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/FrequencyTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/FrequencyTest.java?rev=1083514&r1=1083513&r2=1083514&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/FrequencyTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/FrequencyTest.java Sun Mar 20 17:24:14 2011 @@ -20,9 +20,11 @@ import java.io.BufferedReader; import java.io.StringReader; import java.util.Iterator; -import junit.framework.TestCase; import org.apache.commons.math.TestUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; /** * Test cases for the {@link Frequency} class. @@ -30,7 +32,7 @@ import org.apache.commons.math.TestUtils * @version $Revision$ $Date$ */ -public final class FrequencyTest extends TestCase { +public final class FrequencyTest { private long oneL = 1; private long twoL = 2; private long threeL = 3; @@ -40,45 +42,42 @@ public final class FrequencyTest extends private double tolerance = 10E-15; private Frequency f = null; - public FrequencyTest(String name) { - super(name); - } - - @Override + @Before public void setUp() { f = new Frequency(); } /** test freq counts */ + @Test public void testCounts() { - assertEquals("total count",0,f.getSumFreq()); + Assert.assertEquals("total count",0,f.getSumFreq()); f.addValue(oneL); f.addValue(twoL); f.addValue(1); f.addValue(oneI); - assertEquals("one frequency count",3,f.getCount(1)); - assertEquals("two frequency count",1,f.getCount(2)); - assertEquals("three frequency count",0,f.getCount(3)); - assertEquals("total count",4,f.getSumFreq()); - assertEquals("zero cumulative frequency", 0, f.getCumFreq(0)); - assertEquals("one cumulative frequency", 3, f.getCumFreq(1)); - assertEquals("two cumulative frequency", 4, f.getCumFreq(2)); - assertEquals("Integer argument cum freq",4, f.getCumFreq(Integer.valueOf(2))); - assertEquals("five cumulative frequency", 4, f.getCumFreq(5)); - assertEquals("foo cumulative frequency", 0, f.getCumFreq("foo")); + Assert.assertEquals("one frequency count",3,f.getCount(1)); + Assert.assertEquals("two frequency count",1,f.getCount(2)); + Assert.assertEquals("three frequency count",0,f.getCount(3)); + Assert.assertEquals("total count",4,f.getSumFreq()); + Assert.assertEquals("zero cumulative frequency", 0, f.getCumFreq(0)); + Assert.assertEquals("one cumulative frequency", 3, f.getCumFreq(1)); + Assert.assertEquals("two cumulative frequency", 4, f.getCumFreq(2)); + Assert.assertEquals("Integer argument cum freq",4, f.getCumFreq(Integer.valueOf(2))); + Assert.assertEquals("five cumulative frequency", 4, f.getCumFreq(5)); + Assert.assertEquals("foo cumulative frequency", 0, f.getCumFreq("foo")); f.clear(); - assertEquals("total count",0,f.getSumFreq()); + Assert.assertEquals("total count",0,f.getSumFreq()); // userguide examples ------------------------------------------------------------------- f.addValue("one"); f.addValue("One"); f.addValue("oNe"); f.addValue("Z"); - assertEquals("one cumulative frequency", 1 , f.getCount("one")); - assertEquals("Z cumulative pct", 0.5, f.getCumPct("Z"), tolerance); - assertEquals("z cumulative pct", 1.0, f.getCumPct("z"), tolerance); - assertEquals("Ot cumulative pct", 0.25, f.getCumPct("Ot"), tolerance); + Assert.assertEquals("one cumulative frequency", 1 , f.getCount("one")); + Assert.assertEquals("Z cumulative pct", 0.5, f.getCumPct("Z"), tolerance); + Assert.assertEquals("z cumulative pct", 1.0, f.getCumPct("z"), tolerance); + Assert.assertEquals("Ot cumulative pct", 0.25, f.getCumPct("Ot"), tolerance); f.clear(); f = null; @@ -88,12 +87,12 @@ public final class FrequencyTest extends f.addValue(Long.valueOf(1)); f.addValue(2); f.addValue(Integer.valueOf(-1)); - assertEquals("1 count", 3, f.getCount(1)); - assertEquals("1 count", 3, f.getCount(Integer.valueOf(1))); - assertEquals("0 cum pct", 0.2, f.getCumPct(0), tolerance); - assertEquals("1 pct", 0.6, f.getPct(Integer.valueOf(1)), tolerance); - assertEquals("-2 cum pct", 0, f.getCumPct(-2), tolerance); - assertEquals("10 cum pct", 1, f.getCumPct(10), tolerance); + Assert.assertEquals("1 count", 3, f.getCount(1)); + Assert.assertEquals("1 count", 3, f.getCount(Integer.valueOf(1))); + Assert.assertEquals("0 cum pct", 0.2, f.getCumPct(0), tolerance); + Assert.assertEquals("1 pct", 0.6, f.getPct(Integer.valueOf(1)), tolerance); + Assert.assertEquals("-2 cum pct", 0, f.getCumPct(-2), tolerance); + Assert.assertEquals("10 cum pct", 1, f.getCumPct(10), tolerance); f = null; f = new Frequency(String.CASE_INSENSITIVE_ORDER); @@ -101,28 +100,29 @@ public final class FrequencyTest extends f.addValue("One"); f.addValue("oNe"); f.addValue("Z"); - assertEquals("one count", 3 , f.getCount("one")); - assertEquals("Z cumulative pct -- case insensitive", 1 , f.getCumPct("Z"), tolerance); - assertEquals("z cumulative pct -- case insensitive", 1 , f.getCumPct("z"), tolerance); + Assert.assertEquals("one count", 3 , f.getCount("one")); + Assert.assertEquals("Z cumulative pct -- case insensitive", 1 , f.getCumPct("Z"), tolerance); + Assert.assertEquals("z cumulative pct -- case insensitive", 1 , f.getCumPct("z"), tolerance); f = null; f = new Frequency(); - assertEquals(0L, f.getCount('a')); - assertEquals(0L, f.getCumFreq('b')); + Assert.assertEquals(0L, f.getCount('a')); + Assert.assertEquals(0L, f.getCumFreq('b')); TestUtils.assertEquals(Double.NaN, f.getPct('a'), 0.0); TestUtils.assertEquals(Double.NaN, f.getCumPct('b'), 0.0); f.addValue('a'); f.addValue('b'); f.addValue('c'); f.addValue('d'); - assertEquals(1L, f.getCount('a')); - assertEquals(2L, f.getCumFreq('b')); - assertEquals(0.25, f.getPct('a'), 0.0); - assertEquals(0.5, f.getCumPct('b'), 0.0); - assertEquals(1.0, f.getCumPct('e'), 0.0); + Assert.assertEquals(1L, f.getCount('a')); + Assert.assertEquals(2L, f.getCumFreq('b')); + Assert.assertEquals(0.25, f.getPct('a'), 0.0); + Assert.assertEquals(0.5, f.getCumPct('b'), 0.0); + Assert.assertEquals(1.0, f.getCumPct('e'), 0.0); } /** test pcts */ + @Test public void testPcts() { f.addValue(oneL); f.addValue(twoL); @@ -132,21 +132,22 @@ public final class FrequencyTest extends f.addValue(threeL); f.addValue(3); f.addValue(threeI); - assertEquals("one pct",0.25,f.getPct(1),tolerance); - assertEquals("two pct",0.25,f.getPct(Long.valueOf(2)),tolerance); - assertEquals("three pct",0.5,f.getPct(threeL),tolerance); - assertEquals("five pct",0,f.getPct(5),tolerance); - assertEquals("foo pct",0,f.getPct("foo"),tolerance); - assertEquals("one cum pct",0.25,f.getCumPct(1),tolerance); - assertEquals("two cum pct",0.50,f.getCumPct(Long.valueOf(2)),tolerance); - assertEquals("Integer argument",0.50,f.getCumPct(Integer.valueOf(2)),tolerance); - assertEquals("three cum pct",1.0,f.getCumPct(threeL),tolerance); - assertEquals("five cum pct",1.0,f.getCumPct(5),tolerance); - assertEquals("zero cum pct",0.0,f.getCumPct(0),tolerance); - assertEquals("foo cum pct",0,f.getCumPct("foo"),tolerance); + Assert.assertEquals("one pct",0.25,f.getPct(1),tolerance); + Assert.assertEquals("two pct",0.25,f.getPct(Long.valueOf(2)),tolerance); + Assert.assertEquals("three pct",0.5,f.getPct(threeL),tolerance); + Assert.assertEquals("five pct",0,f.getPct(5),tolerance); + Assert.assertEquals("foo pct",0,f.getPct("foo"),tolerance); + Assert.assertEquals("one cum pct",0.25,f.getCumPct(1),tolerance); + Assert.assertEquals("two cum pct",0.50,f.getCumPct(Long.valueOf(2)),tolerance); + Assert.assertEquals("Integer argument",0.50,f.getCumPct(Integer.valueOf(2)),tolerance); + Assert.assertEquals("three cum pct",1.0,f.getCumPct(threeL),tolerance); + Assert.assertEquals("five cum pct",1.0,f.getCumPct(5),tolerance); + Assert.assertEquals("zero cum pct",0.0,f.getCumPct(0),tolerance); + Assert.assertEquals("foo cum pct",0,f.getCumPct("foo"),tolerance); } /** test adding incomparable values */ + @Test public void testAdd() { char aChar = 'a'; char bChar = 'b'; @@ -155,47 +156,49 @@ public final class FrequencyTest extends f.addValue(bChar); try { f.addValue(aString); - fail("Expecting IllegalArgumentException"); + Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } try { f.addValue(2); - fail("Expecting IllegalArgumentException"); + Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } - assertEquals("a pct",0.5,f.getPct(aChar),tolerance); - assertEquals("b cum pct",1.0,f.getCumPct(bChar),tolerance); - assertEquals("a string pct",0.0,f.getPct(aString),tolerance); - assertEquals("a string cum pct",0.0,f.getCumPct(aString),tolerance); + Assert.assertEquals("a pct",0.5,f.getPct(aChar),tolerance); + Assert.assertEquals("b cum pct",1.0,f.getCumPct(bChar),tolerance); + Assert.assertEquals("a string pct",0.0,f.getPct(aString),tolerance); + Assert.assertEquals("a string cum pct",0.0,f.getCumPct(aString),tolerance); f = new Frequency(); f.addValue("One"); try { f.addValue(new Integer("One")); - fail("Expecting IllegalArgumentException"); + Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } } /** test empty table */ + @Test public void testEmptyTable() { - assertEquals("freq sum, empty table", 0, f.getSumFreq()); - assertEquals("count, empty table", 0, f.getCount(0)); - assertEquals("count, empty table",0, f.getCount(Integer.valueOf(0))); - assertEquals("cum freq, empty table", 0, f.getCumFreq(0)); - assertEquals("cum freq, empty table", 0, f.getCumFreq("x")); - assertTrue("pct, empty table", Double.isNaN(f.getPct(0))); - assertTrue("pct, empty table", Double.isNaN(f.getPct(Integer.valueOf(0)))); - assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(0))); - assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(Integer.valueOf(0)))); + Assert.assertEquals("freq sum, empty table", 0, f.getSumFreq()); + Assert.assertEquals("count, empty table", 0, f.getCount(0)); + Assert.assertEquals("count, empty table",0, f.getCount(Integer.valueOf(0))); + Assert.assertEquals("cum freq, empty table", 0, f.getCumFreq(0)); + Assert.assertEquals("cum freq, empty table", 0, f.getCumFreq("x")); + Assert.assertTrue("pct, empty table", Double.isNaN(f.getPct(0))); + Assert.assertTrue("pct, empty table", Double.isNaN(f.getPct(Integer.valueOf(0)))); + Assert.assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(0))); + Assert.assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(Integer.valueOf(0)))); } /** * Tests toString() */ + @Test public void testToString() throws Exception { f.addValue(oneL); f.addValue(twoL); @@ -204,21 +207,22 @@ public final class FrequencyTest extends String s = f.toString(); //System.out.println(s); - assertNotNull(s); + Assert.assertNotNull(s); BufferedReader reader = new BufferedReader(new StringReader(s)); String line = reader.readLine(); // header line - assertNotNull(line); + Assert.assertNotNull(line); line = reader.readLine(); // one's or two's line - assertNotNull(line); + Assert.assertNotNull(line); line = reader.readLine(); // one's or two's line - assertNotNull(line); + Assert.assertNotNull(line); line = reader.readLine(); // no more elements - assertNull(line); + Assert.assertNull(line); } + @Test public void testIntegerValues() { Comparable obj1 = null; obj1 = Integer.valueOf(1); @@ -227,34 +231,36 @@ public final class FrequencyTest extends f.addValue(int1); f.addValue(2); f.addValue(Long.valueOf(2)); - assertEquals("Integer 1 count", 2, f.getCount(1)); - assertEquals("Integer 1 count", 2, f.getCount(Integer.valueOf(1))); - assertEquals("Integer 1 count", 2, f.getCount(Long.valueOf(1))); - assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(1), tolerance); - assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(Long.valueOf(1)), tolerance); - assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(Integer.valueOf(1)), tolerance); + Assert.assertEquals("Integer 1 count", 2, f.getCount(1)); + Assert.assertEquals("Integer 1 count", 2, f.getCount(Integer.valueOf(1))); + Assert.assertEquals("Integer 1 count", 2, f.getCount(Long.valueOf(1))); + Assert.assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(1), tolerance); + Assert.assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(Long.valueOf(1)), tolerance); + Assert.assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(Integer.valueOf(1)), tolerance); Iterator it = f.valuesIterator(); while (it.hasNext()) { - assertTrue(it.next() instanceof Long); + Assert.assertTrue(it.next() instanceof Long); } } + @Test public void testSerial() { f.addValue(oneL); f.addValue(twoL); f.addValue(oneI); f.addValue(twoI); - assertEquals(f, TestUtils.serializeAndRecover(f)); + Assert.assertEquals(f, TestUtils.serializeAndRecover(f)); } + @Test public void testGetUniqueCount() { - assertEquals(0, f.getUniqueCount()); + Assert.assertEquals(0, f.getUniqueCount()); f.addValue(oneL); - assertEquals(1, f.getUniqueCount()); + Assert.assertEquals(1, f.getUniqueCount()); f.addValue(oneL); - assertEquals(1, f.getUniqueCount()); + Assert.assertEquals(1, f.getUniqueCount()); f.addValue(twoI); - assertEquals(2, f.getUniqueCount()); + Assert.assertEquals(2, f.getUniqueCount()); } } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/StatUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/StatUtilsTest.java?rev=1083514&r1=1083513&r2=1083514&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/StatUtilsTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/StatUtilsTest.java Sun Mar 20 17:24:14 2011 @@ -16,19 +16,21 @@ */ package org.apache.commons.math.stat; -import junit.framework.TestCase; import org.apache.commons.math.TestUtils; import org.apache.commons.math.stat.descriptive.DescriptiveStatistics; import org.apache.commons.math.util.FastMath; +import org.apache.commons.math.util.MathUtils; import org.apache.commons.math.exception.MathIllegalArgumentException; +import org.junit.Assert; +import org.junit.Test; /** * Test cases for the {@link StatUtils} class. * @version $Revision$ $Date$ */ -public final class StatUtilsTest extends TestCase { +public final class StatUtilsTest { private double one = 1; private float two = 2; @@ -42,56 +44,55 @@ public final class StatUtilsTest extends private double tolerance = 10E-15; private double nan = Double.NaN; - public StatUtilsTest(String name) { - super(name); - } - /** test stats */ + @Test public void testStats() { double[] values = new double[] { one, two, two, three }; - assertEquals("sum", sum, StatUtils.sum(values), tolerance); - assertEquals("sumsq", sumSq, StatUtils.sumSq(values), tolerance); - assertEquals("var", var, StatUtils.variance(values), tolerance); - assertEquals("var with mean", var, StatUtils.variance(values, mean), tolerance); - assertEquals("mean", mean, StatUtils.mean(values), tolerance); - assertEquals("min", min, StatUtils.min(values), tolerance); - assertEquals("max", max, StatUtils.max(values), tolerance); + Assert.assertEquals("sum", sum, StatUtils.sum(values), tolerance); + Assert.assertEquals("sumsq", sumSq, StatUtils.sumSq(values), tolerance); + Assert.assertEquals("var", var, StatUtils.variance(values), tolerance); + Assert.assertEquals("var with mean", var, StatUtils.variance(values, mean), tolerance); + Assert.assertEquals("mean", mean, StatUtils.mean(values), tolerance); + Assert.assertEquals("min", min, StatUtils.min(values), tolerance); + Assert.assertEquals("max", max, StatUtils.max(values), tolerance); } + @Test public void testN0andN1Conditions() throws Exception { double[] values = new double[0]; - assertTrue( + Assert.assertTrue( "Mean of n = 0 set should be NaN", Double.isNaN(StatUtils.mean(values))); - assertTrue( + Assert.assertTrue( "Variance of n = 0 set should be NaN", Double.isNaN(StatUtils.variance(values))); values = new double[] { one }; - assertTrue( + Assert.assertTrue( "Mean of n = 1 set should be value of single item n1", StatUtils.mean(values) == one); - assertTrue( + Assert.assertTrue( "Variance of n = 1 set should be zero", StatUtils.variance(values) == 0); } + @Test public void testArrayIndexConditions() throws Exception { double[] values = { 1.0, 2.0, 3.0, 4.0 }; - assertEquals( + Assert.assertEquals( "Sum not expected", 5.0, StatUtils.sum(values, 1, 2), Double.MIN_VALUE); - assertEquals( + Assert.assertEquals( "Sum not expected", 3.0, StatUtils.sum(values, 0, 2), Double.MIN_VALUE); - assertEquals( + Assert.assertEquals( "Sum not expected", 7.0, StatUtils.sum(values, 2, 2), @@ -99,34 +100,35 @@ public final class StatUtilsTest extends try { StatUtils.sum(values, 2, 3); - fail("Expected RuntimeException"); + Assert.fail("Expected RuntimeException"); } catch (RuntimeException e) { // expected } try { StatUtils.sum(values, -1, 2); - fail("Expected RuntimeException"); + Assert.fail("Expected RuntimeException"); } catch (RuntimeException e) { // expected } } + @Test public void testSumSq() { double[] x = null; // test null try { StatUtils.sumSq(x); - fail("null is not a valid data array."); + Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException ex) { // success } try { StatUtils.sumSq(x, 0, 4); - fail("null is not a valid data array."); + Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException ex) { // success } @@ -147,20 +149,21 @@ public final class StatUtilsTest extends TestUtils.assertEquals(8, StatUtils.sumSq(x, 1, 2), tolerance); } + @Test public void testProduct() { double[] x = null; // test null try { StatUtils.product(x); - fail("null is not a valid data array."); + Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException ex) { // success } try { StatUtils.product(x, 0, 4); - fail("null is not a valid data array."); + Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException ex) { // success } @@ -181,20 +184,21 @@ public final class StatUtilsTest extends TestUtils.assertEquals(4, StatUtils.product(x, 1, 2), tolerance); } + @Test public void testSumLog() { double[] x = null; // test null try { StatUtils.sumLog(x); - fail("null is not a valid data array."); + Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException ex) { // success } try { StatUtils.sumLog(x, 0, 4); - fail("null is not a valid data array."); + Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException ex) { // success } @@ -215,12 +219,13 @@ public final class StatUtilsTest extends TestUtils.assertEquals(2.0 * FastMath.log(two), StatUtils.sumLog(x, 1, 2), tolerance); } + @Test public void testMean() { double[] x = null; try { StatUtils.mean(x, 0, 4); - fail("null is not a valid data array."); + Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException ex) { // success } @@ -238,12 +243,13 @@ public final class StatUtilsTest extends TestUtils.assertEquals(2.5, StatUtils.mean(x, 2, 2), tolerance); } + @Test public void testVariance() { double[] x = null; try { StatUtils.variance(x, 0, 4); - fail("null is not a valid data array."); + Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException ex) { // success } @@ -265,12 +271,13 @@ public final class StatUtilsTest extends TestUtils.assertEquals(0.5, StatUtils.variance(x,2.5, 2, 2), tolerance); } + @Test public void testMax() { double[] x = null; try { StatUtils.max(x, 0, 4); - fail("null is not a valid data array."); + Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException ex) { // success } @@ -304,12 +311,13 @@ public final class StatUtilsTest extends TestUtils.assertEquals(nan, StatUtils.max(x), tolerance); } + @Test public void testMin() { double[] x = null; try { StatUtils.min(x, 0, 4); - fail("null is not a valid data array."); + Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException ex) { // success } @@ -343,20 +351,21 @@ public final class StatUtilsTest extends TestUtils.assertEquals(nan, StatUtils.min(x), tolerance); } + @Test public void testPercentile() { double[] x = null; // test null try { StatUtils.percentile(x, .25); - fail("null is not a valid data array."); + Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException ex) { // success } try { StatUtils.percentile(x, 0, 4, 0.25); - fail("null is not a valid data array."); + Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException ex) { // success } @@ -377,49 +386,51 @@ public final class StatUtilsTest extends TestUtils.assertEquals(2.5, StatUtils.percentile(x, 1, 3, 62.5), tolerance); } + @Test public void testDifferenceStats() throws Exception { double sample1[] = {1d, 2d, 3d, 4d}; double sample2[] = {1d, 3d, 4d, 2d}; double diff[] = {0d, -1d, -1d, 2d}; double small[] = {1d, 4d}; double meanDifference = StatUtils.meanDifference(sample1, sample2); - assertEquals(StatUtils.sumDifference(sample1, sample2), StatUtils.sum(diff), tolerance); - assertEquals(meanDifference, StatUtils.mean(diff), tolerance); - assertEquals(StatUtils.varianceDifference(sample1, sample2, meanDifference), + Assert.assertEquals(StatUtils.sumDifference(sample1, sample2), StatUtils.sum(diff), tolerance); + Assert.assertEquals(meanDifference, StatUtils.mean(diff), tolerance); + Assert.assertEquals(StatUtils.varianceDifference(sample1, sample2, meanDifference), StatUtils.variance(diff), tolerance); try { StatUtils.meanDifference(sample1, small); - fail("Expecting MathIllegalArgumentException"); + Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { StatUtils.varianceDifference(sample1, small, meanDifference); - fail("Expecting MathIllegalArgumentException"); + Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { double[] single = {1.0}; StatUtils.varianceDifference(single, single, meanDifference); - fail("Expecting MathIllegalArgumentException"); + Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } } + @Test public void testGeometricMean() throws Exception { double[] test = null; try { StatUtils.geometricMean(test); - fail("Expecting MathIllegalArgumentException"); + Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } test = new double[] {2, 4, 6, 8}; - assertEquals(FastMath.exp(0.25d * StatUtils.sumLog(test)), + Assert.assertEquals(FastMath.exp(0.25d * StatUtils.sumLog(test)), StatUtils.geometricMean(test), Double.MIN_VALUE); - assertEquals(FastMath.exp(0.5 * StatUtils.sumLog(test, 0, 2)), + Assert.assertEquals(FastMath.exp(0.5 * StatUtils.sumLog(test, 0, 2)), StatUtils.geometricMean(test, 0, 2), Double.MIN_VALUE); } @@ -428,12 +439,13 @@ public final class StatUtilsTest extends * Run the test with the values 50 and 100 and assume standardized values */ + @Test public void testNormalize1() { double sample[] = { 50, 100 }; double expectedSample[] = { -25 / Math.sqrt(1250), 25 / Math.sqrt(1250) }; double[] out = StatUtils.normalize(sample); for (int i = 0; i < out.length; i++) { - assertEquals(out[i], expectedSample[i]); + Assert.assertTrue(MathUtils.equals(out[i], expectedSample[i], 1)); } } @@ -443,6 +455,7 @@ public final class StatUtilsTest extends * precision of 1E-10. */ + @Test public void testNormalize2() { // create an sample with 77 values int length = 77; @@ -461,8 +474,8 @@ public final class StatUtilsTest extends // the calculations do have a limited precision double distance = 1E-10; // check the mean an standard deviation - assertEquals(0.0, stats.getMean(), distance); - assertEquals(1.0, stats.getStandardDeviation(), distance); + Assert.assertEquals(0.0, stats.getMean(), distance); + Assert.assertEquals(1.0, stats.getStandardDeviation(), distance); } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/clustering/EuclideanIntegerPointTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/clustering/EuclideanIntegerPointTest.java?rev=1083514&r1=1083513&r2=1083514&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/clustering/EuclideanIntegerPointTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/clustering/EuclideanIntegerPointTest.java Sun Mar 20 17:24:14 2011 @@ -17,14 +17,13 @@ package org.apache.commons.math.stat.clustering; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; import org.apache.commons.math.TestUtils; import org.apache.commons.math.util.FastMath; +import org.junit.Assert; import org.junit.Test; public class EuclideanIntegerPointTest { @@ -32,16 +31,16 @@ public class EuclideanIntegerPointTest { @Test public void testArrayIsReference() { int[] array = { -3, -2, -1, 0, 1 }; - assertTrue(array == new EuclideanIntegerPoint(array).getPoint()); + Assert.assertTrue(array == new EuclideanIntegerPoint(array).getPoint()); } @Test public void testDistance() { EuclideanIntegerPoint e1 = new EuclideanIntegerPoint(new int[] { -3, -2, -1, 0, 1 }); EuclideanIntegerPoint e2 = new EuclideanIntegerPoint(new int[] { 1, 0, -1, 1, 1 }); - assertEquals(FastMath.sqrt(21.0), e1.distanceFrom(e2), 1.0e-15); - assertEquals(0.0, e1.distanceFrom(e1), 1.0e-15); - assertEquals(0.0, e2.distanceFrom(e2), 1.0e-15); + Assert.assertEquals(FastMath.sqrt(21.0), e1.distanceFrom(e2), 1.0e-15); + Assert.assertEquals(0.0, e1.distanceFrom(e1), 1.0e-15); + Assert.assertEquals(0.0, e2.distanceFrom(e2), 1.0e-15); } @Test @@ -52,14 +51,14 @@ public class EuclideanIntegerPointTest { list.add(new EuclideanIntegerPoint(new int[] { 3, 3 })); list.add(new EuclideanIntegerPoint(new int[] { 2, 4 })); EuclideanIntegerPoint c = list.get(0).centroidOf(list); - assertEquals(2, c.getPoint()[0]); - assertEquals(3, c.getPoint()[1]); + Assert.assertEquals(2, c.getPoint()[0]); + Assert.assertEquals(3, c.getPoint()[1]); } @Test public void testSerial() { EuclideanIntegerPoint p = new EuclideanIntegerPoint(new int[] { -3, -2, -1, 0, 1 }); - assertEquals(p, TestUtils.serializeAndRecover(p)); + Assert.assertEquals(p, TestUtils.serializeAndRecover(p)); } } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/clustering/KMeansPlusPlusClustererTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/clustering/KMeansPlusPlusClustererTest.java?rev=1083514&r1=1083513&r2=1083514&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/clustering/KMeansPlusPlusClustererTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/clustering/KMeansPlusPlusClustererTest.java Sun Mar 20 17:24:14 2011 @@ -17,8 +17,6 @@ package org.apache.commons.math.stat.clustering; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.Arrays; @@ -68,7 +66,7 @@ public class KMeansPlusPlusClustererTest List> clusters = transformer.cluster(Arrays.asList(points), 3, 10); - assertEquals(3, clusters.size()); + Assert.assertEquals(3, clusters.size()); boolean cluster1Found = false; boolean cluster2Found = false; boolean cluster3Found = false; @@ -76,24 +74,24 @@ public class KMeansPlusPlusClustererTest int[] center = cluster.getCenter().getPoint(); if (center[0] < 0) { cluster1Found = true; - assertEquals(8, cluster.getPoints().size()); - assertEquals(-14, center[0]); - assertEquals( 4, center[1]); + Assert.assertEquals(8, cluster.getPoints().size()); + Assert.assertEquals(-14, center[0]); + Assert.assertEquals( 4, center[1]); } else if (center[1] < 0) { cluster2Found = true; - assertEquals(5, cluster.getPoints().size()); - assertEquals( 0, center[0]); - assertEquals(-1, center[1]); + Assert.assertEquals(5, cluster.getPoints().size()); + Assert.assertEquals( 0, center[0]); + Assert.assertEquals(-1, center[1]); } else { cluster3Found = true; - assertEquals(8, cluster.getPoints().size()); - assertEquals(15, center[0]); - assertEquals(5, center[1]); + Assert.assertEquals(8, cluster.getPoints().size()); + Assert.assertEquals(15, center[0]); + Assert.assertEquals(5, center[1]); } } - assertTrue(cluster1Found); - assertTrue(cluster2Found); - assertTrue(cluster3Found); + Assert.assertTrue(cluster1Found); + Assert.assertTrue(cluster2Found); + Assert.assertTrue(cluster3Found); } @@ -110,12 +108,12 @@ public class KMeansPlusPlusClustererTest new EuclideanIntegerPoint(new int[] { 1959, 325100 }), new EuclideanIntegerPoint(new int[] { 1960, 373200 }), }; List> clusters = transformer.cluster(Arrays.asList(points), 1, 1); - assertEquals(1, clusters.size()); - assertEquals(2, (clusters.get(0).getPoints().size())); + Assert.assertEquals(1, clusters.size()); + Assert.assertEquals(2, (clusters.get(0).getPoints().size())); EuclideanIntegerPoint pt1 = new EuclideanIntegerPoint(new int[] { 1959, 325100 }); EuclideanIntegerPoint pt2 = new EuclideanIntegerPoint(new int[] { 1960, 373200 }); - assertTrue(clusters.get(0).getPoints().contains(pt1)); - assertTrue(clusters.get(0).getPoints().contains(pt2)); + Assert.assertTrue(clusters.get(0).getPoints().contains(pt1)); + Assert.assertTrue(clusters.get(0).getPoints().contains(pt2)); } @@ -246,6 +244,6 @@ public class KMeansPlusPlusClustererTest uniquePointIsCenter = true; } } - assertTrue(uniquePointIsCenter); + Assert.assertTrue(uniquePointIsCenter); } } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/correlation/CovarianceTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/correlation/CovarianceTest.java?rev=1083514&r1=1083513&r2=1083514&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/correlation/CovarianceTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/correlation/CovarianceTest.java Sun Mar 20 17:24:14 2011 @@ -20,10 +20,11 @@ import org.apache.commons.math.TestUtils import org.apache.commons.math.linear.RealMatrix; import org.apache.commons.math.linear.Array2DRowRealMatrix; import org.apache.commons.math.stat.descriptive.moment.Variance; +import org.junit.Assert; +import org.junit.Test; -import junit.framework.TestCase; -public class CovarianceTest extends TestCase { +public class CovarianceTest { protected final double[] longleyData = new double[] { 60323,83.0,234289,2356,1590,107608,1947, @@ -105,6 +106,7 @@ public class CovarianceTest extends Test * Data are from NIST: * http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Longley.dat */ + @Test public void testLongly() { RealMatrix matrix = createRealMatrix(longleyData, 16, 7); RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix(); @@ -133,6 +135,7 @@ public class CovarianceTest extends Test * Test R Swiss fertility dataset against R. * Data Source: R datasets package */ + @Test public void testSwissFertility() { RealMatrix matrix = createRealMatrix(swissData, 47, 5); RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix(); @@ -150,30 +153,32 @@ public class CovarianceTest extends Test /** * Constant column */ + @Test public void testConstant() { double[] noVariance = new double[] {1, 1, 1, 1}; double[] values = new double[] {1, 2, 3, 4}; - assertEquals(0d, new Covariance().covariance(noVariance, values, true), Double.MIN_VALUE); - assertEquals(0d, new Covariance().covariance(noVariance, noVariance, true), Double.MIN_VALUE); + Assert.assertEquals(0d, new Covariance().covariance(noVariance, values, true), Double.MIN_VALUE); + Assert.assertEquals(0d, new Covariance().covariance(noVariance, noVariance, true), Double.MIN_VALUE); } /** * Insufficient data */ + @Test public void testInsufficientData() { double[] one = new double[] {1}; double[] two = new double[] {2}; try { new Covariance().covariance(one, two, false); - fail("Expecting IllegalArgumentException"); + Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // Expected } RealMatrix matrix = new Array2DRowRealMatrix(new double[][] {{0},{1}}); try { new Covariance(matrix); - fail("Expecting IllegalArgumentException"); + Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // Expected } @@ -183,6 +188,7 @@ public class CovarianceTest extends Test * Verify that diagonal entries are consistent with Variance computation and matrix matches * column-by-column covariances */ + @Test public void testConsistency() { final RealMatrix matrix = createRealMatrix(swissData, 47, 5); final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix(); @@ -190,13 +196,13 @@ public class CovarianceTest extends Test // Variances on the diagonal Variance variance = new Variance(); for (int i = 0; i < 5; i++) { - assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14); + Assert.assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14); } // Symmetry, column-consistency - assertEquals(covarianceMatrix.getEntry(2, 3), + Assert.assertEquals(covarianceMatrix.getEntry(2, 3), new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14); - assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE); + Assert.assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE); // All columns same -> all entries = column variance RealMatrix repeatedColumns = new Array2DRowRealMatrix(47, 3); @@ -207,7 +213,7 @@ public class CovarianceTest extends Test double columnVariance = variance.evaluate(matrix.getColumn(0)); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { - assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14); + Assert.assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14); } } @@ -220,7 +226,7 @@ public class CovarianceTest extends Test double[] x = data[0]; double[] y = data[1]; - assertEquals(new Covariance().covariance(x, y), + Assert.assertEquals(new Covariance().covariance(x, y), new Covariance().covariance(x, y, true), Double.MIN_VALUE); }