-
Notifications
You must be signed in to change notification settings - Fork 0
/
adam_test_part_c.cpp
447 lines (391 loc) · 14.1 KB
/
adam_test_part_c.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#include "base_event.h"
#include "closed_event.h"
#include "custom_event.h"
#include "date_wrap.h"
#include "event_container.h"
#include "exceptions.h"
#include "festival.h"
#include "one_time_event.h"
#include "open_event.h"
#include "recurring_event.h"
#include "schedule.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
using mtm::Schedule;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using mtm::Festival;
using mtm::RecurringEvent;
using mtm::OneTimeEvent;
using mtm::DateWrap;
using mtm::BaseEvent;
using mtm::OpenEvent;
using mtm::ClosedEvent;
using mtm::CustomEvent;
static const std::string FILE_PATH = "testOutputs/partC";
#define ASSERT_TEST(expr, backUpbuf) \
do { \
cout.rdbuf(backUpbuf);\
if (!(expr)) { \
cout << "\nAssertion failed at line"<< __LINE__ << " " << __FILE__ << #expr << endl; \
result = false; \
} \
} while (0);
#define RUN_TEST(test, name) \
do { \
cout << "+ Running" << (name) << "..."; \
if (test()) { \
cout << "[OK]\n" << endl; \
} else { \
cout << "[Failed]\n\n <span>To see what the test does and why it failed, please check the link at the top of the page to the test file</span>" << endl; \
} \
} while (0);
#define ASSERT(expr, backUpbuf ) ASSERT_TEST(expr, backUpbuf)
#define REDIRECT_OUTPUT(fileName, PATH)\
std::string fileName = PATH;\
std::ofstream out(fileName, std::ios_base::trunc);\
std::streambuf* stream_buffer_cout = cout.rdbuf();\
std::cout.rdbuf(out.rdbuf());
bool matchFiles(const std::string& out, const std::string& exp) {
ifstream output(out);
if (!output) {
cout << "can't open file" << endl;
}
ifstream expOutput(exp);
if (!expOutput) {
cout << "can't open file" << endl;
}
while (!output.eof()) {
char c;
output >> std::noskipws >> c;
char ex;
expOutput >> std::noskipws >> ex;
if (ex != c) {
return false;
}
}
return true;
}
struct Filter2 {
bool operator()(int student) {
return student >= 2 && student <= 8;
}
};
BaseEvent* generate(int i) {
switch (i) {
case 0:
return new OpenEvent(DateWrap(1, 1, 2000), "an open event");
case 1:
return new ClosedEvent(DateWrap(1, 1, 2000), "a closed event");
case 2:
return new CustomEvent<Filter2>(DateWrap(1, 1, 2000), "a custom event", Filter2());
default:
return nullptr;
}
}
struct pred{
OpenEvent op = OpenEvent(DateWrap(1,1,2000), "an open event");
ClosedEvent cp = ClosedEvent(DateWrap(1,1,2000), "a closed event");
CustomEvent<Filter2> cup = CustomEvent<Filter2>(DateWrap(1,1,2000), "a custom event", Filter2());
public:
bool operator()(const BaseEvent& b) const{
return b == op || cp == b || cup == b;
}
};
void test1(const Schedule &schedule) {
schedule.printAllEvents(); }
void test2(const Schedule& schedule) {
schedule.printEventDetails(mtm::DateWrap(27, 12, 2020), "Publish Test");
}
void test3(const Schedule &schedule) {
schedule.printEventDetails(mtm::DateWrap(5, 1, 2021), "Update Q&A");
}
void test4(const mtm::Schedule& schedule) { schedule.printMonthEvents(12, 2020); }
class MutatingPredicate {
int counter = 0;
public:
bool operator()(const mtm::BaseEvent& event) {
++counter;
return true;
}
};
void test5(const mtm::Schedule& schedule) {
schedule.printSomeEvents(MutatingPredicate(), true);
}
bool testYan1()
{
bool result = true;
REDIRECT_OUTPUT(fileName, FILE_PATH + std::string("/your_outputs/testYan1.txt"))
mtm::Schedule schedule;
//notice events should be merged lexicographically
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(1, 1, 2020), "YanB"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(1, 1, 2020), "YanR"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(1, 1, 2020), "YanZ"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(1, 1, 2020), "YanA"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(1, 1, 2020), "YanC"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(1, 1, 2020), "YanX"));
schedule.printAllEvents();
out.close();
ASSERT(matchFiles(fileName, FILE_PATH + std::string("/expected/testYan1.txt")), stream_buffer_cout)
return result;
}
bool testYan2()
{
bool result = true;
REDIRECT_OUTPUT(fileName, FILE_PATH + std::string("/your_outputs/testYan2.txt"))
mtm::Schedule schedule;
//notice events should be merged by date
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(20, 1, 2022), "YanZ"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(30, 1, 2020), "YanR"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(1, 1, 2020), "YanZ"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(2, 2, 2020), "YanA"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(1, 2, 2020), "YanC"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(1, 2, 2020), "YanB"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(30, 1, 2020), "YanM"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(1, 1, 2020), "YanX"));
try{
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(1, 1, 2020), "YanX"));
}catch(mtm::EventAlreadyExists&){
}
try{
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(1, 2, 2020), "YanC"));
}catch(mtm::EventAlreadyExists&){
}
schedule.printAllEvents();
out.close();
ASSERT(matchFiles(fileName, FILE_PATH + std::string("/expected/testYan2.txt")),stream_buffer_cout)
return result;
}
class StudentOddFilter {
public:
bool operator()(int student) {
for(int i = 0; i < 100; i++){
if(student == i*2){
return true;
}
}
return false;
}
};
class Predicate {
public:
bool operator()(mtm::BaseEvent& event) {
if(event.getName() == "YanA") return false;
return true;
}
};
bool testYan3()
{
bool result = true;
REDIRECT_OUTPUT(fileName, FILE_PATH + std::string("/your_outputs/testYan3.txt"))
mtm::Festival festival(mtm::DateWrap(21, 10, 2020));
mtm::OpenEvent open(mtm::DateWrap(21, 10, 2020), "YanA");
open.registerParticipant(20000);
festival.add(open);
mtm::ClosedEvent closed(mtm::DateWrap(21, 10, 2020), "YanC");
for(int i = 10; i < 20; i++){
closed.addInvitee(i);
}
for(int i = 1; i < 10; i++){
closed.addInvitee(i);
}
for(int i = 10; i < 20; i++){
closed.registerParticipant(i);
}
for(int i = 1; i < 10; i++){
closed.registerParticipant(i);
}
festival.add(closed);
mtm::CustomEvent<StudentOddFilter> custom(mtm::DateWrap(21, 10, 2020),
"YanB", StudentOddFilter());
for(int i = 10; i < 20; i++){
custom.registerParticipant(2*i);
}
for(int i = 1; i < 10; i++){
custom.registerParticipant(2*i);
}
festival.add(custom);
mtm::Schedule schedule;
schedule.addEvents(festival);
schedule.registerToEvent(mtm::DateWrap(21, 10, 2020),"YanB",198);
schedule.printSomeEvents(Predicate(), true);
out.close();
ASSERT_TEST(matchFiles(fileName, FILE_PATH + std::string("/expected/testYan3.txt")), stream_buffer_cout)
return result;
}
bool testYan4()
{
bool result = true;
REDIRECT_OUTPUT(fileName, FILE_PATH + std::string("/your_outputs/testYan4.txt"))
mtm::Schedule schedule;
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(1, 12, 2020), "YanZ"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(27, 12, 2020), "YanW"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(22, 12, 2020), "YanD"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(18, 12, 2020), "YanY"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(14, 12, 2020), "YanE"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(29, 12, 2020), "YanA"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(2, 12, 2020), "YanM"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(30, 12, 2020), "YanS"));
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(25, 12, 2020), "YanR"));
for(int i = 60 ; i<65; i++){
schedule.registerToEvent(mtm::DateWrap(1, 12, 2020), "YanZ", i);
}
for(int i = 20 ; i<25; i++){
schedule.registerToEvent(mtm::DateWrap(1, 12, 2020), "YanZ", i);
}
for(int i = 5 ; i<10; i++){
schedule.registerToEvent(mtm::DateWrap(14, 12, 2020), "YanE", i);
}
schedule.printEventDetails(mtm::DateWrap(14, 12, 2020), "YanE");
schedule.printEventDetails(mtm::DateWrap(1, 12, 2020), "YanZ");
schedule.printEventDetails(mtm::DateWrap(25, 12, 2020), "YanR");
out.close();
ASSERT_TEST(matchFiles(fileName, FILE_PATH + std::string("/expected/testYan4.txt")),stream_buffer_cout)
return result;
}
bool allSegelTests(){
bool result = true;
REDIRECT_OUTPUT(fileName, FILE_PATH + std::string("/your_outputs/allSegelTests.txt"))
mtm::Schedule schedule;
schedule.addEvents(mtm::OneTimeEvent<mtm::OpenEvent>(
mtm::DateWrap(27, 12, 2020), "Publish Test"));
mtm::RecurringEvent<mtm::ClosedEvent> closed(mtm::DateWrap(20, 12, 2020),
"Update Q&A", 6, 5);
for (mtm::BaseEvent &event : closed) {
mtm::ClosedEvent &closed_event = dynamic_cast<mtm::ClosedEvent &>(event);
closed_event.addInvitee(1337);
closed_event.addInvitee(850);
closed_event.addInvitee(1500);
}
schedule.addEvents(closed);
schedule.registerToEvent(mtm::DateWrap(20, 12, 2020), "Update Q&A", 850);
schedule.registerToEvent(mtm::DateWrap(20, 12, 2020), "Update Q&A", 1500);
schedule.registerToEvent(mtm::DateWrap(5, 1, 2021), "Update Q&A", 850);
schedule.registerToEvent(mtm::DateWrap(5, 1, 2021), "Update Q&A", 1500);
schedule.unregisterFromEvent(mtm::DateWrap(20, 12, 2020), "Update Q&A",
1500);
test1(schedule);
test2(schedule);
test3(schedule);
test4(schedule);
test5(schedule);
out.close();
ASSERT_TEST(matchFiles(fileName, FILE_PATH + std::string("/expected/allSegelTests.txt")), stream_buffer_cout)
return result;
}
bool testSchedulePolymorphism() {
bool result = true;
REDIRECT_OUTPUT(fileName, FILE_PATH + std::string("/your_outputs/testSchedulePolymorphism.txt"))
Schedule s;
Festival f(DateWrap(1, 1, 2000));
for (int i = 0; i < 3; i++) {
if (i == 1) {
ClosedEvent *cl = dynamic_cast<ClosedEvent *>(generate(i));
for (int j = 1; j < 10; j++) {
cl->addInvitee(j);
}
f.add(*cl);
delete cl;
} else {
BaseEvent *b = generate(i);
f.add(*b);
delete b;
}
}
s.addEvents(f);
for (int i = 1; i < 10 ; i++){
s.registerToEvent(DateWrap(1,1,2000), "an open event", i);
s.registerToEvent(DateWrap(1,1,2000), "a closed event", i);
try{
s.registerToEvent(DateWrap(1,1,2000), "a custom event", i);
}catch(mtm::RegistrationBlocked&){
cout << "RegistrationBlocked" << endl;
}
}
RecurringEvent<OpenEvent> recurringEvent(DateWrap(1,1,2000), "an open event", 3, 2);
try{
s.addEvents(recurringEvent);
}catch(mtm::EventAlreadyExists&){
cout << "EventAlreadyExists" << endl;
}
RecurringEvent<ClosedEvent> recurringEvent1(DateWrap(1,1,2000), "an open event", 1,1);
try{
s.addEvents(recurringEvent1);
}catch(mtm::EventAlreadyExists&){
cout << "EventAlreadyExists" << endl;
}
RecurringEvent<OpenEvent> recurringEvent2(DateWrap(2,1,2000), "an open event", 2,2);
s.addEvents(recurringEvent2);
s.registerToEvent(DateWrap(2,1,2000), "an open event", 2);
s.printSomeEvents(pred(), true);
out.close();
ASSERT_TEST(matchFiles(fileName, FILE_PATH + std::string("/expected/testSchedulePolymorphism.txt")), stream_buffer_cout)
return result;
}
/*bool testRegistrationAndUnRegistration(){
bool result = true;
REDIRECT_OUTPUT(fileName, FILE_PATH + std::string("/your_outputs/testRegistrationAndUnRegistration.txt"))
Schedule s;
RecurringEvent<OpenEvent> recurringEvent(DateWrap(2,1,2000), "an open event", 5,3);
s.addEvents(recurringEvent);
return result;
}*/
#define TEST_NAMES\
X(allSegelTests)\
X(testYan1)\
X(testYan2)\
X(testYan3)\
X(testYan4) \
X(testSchedulePolymorphism)
static const int NUMBER_OF_TESTS = 6;
const char* testNames[] = {
#define X(name) #name,
TEST_NAMES
#undef X
};
bool (*tests[])(void) = {
#define X(test_name) test_name,
TEST_NAMES
#undef X
};
int main(int argc, char* argv[]) {
if (argc < 2) {
for (int i = 0; i < NUMBER_OF_TESTS; i++) {
RUN_TEST(tests[i], testNames[i])
}
} else if (argc > 2) {
std::cout << "invalid arguments" << std::endl;
} else {
int i = std::atoi(argv[1]);
tests[i - 1]();
}
return 0;
}