662ef5d6c52edd1e1b30514b9098ab0042738fc7
[debian/openrocket] / core / test / net / sf / openrocket / util / ArrayUtilsTest.java
1 package net.sf.openrocket.util;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNull;
5
6 import org.junit.Test;
7
8 public class ArrayUtilsTest {
9
10         @Test(expected=NullPointerException.class)
11         public void testCopyOfRange_NullArg() {
12                 ArrayUtils.copyOfRange( (Byte[]) null, 0 , 14);
13         }
14
15         @Test(expected=ArrayIndexOutOfBoundsException.class)
16         public void testCopyOfRange_StartTooBig() {
17                 Integer[] original = new Integer[5];
18                 ArrayUtils.copyOfRange( original, 8 , 14);
19         }
20
21         @Test(expected=ArrayIndexOutOfBoundsException.class)
22         public void testCopyOfRange_StartTooSmall() {
23                 Integer[] original = new Integer[5];
24                 ArrayUtils.copyOfRange( original, -1 , 14);
25         }
26
27         @Test(expected=IllegalArgumentException.class)
28         public void testCopyOfRange_IllegalRange() {
29                 Integer[] original = new Integer[5];
30                 ArrayUtils.copyOfRange( original, 5, 0 );
31         }
32
33         @Test
34         public void testCopyOfRange() {
35                 Integer[] original = new Integer[5];
36                 for ( int i =0; i < 5; i++ ) {
37                         original[i] = i;
38                 }
39                 Integer[] copy = ArrayUtils.copyOfRange( original, 0, 0 );
40                 assertEquals( 0, copy.length );
41
42                 copy = ArrayUtils.copyOfRange( original, 2, 2 );
43                 assertEquals( 0, copy.length );
44
45                 copy = ArrayUtils.copyOfRange( original, 0, 2 );
46                 assertEquals( 2, copy.length );
47                 for( int i =0; i< 2; i++ ) {
48                         assertEquals( original[i], copy[i] );
49                 }
50
51                 copy = ArrayUtils.copyOfRange( original, 2, 5 );
52                 assertEquals( 3, copy.length );
53                 for( int i =0; i< 3; i++ ) {
54                         assertEquals( original[i+2], copy[i] );
55                 }
56
57                 copy = ArrayUtils.copyOfRange( original, 2, 15 );
58                 assertEquals( 13, copy.length );
59                 for( int i =0; i< 3; i++ ) {
60                         assertEquals( original[i+2], copy[i] );
61                 }
62                 for ( int i=3; i< 13; i++ ) {
63                         assertNull(copy[i]);
64                 }
65
66         }
67
68         @Test
69         public void testCopyOfRange_ZeroSize() {
70                 Integer[] original = new Integer[0];
71
72                 Integer[] copy = ArrayUtils.copyOfRange( original, 0, 0 );
73                 assertEquals( 0, copy.length );
74
75                 copy = ArrayUtils.copyOfRange( original, 0, 2 );
76                 assertEquals( 2, copy.length );
77                 for( int i =0; i< 2; i++ ) {
78                         assertEquals( null, copy[i] );
79                 }
80
81         }
82
83         @Test
84         public void testRange1() {
85                 double[] ary = ArrayUtils.range(0.0, 0.5, 1.0);
86                 assertEquals(1, ary.length);
87                 assertEquals( 0.0, ary[0], 0.0 );
88         }
89
90         @Test
91         public void testRange2() {
92                 double[] ary = ArrayUtils.range(0.0, 1.0, 0.5);
93                 assertEquals(2, ary.length);
94                 assertEquals( 0.0, ary[0], 0.0 );
95                 assertEquals( 0.5, ary[1], 0.0 );
96         }
97         
98         @Test
99         public void testRange3() {
100                 double [] ary = ArrayUtils.range(0.0, 1.0, 0.4 );
101                 assertEquals(3, ary.length);
102                 assertEquals( 0.0, ary[0], 0.0 );
103                 assertEquals( 0.4, ary[1], 0.0 );
104                 assertEquals( 0.8, ary[2], 0.0 );
105         }
106
107         @Test
108         public void testRange4() {
109                 double[] ary = ArrayUtils.range(0.0,10.0, 0.5);
110                 assertEquals( 20, ary.length );
111                 int i =0;
112                 for( double d = 0.0; d < 10.0; d+=0.5){
113                         assertEquals(d,ary[i++],0.0);
114                 }
115                 assertEquals( i, ary.length );
116         }
117         
118 }