xref: /llvm-project/libcxx/test/std/input.output/file.streams/fstreams/ofstream.cons/path.pass.cpp (revision 4dee6411e0d993fd17099bd7564276474412383e)
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 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // UNSUPPORTED: availability-filesystem-missing
12 
13 // <fstream>
14 
15 // plate <class charT, class traits = char_traits<charT> >
16 // class basic_ofstream
17 
18 // template<class T>
19 // explicit basic_ofstream(const T& s, ios_base::openmode mode = ios_base::out); // Since C++17
20 // Constraints: is_same_v<T, filesystem::path> is true
21 
22 #include <cassert>
23 #include <filesystem>
24 #include <fstream>
25 #include <type_traits>
26 
27 #include "platform_support.h"
28 #include "operator_hijacker.h"
29 #include "test_macros.h"
30 #include "test_iterators.h"
31 
32 namespace fs = std::filesystem;
33 
34 template <class CharT>
35 constexpr bool test_non_convert_to_path() {
36   // String types
37   static_assert(!std::is_constructible_v<std::ofstream, std::basic_string_view<CharT>>);
38   static_assert(!std::is_constructible_v<std::ofstream, const std::basic_string_view<CharT>>);
39 
40   // Char* pointers
41   if constexpr (!std::is_same_v<CharT, char> && !std::is_same_v<CharT, fs::path::value_type>)
42     static_assert(!std::is_constructible_v<std::ofstream, const CharT*>);
43 
44   // Iterators
45   static_assert(!std::is_convertible_v<std::ofstream, cpp17_input_iterator<const CharT*>>);
46 
47   return true;
48 }
49 
50 static_assert(test_non_convert_to_path<char>());
51 
52 #if !defined(TEST_HAS_NO_WIDE_CHARACTERS) && !defined(TEST_HAS_OPEN_WITH_WCHAR)
53 static_assert(test_non_convert_to_path<wchar_t>());
54 #endif // !TEST_HAS_NO_WIDE_CHARACTERS && !TEST_HAS_OPEN_WITH_WCHAR
55 
56 #ifndef TEST_HAS_NO_CHAR8_T
57 static_assert(test_non_convert_to_path<char8_t>());
58 #endif // TEST_HAS_NO_CHAR8_T
59 
60 static_assert(test_non_convert_to_path<char16_t>());
61 static_assert(test_non_convert_to_path<char32_t>());
62 
63 int main(int, char**) {
64   {
65     static_assert(!std::is_convertible<fs::path, std::ofstream>::value,
66                   "ctor should be explicit");
67     static_assert(std::is_constructible<std::ofstream, fs::path const&,
68                                         std::ios_base::openmode>::value,
69                   "");
70   }
71   fs::path p = get_temp_file_name();
72   {
73     std::ofstream stream(p);
74     stream << 3.25;
75   }
76   {
77     std::ifstream stream(p);
78     double x = 0;
79     stream >> x;
80     assert(x == 3.25);
81   }
82   std::remove(p.string().c_str());
83 
84   {
85     std::basic_ofstream<char, operator_hijacker_char_traits<char> > stream(p);
86     stream << "3.25";
87   }
88   {
89     std::ifstream stream(p);
90     double x = 0;
91     stream >> x;
92     assert(x == 3.25);
93   }
94   std::remove(p.string().c_str());
95 
96   {
97     std::ofstream stream(p, std::ios_base::out);
98     stream << 3.25;
99   }
100   {
101     std::ifstream stream(p);
102     double x = 0;
103     stream >> x;
104     assert(x == 3.25);
105   }
106   std::remove(p.string().c_str());
107 
108   {
109     std::basic_ofstream<char, operator_hijacker_char_traits<char> > stream(p, std::ios_base::out);
110     stream << "3.25";
111   }
112   {
113     std::ifstream stream(p);
114     double x = 0;
115     stream >> x;
116     assert(x == 3.25);
117   }
118   std::remove(p.string().c_str());
119 
120 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
121   {
122     std::wofstream stream(p);
123     stream << 3.25;
124   }
125   {
126     std::wifstream stream(p);
127     double x = 0;
128     stream >> x;
129     assert(x == 3.25);
130   }
131   std::remove(p.string().c_str());
132 
133   {
134     std::basic_ofstream<wchar_t, operator_hijacker_char_traits<wchar_t> > stream(p);
135     stream << L"3.25";
136   }
137   {
138     std::wifstream stream(p);
139     double x = 0;
140     stream >> x;
141     assert(x == 3.25);
142   }
143   std::remove(p.string().c_str());
144 
145   {
146     std::wofstream stream(p, std::ios_base::out);
147     stream << 3.25;
148   }
149   {
150     std::wifstream stream(p);
151     double x = 0;
152     stream >> x;
153     assert(x == 3.25);
154   }
155   std::remove(p.string().c_str());
156 
157   {
158     std::basic_ofstream<wchar_t, operator_hijacker_char_traits<wchar_t> > stream(p, std::ios_base::out);
159     stream << L"3.25";
160   }
161   {
162     std::wifstream stream(p);
163     double x = 0;
164     stream >> x;
165     assert(x == 3.25);
166   }
167   std::remove(p.string().c_str());
168 #endif
169 
170   return 0;
171 }
172