1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03, c++11
11 // utilities
12
13 // exchange
14
15 #include <utility>
16 #include <cassert>
17 #include <string>
18
main()19 int main()
20 {
21 {
22 int v = 12;
23 assert ( std::exchange ( v, 23 ) == 12 );
24 assert ( v == 23 );
25 assert ( std::exchange ( v, static_cast<short>(67) ) == 23 );
26 assert ( v == 67 );
27
28 assert ((std::exchange<int, short> ( v, {} )) == 67 );
29 assert ( v == 0 );
30
31 }
32
33 {
34 bool b = false;
35 assert ( !std::exchange ( b, true ));
36 assert ( b );
37 }
38
39 {
40 const std::string s1 ( "Hi Mom!" );
41 const std::string s2 ( "Yo Dad!" );
42 std::string s3 = s1; // Mom
43 assert ( std::exchange ( s3, s2 ) == s1 );
44 assert ( s3 == s2 );
45 assert ( std::exchange ( s3, "Hi Mom!" ) == s2 );
46 assert ( s3 == s1 );
47
48 s3 = s2; // Dad
49 assert ( std::exchange ( s3, {} ) == s2 );
50 assert ( s3.size () == 0 );
51
52 s3 = s2; // Dad
53 assert ( std::exchange ( s3, "" ) == s2 );
54 assert ( s3.size () == 0 );
55 }
56 }
57