KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > util > MathUtilsTest


1 /*
2  * Copyright 2003-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.math.util;
17
18 import java.math.BigDecimal JavaDoc;
19
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23
24 /**
25  * Test cases for the MathUtils class.
26  *
27  * @version $Revision$ $Date: 2005-06-25 18:19:16 -0700 (Sat, 25 Jun 2005) $
28  */

29 public final class MathUtilsTest extends TestCase {
30
31     public MathUtilsTest(String JavaDoc name) {
32         super(name);
33     }
34
35     public void setUp() {
36     }
37
38     public static Test suite() {
39         TestSuite suite = new TestSuite(MathUtilsTest.class);
40         suite.setName("MathUtils Tests");
41         return suite;
42     }
43     
44     public void testAddAndCheck() {
45         int big = Integer.MAX_VALUE;
46         int bigNeg = Integer.MIN_VALUE;
47         assertEquals(big, MathUtils.addAndCheck(big, 0));
48         try {
49             int res = MathUtils.addAndCheck(big, 1);
50         } catch (ArithmeticException JavaDoc ex) {}
51         try {
52             int res = MathUtils.addAndCheck(bigNeg, -1);
53         } catch (ArithmeticException JavaDoc ex) {}
54     }
55     
56     public void testMulAndCheck() {
57         int big = Integer.MAX_VALUE;
58         int bigNeg = Integer.MIN_VALUE;
59         assertEquals(big, MathUtils.mulAndCheck(big, 1));
60         try {
61             int res = MathUtils.mulAndCheck(big, 2);
62         } catch (ArithmeticException JavaDoc ex) {}
63         try {
64             int res = MathUtils.mulAndCheck(bigNeg, 2);
65         } catch (ArithmeticException JavaDoc ex) {}
66     }
67     
68     public void testSubAndCheck() {
69         int big = Integer.MAX_VALUE;
70         int bigNeg = Integer.MIN_VALUE;
71         assertEquals(big, MathUtils.subAndCheck(big, 0));
72         try {
73             int res = MathUtils.subAndCheck(big, -1);
74         } catch (ArithmeticException JavaDoc ex) {}
75         try {
76             int res = MathUtils.subAndCheck(bigNeg, 1);
77         } catch (ArithmeticException JavaDoc ex) {}
78     }
79     
80     public void testBinomialCoefficient() {
81         long[] bcoef5 = {1,5,10,10,5,1};
82         long[] bcoef6 = {1,6,15,20,15,6,1};
83         for (int i = 0; i < 6; i++) {
84             assertEquals("5 choose " + i, bcoef5[i],
85                 MathUtils.binomialCoefficient(5,i));
86         }
87         for (int i = 0; i < 7; i++) {
88             assertEquals("6 choose " + i, bcoef6[i],
89                 MathUtils.binomialCoefficient(6,i));
90         }
91         
92         for (int n = 1; n < 10; n++) {
93             for (int k = 0; k <= n; k++) {
94                 assertEquals(n + " choose " + k, binomialCoefficient(n, k),
95                     MathUtils.binomialCoefficient(n, k));
96                 assertEquals(n + " choose " + k,(double) binomialCoefficient(n, k),
97                     MathUtils.binomialCoefficientDouble(n, k),Double.MIN_VALUE);
98                 assertEquals(n + " choose " + k,
99                     Math.log((double) binomialCoefficient(n, k)),
100                     MathUtils.binomialCoefficientLog(n, k),10E-12);
101             }
102         }
103       
104       /*
105        * Takes a long time for recursion to unwind, but succeeds
106        * and yields exact value = 2,333,606,220
107         
108         assertEquals(MathUtils.binomialCoefficient(34,17),
109             binomialCoefficient(34,17));
110        */

111     }
112     
113     /** Verify that b(0,0) = 1 */
114     public void test0Choose0() {
115         assertEquals(MathUtils.binomialCoefficientDouble(0, 0), 1d, 0);
116         assertEquals(MathUtils.binomialCoefficientLog(0, 0), 0d, 0);
117         assertEquals(MathUtils.binomialCoefficient(0, 0), 1);
118     }
119     
120     public void testBinomialCoefficientFail() {
121         try {
122             long x = MathUtils.binomialCoefficient(4,5);
123             fail ("expecting IllegalArgumentException");
124         } catch (IllegalArgumentException JavaDoc ex) {
125             ;
126         }
127         
128         try {
129             double x = MathUtils.binomialCoefficientDouble(4,5);
130             fail ("expecting IllegalArgumentException");
131         } catch (IllegalArgumentException JavaDoc ex) {
132             ;
133         }
134         
135         try {
136             double x = MathUtils.binomialCoefficientLog(4,5);
137             fail ("expecting IllegalArgumentException");
138         } catch (IllegalArgumentException JavaDoc ex) {
139             ;
140         }
141         try {
142             long x = MathUtils.binomialCoefficient(67,34);
143             fail ("expecting ArithmeticException");
144         } catch (ArithmeticException JavaDoc ex) {
145             ;
146         }
147         double x = MathUtils.binomialCoefficientDouble(1030,515);
148         assertTrue("expecting infinite binomial coefficient",
149             Double.isInfinite(x));
150     }
151
152     public void testFactorial() {
153         for (int i = 1; i < 10; i++) {
154             assertEquals(i + "! ",factorial(i),MathUtils.factorial(i));
155             assertEquals(i + "! ",(double)factorial(i),
156                 MathUtils.factorialDouble(i),Double.MIN_VALUE);
157             assertEquals(i + "! ",Math.log((double)factorial(i)),
158                 MathUtils.factorialLog(i),10E-12);
159         }
160         assertEquals("0", 1, MathUtils.factorial(0));
161         assertEquals("0", 1.0d, MathUtils.factorialDouble(0), 1E-14);
162         assertEquals("0", 0.0d, MathUtils.factorialLog(0), 1E-14);
163     }
164
165     public void testFactorialFail() {
166         try {
167             long x = MathUtils.factorial(-1);
168             fail ("expecting IllegalArgumentException");
169         } catch (IllegalArgumentException JavaDoc ex) {
170             ;
171         }
172         try {
173             double x = MathUtils.factorialDouble(-1);
174             fail ("expecting IllegalArgumentException");
175         } catch (IllegalArgumentException JavaDoc ex) {
176             ;
177         }
178         try {
179             double x = MathUtils.factorialLog(-1);
180             fail ("expecting IllegalArgumentException");
181         } catch (IllegalArgumentException JavaDoc ex) {
182             ;
183         }
184         try {
185             double x = MathUtils.factorial(21);
186             fail ("expecting ArithmeticException");
187         } catch (ArithmeticException JavaDoc ex) {
188             ;
189         }
190         assertTrue("expecting infinite factorial value",
191             Double.isInfinite(MathUtils.factorialDouble(171)));
192     }
193
194
195     /**
196      * Exact recursive implementation to test against
197      */

198     private long binomialCoefficient(int n, int k) {
199         if ((n == k) || (k == 0)) {
200             return 1;
201         }
202         if ((k == 1) || (k == n - 1)) {
203             return n;
204         }
205         return binomialCoefficient(n - 1, k - 1) +
206             binomialCoefficient(n - 1, k);
207     }
208
209     /**
210      * Finds the largest values of n for which binomialCoefficient and
211      * binomialCoefficientDouble will return values that fit in a long, double,
212      * resp. Remove comments around test below to get this in test-report
213      *
214         public void testLimits() {
215             findBinomialLimits();
216         }
217      */

218
219     private void findBinomialLimits() {
220         /**
221          * will kick out 66 as the limit for long
222          */

223         boolean foundLimit = false;
224         int test = 10;
225         while (!foundLimit) {
226             try {
227                 double x = MathUtils.binomialCoefficient(test, test / 2);
228             } catch (ArithmeticException JavaDoc ex) {
229                 foundLimit = true;
230                 System.out.println
231                     ("largest n for binomialCoefficient = " + (test - 1) );
232             }
233             test++;
234         }
235
236        /**
237         * will kick out 1029 as the limit for double
238         */

239         foundLimit = false;
240         test = 10;
241         while (!foundLimit) {
242             double x = MathUtils.binomialCoefficientDouble(test, test / 2);
243             if (Double.isInfinite(x)) {
244                 foundLimit = true;
245                 System.out.println
246                     ("largest n for binomialCoefficientD = " + (test - 1) );
247             }
248             test++;
249         }
250     }
251
252     /**
253      * Finds the largest values of n for which factiorial and
254      * factorialDouble will return values that fit in a long, double,
255      * resp. Remove comments around test below to get this in test-report
256
257         public void testFactiorialLimits() {
258             findFactorialLimits();
259         }
260      */

261
262     private void findFactorialLimits() {
263         /**
264          * will kick out 20 as the limit for long
265          */

266         boolean foundLimit = false;
267         int test = 10;
268         while (!foundLimit) {
269             try {
270                 double x = MathUtils.factorial(test);
271             } catch (ArithmeticException JavaDoc ex) {
272                 foundLimit = true;
273                 System.out.println
274                     ("largest n for factorial = " + (test - 1) );
275             }
276             test++;
277         }
278
279        /**
280         * will kick out 170 as the limit for double
281         */

282         foundLimit = false;
283         test = 10;
284         while (!foundLimit) {
285             double x = MathUtils.factorialDouble(test);
286             if (Double.isInfinite(x)) {
287                 foundLimit = true;
288                 System.out.println
289                     ("largest n for factorialDouble = " + (test - 1) );
290             }
291             test++;
292         }
293     }
294
295
296     /**
297      * Exact direct multiplication implementation to test against
298      */

299     private long factorial(int n) {
300         long result = 1;
301         for (int i = 2; i <= n; i++) {
302             result *= i;
303         }
304         return result;
305     }
306
307     public void testSignDouble() {
308         double delta = 0.0 ;
309         assertEquals( 1.0, MathUtils.indicator( 2.0 ), delta ) ;
310         assertEquals( -1.0, MathUtils.indicator( -2.0 ), delta ) ;
311     }
312
313     public void testSignFloat() {
314         float delta = 0.0F ;
315         assertEquals( 1.0F, MathUtils.indicator( 2.0F ), delta ) ;
316         assertEquals( -1.0F, MathUtils.indicator( -2.0F ), delta ) ;
317     }
318
319     public void testSignByte() {
320         assertEquals( (byte)1, MathUtils.indicator( (byte)2 ) ) ;
321         assertEquals( (byte)(-1), MathUtils.indicator( (byte)(-2) ) ) ;
322     }
323
324     public void testSignShort() {
325         assertEquals( (short)1, MathUtils.indicator( (short)2 ) ) ;
326         assertEquals( (short)(-1), MathUtils.indicator( (short)(-2) ) ) ;
327     }
328
329     public void testSignInt() {
330         assertEquals( (int)1, MathUtils.indicator( (int)(2) ) ) ;
331         assertEquals( (int)(-1), MathUtils.indicator( (int)(-2) ) ) ;
332     }
333
334     public void testSignLong() {
335         assertEquals( 1L, MathUtils.indicator( 2L ) ) ;
336         assertEquals( -1L, MathUtils.indicator( -2L ) ) ;
337     }
338    
339     public void testIndicatorDouble() {
340         double delta = 0.0 ;
341         assertEquals( 1.0, MathUtils.indicator( 2.0 ), delta ) ;
342         assertEquals( 1.0, MathUtils.indicator( 0.0 ), delta ) ;
343         assertEquals( -1.0, MathUtils.indicator( -2.0 ), delta ) ;
344     }
345     
346     public void testIndicatorFloat() {
347         float delta = 0.0F ;
348         assertEquals( 1.0F, MathUtils.indicator( 2.0F ), delta ) ;
349         assertEquals( 1.0F, MathUtils.indicator( 0.0F ), delta ) ;
350         assertEquals( -1.0F, MathUtils.indicator( -2.0F ), delta ) ;
351     }
352     
353     public void testIndicatorByte() {
354         assertEquals( (byte)1, MathUtils.indicator( (byte)2 ) ) ;
355         assertEquals( (byte)1, MathUtils.indicator( (byte)0 ) ) ;
356         assertEquals( (byte)(-1), MathUtils.indicator( (byte)(-2) ) ) ;
357     }
358     
359     public void testIndicatorShort() {
360         assertEquals( (short)1, MathUtils.indicator( (short)2 ) ) ;
361         assertEquals( (short)1, MathUtils.indicator( (short)0 ) ) ;
362         assertEquals( (short)(-1), MathUtils.indicator( (short)(-2) ) ) ;
363     }
364     
365     public void testIndicatorInt() {
366         assertEquals( (int)1, MathUtils.indicator( (int)(2) ) ) ;
367         assertEquals( (int)1, MathUtils.indicator( (int)(0) ) ) ;
368         assertEquals( (int)(-1), MathUtils.indicator( (int)(-2) ) ) ;
369     }
370     
371     public void testIndicatorLong() {
372         assertEquals( 1L, MathUtils.indicator( 2L ) ) ;
373         assertEquals( 1L, MathUtils.indicator( 0L ) ) ;
374         assertEquals( -1L, MathUtils.indicator( -2L ) ) ;
375     }
376     
377     public void testCosh() {
378         double x = 3.0;
379         double expected = 10.06766;
380         assertEquals(expected, MathUtils.cosh(x), 1.0e-5);
381     }
382     
383     public void testSinh() {
384         double x = 3.0;
385         double expected = 10.01787;
386         assertEquals(expected, MathUtils.sinh(x), 1.0e-5);
387     }
388     
389     public void testCoshNaN() {
390         assertTrue(Double.isNaN(MathUtils.cosh(Double.NaN)));
391     }
392     
393     public void testSinhNaN() {
394         assertTrue(Double.isNaN(MathUtils.sinh(Double.NaN)));
395     }
396     
397     public void testEquals() {
398         double[] testArray = {Double.NaN, Double.POSITIVE_INFINITY,
399                 Double.NEGATIVE_INFINITY, 1d, 0d};
400         for (int i = 0; i < testArray.length; i++) {
401             for (int j = 0; j < testArray.length; j ++) {
402                 if (i == j) {
403                     assertTrue(MathUtils.equals(testArray[i], testArray[j]));
404                     assertTrue(MathUtils.equals(testArray[j], testArray[i]));
405                 } else {
406                     assertTrue(!MathUtils.equals(testArray[i], testArray[j]));
407                     assertTrue(!MathUtils.equals(testArray[j], testArray[i]));
408                 }
409             }
410         }
411     }
412     
413     public void testHash() {
414         double[] testArray = {Double.NaN, Double.POSITIVE_INFINITY,
415                 Double.NEGATIVE_INFINITY, 1d, 0d, 1E-14, (1 + 1E-14),
416                 Double.MIN_VALUE, Double.MAX_VALUE};
417         for (int i = 0; i < testArray.length; i++) {
418             for (int j = 0; j < testArray.length; j ++) {
419                 if (i == j) {
420                     assertEquals(MathUtils.hash(testArray[i]), MathUtils.hash(testArray[j]));
421                     assertEquals(MathUtils.hash(testArray[j]), MathUtils.hash(testArray[i]));
422                 } else {
423                     assertTrue(MathUtils.hash(testArray[i]) != MathUtils.hash(testArray[j]));
424                     assertTrue(MathUtils.hash(testArray[j]) != MathUtils.hash(testArray[i]));
425                 }
426             }
427         }
428     }
429     
430     public void testGcd() {
431         int a = 30;
432         int b = 50;
433         int c = 77;
434
435         assertEquals(0, MathUtils.gcd(0, 0));
436         
437         assertEquals(b, MathUtils.gcd( 0, b));
438         assertEquals(a, MathUtils.gcd( a, 0));
439         assertEquals(b, MathUtils.gcd( 0, -b));
440         assertEquals(a, MathUtils.gcd(-a, 0));
441         
442         assertEquals(10, MathUtils.gcd( a, b));
443         assertEquals(10, MathUtils.gcd(-a, b));
444         assertEquals(10, MathUtils.gcd( a, -b));
445         assertEquals(10, MathUtils.gcd(-a, -b));
446         
447         assertEquals(1, MathUtils.gcd( a, c));
448         assertEquals(1, MathUtils.gcd(-a, c));
449         assertEquals(1, MathUtils.gcd( a, -c));
450         assertEquals(1, MathUtils.gcd(-a, -c));
451     }
452     
453     public void testLcm() {
454         int a = 30;
455         int b = 50;
456         int c = 77;
457         
458         assertEquals(0, MathUtils.lcm(0, b));
459         assertEquals(0, MathUtils.lcm(a, 0));
460         assertEquals(b, MathUtils.lcm(1, b));
461         assertEquals(a, MathUtils.lcm(a, 1));
462         assertEquals(150, MathUtils.lcm(a, b));
463         assertEquals(150, MathUtils.lcm(-a, b));
464         assertEquals(150, MathUtils.lcm(a, -b));
465         assertEquals(2310, MathUtils.lcm(a, c));
466         
467         try {
468             MathUtils.lcm(Integer.MAX_VALUE, Integer.MAX_VALUE - 1);
469             fail("Expecting ArithmeticException");
470         } catch (ArithmeticException JavaDoc ex) {
471             // expected
472
}
473     }
474     
475     public void testRoundFloat() {
476         float x = 1.234567890f;
477         assertEquals(1.23f, MathUtils.round(x, 2), 0.0f);
478         assertEquals(1.235f, MathUtils.round(x, 3), 0.0f);
479         assertEquals(1.2346f, MathUtils.round(x, 4), 0.0f);
480
481         assertEquals(1.23f, MathUtils.round(x, 2, BigDecimal.ROUND_DOWN), 0.0f);
482         assertEquals(1.234f, MathUtils.round(x, 3, BigDecimal.ROUND_DOWN), 0.0f);
483         assertEquals(1.2345f, MathUtils.round(x, 4, BigDecimal.ROUND_DOWN), 0.0f);
484     }
485     
486     public void testRoundDouble() {
487         double x = 1.234567890;
488         assertEquals(1.23, MathUtils.round(x, 2), 0.0);
489         assertEquals(1.235, MathUtils.round(x, 3), 0.0);
490         assertEquals(1.2346, MathUtils.round(x, 4), 0.0);
491
492         assertEquals(1.23, MathUtils.round(x, 2, BigDecimal.ROUND_DOWN), 0.0);
493         assertEquals(1.234, MathUtils.round(x, 3, BigDecimal.ROUND_DOWN), 0.0);
494         assertEquals(1.2345, MathUtils.round(x, 4, BigDecimal.ROUND_DOWN), 0.0);
495     }
496 }
Popular Tags