regex_t.cc
Go to the documentation of this file.
1 #ifdef STANDALONE_TEST
2 #include <cassert>
3 #define BOOST_TEST_REQUIRE assert
4 #define BOOST_TEST assert
5 #else
6 #define BOOST_TEST_MODULE (regex test)
7 #include "boost/test/unit_test.hpp"
9 #endif
10 
11 #include <fstream>
12 #include <iostream>
13 #include <regex>
14 #include <string>
15 #include <vector>
16 
17 #ifdef STANDALONE_TEST
18 int
19 main()
20 {
21 #else
22 BOOST_AUTO_TEST_SUITE(RegexTests)
23 
24 BOOST_AUTO_TEST_CASE(pluginRegex)
25 #endif
26  {
27  std::ifstream inFile("regex.txt");
28  BOOST_TEST_REQUIRE(static_cast<bool>(inFile));
29 #ifdef STANDALONE_TEST
30  std::string const pattern_stem = {"(?:[A-Za-z0-9\\-]*_)*[A-Za-z0-9]+_"};
31 #else
32  cet::LibraryManager dummy("noplugin");
33  std::string const pattern_stem = dummy.patternStem();
34 #endif
36  std::string("lib(") + pattern_stem + ")([A-Za-z0-9]+)\\.so";
37  for (std::string line; std::getline(inFile, line);) {
38  static std::regex const r(pattern);
39  std::smatch subs;
40  bool result = std::regex_match(line, subs, r);
41  std::cout << line << " match: " << std::boolalpha << result
42  << (result ?
43  (std::string(" (stem ") +
44  subs[1].str().substr(0, subs[1].str().size() - 1) +
45  ", type " + subs[2].str() + ")") :
46  "")
47  << std::endl;
48  BOOST_TEST(result);
49  }
50  }
51 
52 #ifndef STANDALONE_TEST
53  BOOST_AUTO_TEST_CASE(alternationCheck_subscript)
54 #endif
55  {
56  std::regex const pattern1{R"(.*\[(?:\d*[1-9]|\d+0)\])"};
57  std::regex const pattern2{R"(.*\[(?:\d+0|\d*[1-9])\])"};
58 
59  std::vector<std::string> const tests{"test[0]", "test[1]", "test[10]"};
60 
61  auto verify_match = [&pattern1, &pattern2](std::string const& test) {
62  return std::regex_match(test, pattern1) ==
63  std::regex_match(test, pattern2);
64  };
65 
66  for (auto const& test : tests)
67  BOOST_TEST(verify_match(test));
68  }
69 
70 #ifndef STANDALONE_TEST
71  BOOST_AUTO_TEST_CASE(alternationCheck_dot)
72 #endif
73  {
74  std::regex const pattern1{R"(.*\.(?:\d*[1-9]|\d+0))"};
75  std::regex const pattern2{R"(.*\.(?:\d+0|\d*[1-9]))"};
76 
77  std::vector<std::string> const tests{
78  "test.0",
79  "test.1",
80  "test.10" // Using pattern1 vs. pattern2 gives different result w/
81  // clang 3.6
82  };
83 
84  auto verify_match = [&pattern1, &pattern2](std::string const& test) {
85  return std::regex_match(test, pattern1) ==
86  std::regex_match(test, pattern2);
87  };
88 
89  for (auto const& test : tests)
90  BOOST_TEST(verify_match(test));
91  }
92 
93 #ifdef STANDALONE_TEST
94 }
95 #else
96 BOOST_AUTO_TEST_SUITE_END()
97 #endif
std::regex const pattern2
Definition: regex_t.cc:57
static QCString result
std::string string
Definition: nybbler.cc:12
BOOST_TEST_REQUIRE(static_cast< bool >(inFile))
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
std::string patternStem() const
BOOST_AUTO_TEST_CASE(SourceListWithComments)
TFile * inFile
Definition: makeDST.cxx:36
std::string pattern
Definition: regex_t.cc:35
void line(double t, double *p, double &x, double &y, double &z)
std::string const pattern_stem
Definition: regex_t.cc:33
cet::LibraryManager dummy("noplugin")
auto verify_match
Definition: regex_t.cc:61
static QCString str
QTextStream & endl(QTextStream &s)