| 1 |
// { dg-do run { target c++11 } } |
| 2 |
// { dg-timeout-factor 2 } |
| 3 |
|
| 4 |
// |
| 5 |
// 2013-08-01 Tim Shen <[email protected]> |
| 6 |
// |
| 7 |
// Copyright (C) 2013-2025 Free Software Foundation, Inc. |
| 8 |
// |
| 9 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 10 |
// software; you can redistribute it and/or modify it under the |
| 11 |
// terms of the GNU General Public License as published by the |
| 12 |
// Free Software Foundation; either version 3, or (at your option) |
| 13 |
// any later version. |
| 14 |
// |
| 15 |
// This library is distributed in the hope that it will be useful, |
| 16 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
// GNU General Public License for more details. |
| 19 |
// |
| 20 |
// You should have received a copy of the GNU General Public License along |
| 21 |
// with this library; see the file COPYING3. If not see |
| 22 |
// <http://www.gnu.org/licenses/>. |
| 23 |
|
| 24 |
// 28.11.2 regex_match |
| 25 |
// Tests Extended bracket expression against a C-string. |
| 26 |
|
| 27 |
#include <regex> |
| 28 |
#include <testsuite_hooks.h> |
| 29 |
#include <testsuite_regex.h> |
| 30 |
|
| 31 |
using namespace __gnu_test; |
| 32 |
using namespace std; |
| 33 |
|
| 34 |
void |
| 35 |
test01() |
| 36 |
{ |
| 37 |
{ |
| 38 |
std::regex re("pre/[za-x]", std::regex::extended); |
| 39 |
VERIFY( regex_match_debug("pre/z", re) ); |
| 40 |
VERIFY( regex_match_debug("pre/a", re) ); |
| 41 |
VERIFY( !regex_match_debug("pre/y", re) ); |
| 42 |
} |
| 43 |
{ |
| 44 |
std::regex re("pre/[[:uPPer:]]", std::regex::extended); |
| 45 |
VERIFY( regex_match_debug("pre/Z", re) ); |
| 46 |
VERIFY( !regex_match_debug("pre/_", re) ); |
| 47 |
VERIFY( !regex_match_debug("pre/a", re) ); |
| 48 |
VERIFY( !regex_match_debug("pre/0", re) ); |
| 49 |
} |
| 50 |
{ |
| 51 |
std::regex re("pre/[[:lOWer:]]", std::regex::extended | std::regex::icase); |
| 52 |
VERIFY( regex_match_debug("pre/Z", re) ); |
| 53 |
VERIFY( regex_match_debug("pre/a", re) ); |
| 54 |
} |
| 55 |
{ |
| 56 |
std::regex re("pre/[[:w:][.tilde.]]", std::regex::extended); |
| 57 |
VERIFY( regex_match_debug("pre/~", re) ); |
| 58 |
VERIFY( regex_match_debug("pre/_", re) ); |
| 59 |
VERIFY( regex_match_debug("pre/a", re) ); |
| 60 |
VERIFY( regex_match_debug("pre/0", re) ); |
| 61 |
} |
| 62 |
{ |
| 63 |
std::regex re("pre/[[=a=]]", std::regex::extended); |
| 64 |
VERIFY( regex_match_debug("pre/a", re) ); |
| 65 |
VERIFY( regex_match_debug("pre/A", re) ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
void |
| 70 |
test02() |
| 71 |
{ |
| 72 |
VERIFY(regex_match("-", regex("[-]", regex_constants::ECMAScript))); |
| 73 |
VERIFY(regex_match("-", regex("[--]", regex_constants::ECMAScript))); |
| 74 |
VERIFY(regex_match("-", regex("[---]", regex_constants::ECMAScript))); |
| 75 |
VERIFY(regex_match("-", regex("[----]", regex_constants::ECMAScript))); |
| 76 |
VERIFY(regex_match("-", regex("[-----]", regex_constants::ECMAScript))); |
| 77 |
|
| 78 |
VERIFY(regex_match("-", regex("[-]", regex_constants::extended))); |
| 79 |
VERIFY(regex_match("-", regex("[--]", regex_constants::extended))); |
| 80 |
VERIFY(regex_match("-", regex("[---]", regex_constants::extended))); |
| 81 |
VERIFY(regex_match("-", regex("[----]", regex_constants::extended))); |
| 82 |
try |
| 83 |
{ |
| 84 |
std::regex re("[-----]", std::regex::extended); |
| 85 |
VERIFY(false); |
| 86 |
} |
| 87 |
catch (const std::regex_error& e) |
| 88 |
{ |
| 89 |
VERIFY(e.code() == std::regex_constants::error_range); |
| 90 |
} |
| 91 |
|
| 92 |
VERIFY(!regex_match("b", regex("[-ac]", regex_constants::extended))); |
| 93 |
VERIFY(!regex_match("b", regex("[ac-]", regex_constants::extended))); |
| 94 |
VERIFY(regex_match("b", regex("[^-ac]", regex_constants::extended))); |
| 95 |
VERIFY(regex_match("b", regex("[^ac-]", regex_constants::extended))); |
| 96 |
VERIFY(regex_match("&", regex("[%--]", regex_constants::extended))); |
| 97 |
VERIFY(regex_match(".", regex("[--@]", regex_constants::extended))); |
| 98 |
try |
| 99 |
{ |
| 100 |
regex("[a--@]", regex_constants::extended); |
| 101 |
VERIFY(false); |
| 102 |
} |
| 103 |
catch (const std::regex_error& e) |
| 104 |
{ |
| 105 |
VERIFY(e.code() == std::regex_constants::error_range); |
| 106 |
} |
| 107 |
try |
| 108 |
{ |
| 109 |
regex("[@--]", regex_constants::extended); |
| 110 |
VERIFY(false); |
| 111 |
} |
| 112 |
catch (const std::regex_error& e) |
| 113 |
{ |
| 114 |
VERIFY(e.code() == std::regex_constants::error_range); |
| 115 |
} |
| 116 |
try |
| 117 |
{ |
| 118 |
regex("[--%]", regex_constants::extended); |
| 119 |
VERIFY(false); |
| 120 |
} |
| 121 |
catch (const std::regex_error& e) |
| 122 |
{ |
| 123 |
VERIFY(e.code() == std::regex_constants::error_range); |
| 124 |
} |
| 125 |
|
| 126 |
VERIFY(regex_match("].", regex("[][.hyphen.]-0]*", regex_constants::extended))); |
| 127 |
} |
| 128 |
|
| 129 |
void |
| 130 |
test03() |
| 131 |
{ |
| 132 |
try |
| 133 |
{ |
| 134 |
std::regex re("[z-a]", std::regex::extended); |
| 135 |
VERIFY(false); |
| 136 |
} |
| 137 |
catch (const std::regex_error& e) |
| 138 |
{ |
| 139 |
VERIFY(e.code() == std::regex_constants::error_range); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
void |
| 144 |
test04() |
| 145 |
{ |
| 146 |
std::regex re("[-0-9a-z]"); |
| 147 |
VERIFY(regex_match_debug("-", re)); |
| 148 |
VERIFY(regex_match_debug("1", re)); |
| 149 |
VERIFY(regex_match_debug("w", re)); |
| 150 |
re.assign("[-0-9a-z]", regex_constants::basic); |
| 151 |
VERIFY(regex_match_debug("-", re)); |
| 152 |
VERIFY(regex_match_debug("1", re)); |
| 153 |
VERIFY(regex_match_debug("w", re)); |
| 154 |
} |
| 155 |
|
| 156 |
// libstdc++/67015 |
| 157 |
void |
| 158 |
test05() |
| 159 |
{ |
| 160 |
regex lanana_namespace("^[a-z0-9]+$", regex::extended); |
| 161 |
regex lsb_namespace("^_?([a-z0-9_.]+-, regex::extended)+[a-z0-9]+$"); |
| 162 |
regex debian_dpkg_conffile_cruft("dpkg-(old|dist|new|tmp, regex::extended)$"); |
| 163 |
regex debian_cron_namespace("^[a-z0-9][a-z0-9-]*$", regex::extended); |
| 164 |
VERIFY(regex_match("test", debian_cron_namespace)); |
| 165 |
VERIFY(!regex_match("-a", debian_cron_namespace)); |
| 166 |
VERIFY(regex_match("a-", debian_cron_namespace)); |
| 167 |
regex debian_cron_namespace_ok("^[a-z0-9][-a-z0-9]*$", regex::extended); |
| 168 |
VERIFY(regex_match("test", debian_cron_namespace_ok)); |
| 169 |
VERIFY(!regex_match("-a", debian_cron_namespace_ok)); |
| 170 |
VERIFY(regex_match("a-", debian_cron_namespace_ok)); |
| 171 |
} |
| 172 |
|
| 173 |
// libstdc++/67015 |
| 174 |
void |
| 175 |
test06() |
| 176 |
{ |
| 177 |
regex lanana_namespace("^[a-z0-9]+$"); |
| 178 |
regex lsb_namespace("^_?([a-z0-9_.]+-)+[a-z0-9]+$"); |
| 179 |
regex debian_dpkg_conffile_cruft("dpkg-(old|dist|new|tmp)$"); |
| 180 |
regex debian_cron_namespace("^[a-z0-9][a-z0-9-]*$"); |
| 181 |
VERIFY(regex_match("test", debian_cron_namespace)); |
| 182 |
VERIFY(!regex_match("-a", debian_cron_namespace)); |
| 183 |
VERIFY(regex_match("a-", debian_cron_namespace)); |
| 184 |
regex debian_cron_namespace_ok("^[a-z0-9][-a-z0-9]*$"); |
| 185 |
VERIFY(regex_match("test", debian_cron_namespace_ok)); |
| 186 |
VERIFY(!regex_match("-a", debian_cron_namespace_ok)); |
| 187 |
VERIFY(regex_match("a-", debian_cron_namespace_ok)); |
| 188 |
} |
| 189 |
|
| 190 |
// libstdc++/102447 |
| 191 |
void |
| 192 |
test07() |
| 193 |
{ |
| 194 |
VERIFY(regex_match("-", std::regex("[\\w-]", std::regex::ECMAScript))); |
| 195 |
VERIFY(regex_match("a", std::regex("[\\w-]", std::regex::ECMAScript))); |
| 196 |
VERIFY(regex_match("-", std::regex("[a-]", std::regex::ECMAScript))); |
| 197 |
VERIFY(regex_match("a", std::regex("[a-]", std::regex::ECMAScript))); |
| 198 |
|
| 199 |
try |
| 200 |
{ |
| 201 |
std::regex re("[\\w-a]", std::regex::ECMAScript); |
| 202 |
VERIFY(false); |
| 203 |
} |
| 204 |
catch (const std::regex_error& e) |
| 205 |
{ |
| 206 |
VERIFY(e.code() == std::regex_constants::error_range); |
| 207 |
} |
| 208 |
|
| 209 |
try |
| 210 |
{ |
| 211 |
std::regex re("[\\w--]", std::regex::ECMAScript); |
| 212 |
VERIFY(false); |
| 213 |
} |
| 214 |
catch (const std::regex_error& e) |
| 215 |
{ |
| 216 |
VERIFY(e.code() == std::regex_constants::error_range); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
int |
| 221 |
main() |
| 222 |
{ |
| 223 |
test01(); |
| 224 |
test02(); |
| 225 |
test03(); |
| 226 |
test04(); |
| 227 |
test05(); |
| 228 |
test06(); |
| 229 |
test07(); |
| 230 |
|
| 231 |
return 0; |
| 232 |
} |