-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
test_queue.cpp
93 lines (77 loc) · 2.18 KB
/
test_queue.cpp
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
#include <cassert> /// for assert
#include <iostream> /// for std::cout
#include "./queue.hpp"
template <typename T>
void testConstructedQueueIsEmpty() {
const queue<T> curQueue;
assert(curQueue.isEmptyQueue());
}
void testEnQueue() {
queue<int> curQueue;
curQueue.enQueue(10);
assert(curQueue.toVector() == std::vector<int>({10}));
curQueue.enQueue(20);
assert(curQueue.toVector() == std::vector<int>({10, 20}));
curQueue.enQueue(30);
curQueue.enQueue(40);
assert(curQueue.toVector() == std::vector<int>({10, 20, 30, 40}));
}
void testDeQueue() {
queue<int> curQueue;
curQueue.enQueue(10);
curQueue.enQueue(20);
curQueue.enQueue(30);
curQueue.deQueue();
assert(curQueue.toVector() == std::vector<int>({20, 30}));
curQueue.deQueue();
assert(curQueue.toVector() == std::vector<int>({30}));
curQueue.deQueue();
assert(curQueue.isEmptyQueue());
}
void testFront() {
queue<int> curQueue;
curQueue.enQueue(10);
assert(curQueue.front() == 10);
curQueue.enQueue(20);
assert(curQueue.front() == 10);
}
void testQueueAfterClearIsEmpty() {
queue<int> curQueue;
curQueue.enQueue(10);
curQueue.enQueue(20);
curQueue.enQueue(30);
curQueue.clear();
assert(curQueue.isEmptyQueue());
}
void testFrontThrowsAnInvalidArgumentWhenQueueEmpty() {
const queue<int> curQueue;
bool wasException = false;
try {
curQueue.front();
} catch (const std::invalid_argument&) {
wasException = true;
}
assert(wasException);
}
void testDeQueueThrowsAnInvalidArgumentWhenQueueEmpty() {
queue<int> curQueue;
bool wasException = false;
try {
curQueue.deQueue();
} catch (const std::invalid_argument&) {
wasException = true;
}
assert(wasException);
}
int main() {
testConstructedQueueIsEmpty<int>();
testConstructedQueueIsEmpty<double>();
testConstructedQueueIsEmpty<std::vector<long double>>();
testEnQueue();
testDeQueue();
testQueueAfterClearIsEmpty();
testFrontThrowsAnInvalidArgumentWhenQueueEmpty();
testDeQueueThrowsAnInvalidArgumentWhenQueueEmpty();
std::cout << "All tests pass!\n";
return 0;
}