t_expr.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #define BOOST_TEST_DYN_LINK
  2. //#define BOOST_TEST_MODULE expr
  3. #include <boost/test/unit_test.hpp>
  4. #include <system.hh>
  5. #include "expr.h"
  6. #include "predicate.h"
  7. #include "query.h"
  8. #include "op.h"
  9. using namespace ledger;
  10. struct expr_fixture {
  11. expr_fixture() {
  12. times_initialize();
  13. amount_t::initialize();
  14. }
  15. ~expr_fixture() {
  16. amount_t::shutdown();
  17. times_shutdown();
  18. }
  19. };
  20. // 1. foo and bar
  21. // 2. 'foo and bar'
  22. // 3. (foo and bar)
  23. // 4. ( foo and bar )
  24. // 5. '( foo and' bar)
  25. // 6. =foo and bar
  26. // 7. ='foo and bar'
  27. // 8. 'expr foo and bar'
  28. // 9. expr 'foo and bar'
  29. // 10. expr foo and bar
  30. // 11. foo and bar or baz
  31. // 12. foo and bar | baz
  32. // 13. foo and bar |baz
  33. // 14. foo and bar| baz
  34. // 15. foo and bar|baz
  35. // 16. foo 'and bar|baz'
  36. BOOST_FIXTURE_TEST_SUITE(expr, expr_fixture)
  37. BOOST_AUTO_TEST_CASE(testPredicateTokenizer1)
  38. {
  39. value_t args;
  40. args.push_back(string_value("foo"));
  41. args.push_back(string_value("and"));
  42. args.push_back(string_value("bar"));
  43. #ifndef NOT_FOR_PYTHON
  44. query_t::lexer_t tokens(args.begin(), args.end());
  45. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  46. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
  47. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  48. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  49. #endif
  50. }
  51. BOOST_AUTO_TEST_CASE(testPredicateTokenizer2)
  52. {
  53. value_t args;
  54. args.push_back(string_value("foo and bar"));
  55. #ifndef NOT_FOR_PYTHON
  56. query_t::lexer_t tokens(args.begin(), args.end(), false);
  57. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  58. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
  59. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  60. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  61. #endif
  62. }
  63. BOOST_AUTO_TEST_CASE(testPredicateTokenizer3)
  64. {
  65. value_t args;
  66. args.push_back(string_value("(foo"));
  67. args.push_back(string_value("and"));
  68. args.push_back(string_value("bar)"));
  69. #ifndef NOT_FOR_PYTHON
  70. query_t::lexer_t tokens(args.begin(), args.end());
  71. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::LPAREN, tokens.next_token().kind);
  72. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  73. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
  74. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  75. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::RPAREN, tokens.next_token().kind);
  76. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  77. #endif
  78. }
  79. BOOST_AUTO_TEST_CASE(testPredicateTokenizer4)
  80. {
  81. value_t args;
  82. args.push_back(string_value("("));
  83. args.push_back(string_value("foo"));
  84. args.push_back(string_value("and"));
  85. args.push_back(string_value("bar"));
  86. args.push_back(string_value(")"));
  87. #ifndef NOT_FOR_PYTHON
  88. query_t::lexer_t tokens(args.begin(), args.end());
  89. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::LPAREN, tokens.next_token().kind);
  90. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  91. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
  92. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  93. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::RPAREN, tokens.next_token().kind);
  94. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  95. #endif
  96. }
  97. BOOST_AUTO_TEST_CASE(testPredicateTokenizer5)
  98. {
  99. value_t args;
  100. args.push_back(string_value("( foo and"));
  101. args.push_back(string_value("bar)"));
  102. #ifndef NOT_FOR_PYTHON
  103. query_t::lexer_t tokens(args.begin(), args.end(), false);
  104. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::LPAREN, tokens.next_token().kind);
  105. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  106. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
  107. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  108. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::RPAREN, tokens.next_token().kind);
  109. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  110. #endif
  111. }
  112. BOOST_AUTO_TEST_CASE(testPredicateTokenizer6)
  113. {
  114. value_t args;
  115. args.push_back(string_value("=foo"));
  116. args.push_back(string_value("and"));
  117. args.push_back(string_value("bar"));
  118. #ifndef NOT_FOR_PYTHON
  119. query_t::lexer_t tokens(args.begin(), args.end());
  120. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_EQ, tokens.next_token().kind);
  121. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  122. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
  123. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  124. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  125. #endif
  126. }
  127. BOOST_AUTO_TEST_CASE(testPredicateTokenizer7)
  128. {
  129. value_t args;
  130. args.push_back(string_value("=foo and bar"));
  131. #ifndef NOT_FOR_PYTHON
  132. query_t::lexer_t tokens(args.begin(), args.end());
  133. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_EQ, tokens.next_token().kind);
  134. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  135. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  136. #endif
  137. }
  138. BOOST_AUTO_TEST_CASE(testPredicateTokenizer8)
  139. {
  140. value_t args;
  141. args.push_back(string_value("expr 'foo and bar'"));
  142. #ifndef NOT_FOR_PYTHON
  143. query_t::lexer_t tokens(args.begin(), args.end(), false);
  144. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_EXPR, tokens.next_token().kind);
  145. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  146. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  147. #endif
  148. }
  149. BOOST_AUTO_TEST_CASE(testPredicateTokenizer9)
  150. {
  151. value_t args;
  152. args.push_back(string_value("expr"));
  153. args.push_back(string_value("'foo and bar'"));
  154. #ifndef NOT_FOR_PYTHON
  155. query_t::lexer_t tokens(args.begin(), args.end());
  156. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_EXPR, tokens.next_token().kind);
  157. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  158. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  159. #endif
  160. }
  161. BOOST_AUTO_TEST_CASE(testPredicateTokenizer10)
  162. {
  163. value_t args;
  164. args.push_back(string_value("expr"));
  165. args.push_back(string_value("foo"));
  166. args.push_back(string_value("and"));
  167. args.push_back(string_value("bar"));
  168. #ifndef NOT_FOR_PYTHON
  169. query_t::lexer_t tokens(args.begin(), args.end());
  170. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_EXPR, tokens.next_token().kind);
  171. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  172. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
  173. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  174. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  175. #endif
  176. }
  177. BOOST_AUTO_TEST_CASE(testPredicateTokenizer11)
  178. {
  179. value_t args;
  180. args.push_back(string_value("foo"));
  181. args.push_back(string_value("and"));
  182. args.push_back(string_value("bar"));
  183. args.push_back(string_value("or"));
  184. args.push_back(string_value("baz"));
  185. #ifndef NOT_FOR_PYTHON
  186. query_t::lexer_t tokens(args.begin(), args.end());
  187. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  188. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
  189. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  190. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_OR, tokens.next_token().kind);
  191. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  192. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  193. #endif
  194. }
  195. BOOST_AUTO_TEST_CASE(testPredicateTokenizer12)
  196. {
  197. value_t args;
  198. args.push_back(string_value("foo"));
  199. args.push_back(string_value("and"));
  200. args.push_back(string_value("bar"));
  201. args.push_back(string_value("|"));
  202. args.push_back(string_value("baz"));
  203. #ifndef NOT_FOR_PYTHON
  204. query_t::lexer_t tokens(args.begin(), args.end());
  205. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  206. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
  207. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  208. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_OR, tokens.next_token().kind);
  209. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  210. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  211. #endif
  212. }
  213. BOOST_AUTO_TEST_CASE(testPredicateTokenizer13)
  214. {
  215. value_t args;
  216. args.push_back(string_value("foo"));
  217. args.push_back(string_value("and"));
  218. args.push_back(string_value("bar"));
  219. args.push_back(string_value("|baz"));
  220. #ifndef NOT_FOR_PYTHON
  221. query_t::lexer_t tokens(args.begin(), args.end());
  222. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  223. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
  224. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  225. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_OR, tokens.next_token().kind);
  226. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  227. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  228. #endif
  229. }
  230. BOOST_AUTO_TEST_CASE(testPredicateTokenizer14)
  231. {
  232. value_t args;
  233. args.push_back(string_value("foo"));
  234. args.push_back(string_value("and"));
  235. args.push_back(string_value("bar|"));
  236. args.push_back(string_value("baz"));
  237. #ifndef NOT_FOR_PYTHON
  238. query_t::lexer_t tokens(args.begin(), args.end());
  239. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  240. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
  241. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  242. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_OR, tokens.next_token().kind);
  243. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  244. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  245. #endif
  246. }
  247. BOOST_AUTO_TEST_CASE(testPredicateTokenizer15)
  248. {
  249. value_t args;
  250. args.push_back(string_value("foo"));
  251. args.push_back(string_value("and"));
  252. args.push_back(string_value("bar|baz"));
  253. #ifndef NOT_FOR_PYTHON
  254. query_t::lexer_t tokens(args.begin(), args.end());
  255. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  256. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
  257. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  258. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_OR, tokens.next_token().kind);
  259. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  260. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  261. #endif
  262. }
  263. BOOST_AUTO_TEST_CASE(testPredicateTokenizer16)
  264. {
  265. value_t args;
  266. args.push_back(string_value("foo"));
  267. args.push_back(string_value("and bar|baz"));
  268. #ifndef NOT_FOR_PYTHON
  269. query_t::lexer_t tokens(args.begin(), args.end(), false);
  270. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  271. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
  272. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  273. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TOK_OR, tokens.next_token().kind);
  274. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
  275. BOOST_CHECK_EQUAL(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
  276. #endif
  277. }
  278. BOOST_AUTO_TEST_SUITE_END()