blob: 136f9958c9b213597e38896ea6b22eba51135f05 [file] [log] [blame]
Howard Hinnant93da3b22010-07-27 19:53:10 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant93da3b22010-07-27 19:53:10 +00007//
8//===----------------------------------------------------------------------===//
9
Asiri Rathnayakef520c142015-11-10 11:41:22 +000010// XFAIL: libcpp-no-exceptions
Howard Hinnant93da3b22010-07-27 19:53:10 +000011// <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 Hinnant93da3b22010-07-27 19:53:10 +000020#include <regex>
21#include <cassert>
22
Eric Fiselier1e051aa2015-08-28 05:46:17 +000023#include "test_macros.h"
Marshall Clow32227082013-01-05 03:21:01 +000024#include "test_iterators.h"
Howard Hinnant93da3b22010-07-27 19:53:10 +000025
Marshall Clow550dfe72015-08-24 15:57:09 +000026extern "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 Fiselierd04c6852016-06-01 21:35:39 +000037 }
38 catch (std::regex_error &) {}
39 }
Marshall Clow550dfe72015-08-24 15:57:09 +000040}
41
42
43void fuzz_tests() // patterns that the fuzzer has found
44{
Eric Fiselierde1495b2015-08-30 17:58:50 +000045// Raw string literals are a C++11 feature.
Eric Fiselier1e051aa2015-08-28 05:46:17 +000046#if TEST_STD_VER >= 11
Marshall Clow550dfe72015-08-24 15:57:09 +000047 LLVMFuzzerTestOneInput(R"XX(Õ)_%()()((\8'_%()_%()_%()_%(()_%()_%()_%(.t;)()¥f()_%()(.)_%;)()!¥f(((()()XX");
Eric Fiselier1e051aa2015-08-28 05:46:17 +000048#endif
Marshall Clow550dfe72015-08-24 15:57:09 +000049}
50
Howard Hinnant93da3b22010-07-27 19:53:10 +000051int 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 Clow550dfe72015-08-24 15:57:09 +000085 fuzz_tests();
Howard Hinnant93da3b22010-07-27 19:53:10 +000086}