Marshall Clow | a7b0e5d | 2013-07-08 20:54:40 +0000 | [diff] [blame] | 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 | // utilities |
| 11 | |
| 12 | // exchange |
| 13 | |
| 14 | #include <utility> |
| 15 | #include <cassert> |
| 16 | #include <string> |
| 17 | |
| 18 | int main() |
| 19 | { |
| 20 | #if _LIBCPP_STD_VER > 11 |
Howard Hinnant | e0fe3d2 | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 21 | { |
| 22 | int v = 12; |
| 23 | assert ( std::exchange ( v, 23 ) == 12 ); |
| 24 | assert ( v == 23 ); |
| 25 | assert ( std::exchange ( v, 67.2 ) == 23 ); |
| 26 | assert ( v = 67 ); |
| 27 | } |
Marshall Clow | a7b0e5d | 2013-07-08 20:54:40 +0000 | [diff] [blame] | 28 | |
Howard Hinnant | e0fe3d2 | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 29 | { |
| 30 | bool b = false; |
| 31 | assert ( !std::exchange ( b, true )); |
| 32 | assert ( b ); |
| 33 | } |
Marshall Clow | a7b0e5d | 2013-07-08 20:54:40 +0000 | [diff] [blame] | 34 | |
Howard Hinnant | e0fe3d2 | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 35 | { |
| 36 | const std::string s1 ( "Hi Mom!" ); |
| 37 | const std::string s2 ( "Yo Dad!" ); |
| 38 | std::string s3 = s1; // Mom |
| 39 | assert ( std::exchange ( s3, s2 ) == s1 ); |
| 40 | assert ( s3 == s2 ); |
| 41 | assert ( std::exchange ( s3, "Hi Mom!" ) == s2 ); |
| 42 | assert ( s3 == s1 ); |
| 43 | assert ( std::exchange ( s3, "" ) == s1 ); |
| 44 | assert ( s3.size () == 0 ); |
| 45 | } |
Marshall Clow | a7b0e5d | 2013-07-08 20:54:40 +0000 | [diff] [blame] | 46 | #endif |
| 47 | } |