Skip to content

Commit

Permalink
Added unit tests (#10)
Browse files Browse the repository at this point in the history
Added unit tests to increase code coverage in ArrayMath
  • Loading branch information
chrisengelsma authored Jul 24, 2017
1 parent 61fdb14 commit 3fb4751
Show file tree
Hide file tree
Showing 10 changed files with 1,421 additions and 44 deletions.
9 changes: 4 additions & 5 deletions core/src/main/java/edu/mines/jtk/util/Almost.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,13 @@ public double divide(double top, double bottom, final double limit) {
bottom = -bottom;
}

if (bottom >= 1.0 // Might underflow, but don't care.
|| top < bottom * 0.1 * Float.MAX_VALUE) { // bottom<1 won't overflow
return sign * top / bottom; // safe
if (bottom >= 1.0 || top < bottom * 0.1 * Float.MAX_VALUE) {
return sign * top / bottom; // safe
} else if (equal(top, bottom)) {
if (zero(top)) { // define 0/0 = limit
if (zero(top)) { // define 0/0 = limit
return limit;
}
return sign; // ratio is within precision of unity
return sign; // ratio is within precision of unity
} else { // clip overflow
return sign * 0.01 * Float.MAX_VALUE;
}
Expand Down
30 changes: 30 additions & 0 deletions core/src/main/java/edu/mines/jtk/util/ArrayMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,7 @@ public static void rand(Random random, long[][][] rx) {
/**
* Returns a new array of random values.
* @param n1 1st array dimension.
* @return an array[n1] of random floats.
*/
public static float[] randfloat(int n1) {
return randfloat(_random,n1);
Expand All @@ -1803,6 +1804,7 @@ public static float[] randfloat(int n1) {
* Returns a new array of random values.
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @return an array[n2][n1] of random floats.
*/
public static float[][] randfloat(int n1, int n2) {
return randfloat(_random,n1,n2);
Expand All @@ -1813,6 +1815,7 @@ public static float[][] randfloat(int n1, int n2) {
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @param n3 3rd array dimension.
* @return an array[n3][n2][n1] of random floats.
*/
public static float[][][] randfloat(int n1, int n2, int n3) {
return randfloat(_random,n1,n2,n3);
Expand All @@ -1822,6 +1825,7 @@ public static float[][][] randfloat(int n1, int n2, int n3) {
* Returns a new array of random values.
* @param random random number generator.
* @param n1 1st array dimension.
* @return an array[n1] of random floats.
*/
public static float[] randfloat(Random random, int n1) {
float[] rx = new float[n1];
Expand All @@ -1834,6 +1838,7 @@ public static float[] randfloat(Random random, int n1) {
* @param random random number generator.
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @return an array[n2][n1] of random floats.
*/
public static float[][] randfloat(Random random, int n1, int n2) {
float[][] rx = new float[n2][n1];
Expand All @@ -1847,6 +1852,7 @@ public static float[][] randfloat(Random random, int n1, int n2) {
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @param n3 3rd array dimension.
* @return an array[n3][n2][n1] of random floats.
*/
public static float[][][] randfloat(Random random, int n1, int n2, int n3) {
float[][][] rx = new float[n3][n2][n1];
Expand Down Expand Up @@ -1914,6 +1920,7 @@ public static void rand(Random random, float[][][] rx) {
/**
* Returns a new array of random values.
* @param n1 1st array dimension.
* @return an array[2*n1] of random complex floats.
*/
public static float[] crandfloat(int n1) {
return crandfloat(_random,n1);
Expand All @@ -1923,6 +1930,7 @@ public static float[] crandfloat(int n1) {
* Returns a new array of random values.
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @return an array[n2][2*n1] of random complex floats.
*/
public static float[][] crandfloat(int n1, int n2) {
return crandfloat(_random,n1,n2);
Expand All @@ -1933,6 +1941,7 @@ public static float[][] crandfloat(int n1, int n2) {
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @param n3 3rd array dimension.
* @return an array[n3][n2][2*n1] of random complex floats.
*/
public static float[][][] crandfloat(int n1, int n2, int n3) {
return crandfloat(_random,n1,n2,n3);
Expand All @@ -1942,6 +1951,7 @@ public static float[][][] crandfloat(int n1, int n2, int n3) {
* Returns a new array of random values.
* @param random random number generator.
* @param n1 1st array dimension.
* @return an array[2*n1] of random complex floats.
*/
public static float[] crandfloat(Random random, int n1) {
float[] cx = new float[2*n1];
Expand All @@ -1954,6 +1964,7 @@ public static float[] crandfloat(Random random, int n1) {
* @param random random number generator.
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @return an array[n2][2*n1] of random complex floats.
*/
public static float[][] crandfloat(Random random, int n1, int n2) {
float[][] cx = new float[n2][2*n1];
Expand All @@ -1967,6 +1978,7 @@ public static float[][] crandfloat(Random random, int n1, int n2) {
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @param n3 3rd array dimension.
* @return an array[n3][n2][2*n1] of random complex floats.
*/
public static float[][][] crandfloat(Random random, int n1, int n2, int n3) {
float[][][] cx = new float[n3][n2][2*n1];
Expand Down Expand Up @@ -2028,6 +2040,7 @@ public static void crand(Random random, float[][][] cx) {
/**
* Returns a new array of random values.
* @param n1 1st array dimension.
* @return an array[n1] of random doubles.
*/
public static double[] randdouble(int n1) {
return randdouble(_random,n1);
Expand All @@ -2037,6 +2050,7 @@ public static double[] randdouble(int n1) {
* Returns a new array of random values.
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @return an array[n2][n1] of random doubles.
*/
public static double[][] randdouble(int n1, int n2) {
return randdouble(_random,n1,n2);
Expand All @@ -2047,6 +2061,7 @@ public static double[][] randdouble(int n1, int n2) {
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @param n3 3rd array dimension.
* @return an array[n3][n2][n1] of random doubles.
*/
public static double[][][] randdouble(int n1, int n2, int n3) {
return randdouble(_random,n1,n2,n3);
Expand All @@ -2056,6 +2071,7 @@ public static double[][][] randdouble(int n1, int n2, int n3) {
* Returns a new array of random values.
* @param random random number generator.
* @param n1 1st array dimension.
* @return an array[n1] of random doubles.
*/
public static double[] randdouble(Random random, int n1) {
double[] rx = new double[n1];
Expand All @@ -2068,6 +2084,7 @@ public static double[] randdouble(Random random, int n1) {
* @param random random number generator.
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @return an array[n2][n1] of random doubles.
*/
public static double[][] randdouble(Random random, int n1, int n2) {
double[][] rx = new double[n2][n1];
Expand All @@ -2081,6 +2098,7 @@ public static double[][] randdouble(Random random, int n1, int n2) {
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @param n3 3rd array dimension.
* @return an array[n3][n2][n1] of random doubles.
*/
public static double[][][] randdouble(Random random, int n1, int n2, int n3) {
double[][][] rx = new double[n3][n2][n1];
Expand Down Expand Up @@ -2148,6 +2166,7 @@ public static void rand(Random random, double[][][] rx) {
/**
* Returns a new array of random values.
* @param n1 1st array dimension.
* @return an array[2*n1] of random doubles.
*/
public static double[] cranddouble(int n1) {
return cranddouble(_random,n1);
Expand All @@ -2157,6 +2176,7 @@ public static double[] cranddouble(int n1) {
* Returns a new array of random values.
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @return an array[n2][2*n1] of random doubles.
*/
public static double[][] cranddouble(int n1, int n2) {
return cranddouble(_random,n1,n2);
Expand All @@ -2167,6 +2187,7 @@ public static double[][] cranddouble(int n1, int n2) {
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @param n3 3rd array dimension.
* @return an array[n3][n2][2*n1] of random doubles.
*/
public static double[][][] cranddouble(int n1, int n2, int n3) {
return cranddouble(_random,n1,n2,n3);
Expand All @@ -2176,6 +2197,7 @@ public static double[][][] cranddouble(int n1, int n2, int n3) {
* Returns a new array of random values.
* @param random random number generator.
* @param n1 1st array dimension.
* @return an array[2*n1] of random doubles.
*/
public static double[] cranddouble(Random random, int n1) {
double[] cx = new double[2*n1];
Expand All @@ -2188,6 +2210,7 @@ public static double[] cranddouble(Random random, int n1) {
* @param random random number generator.
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @return an array[n2][2*n1] of random doubles.
*/
public static double[][] cranddouble(Random random, int n1, int n2) {
double[][] cx = new double[n2][2*n1];
Expand All @@ -2201,6 +2224,7 @@ public static double[][] cranddouble(Random random, int n1, int n2) {
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @param n3 3rd array dimension.
* @return an array[n3][n2][2*n1] of random doubles.
*/
public static double[][][] cranddouble(Random random, int n1, int n2, int n3) {
double[][][] cx = new double[n3][n2][2*n1];
Expand Down Expand Up @@ -2267,6 +2291,7 @@ public static void crand(Random random, double[][][] cx) {
* Returns an array initialized to a specified value.
* @param ra the value.
* @param n1 1st array dimension.
* @return an array[n1] of bytes.
*/
public static byte[] fillbyte(byte ra, int n1) {
byte[] rx = new byte[n1];
Expand All @@ -2279,6 +2304,7 @@ public static byte[] fillbyte(byte ra, int n1) {
* @param ra the value.
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @return an array[n2][n1] of bytes.
*/
public static byte[][] fillbyte(byte ra, int n1, int n2) {
byte[][] rx = new byte[n2][n1];
Expand All @@ -2292,6 +2318,7 @@ public static byte[][] fillbyte(byte ra, int n1, int n2) {
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @param n3 3rd array dimension.
* @return an array[n3][n2][n1] of bytes.
*/
public static byte[][][] fillbyte(byte ra, int n1, int n2, int n3) {
byte[][][] rx = new byte[n3][n2][n1];
Expand Down Expand Up @@ -2336,6 +2363,7 @@ public static void fill(byte ra, byte[][][] rx) {
* Returns an array initialized to a specified value.
* @param ra the value.
* @param n1 1st array dimension.
* @return an array[n1] of shorts.
*/
public static short[] fillshort(short ra, int n1) {
short[] rx = new short[n1];
Expand All @@ -2348,6 +2376,7 @@ public static short[] fillshort(short ra, int n1) {
* @param ra the value.
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @return an array[n2][n1] of shorts.
*/
public static short[][] fillshort(short ra, int n1, int n2) {
short[][] rx = new short[n2][n1];
Expand All @@ -2361,6 +2390,7 @@ public static short[][] fillshort(short ra, int n1, int n2) {
* @param n1 1st array dimension.
* @param n2 2nd array dimension.
* @param n3 3rd array dimension.
* @return an array[n3][n2][n1] of shorts.
*/
public static short[][][] fillshort(short ra, int n1, int n2, int n3) {
short[][][] rx = new short[n3][n2][n1];
Expand Down
Loading

0 comments on commit 3fb4751

Please sign in to comment.