-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
105 lines (93 loc) · 1.1 KB
/
stack.cpp
File metadata and controls
105 lines (93 loc) · 1.1 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "Stack.h"
#include "test.h"
#include <iostream>
using namespace std;
class StackTest : public Test
{
enum {SIZE = 5};
Stack<int> stk;
public:
StackTest() : stk(SIZE)
{}
void run()
{
testUnderflow();
testPopulate();
testOverflow();
testPop();
testBadSize();
}
void testBadSize()
{
try
{
Stack<int> s(0);
fail_("Bad Size");
}
catch (StackError&)
{
succeed_();
}
}
void testUnderflow()
{
test_(stk.size() == 0);
try
{
stk.top();
fail_("Underflow");
}
catch (StackError&)
{
succeed_();
}
try
{
stk.pop();
fail_("Underflow");
} catch (StackError&)
{
succeed_();
}
}
void testPopulate()
{
try
{
for (int i = 0; i < SIZE; ++i)
stk.push(i);
succeed_();
}
catch (StackError&)
{
fail_("Populate");
}
test_(stk.size() == SIZE);
test_(stk.top() == SIZE-1);
}
void testOverflow()
{
try
{
stk.push(SIZE);
fail_("Overflow");
}
catch (StackError&)
{
succeed_();
}
}
void testPop()
{
for (int i = 0; i < SIZE; ++i)
test_(stk.pop() == SIZE-i-1);
test_(stk.size() == 0);
}
};
int main()
{
StackTest t;
t.setStream(&cout);
t.run();
t.report();
}