-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
126 lines (114 loc) · 3.77 KB
/
main.cpp
File metadata and controls
126 lines (114 loc) · 3.77 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 "database.h"
#include "date.h"
#include "condition_parser.h"
#include "node.h"
#include "test_runner.h"
#include "test_db.h"
#include <iostream>
#include <stdexcept>
#include <vector>
using namespace std;
string ParseEvent(istream& is) {
string event;
is >> ws;
getline(is, event);
return event;
}
void TestAll();
void TestParseEvent();
void TestParseDate ();
void TestParseCondition ();
int main() {
// TestAll();
// return 0;
Database db;
for (string line; getline(cin, line); ) {
istringstream is(line);
string command;
is >> command;
if (command == "Add") {
const auto date = ParseDate(is);
const auto event = ParseEvent(is);
db.Add(date, event);
} else if (command == "Print") {
db.Print(cout);
} else if (command == "Del") {
auto condition = ParseCondition(is);
auto predicate = [condition](const Date& date, const string& event) {
return condition->Evaluate(date, event);
};
int count = db.RemoveIf(predicate);
cout << "Removed " << count << " entries" << endl;
} else if (command == "Find") {
auto condition = ParseCondition(is);
auto predicate = [condition](const Date& date, const string& event) {
return condition->Evaluate(date, event);
};
const auto entries = db.FindIf(predicate);
for (const auto& entry : entries) {
cout << entry << endl;
}
cout << "Found " << entries.size() << " entries" << endl;
} else if (command == "Last") {
try {
cout << db.Last(ParseDate(is)) << endl;
} catch (invalid_argument&) {
cout << "No entries" << endl;
}
} else if (command.empty()) {
continue;
} else {
throw logic_error("Unknown command: " + command);
}
}
return 0;
}
void TestParseEvent() {
{
istringstream is("event");
AssertEqual(ParseEvent(is), "event", "Parse event without leading spaces");
}
{
istringstream is(" sport event ");
AssertEqual(ParseEvent(is), "sport event ", "Parse event with leading spaces");
}
{
istringstream is(" first event \n second event");
vector<string> events;
events.push_back(ParseEvent(is));
events.push_back(ParseEvent(is));
// AssertEqual(events, vector<string>{"first event ", "second event"}, "Parse multiple events");
}
}
void TestParseDate () {
{
istringstream is("1990-5-26 event");
AssertEqual(ParseDate(is), Date {1990, 5, 26}, "first Parse date");
}
{
istringstream is("1990-5-26");
auto date = ParseDate(is);
}
{
istringstream is("1990-5-26");
auto date = ParseDate(is);
}
}
void TestAll() {
TestRunner tr;
tr.RunTest(TestParseDate, "TestParseDate");
tr.RunTest(TestParseEvent, "TestParseEvent");
tr.RunTest(TestParseCondition, "TestParseCondition");
tr.RunTest(TestEmptyNode, "Тест 2 из Coursera");
tr.RunTest(TestDbAdd, "Тест 3(1) из Coursera");
tr.RunTest(TestDbFind, "Тест 3(2) из Coursera");
tr.RunTest(TestDbLast, "Тест 3(3) из Coursera");
tr.RunTest(TestDbRemoveIf, "Тест 3(4) из Coursera");
tr.RunTest(TestInsertionOrder, "Тест на порядок вывода");
tr.RunTest(TestsMyCustom, "Мои тесты");
tr.RunTest(TestDatabase, "Тест базы данных с GitHub");
tr.RunTest(TestDateComparisonNode, "TestDateComparisonNode");
tr.RunTest(TestEventComparisonNode, "TestEventComparisonNode");
tr.RunTest(TestLogicalOperationNode, "TestLogicalOpeationNode");
tr.RunTest(TestEmptyNode, "TestEmptyNode");
}