Skip to content

Commit

Permalink
Merge ff0124f into 72a0c9d
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealHaui committed Sep 20, 2017
2 parents 72a0c9d + ff0124f commit 30a34c8
Show file tree
Hide file tree
Showing 19 changed files with 1,461 additions and 487 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.numbers.fraction.BigFraction;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
* Test cases for the {@link LinearCombination} class.
*/
Expand Down Expand Up @@ -294,4 +297,19 @@ public void testInfinite() {
a[7][3], b[7][3])));
Assert.assertTrue(Double.isNaN(LinearCombination.value(a[7], b[7])));
}

@Test
public void testValueTakingThreeArgumentsThrowsIllegalArgumentException() {
double[] doubleArray = new double[4];
double[] doubleArrayTwo = new double[0];

try {
LinearCombination.value(doubleArray, doubleArrayTwo);
fail("Expecting exception: IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertEquals("Dimension mismatch: 4 != 0", e.getMessage());
assertEquals(LinearCombination.class.getName(), e.getStackTrace()[0].getClassName());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Test cases for the {@link SafeNorm} class.
*/
Expand All @@ -24,39 +26,50 @@ public class SafeNormTest {
@Test
public void testTiny() {
final double s = 1e-320;
final double[] v = new double[] { s, s };
Assert.assertEquals(Math.sqrt(2) * s, SafeNorm.value(v), 0d);
final double[] v = new double[]{s, s};
assertEquals(Math.sqrt(2) * s, SafeNorm.value(v), 0d);
}

@Test
public void testBig() {
final double s = 1e300;
final double[] v = new double[] { s, s };
Assert.assertEquals(Math.sqrt(2) * s, SafeNorm.value(v), 0d);
final double[] v = new double[]{s, s};
assertEquals(Math.sqrt(2) * s, SafeNorm.value(v), 0d);
}

@Test
public void testOne3D() {
final double s = 1;
final double[] v = new double[] { s, s, s };
Assert.assertEquals(Math.sqrt(3), SafeNorm.value(v), 0d);
final double[] v = new double[]{s, s, s};
assertEquals(Math.sqrt(3), SafeNorm.value(v), 0d);
}

@Test
public void testUnit3D() {
Assert.assertEquals(1, SafeNorm.value(new double[] { 1, 0, 0 }), 0d);
Assert.assertEquals(1, SafeNorm.value(new double[] { 0, 1, 0 }), 0d);
Assert.assertEquals(1, SafeNorm.value(new double[] { 0, 0, 1 }), 0d);
assertEquals(1, SafeNorm.value(new double[]{1, 0, 0}), 0d);
assertEquals(1, SafeNorm.value(new double[]{0, 1, 0}), 0d);
assertEquals(1, SafeNorm.value(new double[]{0, 0, 1}), 0d);
}

@Test
public void testSimple() {
final double[] v = new double[] { -0.9, 8.7, -6.5, -4.3, -2.1, 0, 1.2, 3.4, -5.6, 7.8, 9.0 };
final double[] v = new double[]{-0.9, 8.7, -6.5, -4.3, -2.1, 0, 1.2, 3.4, -5.6, 7.8, 9.0};
double n = 0;
for (int i = 0; i < v.length; i++) {
n += v[i] * v[i];
}
final double expected = Math.sqrt(n);
Assert.assertEquals(expected, SafeNorm.value(v), 0d);
assertEquals(expected, SafeNorm.value(v), 0d);
}

@Test
public void testValueReturningPositive() {
double[] doubleArray = new double[5];
doubleArray[0] = 3.834E-20;
doubleArray[1] = 5.8798224E-39;
double doubleValue = SafeNorm.value(doubleArray);

assertEquals(3.834E-20, doubleValue, 0.01);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,81 +16,87 @@
*/
package org.apache.commons.numbers.combinatorics;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import org.apache.commons.numbers.core.ArithmeticUtils;
import org.junit.Assert;
import org.junit.Test;

import org.apache.commons.numbers.core.ArithmeticUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
* Test cases for the {@link BinomialCoefficient} class.
*/
public class BinomialCoefficientTest {
/** Cached binomial coefficients. */
/**
* Cached binomial coefficients.
*/
private static final List<Map<Integer, Long>> binomialCache =
new ArrayList<Map<Integer, Long>>();
new ArrayList<Map<Integer, Long>>();

/** Verify that b(0,0) = 1 */
/**
* Verify that b(0,0) = 1
*/
@Test
public void test0Choose0() {
Assert.assertEquals(BinomialCoefficient.value(0, 0), 1);
assertEquals(BinomialCoefficient.value(0, 0), 1);
}

@Test
public void testBinomialCoefficient() {
final long[] bcoef5 = { 1, 5, 10, 10, 5, 1 };
final long[] bcoef6 = { 1, 6, 15, 20, 15, 6, 1 };
final long[] bcoef5 = {1, 5, 10, 10, 5, 1};
final long[] bcoef6 = {1, 6, 15, 20, 15, 6, 1};

for (int i = 0; i < 6; i++) {
Assert.assertEquals("5 choose " + i, bcoef5[i], BinomialCoefficient.value(5, i));
assertEquals("5 choose " + i, bcoef5[i], BinomialCoefficient.value(5, i));
}
for (int i = 0; i < 7; i++) {
Assert.assertEquals("6 choose " + i, bcoef6[i], BinomialCoefficient.value(6, i));
assertEquals("6 choose " + i, bcoef6[i], BinomialCoefficient.value(6, i));
}

for (int n = 1; n < 10; n++) {
for (int k = 0; k <= n; k++) {
Assert.assertEquals(n + " choose " + k,
binomialCoefficient(n, k),
BinomialCoefficient.value(n, k));
assertEquals(n + " choose " + k,
binomialCoefficient(n, k),
BinomialCoefficient.value(n, k));
}
}

final int[] n = { 34, 66, 100, 1500, 1500 };
final int[] k = { 17, 33, 10, 1500 - 4, 4 };
final int[] n = {34, 66, 100, 1500, 1500};
final int[] k = {17, 33, 10, 1500 - 4, 4};
for (int i = 0; i < n.length; i++) {
final long expected = binomialCoefficient(n[i], k[i]);
Assert.assertEquals(n[i] + " choose " + k[i],
expected,
BinomialCoefficient.value(n[i], k[i]));
assertEquals(n[i] + " choose " + k[i],
expected,
BinomialCoefficient.value(n[i], k[i]));
}
}

@Test(expected=CombinatoricsException.class)
@Test(expected = CombinatoricsException.class)
public void testBinomialCoefficientFail1() {
BinomialCoefficient.value(4, 5);
}

@Test(expected=CombinatoricsException.class)
@Test(expected = CombinatoricsException.class)
public void testBinomialCoefficientFail2() {
BinomialCoefficient.value(-1, -2);
}

@Test(expected=ArithmeticException.class)
@Test(expected = ArithmeticException.class)
public void testBinomialCoefficientFail3() {
BinomialCoefficient.value(67, 30);
}

@Test(expected=ArithmeticException.class)
@Test(expected = ArithmeticException.class)
public void testBinomialCoefficientFail4() {
BinomialCoefficient.value(67, 34);
}

@Test(expected=ArithmeticException.class)
@Test(expected = ArithmeticException.class)
public void testBinomialCoefficientFail5() {
BinomialCoefficient.value(700, 300);
}
Expand Down Expand Up @@ -118,34 +124,34 @@ public void testBinomialCoefficientLarge() throws Exception {
} catch (ArithmeticException ex) {
shouldThrow = true;
}
Assert.assertEquals(n + " choose " + k, exactResult, ourResult);
Assert.assertEquals(n + " choose " + k, shouldThrow, didThrow);
assertEquals(n + " choose " + k, exactResult, ourResult);
assertEquals(n + " choose " + k, shouldThrow, didThrow);
Assert.assertTrue(n + " choose " + k, (n > 66 || !didThrow));
}
}

long ourResult = BinomialCoefficient.value(300, 3);
long exactResult = binomialCoefficient(300, 3);
Assert.assertEquals(exactResult, ourResult);
assertEquals(exactResult, ourResult);

ourResult = BinomialCoefficient.value(700, 697);
exactResult = binomialCoefficient(700, 697);
Assert.assertEquals(exactResult, ourResult);
assertEquals(exactResult, ourResult);

final int n = 10000;
ourResult = BinomialCoefficient.value(n, 3);
exactResult = binomialCoefficient(n, 3);
Assert.assertEquals(exactResult, ourResult);
assertEquals(exactResult, ourResult);

}

@Test(expected=CombinatoricsException.class)
@Test(expected = CombinatoricsException.class)
public void testCheckBinomial1() {
// n < 0
BinomialCoefficient.checkBinomial(-1, -2);
}

@Test(expected=CombinatoricsException.class)
@Test(expected = CombinatoricsException.class)
public void testCheckBinomial2() {
// k > n
BinomialCoefficient.checkBinomial(4, 5);
Expand Down Expand Up @@ -181,7 +187,7 @@ static long binomialCoefficient(int n, int k) {
binomialCoefficient(n - 100, k - 100);
}
result = ArithmeticUtils.addAndCheck(binomialCoefficient(n - 1, k - 1),
binomialCoefficient(n - 1, k));
binomialCoefficient(n - 1, k));
}
if (result == -1) {
throw new IllegalArgumentException();
Expand All @@ -192,4 +198,16 @@ static long binomialCoefficient(int n, int k) {
binomialCache.get(n).put(Integer.valueOf(k), Long.valueOf(result));
return result;
}

@Test
public void testCheckBinomialThrowsCombinatoricsException() {
try {
BinomialCoefficient.checkBinomial(66, (-2802));
fail("Expecting exception: CombinatoricsException");
} catch (CombinatoricsException e) {
assertEquals("Number -2,802 is out of range [0, 66]", e.getMessage());
assertEquals(BinomialCoefficient.class.getName(), e.getStackTrace()[0].getClassName());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import org.junit.Assert;
import org.junit.Test;

import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.fail;

/**
* Test cases for the {@link FactorialDouble} class.
*/
Expand Down Expand Up @@ -122,9 +125,25 @@ public void testCacheDecrease() {
*/
private double factorialDirect(int n) {
double result = 1;

for (int i = 2; i <= n; i++) {
result *= i;
}

return result;
}

@Test
public void testWithCacheThrowsCombinatoricsException() {
FactorialDouble factorialDouble = FactorialDouble.create();

try {
factorialDouble.withCache((-2194));
fail("Expecting exception: CombinatoricsException");
} catch(CombinatoricsException e) {
assertEquals("Number -2,194 is negative",e.getMessage());
assertEquals(FactorialDouble.class.getName(), e.getStackTrace()[0].getClassName());
}
}

}

0 comments on commit 30a34c8

Please sign in to comment.