O_Egor

test_unit.cpp

Oct 15th, 2022
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.50 KB | None | 0 0
  1. #include "database.h"
  2. #include "test_runner.h"
  3. #include "condition_parser.h"
  4.  
  5. //----------------------------------------------------------------------------------------------------
  6. //std::string FindIf_test(const Database& db, const std::string& str)
  7. //{
  8. //  std::istringstream is(str);
  9. //  auto condition = ParseCondition(is);
  10. //  auto predicate = [condition](const Date& date, const std::string& event)
  11. //  {
  12. //      return condition->Evaluate(date, event);
  13. //  };
  14. //  const auto entries = db.FindIf(predicate);
  15. //  std::ostringstream os;
  16. //  for (const auto& entry : entries)
  17. //  {
  18. //      os << entry << std::endl;
  19. //  }
  20. //  os << entries.size();
  21. //  return os.str();
  22. //}
  23. //----------------------------------------------------------------------------------------------------
  24. int RemoveIf_test(Database& db, const std::string& str)
  25. {
  26.     std::istringstream is(str);
  27.     auto condition = ParseCondition(is);
  28.     auto predicate = [condition](const Date& date, const std::string& event)
  29.     {
  30.         return condition->Evaluate(date, event);
  31.     };
  32.     return db.RemoveIf(predicate);
  33. }
  34. //----------------------------------------------------------------------------------------------------
  35. class AlwaysFalseNode : public Node
  36. {
  37.     bool Evaluate(const Date&, const std::string& event) const noexcept override
  38.     {
  39.         return false;
  40.     }
  41. };
  42. //----------------------------------------------------------------------------------------------------
  43. void TestParseDate()
  44. {
  45.     {
  46.         std::istringstream is("0-2-31");
  47.         AssertEqual(ParseDate(is), Date(0, 2, 31), "Parse Date 0-2-31");
  48.     }
  49.     {
  50.         std::istringstream is("2017-11-07");
  51.         AssertEqual(ParseDate(is), Date(2017, 11, 07), "Parse Date 2017-11-07");
  52.     }
  53.     {
  54.         try
  55.         {
  56.             std::istringstream is("--11-07");
  57.             ParseDate(is);
  58.         }
  59.         catch (const std::logic_error& err)
  60.         {
  61.             AssertEqual(std::string(err.what()), "Wrong date format: --11-07", "Parse Date --11-07");
  62.         }
  63.     }
  64.     {
  65.         try
  66.         {
  67.             std::istringstream is("1900---07");
  68.             ParseDate(is);
  69.         }
  70.         catch (const std::logic_error& err)
  71.         {
  72.             AssertEqual(std::string(err.what()), "Wrong date format: 1900---07", "Parse Date 1900---07");
  73.         }
  74.     }
  75.     {
  76.         try
  77.         {
  78.             std::istringstream is("1900-10--");
  79.             ParseDate(is);
  80.         }
  81.         catch (const std::logic_error& err)
  82.         {
  83.             AssertEqual(std::string(err.what()), "Wrong date format: 1900-10--", "Parse Date 1900-10--");
  84.         }
  85.     }
  86.     {
  87.         try
  88.         {
  89.             std::istringstream is("1900-13-1");
  90.             ParseDate(is);
  91.         }
  92.         catch (const std::logic_error& err)
  93.         {
  94.             AssertEqual(std::string(err.what()), "Month value is invalid: 13", "Month value is invalid");
  95.         }
  96.     }
  97.     {
  98.         try
  99.         {
  100.             std::istringstream is("1900-12-35");
  101.             ParseDate(is);
  102.         }
  103.         catch (const std::logic_error& err)
  104.         {
  105.             AssertEqual(std::string(err.what()), "Day value is invalid: 35", "Day value is invalid");
  106.         }
  107.     }
  108. }
  109. //----------------------------------------------------------------------------------------------------
  110. void TestEmptyNode()
  111. {
  112.     {
  113.         EmptyNode en;
  114.         Assert(en.Evaluate(Date{ 0, 1, 1 }, "abc"), "EmptyNode 1");
  115.         Assert(en.Evaluate(Date{ 2017, 11, 18 }, "abc"), "EmptyNode 2");
  116.         Assert(en.Evaluate(Date{ 9999, 12, 31 }, "abc"), "EmptyNode 3");
  117.     }
  118. }
  119. //----------------------------------------------------------------------------------------------------
  120. void TestDateComparisonNode()
  121. {
  122.     {
  123.         DateComparisonNode dcn(Comparison::Equal, { 2017, 11, 18 });
  124.         Assert(dcn.Evaluate(Date{ 2017, 11, 18 }, ""), "DateComparison 1");
  125.         Assert(!dcn.Evaluate(Date{ 2017, 11, 19 }, ""), "DateComparison 2");
  126.     }
  127.     {
  128.         DateComparisonNode dcn(Comparison::NotEqual, { 2017, 11, 18 });
  129.         Assert(dcn.Evaluate(Date{ 2017, 11, 19 }, ""), "DateComparison 3");
  130.         Assert(!dcn.Evaluate(Date{ 2017, 11, 18 }, ""), "DateComparison 4");
  131.     }
  132.     {
  133.         DateComparisonNode dcn(Comparison::Less, { 2017, 11, 18 });
  134.         Assert(dcn.Evaluate(Date{ 2017, 11, 17 }, ""), "DateComparison 5");
  135.         Assert(!dcn.Evaluate(Date{ 2017, 11, 18 }, ""), "DateComparison 6");
  136.         Assert(!dcn.Evaluate(Date{ 2017, 11, 19 }, ""), "DateComparison 7");
  137.     }
  138.     {
  139.         DateComparisonNode dcn(Comparison::Greater, { 2017, 11, 18 });
  140.         Assert(dcn.Evaluate(Date{ 2017, 11, 19 }, ""), "DateComparison 8");
  141.         Assert(!dcn.Evaluate(Date{ 2017, 11, 18 }, ""), "DateComparison 9");
  142.         Assert(!dcn.Evaluate(Date{ 2017, 11, 17 }, ""), "DateComparison 10");
  143.     }
  144.     {
  145.         DateComparisonNode dcn(Comparison::LessOrEqual, { 2017, 11, 18 });
  146.         Assert(dcn.Evaluate(Date{ 2017, 11, 17 }, ""), "DateComparison 11");
  147.         Assert(dcn.Evaluate(Date{ 2017, 11, 18 }, ""), "DateComparison 12");
  148.         Assert(!dcn.Evaluate(Date{ 2017, 11, 19 }, ""), "DateComparison 13");
  149.     }
  150.     {
  151.         DateComparisonNode dcn(Comparison::GreaterOrEqual, { 2017, 11, 18 });
  152.         Assert(dcn.Evaluate(Date{ 2017, 11, 19 }, ""), "DateComparison 14");
  153.         Assert(dcn.Evaluate(Date{ 2017, 11, 18 }, ""), "DateComparison 15");
  154.         Assert(!dcn.Evaluate(Date{ 2017, 11, 17 }, ""), "DateComparison 16");
  155.     }
  156. }
  157. //----------------------------------------------------------------------------------------------------
  158. void TestEventComparisonNode()
  159. {
  160.     {
  161.         EventComparisonNode ecn(Comparison::Equal, "abc");
  162.         Assert(ecn.Evaluate(Date{ 0, 1, 1 }, "abc"), "EventComparison 1");
  163.         Assert(!ecn.Evaluate(Date{ 0, 1, 1 }, "cbe"), "EventComparison 2");
  164.     }
  165.     {
  166.         EventComparisonNode ecn(Comparison::NotEqual, "abc");
  167.         Assert(ecn.Evaluate(Date{ 0, 1, 1 }, "cde"), "EventComparison 3");
  168.         Assert(!ecn.Evaluate(Date{ 0, 1, 1 }, "abc"), "EventComparison 4");
  169.     }
  170.     {
  171.         EventComparisonNode ecn(Comparison::Less, "abc");
  172.         Assert(ecn.Evaluate(Date{ 0, 1, 1 }, "abb"), "EventComparison 5");
  173.         Assert(!ecn.Evaluate(Date{ 0, 1, 1 }, "abc"), "EventComparison 6");
  174.         Assert(!ecn.Evaluate(Date{ 0, 1, 1 }, "abd"), "EventComparison 7");
  175.     }
  176.     {
  177.         EventComparisonNode ecn(Comparison::Greater, "abc");
  178.         Assert(ecn.Evaluate(Date{ 0, 1, 1 }, "abd"), "EventComparison 8");
  179.         Assert(!ecn.Evaluate(Date{ 0, 1, 1 }, "abc"), "EventComparison 9");
  180.         Assert(!ecn.Evaluate(Date{ 0, 1, 1 }, "abb"), "EventComparison 10");
  181.     }
  182.     {
  183.         EventComparisonNode ecn(Comparison::LessOrEqual, "abc");
  184.         Assert(ecn.Evaluate(Date{ 0, 1, 1 }, "abb"), "EventComparison 11");
  185.         Assert(ecn.Evaluate(Date{ 0, 1, 1 }, "abc"), "EventComparison 12");
  186.         Assert(!ecn.Evaluate(Date{ 0, 1, 1 }, "abd"), "EventComparison 13");
  187.     }
  188.     {
  189.         EventComparisonNode ecn(Comparison::GreaterOrEqual, "abc");
  190.         Assert(ecn.Evaluate(Date{ 0, 1, 1 }, "abd"), "EventComparison 14");
  191.         Assert(ecn.Evaluate(Date{ 0, 1, 1 }, "abc"), "EventComparison 15");
  192.         Assert(!ecn.Evaluate(Date{ 0, 1, 1 }, "abb"), "EventComparison 16");
  193.     }
  194. }
  195. //----------------------------------------------------------------------------------------------------
  196. void TestLogicalOperationNode()
  197. {
  198.     {
  199.         LogicalOperationNode lon(LogicalOperation::And, std::make_shared<EmptyNode>(), std::make_shared<EmptyNode>());
  200.         Assert(lon.Evaluate(Date{ 0, 1, 1 }, "abc"), "LogicalOperationNode 1");
  201.     }
  202.     {
  203.         LogicalOperationNode lon(LogicalOperation::And, std::make_shared<AlwaysFalseNode>(), std::make_shared<EmptyNode>());
  204.         Assert(!lon.Evaluate(Date{ 0, 1, 1 }, "abc"), "LogicalOperationNode 2");
  205.     }
  206.     {
  207.         LogicalOperationNode lon(LogicalOperation::And, std::make_shared<EmptyNode>(), std::make_shared<AlwaysFalseNode>());
  208.         Assert(!lon.Evaluate(Date{ 0, 1, 1 }, "abc"), "LogicalOperationNode 3");
  209.     }
  210.     {
  211.         LogicalOperationNode lon(LogicalOperation::And, std::make_shared<AlwaysFalseNode>(), std::make_shared<AlwaysFalseNode>());
  212.         Assert(!lon.Evaluate(Date{ 0, 1, 1 }, "abc"), "LogicalOperationNode 4");
  213.     }
  214.     {
  215.         LogicalOperationNode lon(LogicalOperation::Or, std::make_shared<EmptyNode>(), std::make_shared<EmptyNode>());
  216.         Assert(lon.Evaluate(Date{ 0, 1, 1 }, "abc"), "LogicalOperationNode 5");
  217.     }
  218.     {
  219.         LogicalOperationNode lon(LogicalOperation::Or, std::make_shared<AlwaysFalseNode>(), std::make_shared<EmptyNode>());
  220.         Assert(lon.Evaluate(Date{ 0, 1, 1 }, "abc"), "LogicalOperationNode 6");
  221.     }
  222.     {
  223.         LogicalOperationNode lon(LogicalOperation::Or, std::make_shared<EmptyNode>(), std::make_shared<AlwaysFalseNode>());
  224.         Assert(lon.Evaluate(Date{ 0, 1, 1 }, "abc"), "LogicalOperationNode 7");
  225.     }
  226.     {
  227.         LogicalOperationNode lon(LogicalOperation::Or, std::make_shared<AlwaysFalseNode>(), std::make_shared<AlwaysFalseNode>());
  228.         Assert(!lon.Evaluate(Date{ 0, 1, 1 }, "abc"), "LogicalOperationNode 8");
  229.     }
  230. }
  231. //----------------------------------------------------------------------------------------------------
  232. void TestAdd()
  233. {
  234.     {
  235.         Database db;
  236.         db.Add({ 2017, 1, 1 }, "new year");
  237.         db.Add({ 2017, 1, 7 }, "xmas");
  238.         std::ostringstream out;
  239.         db.Print(out);
  240.         AssertEqual("2017-01-01 new year\n2017-01-07 xmas\n", out.str(), "straight ordering");
  241.     }
  242.     {
  243.         Database db;
  244.         db.Add({ 2017, 1, 1 }, "new year");
  245.         db.Add({ 2017, 1, 1 }, "holiday");
  246.         std::ostringstream out;
  247.         db.Print(out);
  248.         AssertEqual("2017-01-01 new year\n2017-01-01 holiday\n", out.str(), "several in one day");
  249.     }
  250.     {
  251.         Database db;
  252.         db.Add({ 2017, 1, 7 }, "xmas");
  253.         db.Add({ 2017, 1, 1 }, "new year");
  254.         std::ostringstream out;
  255.         db.Print(out);
  256.         AssertEqual("2017-01-01 new year\n2017-01-07 xmas\n", out.str(), "reverse ordering");
  257.     }
  258.     {
  259.         Database db;
  260.         db.Add({ 2017, 1, 1 }, "new year");
  261.         db.Add({ 2017, 1, 1 }, "new year");
  262.         db.Add({ 2017, 1, 1 }, "xmas");
  263.         db.Add({ 2017, 1, 1 }, "new year");
  264.         std::ostringstream out;
  265.         db.Print(out);
  266.         AssertEqual("2017-01-01 new year\n2017-01-01 xmas\n", out.str(), "uniq adding");
  267.     }
  268.     {
  269.         Database db;
  270.         db.Add({ 2017, 1, 1 }, "new year");
  271.         db.Add({ 2017, 1, 1 }, "new year");
  272.         db.Add({ 2017, 1, 1 }, "xmas");
  273.         db.Add({ 2017, 1, 1 }, "holiday");
  274.         db.Add({ 2017, 1, 1 }, "holiday");
  275.         std::ostringstream out;
  276.         db.Print(out);
  277.         AssertEqual("2017-01-01 new year\n2017-01-01 xmas\n2017-01-01 holiday\n", out.str(), "several uniq adding");
  278.     }
  279. }
  280. //----------------------------------------------------------------------------------------------------
  281. void TestRemove()
  282. {
  283.     {
  284.         Database db;
  285.         db.Add({ 2017, 1, 1 }, "new year");
  286.         db.Add({ 2017, 1, 7 }, "xmas");
  287.         db.Add({ 2017, 1, 7 }, "xmas2");
  288.         db.Add({ 2017, 1, 7 }, "xmas3");
  289.         db.Add({ 2017, 1, 2 }, "xmas4");
  290.         AssertEqual(0, RemoveIf_test(db, R"(event == "nothing")"), "Remove nothing");
  291.         AssertEqual(1, RemoveIf_test(db, R"(event == "xmas2")"), "Remove xmas2");
  292.         AssertEqual(1, RemoveIf_test(db, R"(date == "2017-01-01")"), "Remove by date1");
  293.         AssertEqual(2, RemoveIf_test(db, R"(date == "2017-01-07")"), "Remove by date2");
  294.         std::ostringstream out;
  295.         db.Print(out);
  296.         AssertEqual("2017-01-02 xmas4\n", out.str(), "Remove by date, left");
  297.     }
  298.     {
  299.         Database db;
  300.         db.Add({ 2017, 1, 1 }, "new year");
  301.         db.Add({ 2017, 1, 7 }, "xmas");
  302.         AssertEqual(1, RemoveIf_test(db, R"(event == "xmas")"), "Remove by event");
  303.         std::ostringstream out;
  304.         db.Print(out);
  305.         AssertEqual("2017-01-01 new year\n", out.str(), "Remove by event, left");
  306.     }
  307.     {
  308.         Database db;
  309.         db.Add({ 2017, 1, 1 }, "new year");
  310.         db.Add({ 2017, 1, 7 }, "xmas");
  311.         db.Add({ 2017, 1, 7 }, "new year");
  312.         AssertEqual(2, RemoveIf_test(db, R"(event == "new year")"), "Multiple remove by event");
  313.         std::ostringstream out;
  314.         db.Print(out);
  315.         AssertEqual("2017-01-07 xmas\n", out.str(), "Multiple remove by event, left");
  316.     }
  317. }
  318. //----------------------------------------------------------------------------------------------------
  319. //void TestFind()
  320. //{
  321. //  {
  322. //      Database db;
  323. //      db.Add({ 2017, 1, 1 }, "new year");
  324. //      db.Add({ 2017, 1, 7 }, "xmas");
  325. //      AssertEqual("2017-01-01 new year\n1", FindIf_test(db, "date == 2017-01-01"), "simple find by date");
  326. //      AssertEqual("2017-01-01 new year\n2017-01-07 xmas\n2", FindIf_test(db, "date < 2017-01-31"), "multiple find by date");
  327. //      AssertEqual("2017-01-01 new year\n1", FindIf_test(db, R"(event != "xmas")"), "multiple find by holiday");
  328. //  }
  329. //  {
  330. //      Database db;
  331. //      db.Add({ 2017, 1, 1 }, "new year");
  332. //      db.Add({ 2017, 1, 1 }, "new year2");
  333. //      db.Add({ 2017, 1, 7 }, "xmas");
  334. //      AssertEqual("2017-01-01 new year\n2017-01-07 xmas\n2", FindIf_test(db, R"(date == 2017-01-07 OR event == "new year")"),
  335. //          "complex find or");
  336. //      AssertEqual("2017-01-01 new year\n1", FindIf_test(db, R"(date == 2017-01-01 AND event == "new year")"),
  337. //          "complex find and");
  338. //      AssertEqual("0", FindIf_test(db, R"(date == 2017-01-09 AND event == "new year")"),
  339. //          "complex find and, nothing");
  340. //  }
  341. //  {
  342. //      Database db;
  343. //      db.Add({ 2017, 1, 1 }, "new year");
  344. //      db.Add({ 2017, 1, 1 }, "Holiday");
  345. //      db.Add({ 2017, 1, 7 }, "xmas");
  346. //      AssertEqual("2017-01-07 xmas\n1", FindIf_test(db, "date == 2017-01-07"), "single find by date");
  347. //      AssertEqual("2017-01-01 new year\n2017-01-01 Holiday\n2017-01-07 xmas\n3", FindIf_test(db, "date < 2017-01-31"), "multiple find by date");
  348. //      AssertEqual("2017-01-01 new year\n2017-01-01 Holiday\n2", FindIf_test(db, R"(event != "xmas")"), "multiple find by event != xmas ");
  349. //      AssertEqual("2017-01-01 Holiday\n2017-01-07 xmas\n2", FindIf_test(db, R"(event != "new year")"), "multiple find by event != new year");
  350. //  }
  351. //}
  352. //----------------------------------------------------------------------------------------------------
  353. void TestLast()
  354. {
  355.     Database db;
  356.     db.Add({ 2017, 1, 1 }, "new year");
  357.     db.Add({ 2017, 1, 7 }, "xmas");
  358.     {
  359.         try
  360.         {
  361.             db.Last({ 2016, 12, 31 });
  362.             Assert(false, "last, found no entries");
  363.         }
  364.         catch (...)
  365.         {
  366.             Assert(true, "last, found no entries");
  367.  
  368.         }
  369.     }
  370.     {
  371.         std::ostringstream os;
  372.         os << db.Last({ 2017, 1, 2 });
  373.         AssertEqual("2017-01-01 new year", os.str(), "greater than date");
  374.     }
  375.     {
  376.         std::ostringstream os;
  377.         os << db.Last({ 2017, 1, 1 });
  378.         AssertEqual("2017-01-01 new year", os.str(), "eq to date");
  379.     }
  380.     {
  381.         std::ostringstream os;
  382.         os << db.Last({ 2017, 1, 10 });
  383.         AssertEqual("2017-01-07 xmas", os.str(), "greater than max date");
  384.     }
  385.     {
  386.         std::ostringstream os;
  387.         os << db.Last({ 2017, 1, 6 });
  388.         AssertEqual("2017-01-01 new year", os.str(), "greater than before max");
  389.     }
  390. }
  391. //----------------------------------------------------------------------------------------------------
  392. void TestInsertionOrder()
  393. {
  394.     {
  395.         Database db;
  396.         db.Add({ 2017, 1, 1 }, "new year");
  397.         db.Add({ 2017, 1, 7 }, "xmas");
  398.         db.Add({ 2017, 1, 7 }, "party");
  399.         db.Add({ 2017, 1, 7 }, "pie");
  400.         std::ostringstream out;
  401.         db.Print(out);
  402.         AssertEqual("2017-01-01 new year\n2017-01-07 xmas\n2017-01-07 party\n2017-01-07 pie\n", out.str(), "Remove by date, left");
  403.     }
  404.  
  405. }
  406. //----------------------------------------------------------------------------------------------------
  407. void TestDatabase()
  408. {
  409.     std::istringstream empty_is("");
  410.     auto const empty_condition = ParseCondition(empty_is);
  411.     auto empty_predicate = [empty_condition](const Date& date, const std::string& event)
  412.     {
  413.         return empty_condition->Evaluate(date, event);
  414.     };
  415.  
  416.     //Add 2 - Del 1 - Add deleted again
  417.     {
  418.         Database db;
  419.         Date const d(2019, 1, 1);
  420.         db.Add(d, "e1");
  421.         db.Add(d, "e2");
  422.         std::istringstream is(R"(event == "e1")");
  423.         auto const condition = ParseCondition(is);
  424.         auto predicate = [condition](const Date& date, const std::string& event)
  425.         {
  426.             return condition->Evaluate(date, event);
  427.         };
  428.         AssertEqual(db.RemoveIf(predicate), 1, "Db Add2-Del-Add 1");
  429.         db.Add(d, "e1");
  430.         AssertEqual(db.FindIf(empty_predicate).size(), 2, "Db Add2-Del-Add 2");
  431.     }
  432.     {
  433.         Database db;
  434.         Date const d(2019, 1, 1);
  435.         db.Add(d, "e1");
  436.         db.Add(d, "e1");
  437.         std::istringstream is("date == 2019-01-01");
  438.         auto const condition = ParseCondition(is);
  439.         auto predicate = [condition](const Date& date, const std::string& event)
  440.         {
  441.             return condition->Evaluate(date, event);
  442.         };
  443.         AssertEqual(db.FindIf(predicate).size(), 1, "db Add Duplicates 1");
  444.     }
  445.  
  446.     // Last
  447.     {
  448.         Database db;
  449.         Date const d(2019, 1, 1);
  450.         Date const d1(2019, 1, 2);
  451.         Date const d2(2018, 12, 22);
  452.         db.Add(d1, "e1");
  453.         db.Add(d2, "e2");
  454.         AssertEqual(db.Last(d), ((Date(2018, 12, 22).DateToStr()) + " e2"), "db Last 1");
  455.         Date const d3(2018, 12, 24);
  456.         db.Add(d3, "e3");
  457.         AssertEqual(db.Last(d), ((Date(2018, 12, 24).DateToStr()) + " e3"), "db Last 2");
  458.  
  459.         // Get last event for date before first event
  460.         //try
  461.         //{
  462.         //  Date const d4(2017, 2, 2);
  463.         //  db.Last(d4);
  464.         //  Assert(false, "db Last 3");
  465.         //}
  466.         //catch (const std::invalid_argument& e)
  467.         //{
  468.         //  std::cout << e.what() << std::endl; //PASS
  469.         //}
  470.  
  471.         // Delete event and get last
  472.         std::istringstream is("date == 2018-12-24");
  473.         auto const condition = ParseCondition(is);
  474.         auto predicate = [condition](const Date& date, const std::string& event)
  475.         {
  476.             return condition->Evaluate(date, event);
  477.         };
  478.         db.RemoveIf(predicate);
  479.         AssertEqual(db.Last(d), ((Date(2018, 12, 22).DateToStr()) + " e2"), "db Last 4");
  480.         AssertEqual(db.Last(d1), ((Date(2019, 1, 2).DateToStr()) + " e1"), "Db Last 5");
  481.         db.Add(d2, "e4");
  482.         AssertEqual(db.Last(d2), ((Date(2018, 12, 22).DateToStr()) + " e4"), "db Last 6");
  483.     }
  484.  
  485.     // Del
  486.     {
  487.         Database db;
  488.         db.Add({ 2019, 1, 1 }, "e1");
  489.         db.Add({ 2019, 1, 1 }, "e2");
  490.         db.Add({ 2018, 1, 7 }, "e3");
  491.         db.Add({ 2018, 1, 7 }, "e4");
  492.         std::istringstream is("date == 2018-01-07");
  493.         auto const condition = ParseCondition(is);
  494.         auto predicate = [condition](const Date& date, const std::string& event)
  495.         {
  496.             return condition->Evaluate(date, event);
  497.         };
  498.         AssertEqual(db.RemoveIf(predicate), 2, "db Del 1");
  499.     }
  500.     {
  501.         Database db;
  502.         db.Add({ 2019, 1, 1 }, "e1");
  503.         db.Add({ 2019, 1, 1 }, "e2");
  504.         db.Add({ 2018, 1, 7 }, "e3");
  505.         db.Add({ 2018, 1, 7 }, "e4");
  506.         std::istringstream is("date >= 2018-01-07 AND date <= 2020-01-01");
  507.         auto const condition = ParseCondition(is);
  508.         auto predicate = [condition](const Date& date, const std::string& event)
  509.         {
  510.             return condition->Evaluate(date, event);
  511.         };
  512.         AssertEqual(db.RemoveIf(predicate), 4, "db Del 2");
  513.     }
  514.     {
  515.         Database db;
  516.         db.Add({ 2019, 1, 1 }, "e1");
  517.         db.Add({ 2019, 1, 1 }, "e2");
  518.         db.Add({ 2018, 1, 7 }, "e3");
  519.         db.Add({ 2018, 1, 7 }, "e4");
  520.         AssertEqual(db.RemoveIf(empty_predicate), 4, "db Del 3");
  521.     }
  522.     {
  523.         Database db;
  524.         db.Add({ 2019, 1, 1 }, "e1");
  525.         db.Add({ 2019, 1, 1 }, "e2");
  526.         db.Add({ 2018, 1, 7 }, "e3");
  527.         db.Add({ 2018, 1, 7 }, "e4");
  528.         std::istringstream is(R"(event == "e1")");
  529.         auto const condition = ParseCondition(is);
  530.         auto predicate = [condition](const Date& date, const std::string& event) {
  531.             return condition->Evaluate(date, event);
  532.         };
  533.         AssertEqual(db.RemoveIf(predicate), 1, "db Del 4");
  534.     }
  535.     {
  536.         Database db;
  537.         db.Add({ 2019, 1, 1 }, "e1");
  538.         db.Add({ 2019, 1, 1 }, "e2");
  539.         db.Add({ 2018, 1, 7 }, "e3");
  540.         db.Add({ 2018, 1, 7 }, "e4");
  541.         std::istringstream is(R"(event == "e1" OR date == 2019-01-01)");
  542.         auto const condition = ParseCondition(is);
  543.         auto predicate = [condition](const Date& date, const std::string& event)
  544.         {
  545.             return condition->Evaluate(date, event);
  546.         };
  547.         AssertEqual(db.RemoveIf(predicate), 2, "db Del 5");
  548.     }
  549.  
  550.     // Find
  551.     {
  552.         Database db;
  553.         db.Add({ 2019, 1, 1 }, "e1");
  554.         db.Add({ 2019, 1, 1 }, "e2");
  555.         db.Add({ 2018, 1, 7 }, "e3");
  556.         db.Add({ 2018, 1, 7 }, "e4");
  557.         std::istringstream is("date == 2018-01-07");
  558.         auto const condition = ParseCondition(is);
  559.         auto predicate = [condition](const Date& date, const std::string& event)
  560.         {
  561.             return condition->Evaluate(date, event);
  562.         };
  563.         AssertEqual(db.FindIf(predicate).size(), 2, "db Find 1"); //-V807
  564.     }
  565.     {
  566.         Database db;
  567.         db.Add({ 2019, 1, 1 }, "e1");
  568.         db.Add({ 2019, 1, 1 }, "e2");
  569.         db.Add({ 2018, 1, 7 }, "e3");
  570.         db.Add({ 2018, 1, 7 }, "e4");
  571.         std::istringstream is("date >= 2018-01-07 AND date <= 2020-01-01");
  572.         auto const condition = ParseCondition(is);
  573.         auto predicate = [condition](const Date& date, const std::string& event)
  574.         {
  575.             return condition->Evaluate(date, event);
  576.         };
  577.         AssertEqual(db.FindIf(predicate).size(), 4, "db Find 2");
  578.     }
  579.     {
  580.         Database db;
  581.         db.Add({ 2019, 1, 1 }, "e1");
  582.         db.Add({ 2019, 1, 1 }, "e2");
  583.         db.Add({ 2018, 1, 7 }, "e3");
  584.         db.Add({ 2018, 1, 7 }, "e4");
  585.         AssertEqual(db.FindIf(empty_predicate).size(), 4, "db Find 3");
  586.     }
  587.     {
  588.         Database db;
  589.         db.Add({ 2019, 1, 1 }, "e1");
  590.         db.Add({ 2019, 1, 1 }, "e2");
  591.         db.Add({ 2018, 1, 7 }, "e3");
  592.         db.Add({ 2018, 1, 7 }, "e4");
  593.         std::istringstream is(R"(event == "e1")");
  594.         auto const condition = ParseCondition(is);
  595.         auto predicate = [condition](const Date& date, const std::string& event)
  596.         {
  597.             return condition->Evaluate(date, event);
  598.         };
  599.         AssertEqual(db.FindIf(predicate).size(), 1, "db Find 4");
  600.     }
  601.     {
  602.         Database db;
  603.         db.Add({ 2019, 1, 1 }, "e1");
  604.         db.Add({ 2019, 1, 1 }, "e2");
  605.         db.Add({ 2018, 1, 7 }, "e3");
  606.         db.Add({ 2018, 1, 7 }, "e4");
  607.         std::istringstream is(R"(event == "e1" OR date == 2019-01-01)");
  608.         auto const condition = ParseCondition(is);
  609.         auto predicate = [condition](const Date& date, const std::string& event) {
  610.             return condition->Evaluate(date, event);
  611.         };
  612.         AssertEqual(db.FindIf(predicate).size(), 2, "db Find 5");
  613.     }
  614.  
  615.     // Add - Del - Add - Del
  616.     {
  617.         Database db;
  618.         db.Add({ 2019, 1, 1 }, "e1");
  619.         db.Add({ 2019, 1, 1 }, "e2");
  620.         AssertEqual(db.RemoveIf(empty_predicate), 2, "db Add-Del-Add-Del 1");
  621.  
  622.         db.Add({ 2019, 1, 1 }, "e1");
  623.         db.Add({ 2019, 1, 1 }, "e2");
  624.         AssertEqual(db.RemoveIf(empty_predicate), 2, "db Add-Del-Add-Del 1");
  625.     }
  626.     {
  627.         Database db;
  628.         db.Add({ 2017, 1, 1 }, "first");
  629.         db.Add({ 2017, 1, 1 }, "second");
  630.         db.Add({ 2017, 1, 1 }, "third");
  631.         db.Add({ 2017, 1, 1 }, "fourth");
  632.         db.Add({ 2017, 1, 1 }, "five");
  633.         AssertEqual(2, RemoveIf_test(db, R"(event == "second" OR event == "fourth")"), "Remove several");
  634.         std::ostringstream os;
  635.         db.Print(os);
  636.         AssertEqual("2017-01-01 first\n2017-01-01 third\n2017-01-01 five\n", os.str(), "Check print after remove several- 3");
  637.     }
  638. }
  639. //----------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment