Skip to content

Commit 6a8ecab

Browse files
Wosinpivovarit
authored andcommitted
BAEL-1310: Code for Quick Guide to Stack. (eugenp#2993)
1 parent 182a752 commit 6a8ecab

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.baeldung.stack;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.junit.Assert.*;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.ListIterator;
9+
import java.util.Stack;
10+
11+
import org.junit.Test;
12+
public class StackUnitTest {
13+
14+
@Test
15+
public void whenStackIsCreated_thenItHasSize0() {
16+
Stack intStack = new Stack();
17+
assertEquals(0, intStack.size());
18+
}
19+
20+
@Test
21+
public void givenEmptyStack_whenElementIsPushed_thenStackSizeisIncreased() {
22+
Stack intStack = new Stack();
23+
intStack.push(1);
24+
assertEquals(1, intStack.size());
25+
}
26+
27+
@Test
28+
public void givenEmptyStack_whenMultipleElementsArePushed_thenStackSizeisIncreased() {
29+
Stack intStack = new Stack();
30+
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
31+
boolean result = intStack.addAll(intList);
32+
assertTrue(result);
33+
assertEquals(7, intList.size());
34+
}
35+
36+
@Test
37+
public void whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() {
38+
Stack<Integer> intStack = new Stack();
39+
intStack.push(5);
40+
intStack.pop();
41+
assertTrue(intStack.isEmpty());
42+
}
43+
44+
@Test
45+
public void whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
46+
Stack<Integer> intStack = new Stack();
47+
intStack.push(5);
48+
intStack.peek();
49+
assertEquals(1, intStack.search(5));
50+
assertEquals(1, intStack.size());
51+
}
52+
53+
@Test
54+
public void whenElementIsOnStack_thenSearchReturnsItsDistanceFromTheTop() {
55+
Stack<Integer> intStack = new Stack();
56+
intStack.push(5);
57+
assertEquals(1, intStack.search(5));
58+
}
59+
60+
@Test
61+
public void whenElementIsOnStack_thenIndexOfReturnsItsIndex() {
62+
Stack<Integer> intStack = new Stack();
63+
intStack.push(5);
64+
int indexOf = intStack.indexOf(5);
65+
assertEquals(0, indexOf);
66+
}
67+
68+
@Test
69+
public void whenMultipleElementsAreOnStack_thenIndexOfReturnsLastElementIndex() {
70+
Stack<Integer> intStack = new Stack();
71+
intStack.push(5);
72+
intStack.push(5);
73+
intStack.push(5);
74+
int lastIndexOf = intStack.lastIndexOf(5);
75+
assertEquals(2, lastIndexOf);
76+
}
77+
78+
@Test
79+
public void givenElementOnStack_whenRemoveElementIsInvoked_thenElementIsRemoved() {
80+
Stack<Integer> intStack = new Stack();
81+
intStack.push(5);
82+
intStack.push(5);
83+
intStack.removeElement(5);
84+
assertEquals(1, intStack.size());
85+
}
86+
87+
@Test
88+
public void givenElementOnStack_whenRemoveElementAtIsInvoked_thenElementIsRemoved() {
89+
Stack<Integer> intStack = new Stack();
90+
intStack.push(5);
91+
intStack.push(7);
92+
intStack.removeElementAt(1);
93+
assertEquals(-1, intStack.search(7));
94+
}
95+
96+
@Test
97+
public void givenElementsOnStack_whenRemoveAllElementsIsInvoked_thenAllElementsAreRemoved() {
98+
Stack<Integer> intStack = new Stack();
99+
intStack.push(5);
100+
intStack.push(7);
101+
intStack.removeAllElements();
102+
assertTrue(intStack.isEmpty());
103+
}
104+
105+
@Test
106+
public void givenElementsOnStack_whenRemoveAllIsInvoked_thenAllElementsFromCollectionAreRemoved() {
107+
Stack<Integer> intStack = new Stack();
108+
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
109+
intStack.addAll(intList);
110+
intStack.add(500);
111+
intStack.removeAll(intList);
112+
assertEquals(1, intStack.size());
113+
}
114+
115+
@Test
116+
public void givenElementsOnStack_whenRemoveIfIsInvoked_thenAllElementsSatysfyingConditionAreRemoved() {
117+
Stack<Integer> intStack = new Stack();
118+
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
119+
intStack.addAll(intList);
120+
intStack.removeIf(element -> element < 6);
121+
assertEquals(2, intStack.size());
122+
}
123+
124+
@Test
125+
public void whenAnotherStackCreatedWhileTraversingStack_thenStacksAreEqual() {
126+
Stack<Integer> intStack = new Stack<>();
127+
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
128+
intStack.addAll(intList);
129+
ListIterator<Integer> it = intStack.listIterator();
130+
Stack<Integer> result = new Stack();
131+
while(it.hasNext()) {
132+
result.push(it.next());
133+
}
134+
135+
assertThat(result, equalTo(intStack));
136+
}
137+
}

0 commit comments

Comments
 (0)