Debian Codegraph @

libstdc++-v3/testsuite/23_containers/forward_list/modifiers/2.cc

raw
1 // { dg-do run { target c++11 } }
2
3 // Copyright (C) 2008-2025 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // 23.2.3.n forward_list xxx [lib.forward_list.xxx]
21
22 #include <forward_list>
23 #include <testsuite_hooks.h>
24
25 #include <string>
26
27 // This test verifies the following:
28 // insert_after single item
29 // before_begin iterator
30 void
31 test01()
32 {
33 std::forward_list<int> fl({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
34
35 std::forward_list<int>::iterator ret = fl.insert_after(fl.before_begin(),
36 42);
37 VERIFY( ret == fl.begin() );
38 VERIFY( fl.front() == 42 );
39 }
40
41 // This test verifies the following:
42 void
43 test02()
44 {
45 std::forward_list<int> fl({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
46
47 std::forward_list<int>::const_iterator pos = fl.cbegin();
48 ++pos;
49 VERIFY( *pos == 1 );
50
51 std::forward_list<int>::iterator ret = fl.insert_after(pos, 0, 42);
52 VERIFY( ret == pos );
53
54 ret = fl.insert_after(pos, 5, 42);
55 VERIFY( *pos == 1 );
56
57 ++pos;
58 VERIFY( *pos == 42 );
59 ++pos;
60 ++pos;
61 ++pos;
62 ++pos;
63 VERIFY( *pos == 42 );
64 VERIFY( ret == pos );
65 ++pos;
66 VERIFY( *pos == 2 );
67 }
68
69 // This test verifies the following:
70 void
71 test03()
72 {
73 std::forward_list<int> fl({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
74
75 std::forward_list<int>::const_iterator pos = fl.cbegin();
76 ++pos;
77 VERIFY( *pos == 1 );
78
79 int i[3] = {666, 777, 888};
80 std::forward_list<int>::iterator ret = fl.insert_after(pos, i, i);
81 VERIFY( ret == pos );
82
83 ret = fl.insert_after(pos, i, i + 3);
84 VERIFY( *pos == 1 );
85
86 ++pos;
87 ++pos;
88 ++pos;
89 VERIFY( *pos == 888 );
90 VERIFY( ret == pos );
91 ++pos;
92 VERIFY( *pos == 2 );
93 }
94
95 // This test verifies the following:
96 void
97 test04()
98 {
99 std::forward_list<int> fl({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
100
101 std::forward_list<int>::const_iterator pos = fl.cbegin();
102 ++pos;
103 VERIFY( *pos == 1 );
104
105 std::forward_list<int>::iterator ret = fl.insert_after(pos, { });
106 VERIFY( ret == pos);
107
108 ret = fl.insert_after(pos, {-1, -2, -3, -4, -5});
109 VERIFY( *pos == 1);
110
111 ++pos;
112 ++pos;
113 ++pos;
114 VERIFY( *pos == -3 );
115 ++pos;
116 ++pos;
117 VERIFY( ret == pos );
118 ++pos;
119 VERIFY( *pos == 2 );
120 }
121
122 // This test verifies the following:
123 void
124 test05()
125 {
126 std::forward_list<std::string> fl({"AAA", "BBB", "CCC"});
127
128 std::forward_list<std::string>::const_iterator pos = fl.cbegin();
129 ++pos;
130 VERIFY( *pos == "BBB" );
131
132 std::string x( "XXX" );
133 std::forward_list<std::string>::iterator ret
134 = fl.insert_after(pos, std::move(x));
135 VERIFY( *pos == "BBB" );
136 ++pos;
137 VERIFY( ret == pos );
138 VERIFY( *pos == "XXX" );
139 ++pos;
140 VERIFY( *pos == "CCC" );
141 }
142
143 int
144 main()
145 {
146 test01();
147 test02();
148 test03();
149 test04();
150 test05();
151 return 0;
152 }