| 1 | /* |
|---|
| 2 | * |
|---|
| 3 | * Copyright (c) 2004 |
|---|
| 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 | /* |
|---|
| 13 | * LOCATION: see http://www.boost.org for most recent version. |
|---|
| 14 | * FILE captures_test.cpp |
|---|
| 15 | * VERSION see <boost/version.hpp> |
|---|
| 16 | * DESCRIPTION: Basic tests for additional captures information. |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include <boost/regex.hpp> |
|---|
| 20 | #include <boost/test/test_tools.hpp> |
|---|
| 21 | #include <boost/array.hpp> |
|---|
| 22 | |
|---|
| 23 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
|---|
| 24 | |
|---|
| 25 | #ifdef __sgi |
|---|
| 26 | template <class T> |
|---|
| 27 | void test_captures(const std::string& regx, const std::string& text, const T& expected) |
|---|
| 28 | #else |
|---|
| 29 | template <class T> |
|---|
| 30 | void test_captures(const std::string& regx, const std::string& text, T& expected) |
|---|
| 31 | #endif |
|---|
| 32 | { |
|---|
| 33 | boost::regex e(regx); |
|---|
| 34 | boost::smatch what; |
|---|
| 35 | if(boost::regex_match(text, what, e, boost::match_extra)) |
|---|
| 36 | { |
|---|
| 37 | unsigned i, j; |
|---|
| 38 | #ifndef __sgi |
|---|
| 39 | // strange type deduction causes this test to fail on SGI: |
|---|
| 40 | BOOST_CHECK(what.size() == ARRAY_SIZE(expected)); |
|---|
| 41 | #endif |
|---|
| 42 | for(i = 0; i < what.size(); ++i) |
|---|
| 43 | { |
|---|
| 44 | BOOST_CHECK(what.captures(i).size() <= ARRAY_SIZE(expected[i])); |
|---|
| 45 | for(j = 0; j < what.captures(i).size(); ++j) |
|---|
| 46 | { |
|---|
| 47 | BOOST_CHECK(what.captures(i)[j] == expected[i][j]); |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | int test_main(int , char* []) |
|---|
| 54 | { |
|---|
| 55 | typedef const char* pchar; |
|---|
| 56 | pchar e1[4][5] = |
|---|
| 57 | { |
|---|
| 58 | { "aBBcccDDDDDeeeeeeee", }, |
|---|
| 59 | { "a", "BB", "ccc", "DDDDD", "eeeeeeee", }, |
|---|
| 60 | { "a", "ccc", "eeeeeeee", }, |
|---|
| 61 | { "BB", "DDDDD", }, |
|---|
| 62 | }; |
|---|
| 63 | test_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDDeeeeeeee", e1); |
|---|
| 64 | pchar e2[4][2] = |
|---|
| 65 | { |
|---|
| 66 | { "abd" }, |
|---|
| 67 | { "b", "" }, |
|---|
| 68 | { "" }, |
|---|
| 69 | }; |
|---|
| 70 | test_captures("a(b+|((c)*))+d", "abd", e2); |
|---|
| 71 | pchar e3[3][1] = |
|---|
| 72 | { |
|---|
| 73 | { "abcbar" }, |
|---|
| 74 | { "abc" }, |
|---|
| 75 | }; |
|---|
| 76 | test_captures("(.*)bar|(.*)bah", "abcbar", e3); |
|---|
| 77 | pchar e4[3][1] = |
|---|
| 78 | { |
|---|
| 79 | { "abcbah" }, |
|---|
| 80 | { 0, }, |
|---|
| 81 | { "abc" }, |
|---|
| 82 | }; |
|---|
| 83 | test_captures("(.*)bar|(.*)bah", "abcbah", e4); |
|---|
| 84 | pchar e5[2][16] = |
|---|
| 85 | { |
|---|
| 86 | { "now is the time for all good men to come to the aid of the party" }, |
|---|
| 87 | { "now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the", "party" }, |
|---|
| 88 | }; |
|---|
| 89 | test_captures("^(?:(\\w+)|(?>\\W+))*$", "now is the time for all good men to come to the aid of the party", e5); |
|---|
| 90 | pchar e6[2][16] = |
|---|
| 91 | { |
|---|
| 92 | { "now is the time for all good men to come to the aid of the party" }, |
|---|
| 93 | { "now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the", "party" }, |
|---|
| 94 | }; |
|---|
| 95 | test_captures("^(?>(\\w+)\\W*)*$", "now is the time for all good men to come to the aid of the party", e6); |
|---|
| 96 | pchar e7[4][14] = |
|---|
| 97 | { |
|---|
| 98 | { "now is the time for all good men to come to the aid of the party" }, |
|---|
| 99 | { "now" }, |
|---|
| 100 | { "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the" }, |
|---|
| 101 | { "party" }, |
|---|
| 102 | }; |
|---|
| 103 | test_captures("^(\\w+)\\W+(?>(\\w+)\\W+)*(\\w+)$", "now is the time for all good men to come to the aid of the party", e7); |
|---|
| 104 | pchar e8[5][9] = |
|---|
| 105 | { |
|---|
| 106 | { "now is the time for all good men to come to the aid of the party" } , |
|---|
| 107 | { "now" }, |
|---|
| 108 | { "is", "for", "men", "to", "of" }, |
|---|
| 109 | { "the", "time", "all", "good", "to", "come", "the", "aid", "the" }, |
|---|
| 110 | { "party" }, |
|---|
| 111 | }; |
|---|
| 112 | 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); |
|---|
| 113 | return 0; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | #include <boost/test/included/test_exec_monitor.hpp> |
|---|