1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // <fstream> 10 11 // template <class charT, class traits = char_traits<charT> > 12 // class basic_filebuf 13 14 // template <class charT, class traits> 15 // void 16 // swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y); 17 18 #include <fstream> 19 #include <cassert> 20 #include "test_macros.h" 21 #include "platform_support.h" 22 23 int main(int, char**) 24 { 25 std::string temp = get_temp_file_name(); 26 { 27 std::filebuf f; 28 assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in 29 | std::ios_base::trunc) != 0); 30 assert(f.is_open()); 31 assert(f.sputn("123", 3) == 3); 32 f.pubseekoff(1, std::ios_base::beg); 33 assert(f.sgetc() == '2'); 34 std::filebuf f2; 35 swap(f2, f); 36 assert(!f.is_open()); 37 assert(f2.is_open()); 38 assert(f2.sgetc() == '2'); 39 } 40 std::remove(temp.c_str()); 41 42 // lhs uses a small buffer, rhs is empty 43 { 44 std::filebuf f; 45 assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in 46 | std::ios_base::trunc) != 0); 47 assert(f.is_open()); 48 assert(f.sputn("123", 3) == 3); 49 f.pubseekoff(1, std::ios_base::beg); 50 assert(f.sgetc() == '2'); 51 std::filebuf f2; 52 swap(f, f2); 53 assert(!f.is_open()); 54 assert(f2.is_open()); 55 assert(f2.sgetc() == '2'); 56 } 57 std::remove(temp.c_str()); 58 59 // neither lhs nor rhs use the small buffer 60 { 61 std::string tmpA = get_temp_file_name(); 62 std::string tmpB = get_temp_file_name(); 63 64 { 65 // currently the small buffer has size 8 66 std::ofstream sa(tmpA), sb(tmpB); 67 sa << "0123456789"; 68 sb << "abcdefghij"; 69 } 70 71 std::filebuf f1, f2; 72 assert(f1.open(tmpA, std::ios_base::in) != 0); 73 assert(f2.open(tmpB, std::ios_base::in) != 0); 74 assert(f1.sgetc() == '0'); 75 assert(f2.sgetc() == 'a'); 76 77 swap(f1, f2); 78 79 assert(f1.sgetc() == 'a'); 80 assert(f2.sgetc() == '0'); 81 82 std::remove(tmpA.c_str()); 83 std::remove(tmpB.c_str()); 84 } 85 86 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 87 { 88 std::wfilebuf f; 89 assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in 90 | std::ios_base::trunc) != 0); 91 assert(f.is_open()); 92 assert(f.sputn(L"123", 3) == 3); 93 f.pubseekoff(1, std::ios_base::beg); 94 assert(f.sgetc() == L'2'); 95 std::wfilebuf f2; 96 swap(f2, f); 97 assert(!f.is_open()); 98 assert(f2.is_open()); 99 assert(f2.sgetc() == L'2'); 100 } 101 std::remove(temp.c_str()); 102 #endif 103 104 return 0; 105 } 106