1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.util.EmptyStackException;
25
26 import org.junit.jupiter.api.Test;
27
28
29
30
31 @SuppressWarnings("deprecation")
32 public class ArrayStackTest<E> extends AbstractArrayListTest<E> {
33
34 public ArrayStackTest() {
35 super(ArrayStackTest.class.getSimpleName());
36 }
37
38 @Override
39 public String getCompatibilityVersion() {
40 return "4";
41 }
42
43 @Override
44 public ArrayStack<E> makeObject() {
45 return new ArrayStack<>();
46 }
47
48 @Test
49 public void testNewStack() {
50 final ArrayStack<E> stack = makeObject();
51 assertTrue(stack.empty(), "New stack is empty");
52 assertEquals(0, stack.size(), "New stack has size zero");
53
54 assertThrows(EmptyStackException.class, () -> stack.peek());
55
56 assertThrows(EmptyStackException.class, () -> stack.pop());
57 }
58
59 @Test
60 @SuppressWarnings("unchecked")
61 public void testPushPeekPop() {
62 final ArrayStack<E> stack = makeObject();
63
64 stack.push((E) "First Item");
65 assertFalse(stack.empty(), "Stack is not empty");
66 assertEquals(1, stack.size(), "Stack size is one");
67 assertEquals("First Item", (String) stack.peek(),
68 "Top item is 'First Item'");
69 assertEquals(1, stack.size(), "Stack size is one");
70
71 stack.push((E) "Second Item");
72 assertEquals(2, stack.size(), "Stack size is two");
73 assertEquals("Second Item", (String) stack.peek(),
74 "Top item is 'Second Item'");
75 assertEquals(2, stack.size(), "Stack size is two");
76
77 assertEquals("Second Item", (String) stack.pop(),
78 "Popped item is 'Second Item'");
79 assertEquals("First Item", (String) stack.peek(),
80 "Top item is 'First Item'");
81 assertEquals(1, stack.size(), "Stack size is one");
82
83 assertEquals("First Item", (String) stack.pop(),
84 "Popped item is 'First Item'");
85 assertEquals(0, stack.size(), "Stack size is zero");
86 }
87
88 @Test
89 @Override
90 @SuppressWarnings("unchecked")
91 public void testSearch() {
92 final ArrayStack<E> stack = makeObject();
93
94 stack.push((E) "First Item");
95 stack.push((E) "Second Item");
96 assertEquals(1, stack.search("Second Item"),
97 "Top item is 'Second Item'");
98 assertEquals(2, stack.search("First Item"),
99 "Next Item is 'First Item'");
100 assertEquals(-1, stack.search("Missing Item"),
101 "Cannot find 'Missing Item'");
102 }
103
104
105
106
107
108
109
110
111 }