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