BAEL-2770 Fixed code formatting and improved test cases
This commit is contained in:
parent
923019269c
commit
16553936c7
|
@ -8,7 +8,7 @@ public class AddElementToEndOfArray {
|
||||||
public Integer[] addElementUsingArraysCopyOf(Integer[] srcArray, int elementToAdd) {
|
public Integer[] addElementUsingArraysCopyOf(Integer[] srcArray, int elementToAdd) {
|
||||||
Integer[] destArray = Arrays.copyOf(srcArray, srcArray.length + 1);
|
Integer[] destArray = Arrays.copyOf(srcArray, srcArray.length + 1);
|
||||||
|
|
||||||
destArray[destArray.length-1] = elementToAdd;
|
destArray[destArray.length - 1] = elementToAdd;
|
||||||
return destArray;
|
return destArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ public class AddElementToEndOfArray {
|
||||||
|
|
||||||
System.arraycopy(srcArray, 0, destArray, 0, srcArray.length);
|
System.arraycopy(srcArray, 0, destArray, 0, srcArray.length);
|
||||||
|
|
||||||
destArray[destArray.length-1] = elementToAdd;
|
destArray[destArray.length - 1] = elementToAdd;
|
||||||
|
|
||||||
return destArray;
|
return destArray;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,44 +3,47 @@ package com.baeldung.array;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
|
||||||
public class AddElementToEndOfArrayUnitTest {
|
public class AddElementToEndOfArrayUnitTest {
|
||||||
|
|
||||||
AddElementToEndOfArray addElementToEndOfArray;
|
AddElementToEndOfArray addElementToEndOfArray;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init(){
|
public void init() {
|
||||||
addElementToEndOfArray = new AddElementToEndOfArray();
|
addElementToEndOfArray = new AddElementToEndOfArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSourceArrayAndElement_whenAddElementUsingArraysCopyIsInvoked_thenNewElementMustBeAdded(){
|
public void givenSourceArrayAndElement_whenAddElementUsingArraysCopyIsInvoked_thenNewElementMustBeAdded() {
|
||||||
Integer[] sourceArray = {1,2,3,4};
|
Integer[] sourceArray = {1, 2, 3, 4};
|
||||||
int elementToAdd = 5;
|
int elementToAdd = 5;
|
||||||
|
|
||||||
Integer[] destArray = addElementToEndOfArray.addElementUsingArraysCopyOf(sourceArray, elementToAdd);
|
Integer[] destArray = addElementToEndOfArray.addElementUsingArraysCopyOf(sourceArray, elementToAdd);
|
||||||
|
|
||||||
assertEquals(elementToAdd, destArray[destArray.length-1].intValue());
|
Integer[] expectedArray = {1, 2, 3, 4, 5};
|
||||||
|
assertArrayEquals(expectedArray, destArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSourceArrayAndElement_whenAddElementUsingArrayListIsInvoked_thenNewElementMustBeAdded(){
|
public void givenSourceArrayAndElement_whenAddElementUsingArrayListIsInvoked_thenNewElementMustBeAdded() {
|
||||||
Integer[] sourceArray = {1,2,3,4};
|
Integer[] sourceArray = {1, 2, 3, 4};
|
||||||
int elementToAdd = 5;
|
int elementToAdd = 5;
|
||||||
|
|
||||||
Integer[] destArray = addElementToEndOfArray.addElementUsingArrayList(sourceArray, elementToAdd);
|
Integer[] destArray = addElementToEndOfArray.addElementUsingArrayList(sourceArray, elementToAdd);
|
||||||
|
|
||||||
assertEquals(elementToAdd, destArray[destArray.length-1].intValue());
|
Integer[] expectedArray = {1, 2, 3, 4, 5};
|
||||||
|
assertArrayEquals(expectedArray, destArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSourceArrayAndElement_whenAddElementUsingSystemArrayCopyIsInvoked_thenNewElementMustBeAdded(){
|
public void givenSourceArrayAndElement_whenAddElementUsingSystemArrayCopyIsInvoked_thenNewElementMustBeAdded() {
|
||||||
Integer[] sourceArray = {1,2,3,4};
|
Integer[] sourceArray = {1, 2, 3, 4};
|
||||||
int elementToAdd = 5;
|
int elementToAdd = 5;
|
||||||
|
|
||||||
Integer[] destArray = addElementToEndOfArray.addElementUsingSystemArrayCopy(sourceArray, elementToAdd);
|
Integer[] destArray = addElementToEndOfArray.addElementUsingSystemArrayCopy(sourceArray, elementToAdd);
|
||||||
|
|
||||||
assertEquals(elementToAdd, destArray[destArray.length-1].intValue());
|
Integer[] expectedArray = {1, 2, 3, 4, 5};
|
||||||
|
assertArrayEquals(expectedArray, destArray);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue