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>* close(); 12 13 #include <fstream> 14 #include <cassert> 15 #if defined(__unix__) 16 #include <fcntl.h> 17 #include <unistd.h> 18 #endif 19 #include "test_macros.h" 20 #include "platform_support.h" 21 main(int,char **)22int main(int, char**) 23 { 24 std::string temp = get_temp_file_name(); 25 { 26 std::filebuf f; 27 assert(!f.is_open()); 28 assert(f.open(temp.c_str(), std::ios_base::out) != 0); 29 assert(f.is_open()); 30 assert(f.close() != nullptr); 31 assert(!f.is_open()); 32 assert(f.close() == nullptr); 33 assert(!f.is_open()); 34 } 35 #if defined(__unix__) 36 { 37 std::filebuf f; 38 assert(!f.is_open()); 39 // Use open directly to get the file descriptor. 40 int fd = open(temp.c_str(), O_RDWR); 41 assert(fd >= 0); 42 // Use the internal method to create filebuf from the file descriptor. 43 assert(f.__open(fd, std::ios_base::out) != 0); 44 assert(f.is_open()); 45 // Close the file descriptor directly to force filebuf::close to fail. 46 assert(close(fd) == 0); 47 // Ensure that filebuf::close handles the failure. 48 assert(f.close() == nullptr); 49 assert(!f.is_open()); 50 assert(f.close() == nullptr); 51 } 52 #endif 53 std::remove(temp.c_str()); 54 55 return 0; 56 } 57