xref: /llvm-project/libcxx/test/std/input.output/file.streams/fstreams/fstream.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_fstream
13 
14 // explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in | 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 
26 #include "test_macros.h"
27 #include "platform_support.h"
28 #include "operator_hijacker.h"
29 
30 int main(int, char**)
31 {
32     std::string temp = get_temp_file_name();
33     {
34         std::fstream fs(temp.c_str(), std::ios_base::in | std::ios_base::out
35                                                 | std::ios_base::trunc);
36         double x = 0;
37         fs << 3.25;
38         fs.seekg(0);
39         fs >> x;
40         assert(x == 3.25);
41     }
42     std::remove(temp.c_str());
43 
44     {
45       std::basic_fstream<char, operator_hijacker_char_traits<char> > fs(
46           temp.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::trunc);
47       std::basic_string<char, operator_hijacker_char_traits<char> > x;
48       fs << "3.25";
49       fs.seekg(0);
50       fs >> x;
51       assert(x == "3.25");
52     }
53     std::remove(temp.c_str());
54 
55 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
56     {
57         std::wfstream fs(temp.c_str(), std::ios_base::in | std::ios_base::out
58                                                  | std::ios_base::trunc);
59         double x = 0;
60         fs << 3.25;
61         fs.seekg(0);
62         fs >> x;
63         assert(x == 3.25);
64     }
65     std::remove(temp.c_str());
66 
67     {
68       std::basic_fstream<wchar_t, operator_hijacker_char_traits<wchar_t> > fs(
69           temp.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::trunc);
70       std::basic_string<wchar_t, operator_hijacker_char_traits<wchar_t> > x;
71       fs << L"3.25";
72       fs.seekg(0);
73       fs >> x;
74       assert(x == L"3.25");
75     }
76     std::remove(temp.c_str());
77 #endif
78 
79 #if TEST_STD_VER >= 23
80     // Test all the noreplace flag combinations
81     {
82         std::ios_base::openmode modes[] = {
83             std::ios_base::out | std::ios_base::noreplace,
84             std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,
85             std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,
86             std::ios_base::out | std::ios_base::noreplace | std::ios_base::binary,
87             std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace | std::ios_base::binary,
88             std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace |
89                 std::ios_base::binary,
90         };
91         for (auto mode : modes) {
92           std::string tmp = get_temp_file_name(); // also creates the file
93 
94           {
95             std::fstream f(tmp.c_str(), mode);
96             assert(!f.is_open()); // since it already exists
97           }
98 
99           {
100             std::remove(tmp.c_str());
101 
102             std::fstream f(tmp.c_str(), mode);
103             assert(f.is_open()); // since it doesn't exist
104           }
105         }
106 
107 #  ifndef TEST_HAS_NO_WIDE_CHARACTERS
108         for (auto mode : modes) {
109           std::string tmp = get_temp_file_name(); // also creates the file
110 
111           {
112             std::wfstream f(tmp.c_str(), mode);
113             assert(!f.is_open()); // since it already exists
114           }
115 
116           {
117             std::remove(tmp.c_str());
118 
119             std::wfstream f(tmp.c_str(), mode);
120             assert(f.is_open()); // since it doesn't exist
121           }
122         }
123 #  endif
124     }
125 #endif // TEST_STD_VER >= 23
126 
127     return 0;
128 }
129