commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1500500 - /commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java
Date Sun, 07 Jul 2013 18:06:41 GMT
Author: sebb
Date: Sun Jul  7 18:06:40 2013
New Revision: 1500500

URL: http://svn.apache.org/r1500500
Log:
Turn fixed values into constants

Modified:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java?rev=1500500&r1=1500499&r2=1500500&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java
Sun Jul  7 18:06:40 2013
@@ -35,13 +35,14 @@ import org.junit.Test;
  */
 
 public final class FrequencyTest {
-    private long oneL = 1;
-    private long twoL = 2;
-    private long threeL = 3;
-    private int oneI = 1;
-    private int twoI = 2;
-    private int threeI=3;
-    private double tolerance = 10E-15;
+    private static final long ONE_LONG = 1L;
+    private static final long TWO_LONG = 2L;
+    private static final long THREE_LONG = 3L;
+    private static final int ONE = 1;
+    private static final int TWO = 2;
+    private static final int THREEE = 3 ;
+    private static final double TOLERANCE = 10E-15d;
+
     private Frequency f = null;
 
     @Before
@@ -53,10 +54,10 @@ public final class FrequencyTest {
     @Test
     public void testCounts() {
         Assert.assertEquals("total count",0,f.getSumFreq());
-        f.addValue(oneL);
-        f.addValue(twoL);
+        f.addValue(ONE_LONG);
+        f.addValue(TWO_LONG);
         f.addValue(1);
-        f.addValue(oneI);
+        f.addValue(ONE);
         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));
@@ -77,9 +78,9 @@ public final class FrequencyTest {
         f.addValue("oNe");
         f.addValue("Z");
         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);
+        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;
@@ -91,10 +92,10 @@ public final class FrequencyTest {
         f.addValue(Integer.valueOf(-1));
         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);
+        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);
@@ -103,8 +104,8 @@ public final class FrequencyTest {
         f.addValue("oNe");
         f.addValue("Z");
         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);
+        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();
@@ -126,26 +127,26 @@ public final class FrequencyTest {
     /** test pcts */
     @Test
     public void testPcts() {
-        f.addValue(oneL);
-        f.addValue(twoL);
-        f.addValue(oneI);
-        f.addValue(twoI);
-        f.addValue(threeL);
-        f.addValue(threeL);
+        f.addValue(ONE_LONG);
+        f.addValue(TWO_LONG);
+        f.addValue(ONE);
+        f.addValue(TWO);
+        f.addValue(THREE_LONG);
+        f.addValue(THREE_LONG);
         f.addValue(3);
-        f.addValue(threeI);
-        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);
+        f.addValue(THREEE);
+        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(THREE_LONG),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(THREE_LONG),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 */
@@ -168,10 +169,10 @@ public final class FrequencyTest {
         } catch (IllegalArgumentException ex) {
             // expected
         }
-        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);
+        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");
@@ -202,10 +203,10 @@ public final class FrequencyTest {
      */
     @Test
     public void testToString() throws Exception {
-        f.addValue(oneL);
-        f.addValue(twoL);
-        f.addValue(oneI);
-        f.addValue(twoI);
+        f.addValue(ONE_LONG);
+        f.addValue(TWO_LONG);
+        f.addValue(ONE);
+        f.addValue(TWO);
 
         String s = f.toString();
         //System.out.println(s);
@@ -236,9 +237,9 @@ public final class FrequencyTest {
         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);
+        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()) {
             Assert.assertTrue(it.next() instanceof Long);
@@ -247,80 +248,80 @@ public final class FrequencyTest {
 
     @Test
     public void testSerial() {
-        f.addValue(oneL);
-        f.addValue(twoL);
-        f.addValue(oneI);
-        f.addValue(twoI);
+        f.addValue(ONE_LONG);
+        f.addValue(TWO_LONG);
+        f.addValue(ONE);
+        f.addValue(TWO);
         Assert.assertEquals(f, TestUtils.serializeAndRecover(f));
     }
     
     @Test
     public void testGetUniqueCount() {
         Assert.assertEquals(0, f.getUniqueCount());
-        f.addValue(oneL);
+        f.addValue(ONE_LONG);
         Assert.assertEquals(1, f.getUniqueCount());
-        f.addValue(oneL);
+        f.addValue(ONE_LONG);
         Assert.assertEquals(1, f.getUniqueCount());
-        f.addValue(twoI);
+        f.addValue(TWO);
         Assert.assertEquals(2, f.getUniqueCount());
     }
     
     @Test
     public void testIncrement() {
         Assert.assertEquals(0, f.getUniqueCount());
-        f.incrementValue(oneL, 1);
-        Assert.assertEquals(1, f.getCount(oneL));
+        f.incrementValue(ONE_LONG, 1);
+        Assert.assertEquals(1, f.getCount(ONE_LONG));
 
-        f.incrementValue(oneL, 4);
-        Assert.assertEquals(5, f.getCount(oneL));
+        f.incrementValue(ONE_LONG, 4);
+        Assert.assertEquals(5, f.getCount(ONE_LONG));
         
-        f.incrementValue(oneL, -5);
-        Assert.assertEquals(0, f.getCount(oneL));
+        f.incrementValue(ONE_LONG, -5);
+        Assert.assertEquals(0, f.getCount(ONE_LONG));
     }
     
     @Test
     public void testMerge() {
         Assert.assertEquals(0, f.getUniqueCount());
-        f.addValue(oneL);
-        f.addValue(twoL);
-        f.addValue(oneI);
-        f.addValue(twoI);
+        f.addValue(ONE_LONG);
+        f.addValue(TWO_LONG);
+        f.addValue(ONE);
+        f.addValue(TWO);
         
         Assert.assertEquals(2, f.getUniqueCount());
-        Assert.assertEquals(2, f.getCount(oneI));
-        Assert.assertEquals(2, f.getCount(twoI));
+        Assert.assertEquals(2, f.getCount(ONE));
+        Assert.assertEquals(2, f.getCount(TWO));
 
         Frequency g = new Frequency();
-        g.addValue(oneL);
-        g.addValue(threeL);
-        g.addValue(threeI);
+        g.addValue(ONE_LONG);
+        g.addValue(THREE_LONG);
+        g.addValue(THREEE);
 
         Assert.assertEquals(2, g.getUniqueCount());
-        Assert.assertEquals(1, g.getCount(oneI));
-        Assert.assertEquals(2, g.getCount(threeI));
+        Assert.assertEquals(1, g.getCount(ONE));
+        Assert.assertEquals(2, g.getCount(THREEE));
 
         f.merge(g);
         
         Assert.assertEquals(3, f.getUniqueCount());
-        Assert.assertEquals(3, f.getCount(oneI));
-        Assert.assertEquals(2, f.getCount(twoI));
-        Assert.assertEquals(2, f.getCount(threeI));        
+        Assert.assertEquals(3, f.getCount(ONE));
+        Assert.assertEquals(2, f.getCount(TWO));
+        Assert.assertEquals(2, f.getCount(THREEE));        
     }
     
     @Test
     public void testMergeCollection() {
         Assert.assertEquals(0, f.getUniqueCount());
-        f.addValue(oneL);
+        f.addValue(ONE_LONG);
         
         Assert.assertEquals(1, f.getUniqueCount());
-        Assert.assertEquals(1, f.getCount(oneI));
-        Assert.assertEquals(0, f.getCount(twoI));
+        Assert.assertEquals(1, f.getCount(ONE));
+        Assert.assertEquals(0, f.getCount(TWO));
 
         Frequency g = new Frequency();
-        g.addValue(twoL);
+        g.addValue(TWO_LONG);
 
         Frequency h = new Frequency();
-        h.addValue(threeL);
+        h.addValue(THREE_LONG);
         
         List<Frequency> coll = new ArrayList<Frequency>();
         coll.add(g);
@@ -328,9 +329,9 @@ public final class FrequencyTest {
         f.merge(coll);
         
         Assert.assertEquals(3, f.getUniqueCount());
-        Assert.assertEquals(1, f.getCount(oneI));
-        Assert.assertEquals(1, f.getCount(twoI));
-        Assert.assertEquals(1, f.getCount(threeI));        
+        Assert.assertEquals(1, f.getCount(ONE));
+        Assert.assertEquals(1, f.getCount(TWO));
+        Assert.assertEquals(1, f.getCount(THREEE));        
     }
     
 //    @Test



Mime
View raw message