org.apache.commons.math3

## Class TestUtils

• public class TestUtils
extends Object
• ### Method Summary

Methods
Modifier and Type Method and Description
static void assertChiSquareAccept(double[] expected, long[] observed, double alpha)
Asserts the null hypothesis for a ChiSquare test.
static void assertChiSquareAccept(int[] values, double[] expected, long[] observed, double alpha)
Asserts the null hypothesis for a ChiSquare test.
static void assertChiSquareAccept(String[] valueLabels, double[] expected, long[] observed, double alpha)
Asserts the null hypothesis for a ChiSquare test.
static void assertContains(Complex[] values, Complex z, double epsilon)
Fails iff values does not contain a number within epsilon of z.
static void assertContains(double[] values, double x, double epsilon)
Fails iff values does not contain a number within epsilon of x.
static void assertContains(String msg, Complex[] values, Complex z, double epsilon)
Fails iff values does not contain a number within epsilon of z.
static void assertContains(String msg, double[] values, double x, double epsilon)
Fails iff values does not contain a number within epsilon of x.
static void assertEquals(Complex expected, Complex actual, double delta)
Verifies that real and imaginary parts of the two complex arguments differ by at most delta.
static void assertEquals(double[] expected, double[] observed, double tolerance)
Verifies that two double arrays have equal entries, up to tolerance
static void assertEquals(double expected, double actual, double delta)
Verifies that expected and actual are within delta, or are both NaN or infinities of the same sign.
static void assertEquals(FieldMatrix<? extends FieldElement<?>> expected, FieldMatrix<? extends FieldElement<?>> observed)
verifies that two matrices are equal
static void assertEquals(String msg, double[] expected, double[] observed, double tolerance)
verifies that two arrays are close (sup norm)
static void assertEquals(String message, double[] expected, RealVector actual, double delta)
Asserts that all entries of the specified vectors are equal to within a positive delta.
static void assertEquals(String msg, double expected, double actual, double delta)
Verifies that expected and actual are within delta, or are both NaN or infinities of the same sign.
static void assertEquals(String msg, RealMatrix expected, RealMatrix observed, double tolerance)
verifies that two matrices are close (1-norm)
static void assertEquals(String message, RealVector expected, RealVector actual, double delta)
Asserts that all entries of the specified vectors are equal to within a positive delta.
static <T extends FieldElement<T>> void assertEquals(T[] m, T[] n)
verifies that two arrays are equal
static void assertRelativelyEquals(double expected, double actual, double relativeError)
Verifies that the relative error in actual vs. expected is less than or equal to relativeError.
static void assertRelativelyEquals(String msg, double expected, double actual, double relativeError)
Verifies that the relative error in actual vs. expected is less than or equal to relativeError.
static void assertSame(Complex expected, Complex actual)
Verifies that real and imaginary parts of the two complex arguments are exactly the same.
static void assertSame(double expected, double actual)
Verifies that the two arguments are exactly the same, either both NaN or infinities of same sign, or identical floating point values.
static void checkSerializedEquality(Object object)
Verifies that serialization preserves equals and hashCode.
static int eliminateZeroMassPoints(int[] densityPoints, double[] densityValues)
Eliminates points with zero mass from densityPoints and densityValues parallel arrays.
static double[] getDistributionQuartiles(RealDistribution distribution)
Computes the 25th, 50th and 75th percentiles of the given distribution and returns these values in an array.
static Object serializeAndRecover(Object o)
Serializes an object to a bytes array and then recovers the object from the bytes array.
static double sumSquareDev(double[] values, double target)
Computes the sum of squared deviations of from
static void updateCounts(double value, long[] counts, double[] quartiles)
Updates observed counts of values in quartiles.
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Method Detail

• #### assertEquals

public static void assertEquals(double expected,
double actual,
double delta)
Verifies that expected and actual are within delta, or are both NaN or infinities of the same sign.
• #### assertEquals

public static void assertEquals(String msg,
double expected,
double actual,
double delta)
Verifies that expected and actual are within delta, or are both NaN or infinities of the same sign.
• #### assertSame

public static void assertSame(double expected,
double actual)
Verifies that the two arguments are exactly the same, either both NaN or infinities of same sign, or identical floating point values.
• #### assertSame

public static void assertSame(Complex expected,
Complex actual)
Verifies that real and imaginary parts of the two complex arguments are exactly the same. Also ensures that NaN / infinite components match.
• #### assertEquals

public static void assertEquals(Complex expected,
Complex actual,
double delta)
Verifies that real and imaginary parts of the two complex arguments differ by at most delta. Also ensures that NaN / infinite components match.
• #### assertEquals

public static void assertEquals(double[] expected,
double[] observed,
double tolerance)
Verifies that two double arrays have equal entries, up to tolerance
• #### serializeAndRecover

public static Object serializeAndRecover(Object o)
Serializes an object to a bytes array and then recovers the object from the bytes array. Returns the deserialized object.
Parameters:
o - object to serialize and recover
Returns:
the recovered, deserialized object
• #### checkSerializedEquality

public static void checkSerializedEquality(Object object)
Verifies that serialization preserves equals and hashCode. Serializes the object, then recovers it and checks equals and hash code.
Parameters:
object - the object to serialize and recover
• #### assertRelativelyEquals

public static void assertRelativelyEquals(double expected,
double actual,
double relativeError)
Verifies that the relative error in actual vs. expected is less than or equal to relativeError. If expected is infinite or NaN, actual must be the same (NaN or infinity of the same sign).
Parameters:
expected - expected value
actual - observed value
relativeError - maximum allowable relative error
• #### assertRelativelyEquals

public static void assertRelativelyEquals(String msg,
double expected,
double actual,
double relativeError)
Verifies that the relative error in actual vs. expected is less than or equal to relativeError. If expected is infinite or NaN, actual must be the same (NaN or infinity of the same sign).
Parameters:
msg - message to return with failure
expected - expected value
actual - observed value
relativeError - maximum allowable relative error
• #### assertContains

public static void assertContains(String msg,
Complex[] values,
Complex z,
double epsilon)
Fails iff values does not contain a number within epsilon of z.
Parameters:
msg - message to return with failure
values - complex array to search
z - value sought
epsilon - tolerance
• #### assertContains

public static void assertContains(Complex[] values,
Complex z,
double epsilon)
Fails iff values does not contain a number within epsilon of z.
Parameters:
values - complex array to search
z - value sought
epsilon - tolerance
• #### assertContains

public static void assertContains(String msg,
double[] values,
double x,
double epsilon)
Fails iff values does not contain a number within epsilon of x.
Parameters:
msg - message to return with failure
values - double array to search
x - value sought
epsilon - tolerance
• #### assertContains

public static void assertContains(double[] values,
double x,
double epsilon)
Fails iff values does not contain a number within epsilon of x.
Parameters:
values - double array to search
x - value sought
epsilon - tolerance
• #### assertEquals

public static void assertEquals(String message,
double[] expected,
RealVector actual,
double delta)
Asserts that all entries of the specified vectors are equal to within a positive delta.
Parameters:
message - the identifying message for the assertion error (can be null)
expected - expected value
actual - actual value
delta - the maximum difference between the entries of the expected and actual vectors for which both entries are still considered equal
• #### assertEquals

public static void assertEquals(String message,
RealVector expected,
RealVector actual,
double delta)
Asserts that all entries of the specified vectors are equal to within a positive delta.
Parameters:
message - the identifying message for the assertion error (can be null)
expected - expected value
actual - actual value
delta - the maximum difference between the entries of the expected and actual vectors for which both entries are still considered equal
• #### assertEquals

public static void assertEquals(String msg,
RealMatrix expected,
RealMatrix observed,
double tolerance)
verifies that two matrices are close (1-norm)
• #### assertEquals

public static void assertEquals(FieldMatrix<? extends FieldElement<?>> expected,
FieldMatrix<? extends FieldElement<?>> observed)
verifies that two matrices are equal
• #### assertEquals

public static void assertEquals(String msg,
double[] expected,
double[] observed,
double tolerance)
verifies that two arrays are close (sup norm)
• #### assertEquals

public static <T extends FieldElement<T>> void assertEquals(T[] m,
T[] n)
verifies that two arrays are equal
• #### sumSquareDev

public static double sumSquareDev(double[] values,
double target)
Computes the sum of squared deviations of from
Parameters:
values - array of deviates
target - value to compute deviations from
Returns:
sum of squared deviations
• #### assertChiSquareAccept

public static void assertChiSquareAccept(String[] valueLabels,
double[] expected,
long[] observed,
double alpha)
Asserts the null hypothesis for a ChiSquare test. Fails and dumps arguments and test statistics if the null hypothesis can be rejected with confidence 100 * (1 - alpha)%
Parameters:
valueLabels - labels for the values of the discrete distribution under test
expected - expected counts
observed - observed counts
alpha - significance level of the test
• #### assertChiSquareAccept

public static void assertChiSquareAccept(int[] values,
double[] expected,
long[] observed,
double alpha)
Asserts the null hypothesis for a ChiSquare test. Fails and dumps arguments and test statistics if the null hypothesis can be rejected with confidence 100 * (1 - alpha)%
Parameters:
values - integer values whose observed and expected counts are being compared
expected - expected counts
observed - observed counts
alpha - significance level of the test
• #### assertChiSquareAccept

public static void assertChiSquareAccept(double[] expected,
long[] observed,
double alpha)
Asserts the null hypothesis for a ChiSquare test. Fails and dumps arguments and test statistics if the null hypothesis can be rejected with confidence 100 * (1 - alpha)%
Parameters:
expected - expected counts
observed - observed counts
alpha - significance level of the test
• #### getDistributionQuartiles

public static double[] getDistributionQuartiles(RealDistribution distribution)
Computes the 25th, 50th and 75th percentiles of the given distribution and returns these values in an array.
• #### updateCounts

public static void updateCounts(double value,
long[] counts,
double[] quartiles)
Updates observed counts of values in quartiles. counts[0] <-> 1st quartile ... counts[3] <-> top quartile
• #### eliminateZeroMassPoints

public static int eliminateZeroMassPoints(int[] densityPoints,
double[] densityValues)
Eliminates points with zero mass from densityPoints and densityValues parallel arrays. Returns the number of positive mass points and collapses the arrays so that the first elements of the input arrays represent the positive mass points.