Commit 752416554086d5d34323bc14164d5084db83cfbd

Authored by Jay Berkenbilt
1 parent 49f4600d

Add std::regex to c++11 feature tests

Showing 1 changed file with 65 additions and 0 deletions
libtests/cxx11.cc
... ... @@ -6,6 +6,7 @@
6 6 #include <vector>
7 7 #include <map>
8 8 #include <memory>
  9 +#include <regex>
9 10  
10 11 // Functional programming
11 12  
... ... @@ -290,6 +291,69 @@ void do_smart_pointers()
290 291 C::check(0, 0, 0);
291 292 }
292 293  
  294 +// Regular expressions
  295 +
  296 +void do_regex()
  297 +{
  298 + // Basic match/search. Match matches whole string; search searches
  299 + // within string. Use std::smatch for matching std::string and
  300 + // std::cmatch for matching char const*.
  301 +
  302 + std::regex expr1("([0-9]+)");
  303 + std::regex expr2("([0-9]+)\\s*([a-z]+)[[:space:]]*([0-9]+)");
  304 + std::string str1("one 234 fifth 678 nine");
  305 + std::string str2("234 five 678 nine");
  306 + std::string str3("one 234 five 678");
  307 + std::string str4("234");
  308 + std::string str5("234 five 678");
  309 + std::smatch match1;
  310 + assert(! std::regex_match(str1, match1, expr1));
  311 + assert(! std::regex_match(str2, match1, expr1));
  312 + assert(! std::regex_match(str3, match1, expr1));
  313 + assert(std::regex_match(str4, match1, expr1));
  314 + assert(match1[0].first == match1[1].first);
  315 + assert(match1[0].second == match1[1].second);
  316 + std::string s;
  317 + s.assign(match1[1].first, match1[1].second);
  318 + assert("234" == s);
  319 + assert(s == match1[1].str());
  320 + assert(std::regex_match(str5, match1, expr2));
  321 + assert("234 five 678" == match1[0].str());
  322 + assert("234" == match1[1].str());
  323 + assert("five" == match1[2].str());
  324 + assert("678" == match1[3].str());
  325 + assert(std::regex_search(str1, match1, expr2));
  326 + assert("234 fifth 678" == match1[0].str());
  327 + assert("234" == match1[1].str());
  328 + assert("fifth" == match1[2].str());
  329 + assert("678" == match1[3].str());
  330 +
  331 + // Iterator
  332 + std::regex expr3("[[:digit:]]+");
  333 + std::string str6 = "asdf234erasdf9453.kgdl423asdf";
  334 + std::sregex_iterator m1(str6.begin(), str6.end(), expr3);
  335 + std::sregex_iterator m2;
  336 + s.clear();
  337 + for (std::sregex_iterator iter = m1; iter != m2; ++iter)
  338 + {
  339 + std::smatch const& match2 = *iter;
  340 + s += match2[0].str() + "|";
  341 + }
  342 + assert("234|9453|423|" == s);
  343 +
  344 + // Submatches
  345 + std::regex expr4("(?:(asdf)|(qwer))");
  346 + char const* str7 = "0asdf1qwer2";
  347 + std::cregex_iterator m3(str7, str7 + std::strlen(str7), expr4);
  348 + assert("asdf" == (*m3)[0].str());
  349 + assert((*m3)[1].matched);
  350 + assert(! (*m3)[2].matched);
  351 + ++m3;
  352 + assert("qwer" == (*m3)[0].str());
  353 + assert(! (*m3)[1].matched);
  354 + assert((*m3)[2].matched);
  355 +}
  356 +
293 357 int main()
294 358 {
295 359 do_functional();
... ... @@ -298,6 +362,7 @@ int main()
298 362 do_variadic();
299 363 do_default_deleted();
300 364 do_smart_pointers();
  365 + do_regex();
301 366 std::cout << "assertions passed\n";
302 367 return 0;
303 368 }
... ...