chset_tests.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*=============================================================================
  2. Copyright (c) 1998-2003 Joel de Guzman
  3. Copyright (c) 2001-2003 Daniel Nuffer
  4. Copyright (c) 2003 Martin Wille
  5. http://spirit.sourceforge.net/
  6. Use, modification and distribution is subject to the Boost Software
  7. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. http://www.boost.org/LICENSE_1_0.txt)
  9. =============================================================================*/
  10. #include <iostream>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include "impl/sstream.hpp"
  14. #include <boost/spirit/include/classic_chset.hpp>
  15. using namespace std;
  16. using namespace BOOST_SPIRIT_CLASSIC_NS;
  17. namespace
  18. {
  19. ///////////////////////////////////////////////////////////////////////////
  20. //
  21. // chset tests
  22. //
  23. ///////////////////////////////////////////////////////////////////////////
  24. void
  25. DrawRuler(sstream_t& out, char const* str)
  26. {
  27. out << std::endl << std::endl;
  28. out << "\t_____________________________________________________________\n";
  29. out << "\t" << str << std::endl;
  30. out << "\t";
  31. for (char i = '!'; i < '^'; i++)
  32. out << i;
  33. out << "\n";
  34. out << "\t_____________________________________________________________\n\n";
  35. }
  36. //////////////////////////////////
  37. template <typename CharT>
  38. void
  39. Draw(sstream_t& out, chset<CharT> a, char const* str)
  40. {
  41. out << "\t";
  42. for (int i = '!'; i < '^'; i++)
  43. if (a.test(CharT(i)))
  44. out << '*';
  45. else
  46. out << " ";
  47. out << "\t" << str << std::endl;
  48. }
  49. //////////////////////////////////
  50. template <typename CharT>
  51. void
  52. chset_tests(sstream_t& out, CharT const* a_, CharT b1_, CharT b2_, CharT e1_)
  53. {
  54. chset<CharT> a(a_);
  55. range<CharT> b_(b1_, b2_);
  56. chset<CharT> b(b_);
  57. chset<CharT> c(~a); // ~char_parser code must not interfere
  58. // with chset
  59. negated_char_parser<range<CharT> > d_(~b_);
  60. chset<CharT> d(d_);
  61. chlit<CharT> e_(e1_);
  62. chset<CharT> e(e_);
  63. negated_char_parser<chlit<CharT> > f_(e1_);
  64. chset<CharT> f(f_);
  65. DrawRuler(out, "Initial");
  66. Draw(out, a, "a");
  67. Draw(out, b, "b");
  68. Draw(out, d, "d");
  69. Draw(out, e, "e");
  70. Draw(out, f, "f");
  71. DrawRuler(out, "Inverse");
  72. Draw(out, ~a, "~a");
  73. Draw(out, c, "chset<>(~a)");
  74. Draw(out, ~~a, "~~a");
  75. Draw(out, ~b, "~b");
  76. DrawRuler(out, "Union");
  77. Draw(out, a, "a");
  78. Draw(out, b, "b");
  79. Draw(out, d, "d");
  80. Draw(out, e, "e");
  81. Draw(out, f, "f");
  82. Draw(out, a | b, "a | b");
  83. Draw(out, a | b_, "a | b_");
  84. Draw(out, b_ | a, "b_ | a");
  85. Draw(out, a | anychar_p, "a | anychar_p");
  86. Draw(out, b | anychar_p, "b | anychar_p");
  87. Draw(out, a | d, "a | d");
  88. Draw(out, a | d_, "a | d_");
  89. Draw(out, d_ | a, "d_ | a");
  90. Draw(out, a | e_, "a | e_");
  91. Draw(out, e_ | b, "e_ | b");
  92. Draw(out, a | f_, "a | f_");
  93. Draw(out, f_ | b, "f_ | b");
  94. DrawRuler(out, "Intersection");
  95. Draw(out, a, "a");
  96. Draw(out, b, "b");
  97. Draw(out, d, "d");
  98. Draw(out, e, "e");
  99. Draw(out, f, "f");
  100. Draw(out, a & b, "a & b");
  101. Draw(out, a & b_, "a & b_");
  102. Draw(out, b_ & a, "b_ & a");
  103. Draw(out, a & d, "a & d");
  104. Draw(out, a & d_, "a & d_");
  105. Draw(out, d_ & a, "d_ & a");
  106. Draw(out, a & e_, "a & e_");
  107. Draw(out, e_ & b, "e_ & b");
  108. Draw(out, a & f_, "a & f_");
  109. Draw(out, f_ & b, "f_ & b");
  110. Draw(out, a & anychar_p, "a & anychar_p");
  111. Draw(out, b & anychar_p, "b & anychar_p");
  112. DrawRuler(out, "Difference");
  113. Draw(out, a, "a");
  114. Draw(out, b, "b");
  115. Draw(out, d, "d");
  116. Draw(out, e, "e");
  117. Draw(out, f, "f");
  118. Draw(out, a - b, "a - b");
  119. Draw(out, b - a, "b - a");
  120. Draw(out, a - b_, "a - b_");
  121. Draw(out, b_ - a, "b_ - a");
  122. Draw(out, a - d, "a - d");
  123. Draw(out, d - a, "d - a");
  124. Draw(out, a - d_, "a - d_");
  125. Draw(out, d_ - a, "d_ - a");
  126. Draw(out, a - e_, "a - e_");
  127. Draw(out, e_ - b, "e_ - b");
  128. Draw(out, a - f_, "a - f_");
  129. Draw(out, f_ - b, "f_ - b");
  130. Draw(out, a - anychar_p, "a - anychar_p");
  131. Draw(out, anychar_p - a, "anychar_p - a");
  132. Draw(out, b - anychar_p, "b - anychar_p");
  133. Draw(out, anychar_p - b, "anychar_p - b");
  134. DrawRuler(out, "Xor");
  135. Draw(out, a, "a");
  136. Draw(out, b, "b");
  137. Draw(out, d, "d");
  138. Draw(out, e, "e");
  139. Draw(out, f, "f");
  140. Draw(out, a ^ b, "a ^ b");
  141. Draw(out, a ^ b_, "a ^ b_");
  142. Draw(out, b_ ^ a, "b_ ^ a");
  143. Draw(out, a ^ d, "a ^ d");
  144. Draw(out, a ^ d_, "a ^ d_");
  145. Draw(out, d_ ^ a, "d_ ^ a");
  146. Draw(out, a ^ e_, "a ^ e_");
  147. Draw(out, e_ ^ b, "e_ ^ b");
  148. Draw(out, a ^ f_, "a ^ f_");
  149. Draw(out, f_ ^ b, "f_ ^ b");
  150. Draw(out, a ^ nothing_p, "a ^ nothing_p");
  151. Draw(out, a ^ anychar_p, "a ^ anychar_p");
  152. Draw(out, b ^ nothing_p, "b ^ nothing_p");
  153. Draw(out, b ^ anychar_p, "b ^ anychar_p");
  154. }
  155. char const* expected_output =
  156. "\n\n"
  157. "\t_____________________________________________________________\n"
  158. "\tInitial\n"
  159. "\t!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]\n"
  160. "\t_____________________________________________________________\n"
  161. "\n"
  162. "\t ********** ************************** \ta\n"
  163. "\t ********************** \tb\n"
  164. "\t******************** *******************\td\n"
  165. "\t * \te\n"
  166. "\t***************** *******************************************\tf\n"
  167. "\n"
  168. "\n"
  169. "\t_____________________________________________________________\n"
  170. "\tInverse\n"
  171. "\t!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]\n"
  172. "\t_____________________________________________________________\n"
  173. "\n"
  174. "\t*************** ******* ***\t~a\n"
  175. "\t*************** ******* ***\tchset<>(~a)\n"
  176. "\t ********** ************************** \t~~a\n"
  177. "\t******************** *******************\t~b\n"
  178. "\n"
  179. "\n"
  180. "\t_____________________________________________________________\n"
  181. "\tUnion\n"
  182. "\t!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]\n"
  183. "\t_____________________________________________________________\n"
  184. "\n"
  185. "\t ********** ************************** \ta\n"
  186. "\t ********************** \tb\n"
  187. "\t******************** *******************\td\n"
  188. "\t * \te\n"
  189. "\t***************** *******************************************\tf\n"
  190. "\t ******************************************* \ta | b\n"
  191. "\t ******************************************* \ta | b_\n"
  192. "\t ******************************************* \tb_ | a\n"
  193. "\t*************************************************************\ta | anychar_p\n"
  194. "\t*************************************************************\tb | anychar_p\n"
  195. "\t************************* *****************************\ta | d\n"
  196. "\t************************* *****************************\ta | d_\n"
  197. "\t************************* *****************************\td_ | a\n"
  198. "\t ********** ************************** \ta | e_\n"
  199. "\t * ********************** \te_ | b\n"
  200. "\t*************************************************************\ta | f_\n"
  201. "\t***************** *******************************************\tf_ | b\n"
  202. "\n"
  203. "\n"
  204. "\t_____________________________________________________________\n"
  205. "\tIntersection\n"
  206. "\t!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]\n"
  207. "\t_____________________________________________________________\n"
  208. "\n"
  209. "\t ********** ************************** \ta\n"
  210. "\t ********************** \tb\n"
  211. "\t******************** *******************\td\n"
  212. "\t * \te\n"
  213. "\t***************** *******************************************\tf\n"
  214. "\t ***** ********** \ta & b\n"
  215. "\t ***** ********** \ta & b_\n"
  216. "\t ***** ********** \tb_ & a\n"
  217. "\t ***** **************** \ta & d\n"
  218. "\t ***** **************** \ta & d_\n"
  219. "\t ***** **************** \td_ & a\n"
  220. "\t * \ta & e_\n"
  221. "\t \te_ & b\n"
  222. "\t ** ******* ************************** \ta & f_\n"
  223. "\t ********************** \tf_ & b\n"
  224. "\t ********** ************************** \ta & anychar_p\n"
  225. "\t ********************** \tb & anychar_p\n"
  226. "\n"
  227. "\n"
  228. "\t_____________________________________________________________\n"
  229. "\tDifference\n"
  230. "\t!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]\n"
  231. "\t_____________________________________________________________\n"
  232. "\n"
  233. "\t ********** ************************** \ta\n"
  234. "\t ********************** \tb\n"
  235. "\t******************** *******************\td\n"
  236. "\t * \te\n"
  237. "\t***************** *******************************************\tf\n"
  238. "\t ***** **************** \ta - b\n"
  239. "\t ******* \tb - a\n"
  240. "\t ***** **************** \ta - b_\n"
  241. "\t ******* \tb_ - a\n"
  242. "\t ***** ********** \ta - d\n"
  243. "\t*************** ***\td - a\n"
  244. "\t ***** ********** \ta - d_\n"
  245. "\t*************** ***\td_ - a\n"
  246. "\t ** ******* ************************** \ta - e_\n"
  247. "\t * \te_ - b\n"
  248. "\t * \ta - f_\n"
  249. "\t***************** ** *******************\tf_ - b\n"
  250. "\t \ta - anychar_p\n"
  251. "\t*************** ******* ***\tanychar_p - a\n"
  252. "\t \tb - anychar_p\n"
  253. "\t******************** *******************\tanychar_p - b\n"
  254. "\n"
  255. "\n"
  256. "\t_____________________________________________________________\n"
  257. "\tXor\n"
  258. "\t!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]\n"
  259. "\t_____________________________________________________________\n"
  260. "\n"
  261. "\t ********** ************************** \ta\n"
  262. "\t ********************** \tb\n"
  263. "\t******************** *******************\td\n"
  264. "\t * \te\n"
  265. "\t***************** *******************************************\tf\n"
  266. "\t ***** ******* **************** \ta ^ b\n"
  267. "\t ***** ******* **************** \ta ^ b_\n"
  268. "\t ***** ******* **************** \tb_ ^ a\n"
  269. "\t*************** ***** ********** ***\ta ^ d\n"
  270. "\t*************** ***** ********** ***\ta ^ d_\n"
  271. "\t*************** ***** ********** ***\td_ ^ a\n"
  272. "\t ** ******* ************************** \ta ^ e_\n"
  273. "\t * ********************** \te_ ^ b\n"
  274. "\t*************** * ******* ***\ta ^ f_\n"
  275. "\t***************** ** *******************\tf_ ^ b\n"
  276. "\t ********** ************************** \ta ^ nothing_p\n"
  277. "\t*************** ******* ***\ta ^ anychar_p\n"
  278. "\t ********************** \tb ^ nothing_p\n"
  279. "\t******************** *******************\tb ^ anychar_p\n"
  280. ;
  281. void chset_tests()
  282. {
  283. sstream_t tout, aout, bout;
  284. tout << expected_output;
  285. chset_tests(aout, "0-9A-Z", '5', 'J', '2');
  286. chset_tests(bout, L"0-9A-Z", L'5', L'J', L'2');
  287. #define narrow_chset_works (getstring(aout) == getstring(tout))
  288. #define wide_chset_works (getstring(bout) == getstring(tout))
  289. if (!narrow_chset_works || !wide_chset_works)
  290. {
  291. std::cout << "EXPECTED:\n" <<
  292. getstring(tout);
  293. std::cout << "GOT:\n" <<
  294. getstring(aout);
  295. std::cout << "AND:\n" <<
  296. getstring(bout);
  297. }
  298. BOOST_TEST(narrow_chset_works);
  299. BOOST_TEST(wide_chset_works);
  300. }
  301. } // namespace
  302. int
  303. main()
  304. {
  305. chset_tests();
  306. return boost::report_errors();
  307. }