]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/tuple/swap.cc
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / tuple / swap.cc
CommitLineData
3e93b275
CF
1// { dg-options "-std=gnu++0x" }
2
748086b7 3// Copyright (C) 2007, 2009 Free Software Foundation, Inc.
3e93b275
CF
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
3e93b275
CF
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
748086b7
JJ
17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
3e93b275 19
3e93b275
CF
20
21// NOTE: This makes use of the fact that we know how moveable
22// is implemented on tuple. If the implementation changed
23// this test may begin to fail.
24
25#include <tuple>
26#include <utility>
27#include <testsuite_hooks.h>
28
29struct MoveOnly
30{
31 explicit MoveOnly (int j) : i(j) { }
32
33 MoveOnly (MoveOnly&& m) : i(m.i) { }
34
35 MoveOnly& operator=(MoveOnly&& m)
36 { i = m.i; return *this; }
37
38 MoveOnly(MoveOnly const&) = delete;
39 MoveOnly& operator=(MoveOnly const&) = delete;
40
41 bool operator==(MoveOnly const& m)
42 { return i == m.i; }
43
44 void swap(MoveOnly&& m)
45 { std::swap(m.i, i); }
46
47 int i;
48};
49
50void swap(MoveOnly& m1, MoveOnly& m2)
51{ m1.swap(m2); }
52
53MoveOnly
54make_move_only (int i)
55{ return MoveOnly(i); }
56
57void test01()
58{
59 std::tuple<> t1, t2;
60 std::swap(t1, t2);
61
62 VERIFY( t1 == t2 );
63}
64
65void test02()
66{
67 bool test __attribute__((unused)) = true;
68
69 std::tuple<int> t1(1), t2(2);
70 std::swap(t1, t2);
71
72 VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 );
73}
74
75void test03()
76{
77 bool test __attribute__((unused)) = true;
78
79 std::tuple<int, float> t1(1, 1.0f), t2(2, 2.0f);
80 std::swap(t1, t2);
81
82 VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 );
83 VERIFY( std::get<1>(t1) == 2.0f && std::get<1>(t2) == 1.0f );
84}
85
86void test04()
87{
88 bool test __attribute__((unused)) = true;
89
90 std::tuple<int, float, MoveOnly>
91 t1(1, 1.0f, make_move_only(1)),
92 t2(2, 2.0f, make_move_only(2));
93
94 std::swap(t1, t2);
95
96 VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 );
97 VERIFY( std::get<1>(t1) == 2.0f && std::get<1>(t2) == 1.0f );
98 VERIFY( std::get<2>(t1) == make_move_only(2)
99 && std::get<2>(t2) == make_move_only(1) );
100}
101
102int main()
103{
104 test01();
105 test02();
106 test03();
107 test04();
108 return 0;
109}