BAEL-3775: Clean up unit tests for Stack (#8837)

This commit is contained in:
kwoyke 2020-03-11 06:43:46 +01:00 committed by GitHub
parent 80632f53f7
commit 07bec4d5b2

View File

@ -1,123 +1,153 @@
package com.baeldung.stack; package com.baeldung.stack;
import static org.hamcrest.CoreMatchers.equalTo; import org.junit.Test;
import static org.junit.Assert.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.Stack; import java.util.Stack;
import java.util.stream.Collectors;
import org.junit.Test; import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class StackUnitTest { public class StackUnitTest {
@Test @Test
public void whenStackIsCreated_thenItHasSize0() { public void whenStackIsCreated_thenItHasSizeZero() {
Stack intStack = new Stack(); Stack<Integer> intStack = new Stack<>();
assertEquals(0, intStack.size()); assertEquals(0, intStack.size());
} }
@Test @Test
public void givenEmptyStack_whenElementIsPushed_thenStackSizeisIncreased() { public void whenElementIsPushed_thenStackSizeIsIncreased() {
Stack intStack = new Stack(); Stack<Integer> intStack = new Stack<>();
intStack.push(1); intStack.push(1);
assertEquals(1, intStack.size()); assertEquals(1, intStack.size());
} }
@Test @Test
public void givenEmptyStack_whenMultipleElementsArePushed_thenStackSizeisIncreased() { public void whenMultipleElementsArePushed_thenStackSizeIsIncreased() {
Stack intStack = new Stack(); Stack<Integer> intStack = new Stack<>();
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
boolean result = intStack.addAll(intList); boolean result = intStack.addAll(intList);
assertTrue(result); assertTrue(result);
assertEquals(7, intList.size()); assertEquals(7, intList.size());
} }
@Test @Test
public void whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() { public void whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() {
Stack<Integer> intStack = new Stack(); Stack<Integer> intStack = new Stack<>();
intStack.push(5); intStack.push(5);
intStack.pop();
Integer element = intStack.pop();
assertEquals(Integer.valueOf(5), element);
assertTrue(intStack.isEmpty()); assertTrue(intStack.isEmpty());
} }
@Test @Test
public void whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() { public void whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
Stack<Integer> intStack = new Stack(); Stack<Integer> intStack = new Stack<>();
intStack.push(5); intStack.push(5);
intStack.peek();
Integer element = intStack.peek();
assertEquals(Integer.valueOf(5), element);
assertEquals(1, intStack.search(5)); assertEquals(1, intStack.search(5));
assertEquals(1, intStack.size()); assertEquals(1, intStack.size());
} }
@Test @Test
public void whenElementIsOnStack_thenSearchReturnsItsDistanceFromTheTop() { public void whenElementIsOnStack_thenSearchReturnsItsDistanceFromTheTop() {
Stack<Integer> intStack = new Stack(); Stack<Integer> intStack = new Stack<>();
intStack.push(5); intStack.push(5);
assertEquals(1, intStack.search(5)); intStack.push(8);
assertEquals(2, intStack.search(5));
} }
@Test @Test
public void whenElementIsOnStack_thenIndexOfReturnsItsIndex() { public void whenElementIsOnStack_thenIndexOfReturnsItsIndex() {
Stack<Integer> intStack = new Stack(); Stack<Integer> intStack = new Stack<>();
intStack.push(5); intStack.push(5);
int indexOf = intStack.indexOf(5); int indexOf = intStack.indexOf(5);
assertEquals(0, indexOf); assertEquals(0, indexOf);
} }
@Test @Test
public void whenMultipleElementsAreOnStack_thenIndexOfReturnsLastElementIndex() { public void whenMultipleElementsAreOnStack_thenIndexOfReturnsLastElementIndex() {
Stack<Integer> intStack = new Stack(); Stack<Integer> intStack = new Stack<>();
intStack.push(5); intStack.push(5);
intStack.push(5); intStack.push(5);
intStack.push(5); intStack.push(5);
int lastIndexOf = intStack.lastIndexOf(5); int lastIndexOf = intStack.lastIndexOf(5);
assertEquals(2, lastIndexOf); assertEquals(2, lastIndexOf);
} }
@Test @Test
public void givenElementOnStack_whenRemoveElementIsInvoked_thenElementIsRemoved() { public void whenRemoveElementIsInvoked_thenElementIsRemoved() {
Stack<Integer> intStack = new Stack(); Stack<Integer> intStack = new Stack<>();
intStack.push(5); intStack.push(5);
intStack.push(5); intStack.push(5);
intStack.removeElement(5); intStack.removeElement(5);
assertEquals(1, intStack.size()); assertEquals(1, intStack.size());
} }
@Test @Test
public void givenElementOnStack_whenRemoveElementAtIsInvoked_thenElementIsRemoved() { public void whenRemoveElementAtIsInvoked_thenElementIsRemoved() {
Stack<Integer> intStack = new Stack(); Stack<Integer> intStack = new Stack<>();
intStack.push(5); intStack.push(5);
intStack.push(7); intStack.push(7);
intStack.removeElementAt(1); intStack.removeElementAt(1);
assertEquals(-1, intStack.search(7)); assertEquals(-1, intStack.search(7));
} }
@Test @Test
public void givenElementsOnStack_whenRemoveAllElementsIsInvoked_thenAllElementsAreRemoved() { public void whenRemoveAllElementsIsInvoked_thenAllElementsAreRemoved() {
Stack<Integer> intStack = new Stack(); Stack<Integer> intStack = new Stack<>();
intStack.push(5); intStack.push(5);
intStack.push(7); intStack.push(7);
intStack.removeAllElements(); intStack.removeAllElements();
assertTrue(intStack.isEmpty()); assertTrue(intStack.isEmpty());
} }
@Test @Test
public void givenElementsOnStack_whenRemoveAllIsInvoked_thenAllElementsFromCollectionAreRemoved() { public void givenElementsOnStack_whenRemoveAllIsInvoked_thenAllElementsFromCollectionAreRemoved() {
Stack<Integer> intStack = new Stack(); Stack<Integer> intStack = new Stack<>();
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
intStack.addAll(intList); intStack.addAll(intList);
intStack.add(500); intStack.add(500);
intStack.removeAll(intList); intStack.removeAll(intList);
assertEquals(1, intStack.size()); assertEquals(1, intStack.size());
assertEquals(1, intStack.search(500));
} }
@Test @Test
public void givenElementsOnStack_whenRemoveIfIsInvoked_thenAllElementsSatysfyingConditionAreRemoved() { public void whenRemoveIfIsInvoked_thenAllElementsSatysfyingConditionAreRemoved() {
Stack<Integer> intStack = new Stack(); Stack<Integer> intStack = new Stack<>();
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
intStack.addAll(intList); intStack.addAll(intList);
intStack.removeIf(element -> element < 6); intStack.removeIf(element -> element < 6);
assertEquals(2, intStack.size()); assertEquals(2, intStack.size());
} }
@ -126,12 +156,28 @@ public class StackUnitTest {
Stack<Integer> intStack = new Stack<>(); Stack<Integer> intStack = new Stack<>();
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
intStack.addAll(intList); intStack.addAll(intList);
ListIterator<Integer> it = intStack.listIterator(); ListIterator<Integer> it = intStack.listIterator();
Stack<Integer> result = new Stack();
Stack<Integer> result = new Stack<>();
while(it.hasNext()) { while(it.hasNext()) {
result.push(it.next()); result.push(it.next());
} }
assertThat(result, equalTo(intStack)); assertThat(result, equalTo(intStack));
} }
@Test
public void whenStackIsFiltered_allElementsNotSatisfyingFilterConditionAreDiscarded() {
Stack<Integer> intStack = new Stack<>();
List<Integer> inputIntList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 9, 10);
intStack.addAll(inputIntList);
List<Integer> filtered = intStack
.stream()
.filter(element -> element <= 3)
.collect(Collectors.toList());
assertEquals(3, filtered.size());
}
} }