Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include"database.h"
  2. #include "date.h"
  3. #include "condition_parser.h"
  4. #include "node.h"
  5. #include "test_runner.h"
  6. #include <iostream>
  7. #include <stdexcept>
  8.  
  9. using namespace std;
  10.  
  11. string ParseEvent(istream& is) {
  12.     string r;
  13.     stringstream ss=is;
  14.     getline(ss, r);
  15. }
  16.  
  17. void TestAll();
  18.  
  19. int main() {
  20.     TestAll();
  21.  
  22.     Database db;
  23.  
  24.     for (string line; getline(cin, line); ) {
  25.         istringstream is(line);
  26.  
  27.         string command;
  28.         is >> command;
  29.         if (command == "Add") {
  30.             const auto date = ParseDate(is);
  31.             const auto event = ParseEvent(is);
  32.             db.Add(date, event);
  33.         }
  34.         else if (command == "Print") {
  35.             db.Print(cout);
  36.         }
  37.         else if (command == "Del") {
  38.             auto condition = ParseCondition(is);
  39.             auto predicate = [condition](const Date& date, const string& event) {
  40.                 return condition->Evaluate(date, event);
  41.             };
  42.             int count = db.RemoveIf(predicate);
  43.             cout << "Removed " << count << " entries" << endl;
  44.         }
  45.         else if (command == "Find") {
  46.             auto condition = ParseCondition(is);
  47.             auto predicate = [condition](const Date& date, const string& event) {
  48.                 return condition->Evaluate(date, event);
  49.             };
  50.  
  51.             const auto entries = db.FindIf(predicate);
  52.             for (const auto& entry : entries) {
  53.                 cout << entry << endl;
  54.             }
  55.             cout << "Found " << entries.size() << " entries" << endl;
  56.         }
  57.         else if (command == "Last") {
  58.             try {
  59.                 cout << db.Last(ParseDate(is)) << endl;
  60.             }
  61.             catch (invalid_argument&) {
  62.                 cout << "No entries" << endl;
  63.             }
  64.         }
  65.         else if (command.empty()) {
  66.             continue;
  67.         }
  68.         else {
  69.             throw logic_error("Unknown command: " + command);
  70.         }
  71.     }
  72.  
  73.     return 0;
  74. }
  75.  
  76. void TestParseEvent() {
  77.     {
  78.         istringstream is("event");
  79.         AssertEqual(ParseEvent(is), "event", "Parse event without leading spaces");
  80.     }
  81.     {
  82.         istringstream is("   sport event ");
  83.         AssertEqual(ParseEvent(is), "sport event ", "Parse event with leading spaces");
  84.     }
  85.     {
  86.         istringstream is("  first event  \n  second event");
  87.         vector<string> events;
  88.         events.push_back(ParseEvent(is));
  89.         events.push_back(ParseEvent(is));
  90.         AssertEqual(events, vector<string>{"first event  ", "second event"}, "Parse multiple events");
  91.     }
  92. }
  93.  
  94. void TestAll() {
  95.     TestRunner tr;
  96.     tr.RunTest(TestParseEvent, "TestParseEvent");
  97.     tr.RunTest(TestParseCondition, "TestParseCondition");
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement