O_Egor

main.cpp

Oct 15th, 2022 (edited)
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.48 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.  
  7. #include <iostream>
  8. #include <stdexcept>
  9.  
  10. using namespace std;
  11.  
  12. //void TestParseEvent(); // Тесты для функции ParseEvent
  13. void TestParseDate(); // Тесты для функции ParseDate
  14. void TestEmptyNode(); // Тесты для EmptyNode
  15. void TestDateComparisonNode(); // Тесты для DateComparisonNode
  16. void TestEventComparisonNode(); // Тесты для TestEventComparisonNode
  17. void TestLogicalOperationNode(); // Тесты для TestLogicalOperationNode
  18. void TestInsertionOrder(); // Тесты для db на порядок вставки
  19. void TestAdd(); // Тесты для функции Add
  20. void TestLast(); // Тесты для функции Last
  21. void TestRemove(); // Тесты для функции RemoveIf
  22. //void TestFind(); // Тесты для функции FinIf
  23. void TestDatabase(); // Тесты для всего проекта
  24. //----------------------------------------------------------------------------------------------------
  25. void TestAll()
  26. {
  27.     TestRunner tr;
  28.    // tr.RunTest(TestParseEvent, "TestParseEvent");
  29.     tr.RunTest(TestParseDate, "TestParseDate");
  30.     tr.RunTest(TestEmptyNode, "TestEmptyNode");
  31.     tr.RunTest(TestDateComparisonNode, "TestDateComparisonNode");
  32.     tr.RunTest(TestEventComparisonNode, "TestEventComparisonNode");
  33.     tr.RunTest(TestLogicalOperationNode, "TestLogicalOperationNode");
  34.     tr.RunTest(TestInsertionOrder, "TestInsertionOrder");
  35.     tr.RunTest(TestAdd, "TestAdd");
  36.     tr.RunTest(TestLast, "TestLast");
  37.     tr.RunTest(TestRemove, "TestRemoveIf");
  38.     //tr.RunTest(TestFind, "TestFindIf");
  39.     tr.RunTest(TestParseCondition, "TestParseCondition");
  40.     tr.RunTest(TestDatabase, "TestDatabase");
  41.     //tr.RunTest(TestParseEvent, "TestParseEvent");
  42.     tr.RunTest(TestParseCondition, "TestParseCondition");
  43. }
  44.  
  45. string ParseEvent(istream& is)
  46. {
  47.     string event;
  48.     getline(is, event);
  49.     return string(find_if(event.begin(), event.end(), [](const unsigned char& c) noexcept
  50.         {
  51.             return !isspace(c);
  52.         }),
  53.         event.end());
  54. }
  55.  
  56. int main() {
  57.     TestAll();
  58.  
  59.     Database db;
  60.  
  61.     for (string line; getline(cin, line); ) {
  62.         istringstream is(line);
  63.  
  64.         string command;
  65.         is >> command;
  66.         if (command == "Add") {
  67.             const auto date = ParseDate(is);
  68.             const auto event = ParseEvent(is);
  69.             db.Add(date, event);
  70.         }
  71.         else if (command == "Print") {
  72.             db.Print(cout);
  73.         }
  74.         else if (command == "Del") {
  75.             auto condition = ParseCondition(is);
  76.             auto predicate = [condition](const Date& date, const string& event) {
  77.                 return condition->Evaluate(date, event);
  78.             };
  79.             int count = db.RemoveIf(predicate);
  80.             cout << "Removed " << count << " entries" << endl;
  81.         }
  82.         else if (command == "Find") {
  83.             auto condition = ParseCondition(is);
  84.             auto predicate = [condition](const Date& date, const string& event) {
  85.                 return condition->Evaluate(date, event);
  86.             };
  87.  
  88.             const auto entries = db.FindIf(predicate);
  89.             for (const auto& entry : entries) {
  90.                 cout << entry << endl;
  91.             }
  92.             cout << "Found " << entries.size() << " entries" << endl;
  93.         }
  94.         else if (command == "Last") {
  95.             try {
  96.                 cout << db.Last(ParseDate(is)) << endl;
  97.             }
  98.             catch (invalid_argument&) {
  99.                 cout << "No entries" << endl;
  100.             }
  101.         }
  102.         else if (command.empty()) {
  103.             continue;
  104.         }
  105.         else {
  106.             throw logic_error("Unknown command: " + command);
  107.         }
  108.     }
  109.  
  110.     return 0;
  111. }
  112.  
  113. //void TestParseEvent() {
  114. //    {
  115. //        istringstream is("event");
  116. //        AssertEqual(ParseEvent(is), "event", "Parse event without leading spaces");
  117. //    }
  118. //    {
  119. //        istringstream is("   sport event ");
  120. //        AssertEqual(ParseEvent(is), "sport event ", "Parse event with leading spaces");
  121. //    }
  122. //    {
  123. //        istringstream is("  first event  \n  second event");
  124. //        vector<string> events;
  125. //        events.push_back(ParseEvent(is));
  126. //        events.push_back(ParseEvent(is));
  127. //        AssertEqual(events, vector<string>{"first event  ", "second event"}, "Parse multiple events");
  128. //    }
  129. //}
Advertisement
Add Comment
Please, Sign In to add comment