Howard Hinnant | 93da3b2 | 2010-07-27 19:53:10 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | 93da3b2 | 2010-07-27 19:53:10 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Asiri Rathnayake | f520c14 | 2015-11-10 11:41:22 +0000 | [diff] [blame] | 10 | // XFAIL: libcpp-no-exceptions |
Howard Hinnant | 93da3b2 | 2010-07-27 19:53:10 +0000 | [diff] [blame] | 11 | // <regex> |
| 12 | |
| 13 | // template <class BidirectionalIterator, class Allocator, class charT, class traits> |
| 14 | // bool |
| 15 | // regex_search(BidirectionalIterator first, BidirectionalIterator last, |
| 16 | // match_results<BidirectionalIterator, Allocator>& m, |
| 17 | // const basic_regex<charT, traits>& e, |
| 18 | // regex_constants::match_flag_type flags = regex_constants::match_default); |
| 19 | |
Howard Hinnant | 93da3b2 | 2010-07-27 19:53:10 +0000 | [diff] [blame] | 20 | #include <regex> |
| 21 | #include <cassert> |
| 22 | |
Eric Fiselier | 1e051aa | 2015-08-28 05:46:17 +0000 | [diff] [blame] | 23 | #include "test_macros.h" |
Marshall Clow | 3222708 | 2013-01-05 03:21:01 +0000 | [diff] [blame] | 24 | #include "test_iterators.h" |
Howard Hinnant | 93da3b2 | 2010-07-27 19:53:10 +0000 | [diff] [blame] | 25 | |
Marshall Clow | 550dfe7 | 2015-08-24 15:57:09 +0000 | [diff] [blame] | 26 | extern "C" void LLVMFuzzerTestOneInput(const char *data) |
| 27 | { |
| 28 | size_t size = strlen(data); |
| 29 | if (size > 0) |
| 30 | { |
| 31 | try |
| 32 | { |
| 33 | std::regex::flag_type flag = std::regex_constants::grep; |
| 34 | std::string s((const char *)data, size); |
| 35 | std::regex re(s, flag); |
| 36 | std::regex_match(s, re); |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 37 | } |
| 38 | catch (std::regex_error &) {} |
| 39 | } |
Marshall Clow | 550dfe7 | 2015-08-24 15:57:09 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | |
| 43 | void fuzz_tests() // patterns that the fuzzer has found |
| 44 | { |
Eric Fiselier | de1495b | 2015-08-30 17:58:50 +0000 | [diff] [blame] | 45 | // Raw string literals are a C++11 feature. |
Eric Fiselier | 1e051aa | 2015-08-28 05:46:17 +0000 | [diff] [blame] | 46 | #if TEST_STD_VER >= 11 |
Marshall Clow | 550dfe7 | 2015-08-24 15:57:09 +0000 | [diff] [blame] | 47 | LLVMFuzzerTestOneInput(R"XX(Õ)_%()()((\8'_%()_%()_%()_%(()_%()_%()_%(.t;)()¥f()_%()(.)_%;)()!¥f(((()()XX"); |
Eric Fiselier | 1e051aa | 2015-08-28 05:46:17 +0000 | [diff] [blame] | 48 | #endif |
Marshall Clow | 550dfe7 | 2015-08-24 15:57:09 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Howard Hinnant | 93da3b2 | 2010-07-27 19:53:10 +0000 | [diff] [blame] | 51 | int main() |
| 52 | { |
| 53 | { |
| 54 | std::cmatch m; |
| 55 | const char s[] = "tournament"; |
| 56 | assert(std::regex_search(s, m, std::regex("tour\nto\ntournament", |
| 57 | std::regex_constants::grep))); |
| 58 | assert(m.size() == 1); |
| 59 | assert(!m.prefix().matched); |
| 60 | assert(m.prefix().first == s); |
| 61 | assert(m.prefix().second == m[0].first); |
| 62 | assert(!m.suffix().matched); |
| 63 | assert(m.suffix().first == m[0].second); |
| 64 | assert(m.suffix().second == s + std::char_traits<char>::length(s)); |
| 65 | assert(m.length(0) == 10); |
| 66 | assert(m.position(0) == 0); |
| 67 | assert(m.str(0) == "tournament"); |
| 68 | } |
| 69 | { |
| 70 | std::cmatch m; |
| 71 | const char s[] = "ment"; |
| 72 | assert(std::regex_search(s, m, std::regex("tour\n\ntournament", |
| 73 | std::regex_constants::grep))); |
| 74 | assert(m.size() == 1); |
| 75 | assert(!m.prefix().matched); |
| 76 | assert(m.prefix().first == s); |
| 77 | assert(m.prefix().second == m[0].first); |
| 78 | assert(m.suffix().matched); |
| 79 | assert(m.suffix().first == m[0].second); |
| 80 | assert(m.suffix().second == s + std::char_traits<char>::length(s)); |
| 81 | assert(m.length(0) == 0); |
| 82 | assert(m.position(0) == 0); |
| 83 | assert(m.str(0) == ""); |
| 84 | } |
Marshall Clow | 550dfe7 | 2015-08-24 15:57:09 +0000 | [diff] [blame] | 85 | fuzz_tests(); |
Howard Hinnant | 93da3b2 | 2010-07-27 19:53:10 +0000 | [diff] [blame] | 86 | } |