appkit  1.5.1
RoadNarrows Robotics Application Kit
utRegEx.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: appkit
4 //
5 // Program: utRegEx
6 //
7 // File: utRegEx.cxx
8 //
9 /*! \file
10  *
11  * \brief Unit test RegEx class.
12  *
13  * \author Robin Knight (robin.knight@roadnarrows.com)
14  *
15  * \par Copyright
16  * \h_copy 2017-2017. RoadNarrows LLC.\n
17  * http://www.roadnarrows.com\n
18  * All Rights Reserved
19  */
20 /*
21  * @EulaBegin@
22  * @EulaEnd@
23  */
24 ////////////////////////////////////////////////////////////////////////////////
25 
26 #include <unistd.h>
27 #include <termios.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 
33 #include <iostream>
34 #include <fstream>
35 #include <string>
36 
37 #include "rnr/rnrconfig.h"
38 #include "rnr/log.h"
39 #include "rnr/opts.h"
40 #include "rnr/pkg.h"
41 
42 #include "rnr/appkit/RegEx.h"
43 
44 #include "version.h"
45 
46 using namespace std;
47 using namespace rnr;
48 
49 /*!
50  * \ingroup apps
51  * \defgroup unittest utRegEx
52  * \{
53  */
54 
55 #define APP_EC_OK 0 ///< success exit code
56 #define APP_EC_ARGS 2 ///< command-line options/arguments error exit code
57 #define APP_EC_EXEC 4 ///< execution exit code
58 
59 static char *Argv0; ///< the command
60 static double OptsHz = 2; ///< thread hertz rate
61 
62 /*!
63  * \brief Program information.
64  */
65 static OptsPgmInfo_T PgmInfo =
66 {
67  // usage_args
68  NULL,
69 
70  // synopsis
71  "Unit test librnr_appkit RegEx class.",
72 
73  // long_desc =
74  "The %P command unit tests the librnr_appkit RegEx class operation.",
75 
76  // diagnostics
77  NULL
78 };
79 
80 /*!
81  * \brief Command line options information.
82  */
83 static OptsInfo_T OptsInfo[] =
84 {
85  {NULL, }
86 };
87 
88 /*!
89  * \brief Globally constructed, pre-compiled re's.
90  */
91 const RegEx re0;
92 const RegEx re1("^[a-z]*$", RegEx::ReFlagICase);
93 RegEx re2 = re1;
94 const RegEx re3("['\"]{1,2}([0-9]+)(hello)(^)");
95 const RegEx re4("^[a-z]*$");
96 RegEx reGo = "([cC]at)|([dD]og)";
97 
98 static string getline(istream &is)
99 {
100  char buf[256];
101  char *s;
102 
103  is.getline(buf, sizeof(buf));
104 
105  for(s = buf; *s && isspace(*s); ++s);
106 
107  return string(s);
108 }
109 
110 /*!
111  * \brief Print RegEx data.
112  *
113  * \param re RegEx object.
114  */
115 static void utPr(ostream &os, const string name, const RegEx &re)
116 {
117  os << " ... RE " << name << endl;
118 
119  os << "getRegEx() " << re.getRegEx() << endl;
120  os << "isValid() " << re.isValid() << endl;
121  os << "getFlags() " << re.getFlags() << endl;
122  os << "getReturnCode() " << re.getReturnCode() << endl;
123  os << "getErrorStr() " << re.getErrorStr() << endl;
124  os << "operator<<() " << re << endl;
125 }
126 
127 /*!
128  * \brief Print RegEx data.
129  *
130  * \param re RegEx object.
131  */
132 static void utConstRe(ostream &os, const RegEx &re)
133 {
134  const char *testinputs[] =
135  {
136  "abcd",
137  "ABCD",
138  "wXyZ",
139  "12",
140  "jkl mno",
141  NULL
142  };
143 
144  os << " Test Constant RegEx:" << endl;
145 
146  utPr(os, "sut", re);
147 
148  for(size_t i = 0; testinputs[i] != NULL; ++i)
149  {
150  string input(testinputs[i]);
151 
152  os << input << " --> ";
153 
154  if( re.match(input) )
155  {
156  os << "match";
157  }
158  else
159  {
160  os << "no match";
161  }
162  os << endl;
163  }
164 }
165 
166 /*!
167  * \brief Test RegEx operations.
168  */
169 static void utRun(ostream &os)
170 {
171  string cmd, arg;
172  RegEx::ReMatchVec matches;
173 
174  os << " Test Run-Time Operations:" << endl;
175  os << "q - Quit." << endl;
176  os << "n <re> - Specify new re." << endl;
177  os << "m <input> - Match input to re." << endl;
178  os << "a <input> - Match all input substrings to re." << endl;
179  os << "p - Print re." << endl;
180  os << endl;
181 
182  while( true )
183  {
184  if( cin.fail() )
185  {
186  cin.clear();
187  cin.ignore(INT_MAX, '\n'); // awkward flush
188  }
189 
190  os << "cmd> ";
191  cin >> cmd;
192 
193 
194  if( cmd == "q" )
195  {
196  return;
197  }
198  else if( cmd == "n" )
199  {
200  cin >> reGo;
201  if( cin.fail() )
202  {
203  os << "bad input" << endl;
204  }
205  else if( !reGo.isValid() )
206  {
207  os << "bad re: " << reGo.getErrorStr() << endl;
208  }
209  else
210  {
211  os << "new valid re" << endl;
212  }
213  }
214  else if( cmd == "m" )
215  {
216  arg = getline(cin);
217 
218  os << "try match on '" << arg << "' --> ";
219  if( reGo.match(arg) )
220  {
221  os << "match" << endl;
222  }
223  else
224  {
225  os << "no match" << endl;
226  }
227  }
228  else if( cmd == "a" )
229  {
230  arg = getline(cin);
231 
232  os << "try match all on '" << arg << "'" << endl;
233 
234  reGo.match(arg, matches);
235 
236  for(size_t i = 0; i < matches.size(); ++i)
237  {
238  os << i << ". (" << matches[i].m_uStart << ","
239  << matches[i].m_uEnd << ") '"
240  << matches[i].m_strMatch << "'" << endl;
241  }
242  }
243  else if( cmd == "p" )
244  {
245  utPr(os, "Go", reGo);
246  }
247  else
248  {
249  os << "'" << cmd << "': Bad command. One of: a m n p q" << endl;
250  }
251  }
252 }
253 
254 /*!
255  * \brief Main initialization.
256  *
257  * \param argc Command-line argument count.
258  * \param argv Command-line argument list.
259  *
260  * \par Exits:
261  * Program terminates on conversion error.
262  */
263 static void mainInit(int argc, char *argv[])
264 {
265  // name of this process
266  Argv0 = basename(argv[0]);
267 
268  // parse input options
269  argv = OptsGet(Argv0, &PkgInfo, &PgmInfo, OptsInfo, true, &argc, argv);
270 }
271 
272 /*!
273  * \brief Main.
274  *
275  * \param argc Command-line argument count.
276  * \param argv Command-line argument list.
277  *
278  * \return Returns 0 on succes, non-zero on failure.
279  */
280 int main(int argc, char* argv[])
281 {
282  mainInit(argc, argv);
283 
284  //
285  // Test pre-compiled constructors
286  //
287  cout << " Test Pre-Compiled:" << endl;
288  utPr(cout, "0", re0);
289  utPr(cout, "1", re1);
290  utPr(cout, "copy of 1", re2);
291  utPr(cout, "3", re3);
292  utPr(cout, "4", re4);
293  utPr(cout, "Go", reGo);
294  cout << endl;
295 
296  // Constant regex match
297  utConstRe(cout, re1);
298  utConstRe(cout, re4);
299  cout << endl;
300 
301  //
302  // Test run-time
303  //
304  utRun(cout);
305 
306  return APP_EC_OK;
307 }
308 
309 /*!
310  * \}
311  */
std::vector< ReMatch > ReMatchVec
vector of matches
Definition: RegEx.h:132
static void utPr(ostream &os, const string name, const RegEx &re)
Print RegEx data.
Definition: utRegEx.cxx:115
static char * Argv0
the command
Definition: utRegEx.cxx:59
int getReturnCode() const
Get the extened return code from the last RegEx operation.
Definition: RegEx.h:391
const std::string & getRegEx() const
Get the pre-compiled regular expression.
Definition: RegEx.h:342
Regular Express Class.
Definition: RegEx.h:84
static double OptsHz
thread hertz rate
Definition: utRegEx.cxx:60
#define APP_EC_OK
success exit code
Definition: utRegEx.cxx:55
static OptsInfo_T OptsInfo[]
Command line options information.
Definition: utRegEx.cxx:83
static void utRun(ostream &os)
Test RegEx operations.
Definition: utRegEx.cxx:169
static const PkgInfo_T PkgInfo
Definition: version.h:45
const std::string & getErrorStr() const
Get the last RegExs operation error string.
Definition: RegEx.h:401
static void mainInit(int argc, char *argv[])
Main initialization.
Definition: utRegEx.cxx:263
Package version information.
bool match(const std::string &strInput, const int nFlags=ReFlagDefaults)
Match the input string against the regular expression.
static void utConstRe(ostream &os, const RegEx &re)
Print RegEx data.
Definition: utRegEx.cxx:132
int main(int argc, char *argv[])
Main.
Definition: utLogBook.cxx:774
int getFlags() const
Get compile behavior flags.
Definition: RegEx.h:364
The Regular Expression Class interface.
RoadNarrows Robotics.
Definition: Camera.h:74
bool isValid() const
Test if in a valid state (i.e. compiled).
Definition: RegEx.h:354
static OptsPgmInfo_T PgmInfo
Program information.
Definition: utRegEx.cxx:65
const RegEx re0
Globally constructed, pre-compiled re&#39;s.
Definition: utRegEx.cxx:91