forked from nattee/data2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_queue.cpp
96 lines (85 loc) · 1.98 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
94
95
96
#include <iostream>
#include <queue>
#include "queue.h"
#include <assert.h>
#include <exception>
//typedef CP::queue<int> Queue;
//
typedef CP::queue<int> Queue;
bool test1() {
Queue q;
assert(q.size() == 0);
q.push(1);
q.push(2);
q.push(3);
q.push(3);
assert(q.front() == 1 && q.size() == 4); q.pop();
assert(q.front() == 2 && q.size() == 3); q.pop();
assert(q.front() == 3 && q.size() == 2); q.pop();
assert(q.front() == 3 && q.size() == 1); q.pop();
assert(q.size() == 0);
return true;
}
bool test2() {
Queue q;
q.push(1);
q.push(1);
q.push(1);
q.pop();
q.pop();
q.pop();
Queue s2 = q;
try {
int x = q.front();
std::cout << "x = " << x << std::endl;
q.pop();
} catch (std::exception &e) {
std::cout << "Caught an exception " << e.what() << std::endl;
return true;
}
return false;
}
bool test3() {
int nRun = 20;
int nData = 1000000;
for (int i = 0;i < nRun;i++) {
Queue q;
for (int j = 0;j < nData;j++) {
q.push(j);
}
for (int j = 0;j < nData;j++) {
assert(q.front() == j);
q.pop();
}
}
return true;
}
bool test4() {
Queue q;
q.push(1);
q.push(2);
q.push(3);
assert(q.back() == 3);
q.pop();
q.push(4);
assert(q.back() == 4);
assert(q.front() == 2);
q.pop();
assert(q.front() == 3);
q.pop();
assert(q.back() == 4);
assert(q.front() == 4);
q.pop();
if (q.size() == 0) {
return true;
} else {
return false;
}
}
int main() {
if (test1()) std::cout << "---------------------------------------- Test1 OK!" << std::endl;
if (test2()) std::cout << "---------------------------------------- Test2 OK!" << std::endl;
if (test3()) std::cout << "---------------------------------------- Test3 OK!" << std::endl;
if (test4()) std::cout << "---------------------------------------- Test4 OK!" << std::endl;
return 0;
}