OSDN Git Service

2008-09-30 Chris Fairles <cfairles@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 20_util / tuple / swap.cc
1 // { dg-options "-std=gnu++0x" }
2
3 // Copyright (C) 2007 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 2, 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 COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 // NOTE: This makes use of the fact that we know how moveable
31 // is implemented on tuple.  If the implementation changed
32 // this test may begin to fail.
33
34 #include <tuple>
35 #include <utility>
36 #include <testsuite_hooks.h>
37
38 struct MoveOnly
39 {
40   explicit MoveOnly (int j) : i(j) { }
41
42   MoveOnly (MoveOnly&& m) : i(m.i) { }
43
44   MoveOnly& operator=(MoveOnly&& m)
45   { i = m.i; return *this; }
46
47   MoveOnly(MoveOnly const&) = delete;
48   MoveOnly& operator=(MoveOnly const&) = delete;
49
50   bool operator==(MoveOnly const& m)
51   { return i == m.i; }
52
53   void swap(MoveOnly&& m)
54   { std::swap(m.i, i); }
55
56   int i;
57 };
58
59 void swap(MoveOnly& m1, MoveOnly& m2)
60 { m1.swap(m2); }
61
62 MoveOnly
63 make_move_only (int i)
64 { return MoveOnly(i); }
65
66 void test01()
67 {
68   std::tuple<> t1, t2;
69   std::swap(t1, t2);
70
71   VERIFY( t1 == t2 );
72 }
73
74 void test02()
75 {
76   bool test __attribute__((unused)) = true;
77
78   std::tuple<int> t1(1), t2(2);
79   std::swap(t1, t2);
80   
81   VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 );
82 }
83
84 void test03()
85 {
86   bool test __attribute__((unused)) = true;
87
88   std::tuple<int, float> t1(1, 1.0f), t2(2, 2.0f);
89   std::swap(t1, t2);
90
91   VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 );
92   VERIFY( std::get<1>(t1) == 2.0f && std::get<1>(t2) == 1.0f );
93 }
94
95 void test04()
96 {
97   bool test __attribute__((unused)) = true;
98
99   std::tuple<int, float, MoveOnly> 
100     t1(1, 1.0f, make_move_only(1)), 
101     t2(2, 2.0f, make_move_only(2));
102
103   std::swap(t1, t2);
104
105   VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 );
106   VERIFY( std::get<1>(t1) == 2.0f && std::get<1>(t2) == 1.0f );
107   VERIFY( std::get<2>(t1) == make_move_only(2) 
108           && std::get<2>(t2) == make_move_only(1) );
109 }
110
111 int main()
112 {
113   test01();
114   test02();
115   test03();
116   test04();
117   return 0;
118 }