Added unit tests and fixed rounding problem in ArrayUtils.range().
This commit is contained in:
parent
7c5524f1fc
commit
301067e301
core
@ -10,7 +10,12 @@ public class ArrayUtils {
|
|||||||
*/
|
*/
|
||||||
public static double[] range(double start, double stop, double step){
|
public static double[] range(double start, double stop, double step){
|
||||||
|
|
||||||
int size = (int) Math.floor(((stop - start) / step));
|
int size = 0 ;
|
||||||
|
if ( stop <= start ) {
|
||||||
|
size = 0;
|
||||||
|
} else {
|
||||||
|
size = (int) Math.ceil(((stop - start) / step));
|
||||||
|
}
|
||||||
|
|
||||||
//System.out.println("Range from "+start+" to "+stop+" step "+step+" has length "+size);
|
//System.out.println("Range from "+start+" to "+stop+" step "+step+" has length "+size);
|
||||||
|
|
||||||
|
@ -11,25 +11,25 @@ public class ArrayUtilsTest {
|
|||||||
public void testCopyOfRange_NullArg() {
|
public void testCopyOfRange_NullArg() {
|
||||||
ArrayUtils.copyOfRange( (Byte[]) null, 0 , 14);
|
ArrayUtils.copyOfRange( (Byte[]) null, 0 , 14);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=ArrayIndexOutOfBoundsException.class)
|
@Test(expected=ArrayIndexOutOfBoundsException.class)
|
||||||
public void testCopyOfRange_StartTooBig() {
|
public void testCopyOfRange_StartTooBig() {
|
||||||
Integer[] original = new Integer[5];
|
Integer[] original = new Integer[5];
|
||||||
ArrayUtils.copyOfRange( original, 8 , 14);
|
ArrayUtils.copyOfRange( original, 8 , 14);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=ArrayIndexOutOfBoundsException.class)
|
@Test(expected=ArrayIndexOutOfBoundsException.class)
|
||||||
public void testCopyOfRange_StartTooSmall() {
|
public void testCopyOfRange_StartTooSmall() {
|
||||||
Integer[] original = new Integer[5];
|
Integer[] original = new Integer[5];
|
||||||
ArrayUtils.copyOfRange( original, -1 , 14);
|
ArrayUtils.copyOfRange( original, -1 , 14);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=IllegalArgumentException.class)
|
@Test(expected=IllegalArgumentException.class)
|
||||||
public void testCopyOfRange_IllegalRange() {
|
public void testCopyOfRange_IllegalRange() {
|
||||||
Integer[] original = new Integer[5];
|
Integer[] original = new Integer[5];
|
||||||
ArrayUtils.copyOfRange( original, 5, 0 );
|
ArrayUtils.copyOfRange( original, 5, 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCopyOfRange() {
|
public void testCopyOfRange() {
|
||||||
Integer[] original = new Integer[5];
|
Integer[] original = new Integer[5];
|
||||||
@ -38,10 +38,10 @@ public class ArrayUtilsTest {
|
|||||||
}
|
}
|
||||||
Integer[] copy = ArrayUtils.copyOfRange( original, 0, 0 );
|
Integer[] copy = ArrayUtils.copyOfRange( original, 0, 0 );
|
||||||
assertEquals( 0, copy.length );
|
assertEquals( 0, copy.length );
|
||||||
|
|
||||||
copy = ArrayUtils.copyOfRange( original, 2, 2 );
|
copy = ArrayUtils.copyOfRange( original, 2, 2 );
|
||||||
assertEquals( 0, copy.length );
|
assertEquals( 0, copy.length );
|
||||||
|
|
||||||
copy = ArrayUtils.copyOfRange( original, 0, 2 );
|
copy = ArrayUtils.copyOfRange( original, 0, 2 );
|
||||||
assertEquals( 2, copy.length );
|
assertEquals( 2, copy.length );
|
||||||
for( int i =0; i< 2; i++ ) {
|
for( int i =0; i< 2; i++ ) {
|
||||||
@ -71,7 +71,7 @@ public class ArrayUtilsTest {
|
|||||||
|
|
||||||
Integer[] copy = ArrayUtils.copyOfRange( original, 0, 0 );
|
Integer[] copy = ArrayUtils.copyOfRange( original, 0, 0 );
|
||||||
assertEquals( 0, copy.length );
|
assertEquals( 0, copy.length );
|
||||||
|
|
||||||
copy = ArrayUtils.copyOfRange( original, 0, 2 );
|
copy = ArrayUtils.copyOfRange( original, 0, 2 );
|
||||||
assertEquals( 2, copy.length );
|
assertEquals( 2, copy.length );
|
||||||
for( int i =0; i< 2; i++ ) {
|
for( int i =0; i< 2; i++ ) {
|
||||||
@ -79,6 +79,40 @@ public class ArrayUtilsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRange1() {
|
||||||
|
double[] ary = ArrayUtils.range(0.0, 0.5, 1.0);
|
||||||
|
assertEquals(1, ary.length);
|
||||||
|
assertEquals( 0.0, ary[0], 0.0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRange2() {
|
||||||
|
double[] ary = ArrayUtils.range(0.0, 1.0, 0.5);
|
||||||
|
assertEquals(2, ary.length);
|
||||||
|
assertEquals( 0.0, ary[0], 0.0 );
|
||||||
|
assertEquals( 0.5, ary[1], 0.0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRange3() {
|
||||||
|
double [] ary = ArrayUtils.range(0.0, 1.0, 0.4 );
|
||||||
|
assertEquals(3, ary.length);
|
||||||
|
assertEquals( 0.0, ary[0], 0.0 );
|
||||||
|
assertEquals( 0.4, ary[1], 0.0 );
|
||||||
|
assertEquals( 0.8, ary[2], 0.0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRange4() {
|
||||||
|
double[] ary = ArrayUtils.range(0.0,10.0, 0.5);
|
||||||
|
assertEquals( 20, ary.length );
|
||||||
|
int i =0;
|
||||||
|
for( double d = 0.0; d < 10.0; d+=0.5){
|
||||||
|
assertEquals(d,ary[i++],0.0);
|
||||||
|
}
|
||||||
|
assertEquals( i, ary.length );
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user