1 /*
2  *
3  * Copyright (c) 2009
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 #include <boost/regex.hpp>
13 #include <boost/detail/lightweight_main.hpp>
14 #include "../test_macros.hpp"
15 
16 #ifdef BOOST_INTEL
17 #pragma warning(disable:1418 981 983 383)
18 #endif
19 
20 template <class charT>
test_named_subexpressions(charT)21 void test_named_subexpressions(charT)
22 {
23    //
24    // Really this is just a test that the overloaded access functions work correctly:
25    //
26    static const charT e[] =
27    {
28       '(', '?', '\'', 'o', 'n', 'e', '\'', 'a', '+', ')', '(', '?', '<', 't', 'w', 'o', '>', 'b', '+', ')', '\0'
29    };
30    static const charT t[] =
31    {
32       'm', 'm', 'a', 'a', 'a', 'b', 'b', 'n', 'n', '\0'
33    };
34    static const charT one[] =
35    {
36       'o', 'n', 'e', '\0'
37    };
38    static const charT two[] =
39    {
40       't', 'w', 'o', '\0'
41    };
42    static const std::basic_string<charT> s_one(one);
43    static const std::basic_string<charT> s_two(two);
44    static const charT result1[] = { 'a', 'a', 'a', '\0' };
45    static const charT result2[] = { 'b', 'b', '\0' };
46    static const std::basic_string<charT> s_result1(result1);
47    static const std::basic_string<charT> s_result2(result2);
48 
49    static const char* c_one = "one";
50    static const char* c_two = "two";
51    static const std::string cs_one(c_one);
52    static const std::string cs_two(c_two);
53 
54    boost::basic_regex<charT> expression(e);
55    boost::match_results<const charT*> what;
56    if(regex_search(t, what, expression))
57    {
58       BOOST_CHECK(what.length(1) == 3);
59       BOOST_CHECK(what.length(one) == 3);
60       BOOST_CHECK(what.length(s_one) == 3);
61       BOOST_CHECK(what.length(c_one) == 3);
62       BOOST_CHECK(what.length(cs_one) == 3);
63       BOOST_CHECK(what.position(1) == 2);
64       BOOST_CHECK(what.position(one) == 2);
65       BOOST_CHECK(what.position(s_one) == 2);
66       BOOST_CHECK(what.position(c_one) == 2);
67       BOOST_CHECK(what.position(cs_one) == 2);
68       BOOST_CHECK(what.str(1) == s_result1);
69       BOOST_CHECK(what.str(one) == s_result1);
70       BOOST_CHECK(what.str(s_one) == s_result1);
71       BOOST_CHECK(what.str(c_one) == s_result1);
72       BOOST_CHECK(what.str(cs_one) == s_result1);
73       BOOST_CHECK(what[1] == s_result1);
74       BOOST_CHECK(what[one] == s_result1);
75       BOOST_CHECK(what[s_one] == s_result1);
76       BOOST_CHECK(what[c_one] == s_result1);
77       BOOST_CHECK(what[cs_one] == s_result1);
78 
79       BOOST_CHECK(what.length(2) == 2);
80       BOOST_CHECK(what.length(two) == 2);
81       BOOST_CHECK(what.length(s_two) == 2);
82       BOOST_CHECK(what.length(c_two) == 2);
83       BOOST_CHECK(what.length(cs_two) == 2);
84       BOOST_CHECK(what.position(2) == 5);
85       BOOST_CHECK(what.position(two) == 5);
86       BOOST_CHECK(what.position(s_two) == 5);
87       BOOST_CHECK(what.position(c_two) == 5);
88       BOOST_CHECK(what.position(cs_two) == 5);
89       BOOST_CHECK(what.str(2) == s_result2);
90       BOOST_CHECK(what.str(two) == s_result2);
91       BOOST_CHECK(what.str(s_two) == s_result2);
92       BOOST_CHECK(what.str(c_two) == s_result2);
93       BOOST_CHECK(what.str(cs_two) == s_result2);
94       BOOST_CHECK(what[2] == s_result2);
95       BOOST_CHECK(what[two] == s_result2);
96       BOOST_CHECK(what[s_two] == s_result2);
97       BOOST_CHECK(what[c_two] == s_result2);
98       BOOST_CHECK(what[cs_two] == s_result2);
99    }
100    else
101    {
102       BOOST_ERROR("Expected match not found");
103    }
104 }
105 
cpp_main(int,char * [])106 int cpp_main( int , char* [] )
107 {
108    test_named_subexpressions(char(0));
109 #if !defined(BOOST_NO_WREGEX)
110    test_named_subexpressions(wchar_t(0));
111 #endif
112    return 0;
113 }
114 
115