| 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 | // { dg-additional-files "thirty_years_among_the_dead_preproc.txt" } |
| 21 | |
| 22 | // C++11 27.9.1.8 Assign and swap [ifstream.assign] |
| 23 | |
| 24 | #include <fstream> |
| 25 | #include <sstream> |
| 26 | #include <testsuite_hooks.h> |
| 27 | |
| 28 | void |
| 29 | read(std::istream& in, std::ostream& out) |
| 30 | { |
| 31 | std::string s; |
| 32 | for (int i=0; i < 10; ++i) |
| 33 | { |
| 34 | getline(in, s); |
| 35 | out << s << '\n'; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | test01() |
| 41 | { |
| 42 | std::string const name = "thirty_years_among_the_dead_preproc.txt"; |
| 43 | std::ostringstream ss0; |
| 44 | { |
| 45 | std::ifstream f0(name); |
| 46 | read(f0, ss0); |
| 47 | } |
| 48 | { |
| 49 | std::ifstream f0(name); |
| 50 | std::ifstream f1; |
| 51 | f1 = std::move(f0); |
| 52 | VERIFY( !f0.is_open() ); |
| 53 | VERIFY( f1.is_open() ); |
| 54 | std::ostringstream ss1; |
| 55 | read(f1, ss1); |
| 56 | VERIFY( ss0.str() == ss1.str() ); |
| 57 | } |
| 58 | { |
| 59 | std::ifstream f0(name); |
| 60 | std::ifstream f1; |
| 61 | f1.swap(f0); |
| 62 | VERIFY( !f0.is_open() ); |
| 63 | VERIFY( f1.is_open() ); |
| 64 | std::ostringstream ss1; |
| 65 | read(f1, ss1); |
| 66 | VERIFY( ss0.str() == ss1.str() ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | test02() |
| 72 | { |
| 73 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 74 | std::wifstream f, f2; |
| 75 | f2 = std::move(f); |
| 76 | f2.swap(f); |
| 77 | swap(f2, f); |
| 78 | #endif |
| 79 | } |
| 80 | |
| 81 | int |
| 82 | main() |
| 83 | { |
| 84 | test01(); |
| 85 | test02(); |
| 86 | } |