symbols_tests.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*=============================================================================
  2. Copyright (c) 1998-2003 Joel de Guzman
  3. Copyright (c) 2003 Martin Wille
  4. http://spirit.sourceforge.net/
  5. Use, modification and distribution is subject to the Boost Software
  6. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #include <iostream>
  10. #include <string>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <boost/spirit/include/classic_core.hpp>
  13. #include <boost/spirit/include/classic_symbols.hpp>
  14. #include <boost/detail/lightweight_test.hpp>
  15. ///////////////////////////////////////////////////////////////////////////////
  16. using namespace std;
  17. using namespace BOOST_SPIRIT_CLASSIC_NS;
  18. ///////////////////////////////////////////////////////////////////////////////
  19. template <typename IteratorT>
  20. bool
  21. equal(IteratorT p, IteratorT q)
  22. {
  23. while (*p && *p == *q)
  24. {
  25. ++p;
  26. ++q;
  27. }
  28. return *p == *q;
  29. }
  30. template <class SymbolsT, typename CharT>
  31. void
  32. docheck
  33. (
  34. SymbolsT const &sym,
  35. CharT const *candidate,
  36. bool hit,
  37. CharT const *result,
  38. int length
  39. )
  40. {
  41. parse_info<CharT const*> info = parse(candidate, sym);
  42. #define correctly_matched hit == info.hit
  43. #define correct_match_length unsigned(length) == info.length
  44. #define correct_tail equal(candidate + (hit?1:0)*length, result)
  45. BOOST_TEST(correctly_matched);
  46. if (hit)
  47. {
  48. BOOST_TEST(correct_match_length);
  49. BOOST_TEST(correct_tail);
  50. }
  51. else
  52. {
  53. BOOST_TEST(correct_tail);
  54. }
  55. }
  56. template <typename T>
  57. struct store_action
  58. {
  59. store_action(T const &v) : value(v) {}
  60. void operator()(T &v) const { v = value; }
  61. private:
  62. T const value;
  63. };
  64. template <typename T>
  65. store_action<T>
  66. store(T const &v)
  67. {
  68. return v;
  69. }
  70. template <typename T>
  71. struct check_action
  72. {
  73. check_action(T const &v) : value(v) {}
  74. #define correct_value_stored (v==value)
  75. void operator()(T const &v) const { BOOST_TEST(correct_value_stored); }
  76. private:
  77. T const value;
  78. };
  79. template <typename T>
  80. check_action<T>
  81. docheck(T const &v)
  82. {
  83. return v;
  84. }
  85. static void
  86. default_constructible()
  87. { // this actually a compile time test
  88. symbols<> ns1;
  89. symbols<int, wchar_t> ws1;
  90. symbols<std::string, char> ns2;
  91. symbols<std::string, wchar_t> ws2;
  92. (void)ns1; (void)ws1; (void)ns2; (void)ws2;
  93. }
  94. static void
  95. narrow_match_tests()
  96. {
  97. symbols<> sym;
  98. sym = "pineapple", "orange", "banana", "applepie", "apple";
  99. docheck(sym, "pineapple", true, "", 9);
  100. docheck(sym, "orange", true, "", 6);
  101. docheck(sym, "banana", true, "", 6);
  102. docheck(sym, "apple", true, "", 5);
  103. docheck(sym, "pizza", false, "pizza", -1);
  104. docheck(sym, "steak", false, "steak", -1);
  105. docheck(sym, "applepie", true, "", 8);
  106. docheck(sym, "bananarama", true, "rama", 6);
  107. docheck(sym, "applet", true, "t", 5);
  108. docheck(sym, "applepi", true, "pi", 5);
  109. docheck(sym, "appl", false, "appl", -1);
  110. docheck(sym, "pineapplez", true, "z", 9);
  111. docheck(sym, "orangez", true, "z", 6);
  112. docheck(sym, "bananaz", true, "z", 6);
  113. docheck(sym, "applez", true, "z", 5);
  114. docheck(sym, "pizzaz", false, "pizzaz", -1);
  115. docheck(sym, "steakz", false, "steakz", -1);
  116. docheck(sym, "applepiez", true, "z", 8);
  117. docheck(sym, "bananaramaz", true, "ramaz", 6);
  118. docheck(sym, "appletz", true, "tz", 5);
  119. docheck(sym, "applepix", true, "pix", 5);
  120. }
  121. static void
  122. narrow_copy_ctor_tests()
  123. {
  124. symbols<> sym;
  125. sym = "pineapple", "orange", "banana", "applepie", "apple";
  126. symbols<> sym2(sym);
  127. docheck(sym2, "pineapple", true, "", 9);
  128. docheck(sym2, "pizza", false, "pizza", -1);
  129. docheck(sym2, "bananarama", true, "rama", 6);
  130. }
  131. static void
  132. narrow_assigment_operator_tests()
  133. {
  134. symbols<> sym;
  135. sym = "pineapple", "orange", "banana", "applepie", "apple";
  136. symbols<> sym2;
  137. sym2 = sym;
  138. docheck(sym2, "pineapple", true, "", 9);
  139. docheck(sym2, "pizza", false, "pizza", -1);
  140. docheck(sym2, "bananarama", true, "rama", 6);
  141. }
  142. static void
  143. narrow_value_tests()
  144. { // also tests the add member functions
  145. symbols<> sym;
  146. sym = "orange", "banana";
  147. sym.add("pineapple",1234);
  148. sym.add("lemon");
  149. parse("orange", sym[store(12345)]);
  150. parse("orange", sym[docheck(12345)]);
  151. parse("pineapple", sym[docheck(1234)]);
  152. parse("banana", sym[docheck(int())]);
  153. parse("lemon", sym[docheck(int())]);
  154. }
  155. static void
  156. narrow_free_functions_tests()
  157. {
  158. symbols<> sym;
  159. #define add_returned_non_null_value (res!=0)
  160. #define add_returned_null (res==0)
  161. #define find_returned_non_null_value (res!=0)
  162. #define find_returned_null (res==0)
  163. int *res = add(sym,"pineapple");
  164. BOOST_TEST(add_returned_non_null_value);
  165. res = add(sym,"pineapple");
  166. BOOST_TEST(add_returned_null);
  167. res = find(sym, "pineapple");
  168. BOOST_TEST(find_returned_non_null_value);
  169. res = find(sym, "banana");
  170. BOOST_TEST(find_returned_null);
  171. }
  172. static void
  173. wide_match_tests()
  174. {
  175. symbols<int, wchar_t> sym;
  176. sym = L"pineapple", L"orange", L"banana", L"applepie", L"apple";
  177. docheck(sym, L"pineapple", true, L"", 9);
  178. docheck(sym, L"orange", true, L"", 6);
  179. docheck(sym, L"banana", true, L"", 6);
  180. docheck(sym, L"apple", true, L"", 5);
  181. docheck(sym, L"pizza", false, L"pizza", -1);
  182. docheck(sym, L"steak", false, L"steak", -1);
  183. docheck(sym, L"applepie", true, L"", 8);
  184. docheck(sym, L"bananarama", true, L"rama", 6);
  185. docheck(sym, L"applet", true, L"t", 5);
  186. docheck(sym, L"applepi", true, L"pi", 5);
  187. docheck(sym, L"appl", false, L"appl", -1);
  188. docheck(sym, L"pineapplez", true, L"z", 9);
  189. docheck(sym, L"orangez", true, L"z", 6);
  190. docheck(sym, L"bananaz", true, L"z", 6);
  191. docheck(sym, L"applez", true, L"z", 5);
  192. docheck(sym, L"pizzaz", false, L"pizzaz", -1);
  193. docheck(sym, L"steakz", false, L"steakz", -1);
  194. docheck(sym, L"applepiez", true, L"z", 8);
  195. docheck(sym, L"bananaramaz", true, L"ramaz", 6);
  196. docheck(sym, L"appletz", true, L"tz", 5);
  197. docheck(sym, L"applepix", true, L"pix", 5);
  198. }
  199. static void
  200. wide_copy_ctor_tests()
  201. {
  202. symbols<int, wchar_t> sym;
  203. sym = L"pineapple", L"orange", L"banana", L"applepie", L"apple";
  204. symbols<int, wchar_t> sym2(sym);
  205. docheck(sym2, L"pineapple", true, L"", 9);
  206. docheck(sym2, L"pizza", false, L"pizza", -1);
  207. docheck(sym2, L"bananarama", true, L"rama", 6);
  208. }
  209. static void
  210. wide_assigment_operator_tests()
  211. {
  212. symbols<int, wchar_t> sym;
  213. sym = L"pineapple", L"orange", L"banana", L"applepie", L"apple";
  214. symbols<int, wchar_t> sym2;
  215. sym2 = sym;
  216. docheck(sym2, L"pineapple", true, L"", 9);
  217. docheck(sym2, L"pizza", false, L"pizza", -1);
  218. docheck(sym2, L"bananarama", true, L"rama", 6);
  219. }
  220. static void
  221. wide_value_tests()
  222. { // also tests the add member functions
  223. symbols<int, wchar_t> sym;
  224. sym = L"orange", L"banana";
  225. sym.add(L"pineapple",1234);
  226. sym.add(L"lemon");
  227. parse(L"orange", sym[store(12345)]);
  228. parse(L"orange", sym[docheck(12345)]);
  229. parse(L"pineapple", sym[docheck(1234)]);
  230. parse(L"banana", sym[docheck(int())]);
  231. parse(L"lemon", sym[docheck(int())]);
  232. }
  233. static void
  234. wide_free_functions_tests()
  235. {
  236. symbols<int, wchar_t> sym;
  237. int *res = add(sym,L"pineapple");
  238. BOOST_TEST(add_returned_non_null_value);
  239. res = add(sym,L"pineapple");
  240. BOOST_TEST(add_returned_null);
  241. res = find(sym, L"pineapple");
  242. BOOST_TEST(find_returned_non_null_value);
  243. res = find(sym, L"banana");
  244. BOOST_TEST(find_returned_null);
  245. }
  246. static
  247. void free_add_find_functions_tests()
  248. {
  249. symbols<> sym;
  250. BOOST_TEST(*add(sym, "a", 0) == 0);
  251. BOOST_TEST(*add(sym, "a2", 1) == 1);
  252. BOOST_TEST(find(sym, "a2"));
  253. BOOST_TEST(find(sym, "a"));
  254. }
  255. int
  256. main()
  257. {
  258. default_constructible();
  259. narrow_match_tests();
  260. narrow_copy_ctor_tests();
  261. narrow_assigment_operator_tests();
  262. narrow_value_tests();
  263. narrow_free_functions_tests();
  264. wide_match_tests();
  265. wide_copy_ctor_tests();
  266. wide_assigment_operator_tests();
  267. wide_value_tests();
  268. wide_free_functions_tests();
  269. free_add_find_functions_tests();
  270. return boost::report_errors();
  271. }