77
88public class ArraysJoinAndSplitJUnitTest {
99
10- //pizza toppings
1110 private final String [] sauces = {"Marinara" , "Olive Oil" };
1211 private final String [] cheeses = {"Mozzarella" , "Feta" , "Parmesan" };
1312 private final String [] vegetables = {"Olives" , "Spinach" , "Green Peppers" };
1413
1514
1615 @ Test
1716 public void givenThreeStringArrays_whenJoiningIntoOneStringArray_ShouldSucceed () throws Exception {
18- //create the destination array
1917 String [] toppings = new String [sauces .length + cheeses .length + vegetables .length ];
2018
21- //add the sauces
2219 System .arraycopy (sauces , 0 , toppings , 0 , sauces .length );
2320 int AddedSoFarCount = sauces .length ;
2421
25- //add the cheeses
2622 System .arraycopy (cheeses , 0 , toppings , AddedSoFarCount , cheeses .length );
2723 AddedSoFarCount += cheeses .length ;
2824
29- //add the vegetables
3025 System .arraycopy (vegetables , 0 , toppings , AddedSoFarCount , vegetables .length );
3126
32- //check the result
3327 Assert .assertArrayEquals (toppings ,
3428 new String [] {"Marinara" , "Olive Oil" , "Mozzarella" , "Feta" ,
3529 "Parmesan" , "Olives" , "Spinach" , "Green Peppers" } );
@@ -39,14 +33,11 @@ public void givenThreeStringArrays_whenJoiningIntoOneStringArray_ShouldSucceed()
3933 private final String [] customers = {"Jay" , "Harry" , "Ronnie" , "Gary" , "Ross" };
4034 @ Test
4135 public void givenOneStringArray_whenSplittingInHalfBetweenTwoStringArrays_ShouldSucceed () throws Exception {
42- //split the orders in half, rounding up
4336 int ordersHalved = (customers .length / 2 ) + (customers .length % 2 );
4437
45- //divide the orders between two drivers
4638 String [] driverOne = Arrays .copyOf (customers , ordersHalved );
4739 String [] driverTwo = Arrays .copyOfRange (customers , ordersHalved , customers .length );
4840
49- //check ther result
5041 Assert .assertArrayEquals (driverOne , new String [] {"Jay" , "Harry" , "Ronnie" } );
5142 Assert .assertArrayEquals (driverTwo , new String [] {"Gary" , "Ross" } );
5243 }
0 commit comments