egrep.pass.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_match(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. int main(int, char**)
  20. {
  21. {
  22. std::cmatch m;
  23. const char s[] = "tournament";
  24. assert(std::regex_match(s, m, std::regex("tour\nto\ntournament",
  25. std::regex_constants::egrep)));
  26. assert(m.size() == 1);
  27. assert(!m.prefix().matched);
  28. assert(m.prefix().first == s);
  29. assert(m.prefix().second == m[0].first);
  30. assert(!m.suffix().matched);
  31. assert(m.suffix().first == m[0].second);
  32. assert(m.suffix().second == s + std::char_traits<char>::length(s));
  33. assert(m.length(0) == 10);
  34. assert(m.position(0) == 0);
  35. assert(m.str(0) == "tournament");
  36. }
  37. {
  38. std::cmatch m;
  39. const char s[] = "ment";
  40. assert(!std::regex_match(s, m, std::regex("tour\n\ntournament",
  41. std::regex_constants::egrep)));
  42. assert(m.size() == 0);
  43. }
  44. {
  45. std::cmatch m;
  46. const char s[] = "tournament";
  47. assert(std::regex_match(s, m, std::regex("(tour|to|tournament)+\ntourna",
  48. std::regex_constants::egrep)));
  49. assert(m.size() == 2);
  50. assert(!m.prefix().matched);
  51. assert(m.prefix().first == s);
  52. assert(m.prefix().second == m[0].first);
  53. assert(!m.suffix().matched);
  54. assert(m.suffix().first == m[0].second);
  55. assert(m.suffix().second == s + std::char_traits<char>::length(s));
  56. assert(m.length(0) == 10);
  57. assert(m.position(0) == 0);
  58. assert(m.str(0) == "tournament");
  59. }
  60. {
  61. std::cmatch m;
  62. const char s[] = "tourna";
  63. assert(std::regex_match(s, m, std::regex("(tour|to|tournament)+\ntourna",
  64. std::regex_constants::egrep)));
  65. assert(m.size() == 2);
  66. assert(!m.prefix().matched);
  67. assert(m.prefix().first == s);
  68. assert(m.prefix().second == m[0].first);
  69. assert(!m.suffix().matched);
  70. assert(m.suffix().first == m[0].second);
  71. assert(m.suffix().second == s + std::char_traits<char>::length(s));
  72. assert(m.length(0) == 6);
  73. assert(m.position(0) == 0);
  74. assert(m.str(0) == "tourna");
  75. }
  76. return 0;
  77. }