[Aprog]
[aprog.git] / Aprog / test / net / sourceforge / aprog / tools / MathToolsTest.java
blob7a91d9ca3be664c4c09f8bd5a47938e5882b8def
1 /*
2 * The MIT License
3 *
4 * Copyright 2011 Codist Monk.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 package net.sourceforge.aprog.tools;
27 import static org.junit.Assert.*;
29 import net.sourceforge.aprog.tools.MathTools.Statistics;
31 import org.junit.Test;
33 /**
34 * Automated tests using JUnit for {@link MathTools}.
36 * @author codistmonk (creation 2011-09-02)
38 public final class MathToolsTest {
40 // TODO add more tests
42 @Test
43 public final void testGcd() {
44 assertEquals(4L, MathTools.gcd(8L, 12L));
45 assertEquals(4, MathTools.gcd(8, 12));
48 @Test
49 public final void testLcm() {
50 assertEquals(24L, MathTools.lcm(6L, 8L));
53 @Test
54 public final void testFactorial() {
55 assertEquals(6L, MathTools.factorial(3L));
58 @Test
59 public final void testNPk() {
60 assertEquals(12L, MathTools.nPk(4L, 2L));
63 @Test
64 public void testMultichoose() {
65 assertEquals(126L, MathTools.multichoose(5L, 5L));
68 @Test
69 public final void testNCk() {
70 assertEquals(1L, MathTools.nCk(0L, 0L));
71 assertEquals(1L, MathTools.nCk(4L, 0L));
72 assertEquals(6L, MathTools.nCk(4L, 2L));
73 assertEquals(1L, MathTools.nCk(4L, 4L));
76 @Test
77 public final void testStatistics() {
78 final Statistics statistics = new Statistics();
80 assertEquals(0, statistics.getCount());
81 assertEquals(+0.0, statistics.getSum(), +0.0);
82 assertEquals(+0.0, statistics.getSumOfSquares(), +0.0);
83 assertEquals(Double.NaN, statistics.getMean(), +0.0);
84 assertEquals(Double.POSITIVE_INFINITY, statistics.getMinimum(), +0.0);
85 assertEquals(Double.NEGATIVE_INFINITY, statistics.getMaximum(), +0.0);
87 statistics.addValue(+1.0);
88 statistics.addValue(+2.0);
90 assertEquals(2, statistics.getCount());
91 assertEquals(+3.0, statistics.getSum(), +0.0);
92 assertEquals(+5.0, statistics.getSumOfSquares(), +0.0);
93 assertEquals(+1.5, statistics.getMean(), +0.0);
94 assertEquals(+1.0, statistics.getMinimum(), +0.0);
95 assertEquals(+2.0, statistics.getMaximum(), +0.0);
96 assertEquals(+1.0, statistics.getAmplitude(), +0.0);
97 assertEquals(+0.0, statistics.getNormalizedValue(+1.0), +0.0);
98 assertEquals(+1.0, statistics.getNormalizedValue(+2.0), +0.0);
99 assertEquals(+1.0, statistics.getDenormalizedValue(+0.0), +0.0);
100 assertEquals(+2.0, statistics.getDenormalizedValue(+1.0), +0.0);
101 assertEquals(+0.25, statistics.getVariance(), +0.0);
103 statistics.reset();
105 assertEquals(0, statistics.getCount());
106 assertEquals(+0.0, statistics.getSum(), +0.0);
107 assertEquals(+0.0, statistics.getSumOfSquares(), +0.0);
108 assertEquals(Double.NaN, statistics.getMean(), +0.0);
109 assertEquals(Double.POSITIVE_INFINITY, statistics.getMinimum(), +0.0);
110 assertEquals(Double.NEGATIVE_INFINITY, statistics.getMaximum(), +0.0);