]> git.ipfire.org Git - thirdparty/gcc.git/blame - 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
CommitLineData
be58e01d 1// { dg-do run { target c++11 } }
9d6849f1 2
3//
4// 2013-07-25 Tim Shen <timshen91@gmail.com>
5//
fbd26352 6// Copyright (C) 2013-2019 Free Software Foundation, Inc.
9d6849f1 7//
8// This file is part of the GNU ISO C++ Library. This library is free
9// software; you can redistribute it and/or modify it under the
10// terms of the GNU General Public License as published by the
11// Free Software Foundation; either version 3, or (at your option)
12// any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
18//
19// You should have received a copy of the GNU General Public License along
20// with this library; see the file COPYING3. If not see
21// <http://www.gnu.org/licenses/>.
22
23// 28.12.1 regex_iterator
24// Tests iter->position() behavior
25
26#include <regex>
db23e1ad 27#include <tuple>
9d6849f1 28#include <testsuite_hooks.h>
29
30void
31test01()
32{
9d6849f1 33 std::regex re("asdf");
34 std::string s("asdfasdfasdf");
35 int i = 0;
36 for (std::sregex_iterator it(s.begin(), s.end(), re);
37 it != std::sregex_iterator();
38 ++it, i++) {
39 VERIFY( it->position() == 4 * i );
40 }
41}
42
db23e1ad 43// PR libstdc++/64239
44void
45test02()
46{
db23e1ad 47 std::regex re("\\w+");
48 std::string s("-a-b-c-");
49
50 std::tuple<int, int, const char*> expected[] =
51 {
52 std::make_tuple(1, 1, "a"),
53 std::make_tuple(3, 1, "b"),
54 std::make_tuple(5, 1, "c"),
55 };
56
57 int i = 0;
58 for (auto it1 = std::sregex_iterator(s.begin(), s.end(), re),
59 end = std::sregex_iterator(); it1 != end; ++it1, i++)
60 {
61 auto it2 = it1;
62 VERIFY(it1->position() == std::get<0>(expected[i]));
63 VERIFY(it1->length() == std::get<1>(expected[i]));
64 VERIFY(it1->str() == std::get<2>(expected[i]));
65 VERIFY(it2->position() == std::get<0>(expected[i]));
66 VERIFY(it2->length() == std::get<1>(expected[i]));
67 VERIFY(it2->str() == std::get<2>(expected[i]));
68 }
69}
70
71void
72test03()
73{
db23e1ad 74 std::smatch m;
75 std::string s = "abcde";
76 std::regex_search(s, m, std::regex("bcd"));
77 VERIFY(m.position() == 1);
78 VERIFY(m.position() == m.prefix().length());
79}
80
9d6849f1 81int
82main()
83{
84 test01();
db23e1ad 85 test02();
86 test03();
9d6849f1 87 return 0;
88}