blob: 2d01d6c8c8afb96305bdba93cf98b2496ba2fe92 [file] [log] [blame]
Marshall Clowa7b0e5d2013-07-08 20:54:40 +00001//===----------------------------------------------------------------------===//
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
Asiri Rathnayake6edc12c2016-05-28 08:57:35 +000010// UNSUPPORTED: c++98, c++03, c++11
Marshall Clowa7b0e5d2013-07-08 20:54:40 +000011// utilities
12
13// exchange
14
15#include <utility>
16#include <cassert>
17#include <string>
18
19int main()
20{
Howard Hinnante0fe3d22013-07-08 21:06:38 +000021 {
22 int v = 12;
23 assert ( std::exchange ( v, 23 ) == 12 );
24 assert ( v == 23 );
Stephan T. Lavavejf2e24f52016-12-08 21:38:57 +000025 assert ( std::exchange ( v, static_cast<short>(67) ) == 23 );
Marshall Clow48c9fe22013-07-10 18:01:34 +000026 assert ( v == 67 );
27
Stephan T. Lavavejf2e24f52016-12-08 21:38:57 +000028 assert ((std::exchange<int, short> ( v, {} )) == 67 );
Marshall Clow48c9fe22013-07-10 18:01:34 +000029 assert ( v == 0 );
Eric Fiselierd04c6852016-06-01 21:35:39 +000030
Howard Hinnante0fe3d22013-07-08 21:06:38 +000031 }
Marshall Clowa7b0e5d2013-07-08 20:54:40 +000032
Howard Hinnante0fe3d22013-07-08 21:06:38 +000033 {
34 bool b = false;
35 assert ( !std::exchange ( b, true ));
36 assert ( b );
37 }
Marshall Clowa7b0e5d2013-07-08 20:54:40 +000038
Howard Hinnante0fe3d22013-07-08 21:06:38 +000039 {
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 );
Marshall Clow48c9fe22013-07-10 18:01:34 +000047
48 s3 = s2; // Dad
49 assert ( std::exchange ( s3, {} ) == s2 );
50 assert ( s3.size () == 0 );
Eric Fiselierd04c6852016-06-01 21:35:39 +000051
Marshall Clow48c9fe22013-07-10 18:01:34 +000052 s3 = s2; // Dad
53 assert ( std::exchange ( s3, "" ) == s2 );
Howard Hinnante0fe3d22013-07-08 21:06:38 +000054 assert ( s3.size () == 0 );
55 }
Marshall Clowa7b0e5d2013-07-08 20:54:40 +000056}