xref: /llvm-project/libcxx/test/std/input.output/file.streams/fstreams/ofstream.cons/pointer.pass.cpp (revision b9a2658a3e8bd13b0f9e7a8a440832a95b377216)
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 // explicit basic_ofstream(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 // XFAIL: FROZEN-CXX03-HEADERS-FIXME
22 
23 #include <fstream>
24 #include <cassert>
25 #include <ios>
26 
27 #include "test_macros.h"
28 #include "operator_hijacker.h"
29 #include "platform_support.h"
30 
31 int main(int, char**)
32 {
33     std::string temp = get_temp_file_name();
34 
35     {
36         std::ofstream fs(temp.c_str());
37         fs << 3.25;
38     }
39     {
40         std::ifstream fs(temp.c_str());
41         double x = 0;
42         fs >> x;
43         assert(x == 3.25);
44     }
45     std::remove(temp.c_str());
46 
47     {
48       std::basic_ofstream<char, operator_hijacker_char_traits<char> > fs(temp.c_str());
49       fs << "3.25";
50     }
51     {
52       std::ifstream fs(temp.c_str());
53       double x = 0;
54       fs >> x;
55       assert(x == 3.25);
56     }
57     std::remove(temp.c_str());
58 
59     {
60       std::ofstream fs(temp.c_str(), std::ios_base::out);
61       fs << 3.25;
62     }
63     {
64       std::ifstream fs(temp.c_str());
65       double x = 0;
66       fs >> x;
67       assert(x == 3.25);
68     }
69     std::remove(temp.c_str());
70 
71     {
72       std::basic_ofstream<char, operator_hijacker_char_traits<char> > fs(temp.c_str(), std::ios_base::out);
73       fs << "3.25";
74     }
75     {
76       std::ifstream fs(temp.c_str());
77       double x = 0;
78       fs >> x;
79       assert(x == 3.25);
80     }
81     std::remove(temp.c_str());
82 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
83     {
84         std::wofstream fs(temp.c_str());
85         fs << 3.25;
86     }
87     {
88         std::wifstream fs(temp.c_str());
89         double x = 0;
90         fs >> x;
91         assert(x == 3.25);
92     }
93     std::remove(temp.c_str());
94 
95     {
96       std::basic_ofstream<wchar_t, operator_hijacker_char_traits<wchar_t> > fs(temp.c_str());
97       fs << L"3.25";
98     }
99     {
100       std::ifstream fs(temp.c_str());
101       double x = 0;
102       fs >> x;
103       assert(x == 3.25);
104     }
105     std::remove(temp.c_str());
106 
107     {
108       std::wofstream fs(temp.c_str(), std::ios_base::out);
109       fs << 3.25;
110     }
111     {
112       std::wifstream fs(temp.c_str());
113       double x = 0;
114       fs >> x;
115       assert(x == 3.25);
116     }
117     std::remove(temp.c_str());
118 
119     {
120       std::basic_ofstream<wchar_t, operator_hijacker_char_traits<wchar_t> > fs(temp.c_str(), std::ios_base::out);
121       fs << L"3.25";
122     }
123     {
124       std::ifstream fs(temp.c_str());
125       double x = 0;
126       fs >> x;
127       assert(x == 3.25);
128     }
129     std::remove(temp.c_str());
130 
131 #endif
132 
133 #if TEST_STD_VER >= 23
134     // Test all the noreplace flag combinations
135     {
136         std::ios_base::openmode modes[] = {
137             std::ios_base::out | std::ios_base::noreplace,
138             std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,
139             std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,
140             std::ios_base::out | std::ios_base::noreplace | std::ios_base::binary,
141             std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace | std::ios_base::binary,
142             std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace |
143                 std::ios_base::binary,
144         };
145         for (auto mode : modes) {
146           std::string tmp = get_temp_file_name(); // also creates the file
147 
148           {
149             std::ofstream f(tmp.c_str(), mode);
150             assert(!f.is_open()); // since it already exists
151           }
152 
153           {
154             std::remove(tmp.c_str());
155 
156             std::ofstream f(tmp.c_str(), mode);
157             assert(f.is_open()); // since it doesn't exist
158           }
159         }
160 
161 #  ifndef TEST_HAS_NO_WIDE_CHARACTERS
162         for (auto mode : modes) {
163           std::string tmp = get_temp_file_name(); // also creates the file
164 
165           {
166             std::wofstream f(tmp.c_str(), mode);
167             assert(!f.is_open()); // since it already exists
168           }
169 
170           {
171             std::remove(tmp.c_str());
172 
173             std::wofstream f(tmp.c_str(), mode);
174             assert(f.is_open()); // since it doesn't exist
175           }
176         }
177 #  endif
178     }
179 #endif // TEST_STD_VER >= 23
180 
181     return 0;
182 }
183