BAEL-4600 | Add examples to demonstrate the difference between Array … (#10211)

* BAEL-4600 | Add examples to demonstrate the difference between Array Size vs ArrayList Capacity

* BAEL-4600 | Refactor | Remove ArrayCreator and ArrayListCreator
This commit is contained in:
Vishal Akkalkote 2020-11-20 05:56:15 +05:30 committed by GitHub
parent cd7c84e734
commit 683215b2fe
2 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package com.baeldung.listcapacityvsarraysize;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class ArrayCreatorUnitTest {
@Test
void whenSizeOfAnArrayIsNonZero_thenReturnNewArrayOfGivenSize() {
Integer[] array = new Integer[10];
assertEquals(10, array.length);
}
@Test
void whenSizeOfAnArrayIsLessThanZero_thenThrowException() {
assertThrows(NegativeArraySizeException.class, () -> { Integer[] array = new Integer[-1]; });
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.listcapacityvsarraysize;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
class ArrayListCreatorUnitTest {
@Test
void givenValidCapacityOfList_whenCreateListInvoked_thenCreateNewArrayListWithGivenCapacity() {
ArrayList<Integer> list = new ArrayList<>(100);
assertNotNull(list);
}
@Test
void givenInvalidCapacityOfList_whenCreateListInvoked_thenThrowException() {
assertThrows(IllegalArgumentException.class, () -> new ArrayList<>(-1));
}
@Test
void givenValidCapacityOfList_whenCreateListInvoked_thenCreateNewArrayListWithSizeZero() {
assertEquals(0, new ArrayList<Integer>(100).size());
}
}