forked from msaimraz/Hacktoberfest_2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackTest.java
89 lines (80 loc) · 2.59 KB
/
StackTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
public class StackTest{
Stack stackTest =new Stack(10);
@Test
public void testIsEmpty(){
Assertions.assertTrue(stackTest.isEmpty());
}
@Test
public void testIsEmptyFalse(){
stackTest.push(10);
Assertions.assertFalse(stackTest.isEmpty());
}
@Test
public void testPush()
{
ByteArrayOutputStream out= new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
stackTest.push(5);
stackTest.peek();
Assertions.assertEquals(5,stackTest.peek());
stackTest.push(4);
Assertions.assertEquals(4,stackTest.peek());
stackTest.push(3);
Assertions.assertEquals(3,stackTest.peek());
stackTest.push(2);
Assertions.assertEquals(2,stackTest.peek());
stackTest.push(1);
Assertions.assertEquals(1,stackTest.peek());
}
@Test
public void testPeek()
{
ByteArrayOutputStream out= new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
stackTest.push(5);
stackTest.push(4);
stackTest.push(3);
stackTest.push(2);
stackTest.push(1);
Assertions.assertEquals(1,stackTest.peek());
stackTest.pop();
Assertions.assertEquals(2,stackTest.peek());
stackTest.pop();
Assertions.assertEquals(3,stackTest.peek());
stackTest.pop();
Assertions.assertEquals(4,stackTest.peek());
}
@Test
public void testPop()
{
stackTest.push(5);
stackTest.push(4);
stackTest.push(3);
stackTest.push(2);
stackTest.push(1);
Assertions.assertEquals(1,stackTest.peek());
stackTest.pop();
Assertions.assertEquals(2,stackTest.peek());
stackTest.pop();
Assertions.assertEquals(3,stackTest.peek());
stackTest.pop();
Assertions.assertEquals(4,stackTest.peek());
}
@Test
public void testDisplay()
{
ByteArrayOutputStream out= new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
stackTest.push(5);
stackTest.push(4);
stackTest.push(3);
stackTest.push(2);
stackTest.push(1);
stackTest.display();
Assertions.assertEquals("1 2 3 4 5".replaceAll(" ",""),out.toString().replaceAll(" ",""));
}
}