1+ package com .thealgorithms .datastructures .stacks ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertFalse ;
5+ import static org .junit .jupiter .api .Assertions .assertThrows ;
6+ import static org .junit .jupiter .api .Assertions .assertTrue ;
7+
8+ import org .junit .jupiter .api .Test ;
9+
10+ class StackUsingLinkedListTest {
11+
12+ @ Test
13+ void testPushAndPop () {
14+ StackUsingLinkedList stack = new StackUsingLinkedList ();
15+ stack .push (10 );
16+ stack .push (20 );
17+ stack .push (30 );
18+
19+ assertEquals (30 , stack .pop ());
20+ assertEquals (20 , stack .pop ());
21+ assertEquals (10 , stack .pop ());
22+ }
23+
24+ @ Test
25+ void testPeek () {
26+ StackUsingLinkedList stack = new StackUsingLinkedList ();
27+ stack .push (100 );
28+ stack .push (200 );
29+
30+ assertEquals (200 , stack .peek ());
31+ assertEquals (200 , stack .peek ());
32+ assertEquals (2 , stack .size ());
33+ }
34+
35+ @ Test
36+ void testIsEmpty () {
37+ StackUsingLinkedList stack = new StackUsingLinkedList ();
38+ assertTrue (stack .isEmpty ());
39+
40+ stack .push (5 );
41+ assertFalse (stack .isEmpty ());
42+
43+ stack .pop ();
44+ assertTrue (stack .isEmpty ());
45+ }
46+
47+ @ Test
48+ void testSize () {
49+ StackUsingLinkedList stack = new StackUsingLinkedList ();
50+ assertEquals (0 , stack .size ());
51+
52+ stack .push (1 );
53+ assertEquals (1 , stack .size ());
54+
55+ stack .push (2 );
56+ stack .push (3 );
57+ assertEquals (3 , stack .size ());
58+
59+ stack .pop ();
60+ assertEquals (2 , stack .size ());
61+ }
62+
63+ @ Test
64+ void testPopEmptyStack () {
65+ StackUsingLinkedList stack = new StackUsingLinkedList ();
66+ assertThrows (RuntimeException .class , stack ::pop );
67+ }
68+
69+ @ Test
70+ void testPeekEmptyStack () {
71+ StackUsingLinkedList stack = new StackUsingLinkedList ();
72+ assertThrows (RuntimeException .class , stack ::peek );
73+ }
74+
75+ @ Test
76+ void testMultipleOperations () {
77+ StackUsingLinkedList stack = new StackUsingLinkedList ();
78+
79+ assertTrue (stack .isEmpty ());
80+
81+ stack .push (1 );
82+ stack .push (2 );
83+ assertEquals (2 , stack .pop ());
84+
85+ stack .push (3 );
86+ stack .push (4 );
87+ assertEquals (4 , stack .peek ());
88+ assertEquals (3 , stack .size ());
89+
90+ assertFalse (stack .isEmpty ());
91+ }
92+
93+ @ Test
94+ void testSingleElement () {
95+ StackUsingLinkedList stack = new StackUsingLinkedList ();
96+ stack .push (42 );
97+
98+ assertEquals (42 , stack .peek ());
99+ assertEquals (1 , stack .size ());
100+ assertEquals (42 , stack .pop ());
101+ assertTrue (stack .isEmpty ());
102+ }
103+ }
0 commit comments