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