| 1 | // Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++11 } } |
| 19 | // { dg-require-fileio "" } |
| 20 | |
| 21 | // 27.9.1.16 Assign and swap [fstream.assign] |
| 22 | |
| 23 | #include <fstream> |
| 24 | #include <string> |
| 25 | #include <testsuite_hooks.h> |
| 26 | |
| 27 | using namespace std; |
| 28 | |
| 29 | string const name = "fstream-assign.txt"; |
| 30 | |
| 31 | void |
| 32 | test01() |
| 33 | { |
| 34 | string orig = "Let the whole outside world consist of a long paper tape."; |
| 35 | { |
| 36 | fstream f(name, ios::in|ios::out|ios::trunc); |
| 37 | VERIFY( f.is_open() ); |
| 38 | f << orig; |
| 39 | fstream f1; |
| 40 | f1 = std::move(f); |
| 41 | VERIFY( f1.is_open() ); |
| 42 | VERIFY( !f.is_open() ); |
| 43 | f1.seekg(0); |
| 44 | string result; |
| 45 | getline(f1, result); |
| 46 | VERIFY( result == orig ); |
| 47 | } |
| 48 | { |
| 49 | fstream f(name, ios::in); |
| 50 | VERIFY( f.is_open() ); |
| 51 | fstream f1; |
| 52 | f1.swap(f); |
| 53 | VERIFY( f1.is_open() ); |
| 54 | VERIFY( !f.is_open() ); |
| 55 | string result; |
| 56 | getline(f1, result); |
| 57 | VERIFY( result == orig ); |
| 58 | } |
| 59 | { |
| 60 | fstream f(name, ios::in); |
| 61 | VERIFY( f.is_open() ); |
| 62 | fstream f1; |
| 63 | swap(f1, f); |
| 64 | VERIFY( f1.is_open() ); |
| 65 | VERIFY( !f.is_open() ); |
| 66 | string result; |
| 67 | getline(f1, result); |
| 68 | VERIFY( result == orig ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | test02() |
| 74 | { |
| 75 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 76 | wfstream f, f2; |
| 77 | f2 = std::move(f); |
| 78 | f2.swap(f); |
| 79 | swap(f2, f); |
| 80 | #endif |
| 81 | } |
| 82 | |
| 83 | int |
| 84 | main() |
| 85 | { |
| 86 | test01(); |
| 87 | test02(); |
| 88 | } |