-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
111 lines (97 loc) · 2.79 KB
/
main.cpp
File metadata and controls
111 lines (97 loc) · 2.79 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
#include "database.h"
#include "date.h"
#include "condition_parser.h"
#include "node.h"
#include "test_runner.h"
#include <iostream>
#include <stdexcept>
#include <vector>
using namespace std;
string ParseEvent(istream& is) {
is >> ws;
string event;
getline(is, event);
return event;
}
template <class First, class Second>
ostream& operator<<(ostream& os, const pair<First, Second>& p) {
os << p.first << ' ' << p.second;
return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
bool first = true;
for (const auto& item : v) {
if (!first) os << ' ';
os << item;
first = false;
}
return os;
}
void TestAll();
int main() {
TestAll();
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 TestAll() {
TestRunner tr;
tr.RunTest(TestParseEvent, "TestParseEvent");
tr.RunTest(TestParseCondition, "TestParseCondition");
}