22
33import org .apache .commons .lang3 .ArrayUtils ;
44import org .junit .Test ;
5- import static org . junit . Assert . assertEquals ;
5+
66import static org .junit .Assert .assertArrayEquals ;
7+ import static org .junit .Assert .assertEquals ;
78
89public class ArrayUtilsUnitTest {
910 @ Test
1011 public void givenArray_whenAddingElementAtSpecifiedPosition_thenCorrect () {
11- int [] oldArray = { 2 , 3 , 4 , 5 };
12+ int [] oldArray = {2 , 3 , 4 , 5 };
1213 int [] newArray = ArrayUtils .add (oldArray , 0 , 1 );
13- int [] expectedArray = { 1 , 2 , 3 , 4 , 5 };
14+ int [] expectedArray = {1 , 2 , 3 , 4 , 5 };
1415 assertArrayEquals (expectedArray , newArray );
1516 }
1617
1718 @ Test
1819 public void givenArray_whenAddingElementAtTheEnd_thenCorrect () {
19- int [] oldArray = { 2 , 3 , 4 , 5 };
20+ int [] oldArray = {2 , 3 , 4 , 5 };
2021 int [] newArray = ArrayUtils .add (oldArray , 1 );
21- int [] expectedArray = { 2 , 3 , 4 , 5 , 1 };
22+ int [] expectedArray = {2 , 3 , 4 , 5 , 1 };
2223 assertArrayEquals (expectedArray , newArray );
2324 }
2425
2526 @ Test
2627 public void givenArray_whenAddingAllElementsAtTheEnd_thenCorrect () {
27- int [] oldArray = { 0 , 1 , 2 };
28+ int [] oldArray = {0 , 1 , 2 };
2829 int [] newArray = ArrayUtils .addAll (oldArray , 3 , 4 , 5 );
29- int [] expectedArray = { 0 , 1 , 2 , 3 , 4 , 5 };
30+ int [] expectedArray = {0 , 1 , 2 , 3 , 4 , 5 };
3031 assertArrayEquals (expectedArray , newArray );
3132 }
3233
3334 @ Test
3435 public void givenArray_whenRemovingElementAtSpecifiedPosition_thenCorrect () {
35- int [] oldArray = { 1 , 2 , 3 , 4 , 5 };
36+ int [] oldArray = {1 , 2 , 3 , 4 , 5 };
3637 int [] newArray = ArrayUtils .remove (oldArray , 1 );
37- int [] expectedArray = { 1 , 3 , 4 , 5 };
38+ int [] expectedArray = {1 , 3 , 4 , 5 };
3839 assertArrayEquals (expectedArray , newArray );
3940 }
4041
4142 @ Test
4243 public void givenArray_whenRemovingAllElementsAtSpecifiedPositions_thenCorrect () {
43- int [] oldArray = { 1 , 2 , 3 , 4 , 5 };
44+ int [] oldArray = {1 , 2 , 3 , 4 , 5 };
4445 int [] newArray = ArrayUtils .removeAll (oldArray , 1 , 3 );
45- int [] expectedArray = { 1 , 3 , 5 };
46+ int [] expectedArray = {1 , 3 , 5 };
4647 assertArrayEquals (expectedArray , newArray );
4748 }
4849
4950 @ Test
5051 public void givenArray_whenRemovingAnElement_thenCorrect () {
51- int [] oldArray = { 1 , 2 , 3 , 3 , 4 };
52+ int [] oldArray = {1 , 2 , 3 , 3 , 4 };
5253 int [] newArray = ArrayUtils .removeElement (oldArray , 3 );
53- int [] expectedArray = { 1 , 2 , 3 , 4 };
54+ int [] expectedArray = {1 , 2 , 3 , 4 };
5455 assertArrayEquals (expectedArray , newArray );
5556 }
5657
5758 @ Test
5859 public void givenArray_whenRemovingElements_thenCorrect () {
59- int [] oldArray = { 1 , 2 , 3 , 3 , 4 };
60+ int [] oldArray = {1 , 2 , 3 , 3 , 4 };
6061 int [] newArray = ArrayUtils .removeElements (oldArray , 2 , 3 , 5 );
61- int [] expectedArray = { 1 , 3 , 4 };
62+ int [] expectedArray = {1 , 3 , 4 };
6263 assertArrayEquals (expectedArray , newArray );
6364 }
6465
6566 @ Test
6667 public void givenArray_whenRemovingAllElementOccurences_thenCorrect () {
67- int [] oldArray = { 1 , 2 , 2 , 2 , 3 };
68+ int [] oldArray = {1 , 2 , 2 , 2 , 3 };
6869 int [] newArray = ArrayUtils .removeAllOccurences (oldArray , 2 );
69- int [] expectedArray = { 1 , 3 };
70+ int [] expectedArray = {1 , 3 };
7071 assertArrayEquals (expectedArray , newArray );
7172 }
7273
7374 @ Test
7475 public void givenArray_whenCheckingExistingElement_thenCorrect () {
75- int [] array = { 1 , 3 , 5 , 7 , 9 };
76+ int [] array = {1 , 3 , 5 , 7 , 9 };
7677 boolean evenContained = ArrayUtils .contains (array , 2 );
7778 boolean oddContained = ArrayUtils .contains (array , 7 );
7879 assertEquals (false , evenContained );
@@ -81,57 +82,57 @@ public void givenArray_whenCheckingExistingElement_thenCorrect() {
8182
8283 @ Test
8384 public void givenArray_whenReversingElementsWithinARange_thenCorrect () {
84- int [] originalArray = { 1 , 2 , 3 , 4 , 5 };
85+ int [] originalArray = {1 , 2 , 3 , 4 , 5 };
8586 ArrayUtils .reverse (originalArray , 1 , 4 );
86- int [] expectedArray = { 1 , 4 , 3 , 2 , 5 };
87+ int [] expectedArray = {1 , 4 , 3 , 2 , 5 };
8788 assertArrayEquals (expectedArray , originalArray );
8889 }
8990
9091 @ Test
9192 public void givenArray_whenReversingAllElements_thenCorrect () {
92- int [] originalArray = { 1 , 2 , 3 , 4 , 5 };
93+ int [] originalArray = {1 , 2 , 3 , 4 , 5 };
9394 ArrayUtils .reverse (originalArray );
94- int [] expectedArray = { 5 , 4 , 3 , 2 , 1 };
95+ int [] expectedArray = {5 , 4 , 3 , 2 , 1 };
9596 assertArrayEquals (expectedArray , originalArray );
9697 }
9798
9899 @ Test
99100 public void givenArray_whenShiftingElementsWithinARange_thenCorrect () {
100- int [] originalArray = { 1 , 2 , 3 , 4 , 5 };
101+ int [] originalArray = {1 , 2 , 3 , 4 , 5 };
101102 ArrayUtils .shift (originalArray , 1 , 4 , 1 );
102- int [] expectedArray = { 1 , 4 , 2 , 3 , 5 };
103+ int [] expectedArray = {1 , 4 , 2 , 3 , 5 };
103104 assertArrayEquals (expectedArray , originalArray );
104105 }
105106
106107 @ Test
107108 public void givenArray_whenShiftingAllElements_thenCorrect () {
108- int [] originalArray = { 1 , 2 , 3 , 4 , 5 };
109+ int [] originalArray = {1 , 2 , 3 , 4 , 5 };
109110 ArrayUtils .shift (originalArray , 1 );
110- int [] expectedArray = { 5 , 1 , 2 , 3 , 4 };
111+ int [] expectedArray = {5 , 1 , 2 , 3 , 4 };
111112 assertArrayEquals (expectedArray , originalArray );
112113 }
113114
114115 @ Test
115116 public void givenArray_whenExtractingElements_thenCorrect () {
116- int [] oldArray = { 1 , 2 , 3 , 4 , 5 };
117+ int [] oldArray = {1 , 2 , 3 , 4 , 5 };
117118 int [] newArray = ArrayUtils .subarray (oldArray , 2 , 7 );
118- int [] expectedArray = { 3 , 4 , 5 };
119+ int [] expectedArray = {3 , 4 , 5 };
119120 assertArrayEquals (expectedArray , newArray );
120121 }
121122
122123 @ Test
123124 public void givenArray_whenSwapingElementsWithinARange_thenCorrect () {
124- int [] originalArray = { 1 , 2 , 3 , 4 , 5 };
125+ int [] originalArray = {1 , 2 , 3 , 4 , 5 };
125126 ArrayUtils .swap (originalArray , 0 , 3 , 2 );
126- int [] expectedArray = { 4 , 5 , 3 , 1 , 2 };
127+ int [] expectedArray = {4 , 5 , 3 , 1 , 2 };
127128 assertArrayEquals (expectedArray , originalArray );
128129 }
129130
130131 @ Test
131132 public void givenArray_whenSwapingElementsAtSpecifiedPositions_thenCorrect () {
132- int [] originalArray = { 1 , 2 , 3 , 4 , 5 };
133+ int [] originalArray = {1 , 2 , 3 , 4 , 5 };
133134 ArrayUtils .swap (originalArray , 0 , 3 );
134- int [] expectedArray = { 4 , 2 , 3 , 1 , 5 };
135+ int [] expectedArray = {4 , 2 , 3 , 1 , 5 };
135136 assertArrayEquals (expectedArray , originalArray );
136137 }
137138}
0 commit comments