-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
126 lines (104 loc) · 2.55 KB
/
Copy pathtest.cpp
File metadata and controls
126 lines (104 loc) · 2.55 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <stdexcept>
#include "QUnit.hpp"
#include "BoundedQueue.h"
int testSimpleEnqueueDequeue();
int testEnqueueDequeueAll();
int testEnqueueDequeueRepeated();
int testEnqueueAllButOne_ThenDequeue_ThenEnqueueAllButOne();
int testEnqueueWithReinit();
int testTooManyElementsException();
int testTooFewElementsException();
int main(int argc, char** argv) {
int errors = 0;
errors += testSimpleEnqueueDequeue();
errors += testEnqueueDequeueAll();
errors += testEnqueueDequeueRepeated();
errors += testEnqueueAllButOne_ThenDequeue_ThenEnqueueAllButOne();
errors += testEnqueueWithReinit();
errors += testTooManyElementsException();
errors += testTooFewElementsException();
return errors;
}
int testSimpleEnqueueDequeue() {
QUnit::UnitTest qunit(std::cerr, QUnit::normal);
BoundedQueue q(1);
q.enqueue(0);
QUNIT_IS_EQUAL(0, q.dequeue());
return qunit.errors();
}
int testEnqueueDequeueAll() {
QUnit::UnitTest qunit(std::cerr, QUnit::normal);
int size = 5;
BoundedQueue q(size);
for(int i = 0; i < size; i++) {
q.enqueue(i);
}
for(int i = 0; i < size; i++) {
QUNIT_IS_EQUAL(i, q.dequeue());
}
return qunit.errors();
}
int testEnqueueDequeueRepeated() {
QUnit::UnitTest qunit(std::cerr, QUnit::normal);
int numRepetitions = 5;
BoundedQueue q(2);
for(int i = 0; i < numRepetitions; i++) {
q.enqueue(i);
int result = q.dequeue();
QUNIT_IS_EQUAL(i, result);
}
return qunit.errors();
}
int testEnqueueAllButOne_ThenDequeue_ThenEnqueueAllButOne() {
QUnit::UnitTest qunit(std::cerr, QUnit::normal);
int size = 5;
BoundedQueue q(size);
for(int i = 0; i < size - 1; i++) {
q.enqueue(i);
}
for(int i = 0; i < size - 1; i++) {
q.dequeue();
}
for(int i = 0; i < size - 1; i++) {
q.enqueue(i);
}
for(int i = 0; i < size - 1; i++ ) {
QUNIT_IS_EQUAL(i, q.dequeue());
}
return qunit.errors();
}
int testEnqueueWithReinit() {
QUnit::UnitTest qunit(std::cerr, QUnit::normal);
BoundedQueue q(5);
q.enqueue(0);
q.reinit();
q.enqueue(1);
QUNIT_IS_EQUAL(1, q.dequeue());
return qunit.errors();
}
int testTooManyElementsException() {
QUnit::UnitTest qunit(std::cerr, QUnit::normal);
BoundedQueue q(1);
try {
q.enqueue(0);
q.enqueue(0);
QUNIT_IS_TRUE(false);
}
catch(const std::runtime_error& ex) {
QUNIT_IS_TRUE(true);
}
return qunit.errors();
}
int testTooFewElementsException() {
QUnit::UnitTest qunit(std::cerr, QUnit::normal);
BoundedQueue q(1);
try {
q.dequeue();
QUNIT_IS_TRUE(false);
}
catch(const std::runtime_error& ex) {
QUNIT_IS_TRUE(true);
}
return qunit.errors();
}