main.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. *
  3. * Copyright (c) 2004
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. /*
  12. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE main.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: entry point for test program.
  16. */
  17. #include "test.hpp"
  18. #include "test_locale.hpp"
  19. #include <stdarg.h>
  20. #include <iostream>
  21. #include <iomanip>
  22. #ifdef BOOST_HAS_ICU
  23. #include <unicode/uloc.h>
  24. #endif
  25. #ifdef TEST_THREADS
  26. #include <list>
  27. #include <boost/thread.hpp>
  28. #include <boost/thread/tss.hpp>
  29. #include <boost/shared_ptr.hpp>
  30. #include <boost/array.hpp>
  31. int* get_array_data();
  32. #endif
  33. int error_count = 0;
  34. #ifndef TEST_THREADS
  35. #define RUN_TESTS(name) \
  36. std::cout << "Running test case \"" #name "\".\n";\
  37. name();
  38. #else
  39. #define RUN_TESTS(name) \
  40. name();
  41. #endif
  42. void run_tests()
  43. {
  44. RUN_TESTS(basic_tests);
  45. RUN_TESTS(test_simple_repeats);
  46. RUN_TESTS(test_alt);
  47. RUN_TESTS(test_sets);
  48. RUN_TESTS(test_sets2);
  49. RUN_TESTS(test_anchors);
  50. RUN_TESTS(test_backrefs);
  51. RUN_TESTS(test_character_escapes);
  52. RUN_TESTS(test_assertion_escapes);
  53. RUN_TESTS(test_tricky_cases);
  54. RUN_TESTS(test_grep);
  55. RUN_TESTS(test_replace);
  56. RUN_TESTS(test_non_greedy_repeats);
  57. RUN_TESTS(test_non_marking_paren);
  58. RUN_TESTS(test_partial_match);
  59. RUN_TESTS(test_forward_lookahead_asserts);
  60. RUN_TESTS(test_fast_repeats);
  61. RUN_TESTS(test_fast_repeats2);
  62. RUN_TESTS(test_independent_subs);
  63. RUN_TESTS(test_nosubs);
  64. RUN_TESTS(test_conditionals);
  65. RUN_TESTS(test_options);
  66. RUN_TESTS(test_options2);
  67. #ifndef TEST_THREADS
  68. RUN_TESTS(test_en_locale);
  69. #endif
  70. RUN_TESTS(test_emacs);
  71. RUN_TESTS(test_operators);
  72. RUN_TESTS(test_overloads);
  73. RUN_TESTS(test_unicode);
  74. RUN_TESTS(test_pocessive_repeats);
  75. RUN_TESTS(test_mark_resets);
  76. RUN_TESTS(test_recursion);
  77. RUN_TESTS(test_verbs);
  78. }
  79. int cpp_main(int /*argc*/, char * /*argv*/[])
  80. {
  81. #ifdef BOOST_HAS_ICU
  82. //
  83. // We need to set the default locale used by ICU,
  84. // otherwise some of our tests using equivalence classes fail.
  85. //
  86. UErrorCode err = U_ZERO_ERROR;
  87. uloc_setDefault("en", &err);
  88. if(err != U_ZERO_ERROR)
  89. {
  90. std::cerr << "Unable to set the default ICU locale to \"en\"." << std::endl;
  91. return -1;
  92. }
  93. #endif
  94. #ifdef TEST_THREADS
  95. try{
  96. get_array_data(); // initialises data.
  97. }
  98. catch(const std::exception& e)
  99. {
  100. std::cerr << "TSS Initialisation failed with message: " << e.what() << std::endl;
  101. return -1;
  102. }
  103. std::list<boost::shared_ptr<boost::thread> > threads;
  104. for(int i = 0; i < 5; ++i)
  105. {
  106. try{
  107. threads.push_back(boost::shared_ptr<boost::thread>(new boost::thread(&run_tests)));
  108. }
  109. catch(const std::exception& e)
  110. {
  111. std::cerr << "<note>Thread creation failed with message: " << e.what() << "</note>" << std::endl;
  112. }
  113. }
  114. std::list<boost::shared_ptr<boost::thread> >::const_iterator a(threads.begin()), b(threads.end());
  115. while(a != b)
  116. {
  117. (*a)->join();
  118. ++a;
  119. }
  120. #else
  121. run_tests();
  122. #endif
  123. return error_count;
  124. }
  125. #ifdef TEST_THREADS
  126. int* get_array_data()
  127. {
  128. static boost::thread_specific_ptr<boost::array<int, 200> > tp;
  129. if(tp.get() == 0)
  130. tp.reset(new boost::array<int, 200>);
  131. return tp.get()->data();
  132. }
  133. #endif
  134. const int* make_array(int first, ...)
  135. {
  136. //
  137. // this function takes a variable number of arguments
  138. // and packs them into an array that we can pass through
  139. // our testing macros (ideally we would use an array literal
  140. // but these can't apparently be used as macro arguments).
  141. //
  142. #ifdef TEST_THREADS
  143. int* data = get_array_data();
  144. #else
  145. static int data[200];
  146. #endif
  147. std::fill_n(data, 200, -2);
  148. va_list ap;
  149. va_start(ap, first);
  150. //
  151. // keep packing args, until we get two successive -2 values:
  152. //
  153. int terminator_count;
  154. int next_position = 1;
  155. data[0] = first;
  156. if(first == -2)
  157. terminator_count = 1;
  158. else
  159. terminator_count = 0;
  160. while(terminator_count < 2)
  161. {
  162. data[next_position] = va_arg(ap, int);
  163. if(data[next_position] == -2)
  164. ++terminator_count;
  165. else
  166. terminator_count = 0;
  167. ++next_position;
  168. }
  169. va_end(ap);
  170. return data;
  171. }
  172. void test(const char& c, const test_regex_replace_tag& tag)
  173. {
  174. do_test(c, tag);
  175. }
  176. void test(const char& c, const test_regex_search_tag& tag)
  177. {
  178. do_test(c, tag);
  179. }
  180. void test(const char& c, const test_invalid_regex_tag& tag)
  181. {
  182. do_test(c, tag);
  183. }
  184. #ifndef BOOST_NO_WREGEX
  185. void test(const wchar_t& c, const test_regex_replace_tag& tag)
  186. {
  187. do_test(c, tag);
  188. }
  189. void test(const wchar_t& c, const test_regex_search_tag& tag)
  190. {
  191. do_test(c, tag);
  192. }
  193. void test(const wchar_t& c, const test_invalid_regex_tag& tag)
  194. {
  195. do_test(c, tag);
  196. }
  197. #endif
  198. #ifdef BOOST_NO_EXCEPTIONS
  199. namespace boost{
  200. void throw_exception( std::exception const & e )
  201. {
  202. std::cerr << e.what() << std::endl;
  203. std::exit(1);
  204. }
  205. }
  206. int main(int argc, char * argv[])
  207. {
  208. return cpp_main(argc, argv);
  209. }
  210. #else
  211. #include <boost/detail/lightweight_main.hpp>
  212. #endif