| 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.11 basic_ofstream constructors [ofstream.cons] |
| 22 | |
| 23 | #include <fstream> |
| 24 | #include <string> |
| 25 | #include <testsuite_hooks.h> |
| 26 | |
| 27 | using namespace std; |
| 28 | |
| 29 | std::string const name = "ofstream-move.txt"; |
| 30 | |
| 31 | void |
| 32 | test01() |
| 33 | { |
| 34 | string s1 = "Let the whole outside world"; |
| 35 | string s2 = " consist of a long paper tape."; |
| 36 | ofstream f(name, ios::trunc); |
| 37 | VERIFY( f.is_open() ); |
| 38 | f << s1; |
| 39 | { |
| 40 | ofstream f1 = std::move(f); |
| 41 | VERIFY( f1.is_open() ); |
| 42 | VERIFY( !f.is_open() ); |
| 43 | f1 << s2; |
| 44 | } |
| 45 | ifstream in(name); |
| 46 | string result; |
| 47 | getline(in, result); |
| 48 | VERIFY( result == (s1 + s2) ); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | test02() |
| 53 | { |
| 54 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 55 | wstring s1 = L"Let the whole outside world"; |
| 56 | wstring s2 = L" consist of a long paper tape."; |
| 57 | wofstream f(name, ios::trunc); |
| 58 | VERIFY( f.is_open() ); |
| 59 | f << s1; |
| 60 | { |
| 61 | wofstream f1 = std::move(f); |
| 62 | VERIFY( f1.is_open() ); |
| 63 | VERIFY( !f.is_open() ); |
| 64 | f1 << s2; |
| 65 | } |
| 66 | wifstream in(name); |
| 67 | wstring result; |
| 68 | getline(in, result); |
| 69 | VERIFY( result == (s1 + s2) ); |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | int |
| 74 | main() |
| 75 | { |
| 76 | test01(); |
| 77 | test02(); |
| 78 | } |