grep.pass.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <regex>
  9. // template <class BidirectionalIterator, class Allocator, class charT, class traits>
  10. // bool
  11. // regex_search(BidirectionalIterator first, BidirectionalIterator last,
  12. // match_results<BidirectionalIterator, Allocator>& m,
  13. // const basic_regex<charT, traits>& e,
  14. // regex_constants::match_flag_type flags = regex_constants::match_default);
  15. #include <regex>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #include "test_iterators.h"
  19. extern "C" void LLVMFuzzerTestOneInput(const char *data)
  20. {
  21. #ifndef TEST_HAS_NO_EXCEPTIONS
  22. size_t size = strlen(data);
  23. if (size > 0)
  24. {
  25. try
  26. {
  27. std::regex::flag_type flag = std::regex_constants::grep;
  28. std::string s((const char *)data, size);
  29. std::regex re(s, flag);
  30. TEST_IGNORE_NODISCARD std::regex_match(s, re);
  31. }
  32. catch (std::regex_error &) {}
  33. }
  34. #else
  35. ((void)data);
  36. #endif
  37. }
  38. void fuzz_tests() // patterns that the fuzzer has found
  39. {
  40. // Raw string literals are a C++11 feature.
  41. #if TEST_STD_VER >= 11
  42. LLVMFuzzerTestOneInput(R"XX(Õ)_%()()((\8'_%()_%()_%()_%(()_%()_%()_%(.t;)()¥f()_%()(.)_%;)()!¥f(((()()XX");
  43. #endif
  44. }
  45. int main(int, char**)
  46. {
  47. {
  48. std::cmatch m;
  49. const char s[] = "tournament";
  50. assert(std::regex_search(s, m, std::regex("tour\nto\ntournament",
  51. std::regex_constants::grep)));
  52. assert(m.size() == 1);
  53. assert(!m.prefix().matched);
  54. assert(m.prefix().first == s);
  55. assert(m.prefix().second == m[0].first);
  56. assert(!m.suffix().matched);
  57. assert(m.suffix().first == m[0].second);
  58. assert(m.suffix().second == s + std::char_traits<char>::length(s));
  59. assert(m.length(0) == 10);
  60. assert(m.position(0) == 0);
  61. assert(m.str(0) == "tournament");
  62. }
  63. {
  64. std::cmatch m;
  65. const char s[] = "ment";
  66. assert(std::regex_search(s, m, std::regex("tour\n\ntournament",
  67. std::regex_constants::grep)));
  68. assert(m.size() == 1);
  69. assert(!m.prefix().matched);
  70. assert(m.prefix().first == s);
  71. assert(m.prefix().second == m[0].first);
  72. assert(m.suffix().matched);
  73. assert(m.suffix().first == m[0].second);
  74. assert(m.suffix().second == s + std::char_traits<char>::length(s));
  75. assert(m.length(0) == 0);
  76. assert(m.position(0) == 0);
  77. assert(m.str(0) == "");
  78. }
  79. fuzz_tests();
  80. return 0;
  81. }