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 // basic_filebuf<charT,traits>* open(const char* s, ios_base::openmode mode); 12 13 // In C++23 and later, this test requires support for P2467R1 in the dylib (a3f17ba3febbd546f2342ffc780ac93b694fdc8d) 14 // XFAIL: (!c++03 && !c++11 && !c++14 && !c++17 && !c++20) && using-built-library-before-llvm-18 15 16 // XFAIL: LIBCXX-AIX-FIXME 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) != 0); 29 assert(f.is_open()); 30 assert(f.sputn("123", 3) == 3); 31 } 32 { 33 std::filebuf f; 34 assert(f.open(temp.c_str(), std::ios_base::in) != 0); 35 assert(f.is_open()); 36 assert(f.sbumpc() == '1'); 37 assert(f.sbumpc() == '2'); 38 assert(f.sbumpc() == '3'); 39 } 40 std::remove(temp.c_str()); 41 42 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 43 { 44 std::wfilebuf f; 45 assert(f.open(temp.c_str(), std::ios_base::out) != 0); 46 assert(f.is_open()); 47 assert(f.sputn(L"123", 3) == 3); 48 } 49 { 50 std::wfilebuf f; 51 assert(f.open(temp.c_str(), std::ios_base::in) != 0); 52 assert(f.is_open()); 53 assert(f.sbumpc() == L'1'); 54 assert(f.sbumpc() == L'2'); 55 assert(f.sbumpc() == L'3'); 56 } 57 std::remove(temp.c_str()); 58 #endif 59 60 #if TEST_STD_VER >= 23 61 // Test all the noreplace flag combinations 62 { 63 std::ios_base::openmode modes[] = { 64 std::ios_base::out | std::ios_base::noreplace, 65 std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace, 66 std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace, 67 std::ios_base::out | std::ios_base::noreplace | std::ios_base::binary, 68 std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace | std::ios_base::binary, 69 std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace | 70 std::ios_base::binary, 71 }; 72 for (auto mode : modes) { 73 std::string tmp = get_temp_file_name(); // also creates the file 74 75 { 76 std::filebuf f; 77 f.open(tmp.c_str(), mode); 78 assert(!f.is_open()); // since it already exists 79 } 80 81 { 82 std::remove(tmp.c_str()); 83 84 std::filebuf f; 85 f.open(tmp.c_str(), mode); 86 assert(f.is_open()); // since it doesn't exist 87 } 88 } 89 90 # ifndef TEST_HAS_NO_WIDE_CHARACTERS 91 for (auto mode : modes) { 92 std::string tmp = get_temp_file_name(); // also creates the file 93 94 { 95 std::wfilebuf f; 96 f.open(tmp.c_str(), mode); 97 assert(!f.is_open()); // since it already exists 98 } 99 100 { 101 std::remove(tmp.c_str()); 102 103 std::wfilebuf f; 104 f.open(tmp.c_str(), mode); 105 assert(f.is_open()); // since it doesn't exist 106 } 107 } 108 # endif 109 } 110 #endif // TEST_STD_VER >= 23 111 112 return 0; 113 } 114