Debian Codegraph @

libcxx/test/std/input.output/file.streams/fstreams/ifstream.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 // FILE_DEPENDENCIES: test.dat
14
15 // <fstream>
16
17 // template <class charT, class traits = char_traits<charT> >
18 // class basic_ifstream
19
20 // template<class T>
21 // explicit basic_ifstream(const T& s, ios_base::openmode mode = ios_base::in); // Since C++17
22 // Constraints: is_same_v<T, filesystem::path> is true
23
24 #include <cassert>
25 #include <filesystem>
26 #include <fstream>
27 #include <type_traits>
28
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::ifstream, std::basic_string_view<CharT>>);
38 static_assert(!std::is_constructible_v<std::ifstream, 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::ifstream, const CharT*>);
43
44 // Iterators
45 static_assert(!std::is_convertible_v<std::ifstream, 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 fs::path p;
66 static_assert(!std::is_convertible<fs::path, std::ifstream>::value,
67 "ctor should be explicit");
68 static_assert(std::is_constructible<std::ifstream, fs::path const&,
69 std::ios_base::openmode>::value,
70 "");
71 }
72 {
73 std::ifstream fs(fs::path("test.dat"));
74 double x = 0;
75 fs >> x;
76 assert(x == 3.25);
77 }
78 // std::ifstream(const fs::path&, std::ios_base::openmode) is tested in
79 // test/std/input.output/file.streams/fstreams/ofstream.cons/string.pass.cpp
80 // which creates writable files.
81
82 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
83 {
84 std::wifstream fs(fs::path("test.dat"));
85 double x = 0;
86 fs >> x;
87 assert(x == 3.25);
88 }
89 // std::wifstream(const fs::path&, std::ios_base::openmode) is tested in
90 // test/std/input.output/file.streams/fstreams/ofstream.cons/string.pass.cpp
91 // which creates writable files.
92 #endif
93
94 return 0;
95 }