Skip to content

Commit

Permalink
BAEL-4600 | Add examples to demonstrate the difference between Array … (
Browse files Browse the repository at this point in the history
eugenp#10211)

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

* BAEL-4600 | Refactor | Remove ArrayCreator and ArrayListCreator
  • Loading branch information
vishal1023 authored Nov 20, 2020
1 parent f441017 commit bd3f54e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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]; });
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit bd3f54e

Please sign in to comment.