Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. #include "database.h"
  2. #include "test_runner.h"
  3. #include "condition_parser.h"
  4.  
  5. void TestDatabase() {
  6. istringstream empty_is("");
  7. auto empty_condition = ParseCondition(empty_is);
  8. auto empty_predicate = [empty_condition](const Date& date, const string& event) {
  9. return empty_condition->Evaluate(date, event);
  10. };
  11.  
  12. // Add 2 - Del 1 - Add deleted again
  13. {
  14. Database db;
  15. Date d(2019, 1, 1);
  16. db.Add(d, "e1");
  17. db.Add(d, "e2");
  18. istringstream is(R"(event == "e1")");
  19. auto condition = ParseCondition(is);
  20. auto predicate = [condition](const Date& date, const string& event) {
  21. return condition->Evaluate(date, event);
  22. };
  23. AssertEqual(db.RemoveIf(predicate), 1, "Db Add2-Del-Add 1");
  24. db.Add(d, "e1");
  25. AssertEqual(db.FindIf(empty_predicate).size(), 2, "Db Add2-Del-Add 2");
  26. }
  27.  
  28. // Add
  29. {
  30. Database db;
  31. Date d(2019, 1, 1);
  32. db.Add(d, "e1");
  33. db.Add(d, "e1");
  34. istringstream is("date == 2019-01-01");
  35. auto condition = ParseCondition(is);
  36. auto predicate = [condition](const Date& date, const string& event) {
  37. return condition->Evaluate(date, event);
  38. };
  39. AssertEqual(db.FindIf(predicate).size(), 1, "Db Add Duplicates 1");
  40. }
  41.  
  42. // Last
  43. {
  44. Database db;
  45. Date d(2019, 1, 1);
  46. Date d1(2019, 1, 2);
  47. Date d2(2018, 12, 22);
  48. db.Add(d1, "e1");
  49. db.Add(d2, "e2");
  50. AssertEqual(db.Last(d), "2018-12-22 e2", "Db Last 1");
  51. Date d3(2018, 12, 24);
  52. db.Add(d3, "e3");
  53. AssertEqual(db.Last(d), "2018-12-24 e3", "Db Last 2");
  54.  
  55. // Get last event for date before first event
  56. try {
  57. Date d4(2017, 2, 2);
  58. db.Last(d4);
  59. Assert(false, "Db Last 3");
  60. } catch (invalid_argument e) {
  61. // Pass
  62. }
  63.  
  64. // Delete event and get last
  65. istringstream is("date == 2018-12-24");
  66. auto condition = ParseCondition(is);
  67. auto predicate = [condition](const Date& date, const string& event) {
  68. return condition->Evaluate(date, event);
  69. };
  70. db.RemoveIf(predicate);
  71. AssertEqual(db.Last(d), "2018-12-22 e2", "Db Last 4");
  72.  
  73. AssertEqual(db.Last(d1), "2019-01-02 e1", "Db Last 5");
  74. db.Add(d2, "e4");
  75. AssertEqual(db.Last(d2), "2018-12-22 e4", "Db Last 6");
  76. }
  77.  
  78. // Del
  79. {
  80. Database db;
  81. db.Add({2019, 1, 1}, "e1");
  82. db.Add({2019, 1, 1}, "e2");
  83. db.Add({2018, 1, 7}, "e3");
  84. db.Add({2018, 1, 7}, "e4");
  85. istringstream is("date == 2018-01-07");
  86. auto condition = ParseCondition(is);
  87. auto predicate = [condition](const Date& date, const string& event) {
  88. return condition->Evaluate(date, event);
  89. };
  90. AssertEqual(db.RemoveIf(predicate), 2, "Db Del 1");
  91. }
  92. {
  93. Database db;
  94. db.Add({2019, 1, 1}, "e1");
  95. db.Add({2019, 1, 1}, "e2");
  96. db.Add({2018, 1, 7}, "e3");
  97. db.Add({2018, 1, 7}, "e4");
  98. istringstream is("date >= 2018-01-07 AND date <= 2020-01-01");
  99. auto condition = ParseCondition(is);
  100. auto predicate = [condition](const Date& date, const string& event) {
  101. return condition->Evaluate(date, event);
  102. };
  103. AssertEqual(db.RemoveIf(predicate), 4, "Db Del 2");
  104. }
  105. {
  106. Database db;
  107. db.Add({2019, 1, 1}, "e1");
  108. db.Add({2019, 1, 1}, "e2");
  109. db.Add({2018, 1, 7}, "e3");
  110. db.Add({2018, 1, 7}, "e4");
  111. AssertEqual(db.RemoveIf(empty_predicate), 4, "Db Del 3");
  112. }
  113. {
  114. Database db;
  115. db.Add({2019, 1, 1}, "e1");
  116. db.Add({2019, 1, 1}, "e2");
  117. db.Add({2018, 1, 7}, "e3");
  118. db.Add({2018, 1, 7}, "e4");
  119. istringstream is(R"(event == "e1")");
  120. auto condition = ParseCondition(is);
  121. auto predicate = [condition](const Date& date, const string& event) {
  122. return condition->Evaluate(date, event);
  123. };
  124. AssertEqual(db.RemoveIf(predicate), 1, "Db Del 4");
  125. }
  126.  
  127. {
  128. Database db;
  129. db.Add({2019, 1, 1}, "e1");
  130. db.Add({2019, 1, 1}, "e2");
  131. db.Add({2018, 1, 7}, "e3");
  132. db.Add({2018, 1, 7}, "e4");
  133. istringstream is(R"(event == "e1" OR date == 2019-01-01)");
  134. auto condition = ParseCondition(is);
  135. auto predicate = [condition](const Date& date, const string& event) {
  136. return condition->Evaluate(date, event);
  137. };
  138. AssertEqual(db.RemoveIf(predicate), 2, "Db Del 5");
  139. }
  140.  
  141. // Find
  142. {
  143. Database db;
  144. db.Add({2019, 1, 1}, "e1");
  145. db.Add({2019, 1, 1}, "e2");
  146. db.Add({2018, 1, 7}, "e3");
  147. db.Add({2018, 1, 7}, "e4");
  148. istringstream is("date == 2018-01-07");
  149. auto condition = ParseCondition(is);
  150. auto predicate = [condition](const Date& date, const string& event) {
  151. return condition->Evaluate(date, event);
  152. };
  153. AssertEqual(db.FindIf(predicate).size(), 2, "Db Find 1");
  154. }
  155. {
  156. Database db;
  157. db.Add({2019, 1, 1}, "e1");
  158. db.Add({2019, 1, 1}, "e2");
  159. db.Add({2018, 1, 7}, "e3");
  160. db.Add({2018, 1, 7}, "e4");
  161. istringstream is("date >= 2018-01-07 AND date <= 2020-01-01");
  162. auto condition = ParseCondition(is);
  163. auto predicate = [condition](const Date& date, const string& event) {
  164. return condition->Evaluate(date, event);
  165. };
  166. AssertEqual(db.FindIf(predicate).size(), 4, "Db Find 2");
  167. }
  168. {
  169. Database db;
  170. db.Add({2019, 1, 1}, "e1");
  171. db.Add({2019, 1, 1}, "e2");
  172. db.Add({2018, 1, 7}, "e3");
  173. db.Add({2018, 1, 7}, "e4");
  174. AssertEqual(db.FindIf(empty_predicate).size(), 4, "Db Find 3");
  175. }
  176. {
  177. Database db;
  178. db.Add({2019, 1, 1}, "e1");
  179. db.Add({2019, 1, 1}, "e2");
  180. db.Add({2018, 1, 7}, "e3");
  181. db.Add({2018, 1, 7}, "e4");
  182. istringstream is(R"(event == "e1")");
  183. auto condition = ParseCondition(is);
  184. auto predicate = [condition](const Date& date, const string& event) {
  185. return condition->Evaluate(date, event);
  186. };
  187. AssertEqual(db.FindIf(predicate).size(), 1, "Db Find 4");
  188. }
  189.  
  190. {
  191. Database db;
  192. db.Add({2019, 1, 1}, "e1");
  193. db.Add({2019, 1, 1}, "e2");
  194. db.Add({2018, 1, 7}, "e3");
  195. db.Add({2018, 1, 7}, "e4");
  196. istringstream is(R"(event == "e1" OR date == 2019-01-01)");
  197. auto condition = ParseCondition(is);
  198. auto predicate = [condition](const Date& date, const string& event) {
  199. return condition->Evaluate(date, event);
  200. };
  201. AssertEqual(db.FindIf(predicate).size(), 2, "Db Find 5");
  202. }
  203.  
  204. // Add - Del - Add - Del
  205. {
  206. Database db;
  207. db.Add({2019, 1, 1}, "e1");
  208. db.Add({2019, 1, 1}, "e2");
  209. AssertEqual(db.RemoveIf(empty_predicate), 2, "Db Add-Del-Add-Del 1");
  210.  
  211. db.Add({2019, 1, 1}, "e1");
  212. db.Add({2019, 1, 1}, "e2");
  213. AssertEqual(db.RemoveIf(empty_predicate), 2, "Db Add-Del-Add-Del 1");
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement