Advertisement
chzchz

Untitled

Apr 5th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. #include "condition_parser.h"
  2. #include "test_runner.h"
  3. #include "node.h"
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. void TestParseCondition() {
  9.   {
  10.     istringstream is("date != 2017-11-18");
  11.     shared_ptr<Node> root = ParseCondition(is);
  12.     Assert(root->Evaluate({2017, 1, 1}, ""), "Parse condition 1");
  13.     Assert(!root->Evaluate({2017, 11, 18}, ""), "Parse condition 2");
  14.   }
  15.   {
  16.     istringstream is(R"(event == "sport event")");
  17.     shared_ptr<Node> root = ParseCondition(is);
  18.     Assert(root->Evaluate({2017, 1, 1}, "sport event"), "Parse condition 3");
  19.     Assert(!root->Evaluate({2017, 1, 1}, "holiday"), "Parse condition 4");
  20.   }
  21.   {
  22.     istringstream is("date >= 2017-01-01 AND date < 2017-07-01");
  23.     shared_ptr<Node> root = ParseCondition(is);
  24.  
  25.  
  26.     Assert(root->Evaluate({2017, 1, 1}, ""), "Parse condition 5");
  27.     Assert(root->Evaluate({2017, 3, 1}, ""), "Parse condition 6");
  28.     Assert(root->Evaluate({2017, 6, 30}, ""), "Parse condition 7");
  29.     Assert(!root->Evaluate({2017, 7, 1}, ""), "Parse condition 8");
  30.     Assert(!root->Evaluate({2016, 12, 31}, ""), "Parse condition 9");
  31.   }
  32.   {
  33.     istringstream is(R"(event != "sport event" AND event != "Wednesday")");
  34.     shared_ptr<Node> root = ParseCondition(is);
  35.     Assert(root->Evaluate({2017, 1, 1}, "holiday"), "Parse condition 10");
  36.     Assert(!root->Evaluate({2017, 1, 1}, "sport event"), "Parse condition 11");
  37.     Assert(!root->Evaluate({2017, 1, 1}, "Wednesday"), "Parse condition 12");
  38.   }
  39.   {
  40.     istringstream is(R"(event == "holiday AND date == 2017-11-18")");
  41.     shared_ptr<Node> root = ParseCondition(is);
  42.     Assert(!root->Evaluate({2017, 11, 18}, "holiday"), "Parse condition 13");
  43.     Assert(!root->Evaluate({2017, 11, 18}, "work day"), "Parse condition 14");
  44.     Assert(root->Evaluate({1, 1, 1}, "holiday AND date == 2017-11-18"), "Parse condition 15");
  45.   }
  46.   {
  47.     istringstream is(R"(((event == "holiday" AND date == 2017-01-01)))");
  48.     shared_ptr<Node> root = ParseCondition(is);
  49.     Assert(root->Evaluate({2017, 1, 1}, "holiday"), "Parse condition 16");
  50.     Assert(!root->Evaluate({2017, 1, 2}, "holiday"), "Parse condition 17");
  51.   }
  52.   {
  53.     istringstream is(R"(date > 2017-01-01 AND (event == "holiday" OR date < 2017-07-01))");
  54.     shared_ptr<Node> root = ParseCondition(is);
  55.     Assert(!root->Evaluate({2016, 1, 1}, "holiday"), "Parse condition 18");
  56.     Assert(root->Evaluate({2017, 1, 2}, "holiday"), "Parse condition 19");
  57.     Assert(root->Evaluate({2017, 1, 2}, "workday"), "Parse condition 20");
  58.     Assert(!root->Evaluate({2018, 1, 2}, "workday"), "Parse condition 21");
  59.   }
  60.   {
  61.     istringstream is(R"(date > 2017-01-01 AND event == "holiday" OR date < 2017-07-01)");
  62.     shared_ptr<Node> root = ParseCondition(is);
  63.     Assert(root->Evaluate({2016, 1, 1}, "event"), "Parse condition 22");
  64.     Assert(root->Evaluate({2017, 1, 2}, "holiday"), "Parse condition 23");
  65.     Assert(root->Evaluate({2017, 1, 2}, "workday"), "Parse condition 24");
  66.     Assert(!root->Evaluate({2018, 1, 2}, "workday"), "Parse condition 25");
  67.   }
  68.   {
  69.     istringstream is(R"(((date == 2017-01-01 AND event == "holiday")))");
  70.     shared_ptr<Node> root = ParseCondition(is);
  71.     Assert(root->Evaluate({2017, 1, 1}, "holiday"), "Parse condition 26");
  72.     Assert(!root->Evaluate({2017, 1, 2}, "holiday"), "Parse condition 27");
  73.   }
  74.   {
  75.     istringstream is(R"(((event == "2017-01-01" OR date > 2016-01-01)))");
  76.     shared_ptr<Node> root = ParseCondition(is);
  77.     Assert(root->Evaluate({1, 1, 1}, "2017-01-01"), "Parse condition 28");
  78.     Assert(!root->Evaluate({2016, 1, 1}, "event"), "Parse condition 29");
  79.     Assert(root->Evaluate({2016, 1, 2}, "event"), "Parse condition 30");
  80.   }
  81. }
  82.  
  83. int main() {
  84.   TestRunner test_runner;
  85.   test_runner.RunTest(TestParseCondition, "Testing ParseCondition");
  86.   return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement