Debian Codegraph @

libcxx/test/std/input.output/file.streams/fstreams/ofstream.cons/path.pass.cpp

raw
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_ifstream(const T& s, ios_base::openmode mode = ios_base::in); // 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 "test_macros.h"
29 #include "test_iterators.h"
30
31 namespace fs = std::filesystem;
32
33 template <class CharT>
34 constexpr bool test_non_convert_to_path() {
35 // String types
36 static_assert(!std::is_constructible_v<std::ofstream, std::basic_string_view<CharT>>);
37 static_assert(!std::is_constructible_v<std::ofstream, const std::basic_string_view<CharT>>);
38
39 // Char* pointers
40 if constexpr (!std::is_same_v<CharT, char> && !std::is_same_v<CharT, fs::path::value_type>)
41 static_assert(!std::is_constructible_v<std::ofstream, const CharT*>);
42
43 // Iterators
44 static_assert(!std::is_convertible_v<std::ofstream, cpp17_input_iterator<const CharT*>>);
45
46 return true;
47 }
48
49 static_assert(test_non_convert_to_path<char>());
50
51 #if !defined(TEST_HAS_NO_WIDE_CHARACTERS) && !defined(TEST_HAS_OPEN_WITH_WCHAR)
52 static_assert(test_non_convert_to_path<wchar_t>());
53 #endif // !TEST_HAS_NO_WIDE_CHARACTERS && !TEST_HAS_OPEN_WITH_WCHAR
54
55 #ifndef TEST_HAS_NO_CHAR8_T
56 static_assert(test_non_convert_to_path<char8_t>());
57 #endif // TEST_HAS_NO_CHAR8_T
58
59 static_assert(test_non_convert_to_path<char16_t>());
60 static_assert(test_non_convert_to_path<char32_t>());
61
62 int main(int, char**) {
63 fs::path p = get_temp_file_name();
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 {
72 std::ofstream stream(p);
73 stream << 3.25;
74 }
75 {
76 std::ifstream stream(p);
77 double x = 0;
78 stream >> x;
79 assert(x == 3.25);
80 }
81 {
82 std::ifstream stream(p, std::ios_base::out);
83 double x = 0;
84 stream >> x;
85 assert(x == 3.25);
86 }
87 std::remove(p.string().c_str());
88
89 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
90 {
91 std::wofstream stream(p);
92 stream << 3.25;
93 }
94 {
95 std::wifstream stream(p);
96 double x = 0;
97 stream >> x;
98 assert(x == 3.25);
99 }
100 {
101 std::wifstream stream(p, std::ios_base::out);
102 double x = 0;
103 stream >> x;
104 assert(x == 3.25);
105 }
106 std::remove(p.string().c_str());
107 #endif
108
109 return 0;
110 }