]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/28_regex/iterators/regex_iterator/char/string_position_01.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 28_regex / iterators / regex_iterator / char / string_position_01.cc
1 // { dg-do run { target c++11 } }
2 // { dg-timeout-factor 2 }
3
4 //
5 // 2013-07-25 Tim Shen <timshen91@gmail.com>
6 //
7 // Copyright (C) 2013-2021 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.12.1 regex_iterator
25 // Tests iter->position() behavior
26
27 #include <regex>
28 #include <tuple>
29 #include <testsuite_hooks.h>
30
31 void
32 test01()
33 {
34 std::regex re("asdf");
35 std::string s("asdfasdfasdf");
36 int i = 0;
37 for (std::sregex_iterator it(s.begin(), s.end(), re);
38 it != std::sregex_iterator();
39 ++it, i++) {
40 VERIFY( it->position() == 4 * i );
41 }
42 }
43
44 // PR libstdc++/64239
45 void
46 test02()
47 {
48 std::regex re("\\w+");
49 std::string s("-a-b-c-");
50
51 std::tuple<int, int, const char*> expected[] =
52 {
53 std::make_tuple(1, 1, "a"),
54 std::make_tuple(3, 1, "b"),
55 std::make_tuple(5, 1, "c"),
56 };
57
58 int i = 0;
59 for (auto it1 = std::sregex_iterator(s.begin(), s.end(), re),
60 end = std::sregex_iterator(); it1 != end; ++it1, i++)
61 {
62 auto it2 = it1;
63 VERIFY(it1->position() == std::get<0>(expected[i]));
64 VERIFY(it1->length() == std::get<1>(expected[i]));
65 VERIFY(it1->str() == std::get<2>(expected[i]));
66 VERIFY(it2->position() == std::get<0>(expected[i]));
67 VERIFY(it2->length() == std::get<1>(expected[i]));
68 VERIFY(it2->str() == std::get<2>(expected[i]));
69 }
70 }
71
72 void
73 test03()
74 {
75 std::smatch m;
76 std::string s = "abcde";
77 std::regex_search(s, m, std::regex("bcd"));
78 VERIFY(m.position() == 1);
79 VERIFY(m.position() == m.prefix().length());
80 }
81
82 int
83 main()
84 {
85 test01();
86 test02();
87 test03();
88 return 0;
89 }