File: captures_test.cpp

package info (click to toggle)
boost1.62 1.62.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 686,420 kB
  • sloc: cpp: 2,609,004; xml: 972,558; ansic: 53,674; python: 32,437; sh: 8,829; asm: 3,071; cs: 2,121; makefile: 964; perl: 859; yacc: 472; php: 132; ruby: 94; f90: 55; sql: 13; csh: 6
file content (177 lines) | stat: -rw-r--r-- 5,246 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 *
 * Copyright (c) 2004
 * John Maddock
 *
 * Use, modification and distribution are subject to the
 * Boost Software License, Version 1.0. (See accompanying file
 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 *
 */

 /*
  *   LOCATION:    see http://www.boost.org for most recent version.
  *   FILE         captures_test.cpp
  *   VERSION      see <boost/version.hpp>
  *   DESCRIPTION: Basic tests for additional captures information.
  */

#include <boost/regex.hpp>
#include <boost/detail/lightweight_main.hpp>
#include "../test_macros.hpp"
#include <boost/array.hpp>
#include <cstring>

#ifdef BOOST_HAS_ICU
#include <boost/regex/icu.hpp>
#endif

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))

template <int N>
int array_size(const char* (&p)[N])
{
   for(int i = 0; i < N; ++i)
      if(p[i] == 0)
         return i;
   return N;
}

std::wstring make_wstring(const char* p)
{
   return std::wstring(p, p + std::strlen(p));
}

#ifdef __sgi
template <class T>
void test_captures(const std::string& regx, const std::string& text, const T& expected)
#else
template <class T>
void test_captures(const std::string& regx, const std::string& text, T& expected)
#endif
{
   boost::regex e(regx);
   boost::smatch what;
   if(boost::regex_match(text, what, e, boost::match_extra))
   {
      unsigned i, j;
#ifndef __sgi
      // strange type deduction causes this test to fail on SGI:
      BOOST_CHECK(what.size() == ARRAY_SIZE(expected));
#endif
      for(i = 0; i < what.size(); ++i)
      {
         BOOST_CHECK(what.captures(i).size() == array_size(expected[i]));
         for(j = 0; j < what.captures(i).size(); ++j)
         {
            BOOST_CHECK(what.captures(i)[j] == expected[i][j]);
         }
      }
   }

   std::wstring wre(regx.begin(), regx.end());
   std::wstring wtext(text.begin(), text.end());
   boost::wregex we(wre);
   boost::wsmatch wwhat;
   if(boost::regex_match(wtext, wwhat, we, boost::match_extra))
   {
      unsigned i, j;
#ifndef __sgi
      // strange type deduction causes this test to fail on SGI:
      BOOST_CHECK(wwhat.size() == ARRAY_SIZE(expected));
#endif
      for(i = 0; i < wwhat.size(); ++i)
      {
         BOOST_CHECK(wwhat.captures(i).size() == array_size(expected[i]));
         for(j = 0; j < wwhat.captures(i).size(); ++j)
         {
            BOOST_CHECK(wwhat.captures(i)[j] == make_wstring(expected[i][j]));
         }
      }
   }

#ifdef BOOST_HAS_ICU
   boost::u32regex ure = boost::make_u32regex(regx);
   what = boost::smatch();
   if(boost::u32regex_match(text, what, ure, boost::match_extra))
   {
      unsigned i, j;
#ifndef __sgi
      // strange type deduction causes this test to fail on SGI:
      BOOST_CHECK(what.size() == ARRAY_SIZE(expected));
#endif
      for(i = 0; i < what.size(); ++i)
      {
         BOOST_CHECK(what.captures(i).size() == array_size(expected[i]));
         for(j = 0; j < what.captures(i).size(); ++j)
         {
            BOOST_CHECK(what.captures(i)[j] == expected[i][j]);
         }
      }
   }
#endif
}

int cpp_main(int , char* [])
{
   typedef const char* pchar;
   pchar e1[4][5] = 
   {
      { "aBBcccDDDDDeeeeeeee", },
      { "a", "BB", "ccc", "DDDDD", "eeeeeeee", },
      { "a", "ccc", "eeeeeeee", },
      { "BB", "DDDDD", },
   };
   test_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDDeeeeeeee", e1);
   pchar e2[4][2] = 
   {
      { "abd" },
      { "b", "" },
      { "" },
   };
   test_captures("a(b+|((c)*))+d", "abd", e2);
   pchar e3[3][1] = 
   {
      { "abcbar" },
      { "abc" },
   };
   test_captures("(.*)bar|(.*)bah", "abcbar", e3);
   pchar e4[3][1] = 
   {
      { "abcbah" },
      { 0, },
      { "abc" },
   };
   test_captures("(.*)bar|(.*)bah", "abcbah", e4);
   pchar e5[2][16] = 
   {
      { "now is the time for all good men to come to the aid of the party" },
      { "now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the", "party" },
   };
   test_captures("^(?:(\\w+)|(?>\\W+))*$", "now is the time for all good men to come to the aid of the party", e5);
   pchar e6[2][16] = 
   {
      { "now is the time for all good men to come to the aid of the party" },
      { "now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the", "party" },
   };
   test_captures("^(?>(\\w+)\\W*)*$", "now is the time for all good men to come to the aid of the party", e6);
   pchar e7[4][14] = 
   {
      { "now is the time for all good men to come to the aid of the party" },
      { "now" },
      { "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the" },
      { "party" },
   };
   test_captures("^(\\w+)\\W+(?>(\\w+)\\W+)*(\\w+)$", "now is the time for all good men to come to the aid of the party", e7);
   pchar e8[5][9] = 
   {
      { "now is the time for all good men to come to the aid of the party" } ,
      { "now" },
      { "is", "for", "men", "to", "of" },
      { "the", "time", "all", "good", "to", "come", "the", "aid", "the" },
      { "party" },
   };
   test_captures("^(\\w+)\\W+(?>(\\w+)\\W+(?:(\\w+)\\W+){0,2})*(\\w+)$", "now is the time for all good men to come to the aid of the party", e8);
   return 0;
}