OSDN Git Service

2011-01-05 François Dumont <francois.cppdevs@free.fr>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 23_containers / forward_list / debug / swap.cc
1 // { dg-options "-std=gnu++0x" }
2
3 // Copyright (C) 2010 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 Pred 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 // Check that iterators ownership is correctly manage on swap.
21 #include <vector>
22 #include <forward_list>
23 #include <iostream>
24 #include <testsuite_hooks.h>
25
26 void
27 test01()
28 {
29   bool test __attribute__((unused)) = true;
30   std::forward_list<int> fl1{1, 3, 5};
31   std::forward_list<int> fl2{2, 4, 6};
32
33   std::vector<std::forward_list<int>::iterator> fl1_its;
34   fl1_its.push_back(fl1.before_begin());
35   for (std::forward_list<int>::iterator it = fl1.begin();
36        it != fl1.end(); ++it)
37     {
38       fl1_its.push_back(it);
39     }
40   fl1_its.push_back(fl1.end());
41
42   std::vector<std::forward_list<int>::iterator> fl2_its;
43   fl2_its.push_back(fl2.before_begin());
44   for (std::forward_list<int>::iterator it = fl2.begin();
45        it != fl2.end(); ++it)
46     {
47       fl2_its.push_back(it);
48     }
49   fl2_its.push_back(fl2.end());
50
51   fl1.swap(fl2);
52
53   auto fit = fl1.before_begin();
54   // before-begin iterator is not transfered:
55   // TODO: Validate with LWG group how before begin should be
56   // treated.
57   VERIFY( fit == fl1_its[0] );
58   // All other iterators are, even paste-the-end ones:
59   for (size_t i = 1; i != fl2_its.size(); ++i)
60   {
61     VERIFY( ++fit == fl2_its[i] );
62   }
63
64   fit = fl2.before_begin();
65   // TODO: Validate with LWG group how before begin should be
66   // treated.
67   VERIFY( fit == fl2_its[0] );
68   for (size_t i = 1; i != fl1_its.size(); ++i)
69   {
70     VERIFY( ++fit == fl1_its[i] );
71   }
72 }
73
74 int
75 main()
76 {
77   test01();
78   return 0;
79 }