-
Notifications
You must be signed in to change notification settings - Fork 1
/
lab_stack_test.h
80 lines (63 loc) · 1.7 KB
/
lab_stack_test.h
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
#include <cxxtest/TestSuite.h>
#include "lab_stack.h"
#include <vector>
#include <memory>
using namespace std;
class StackTest : public CxxTest::TestSuite
{
private:
shared_ptr<Stack> s;
vector<int> testvalues;
public:
void setUp()
{
testvalues = {1,2,3,4,5};
s = make_shared<Stack>( testvalues.size() );
}
void tearDown()
{
s.reset();
}
/** test that stack reports the correct number of things on the stack **/
void test_size()
{
for( int i=0; i<testvalues.size(); ++i )
{
TS_ASSERT_EQUALS( i, s->num_items() );
s->push( testvalues[i] );
}
for( int i=testvalues.size(); i>0; --i )
{
TS_ASSERT_EQUALS( s->num_items(), i );
s->pop();
}
TS_ASSERT_EQUALS( s->num_items(), 0 );
}
/** test that stack raises Empty exception when popping an empty stack **/
void test_empty()
{
TS_ASSERT_THROWS( s->pop(), Stack::Empty );
s->push( testvalues[0] );
s->pop();
TS_ASSERT_THROWS_ANYTHING( s->pop() );
}
/** test that the stack raises Full exception when push on a full stack **/
void test_full()
{
for( int i=0; i<testvalues.size(); ++i )
{
s->push( testvalues[i] );
}
TS_ASSERT_THROWS( s->push(42), Stack::Full );
}
/** test that the stack reports the correct values on the top of the stack **/
void test_top()
{
TS_ASSERT_THROWS( s->top(), Stack::Empty );
for( int i=0; i<testvalues.size(); ++i )
{
s->push( testvalues[i] );
TS_ASSERT_EQUALS( s->top(), testvalues[i] );
}
}
};