1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03
12 // <experimental/filesystem>
16 // template <class Source>
17 // path& operator=(Source const&);
18 // path& operator=(string_type&&);
19 // template <class Source>
20 // path& assign(Source const&);
21 // template <class InputIterator>
22 // path& assign(InputIterator first, InputIterator last);
25 #include <experimental/filesystem>
26 #include <type_traits>
27 #include <string_view>
30 #include "test_macros.h"
31 #include "test_iterators.h"
32 #include "count_new.hpp"
33 #include "filesystem_test_helper.hpp"
36 namespace fs = std::experimental::filesystem;
38 template <class CharT>
39 void RunTestCase(MultiStringType const& MS) {
41 const char* Expect = MS;
42 const CharT* TestPath = MS;
43 const CharT* TestPathEnd = StrEnd(TestPath);
44 const std::size_t Size = TestPathEnd - TestPath;
45 const std::size_t SSize = StrEnd(Expect) - Expect;
46 assert(Size == SSize);
47 //////////////////////////////////////////////////////////////////////////////
48 // basic_string<Char, Traits, Alloc>
50 const std::basic_string<CharT> S(TestPath);
51 path p; PathReserve(p, S.length() + 1);
53 // string provides a contiguous iterator. No allocation needed.
54 DisableAllocationGuard g;
58 assert(p.native() == Expect);
59 assert(p.string<CharT>() == TestPath);
60 assert(p.string<CharT>() == S);
63 const std::basic_string<CharT> S(TestPath);
64 path p; PathReserve(p, S.length() + 1);
66 DisableAllocationGuard g;
67 path& pref = p.assign(S);
70 assert(p.native() == Expect);
71 assert(p.string<CharT>() == TestPath);
72 assert(p.string<CharT>() == S);
74 // basic_string<Char, Traits, Alloc>
76 const std::basic_string_view<CharT> S(TestPath);
77 path p; PathReserve(p, S.length() + 1);
79 // string provides a contiguous iterator. No allocation needed.
80 DisableAllocationGuard g;
84 assert(p.native() == Expect);
85 assert(p.string<CharT>() == TestPath);
86 assert(p.string<CharT>() == S);
89 const std::basic_string_view<CharT> S(TestPath);
90 path p; PathReserve(p, S.length() + 1);
92 DisableAllocationGuard g;
93 path& pref = p.assign(S);
96 assert(p.native() == Expect);
97 assert(p.string<CharT>() == TestPath);
98 assert(p.string<CharT>() == S);
100 //////////////////////////////////////////////////////////////////////////////
103 path p; PathReserve(p, Size + 1);
105 // char* pointers are contiguous and can be used with code_cvt directly.
106 // no allocations needed.
107 DisableAllocationGuard g;
108 path& pref = (p = TestPath);
111 assert(p.native() == Expect);
112 assert(p.string<CharT>() == TestPath);
115 path p; PathReserve(p, Size + 1);
117 DisableAllocationGuard g;
118 path& pref = p.assign(TestPath);
121 assert(p.native() == Expect);
122 assert(p.string<CharT>() == TestPath);
125 path p; PathReserve(p, Size + 1);
127 DisableAllocationGuard g;
128 path& pref = p.assign(TestPath, TestPathEnd);
131 assert(p.native() == Expect);
132 assert(p.string<CharT>() == TestPath);
134 //////////////////////////////////////////////////////////////////////////////
137 using It = input_iterator<const CharT*>;
138 path p; PathReserve(p, Size + 1);
141 // Iterators cannot be used with code_cvt directly. This assignment
142 // may allocate if it's larger than a "short-string".
143 path& pref = (p = it);
146 assert(p.native() == Expect);
147 assert(p.string<CharT>() == TestPath);
150 using It = input_iterator<const CharT*>;
151 path p; PathReserve(p, Size + 1);
154 path& pref = p.assign(it);
157 assert(p.native() == Expect);
158 assert(p.string<CharT>() == TestPath);
161 using It = input_iterator<const CharT*>;
162 path p; PathReserve(p, Size + 1);
166 path& pref = p.assign(it, e);
169 assert(p.native() == Expect);
170 assert(p.string<CharT>() == TestPath);
174 template <class It, class = decltype(fs::path{}.assign(std::declval<It>()))>
175 constexpr bool has_assign(int) { return true; }
177 constexpr bool has_assign(long) { return false; }
179 constexpr bool has_assign() { return has_assign<It>(0); }
184 using It = const char* const;
185 static_assert(std::is_assignable<path, It>::value, "");
186 static_assert(has_assign<It>(), "");
189 using It = input_iterator<const char*>;
190 static_assert(std::is_assignable<path, It>::value, "");
191 static_assert(has_assign<It>(), "");
195 using iterator_category = std::input_iterator_tag;
196 using value_type = const char;
197 using pointer = const char*;
198 using reference = const char&;
199 using difference_type = std::ptrdiff_t;
201 using It = input_iterator<const char*, Traits>;
202 static_assert(std::is_assignable<path, It>::value, "");
203 static_assert(has_assign<It>(), "");
206 using It = output_iterator<const char*>;
207 static_assert(!std::is_assignable<path, It>::value, "");
208 static_assert(!has_assign<It>(), "");
212 static_assert(!std::is_assignable<path, int*>::value, "");
213 static_assert(!has_assign<int*>(), "");
217 void RunStringMoveTest(const char* Expect) {
219 std::string ss(Expect);
222 DisableAllocationGuard g; ((void)g);
223 path& pr = (p = std::move(ss));
229 ASSERT_NOEXCEPT(p = std::move(ss));
234 for (auto const& MS : PathList) {
235 RunTestCase<char>(MS);
236 RunTestCase<wchar_t>(MS);
237 RunTestCase<char16_t>(MS);
238 RunTestCase<char32_t>(MS);
239 RunStringMoveTest(MS);