1+ package com .baeldung .collections .dequestack ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import java .util .ArrayDeque ;
6+ import java .util .Deque ;
7+ import java .util .Iterator ;
8+ import java .util .Stack ;
9+
10+ import static org .assertj .core .api .Assertions .assertThat ;
11+
12+ class StackVsDequeUnitTest {
13+
14+ @ Test
15+ void givenAStack_whenAccessByIndex_thenElementCanBeRead () {
16+ Stack <String > myStack = new Stack <>();
17+ myStack .push ("I am the 1st element." ); //index 0
18+ myStack .push ("I am the 2nd element." ); //index 1
19+ myStack .push ("I am the 3rd element." ); //index 2
20+ //access by index
21+ assertThat (myStack .get (0 )).isEqualTo ("I am the 1st element." );
22+ }
23+
24+ @ Test
25+ void givenAStack_whenIterate_thenFromBottomToTop () {
26+ Stack <String > myStack = new Stack <>();
27+ myStack .push ("I am at the bottom." );
28+ myStack .push ("I am in the middle." );
29+ myStack .push ("I am at the top." );
30+
31+ Iterator <String > it = myStack .iterator ();
32+
33+ assertThat (it ).toIterable ().containsExactly (
34+ "I am at the bottom." ,
35+ "I am in the middle." ,
36+ "I am at the top." );
37+ }
38+
39+ @ Test
40+ void givenAStack_whenAddOrRemoveByIndex_thenElementCanBeAddedOrRemoved () {
41+ Stack <String > myStack = new Stack <>();
42+ myStack .push ("I am the 1st element." );
43+ myStack .push ("I am the 3rd element." );
44+
45+ assertThat (myStack .size ()).isEqualTo (2 );
46+
47+ //insert by index
48+ myStack .add (1 , "I am the 2nd element." );
49+ assertThat (myStack .size ()).isEqualTo (3 );
50+ assertThat (myStack .get (1 )).isEqualTo ("I am the 2nd element." );
51+ //remove by index
52+ myStack .remove (1 );
53+ assertThat (myStack .size ()).isEqualTo (2 );
54+ }
55+
56+ @ Test
57+ void givenADeque_whenAddOrRemoveLastElement_thenTheLastElementCanBeAddedOrRemoved () {
58+ Deque <String > myStack = new ArrayDeque <>();
59+ myStack .push ("I am the 1st element." );
60+ myStack .push ("I am the 2nd element." );
61+ myStack .push ("I am the 3rd element." );
62+
63+ assertThat (myStack .size ()).isEqualTo (3 );
64+
65+ //insert element to the bottom of the stack
66+ myStack .addLast ("I am the NEW element." );
67+ assertThat (myStack .size ()).isEqualTo (4 );
68+ assertThat (myStack .peek ()).isEqualTo ("I am the 3rd element." );
69+
70+ //remove element from the bottom of the stack
71+ String removedStr = myStack .removeLast ();
72+ assertThat (myStack .size ()).isEqualTo (3 );
73+ assertThat (removedStr ).isEqualTo ("I am the NEW element." );
74+ }
75+
76+ @ Test
77+ void givenADeque_whenIterate_thenFromTopToBottom () {
78+ Deque <String > myStack = new ArrayDeque <>();
79+ myStack .push ("I am at the bottom." );
80+ myStack .push ("I am in the middle." );
81+ myStack .push ("I am at the top." );
82+
83+ Iterator <String > it = myStack .iterator ();
84+
85+ assertThat (it ).toIterable ().containsExactly (
86+ "I am at the top." ,
87+ "I am in the middle." ,
88+ "I am at the bottom." );
89+ }
90+ }
0 commit comments