path.concat.pass.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // UNSUPPORTED: c++98, c++03
  10. // <experimental/filesystem>
  11. // class path
  12. // path& operator+=(const path& x);
  13. // path& operator+=(const string_type& x);
  14. // path& operator+=(string_view x);
  15. // path& operator+=(const value_type* x);
  16. // path& operator+=(value_type x);
  17. // template <class Source>
  18. // path& operator+=(const Source& x);
  19. // template <class EcharT>
  20. // path& operator+=(EcharT x);
  21. // template <class Source>
  22. // path& concat(const Source& x);
  23. // template <class InputIterator>
  24. // path& concat(InputIterator first, InputIterator last);
  25. #include "filesystem_include.hpp"
  26. #include <type_traits>
  27. #include <string>
  28. #include <string_view>
  29. #include <cassert>
  30. #include "test_macros.h"
  31. #include "test_iterators.h"
  32. #include "count_new.hpp"
  33. #include "filesystem_test_helper.hpp"
  34. struct ConcatOperatorTestcase {
  35. MultiStringType lhs;
  36. MultiStringType rhs;
  37. MultiStringType expect;
  38. };
  39. #define LONGSTR "LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR"
  40. #define S(Str) MKSTR(Str)
  41. const ConcatOperatorTestcase Cases[] =
  42. {
  43. {S(""), S(""), S("")}
  44. , {S("p1"), S("p2"), S("p1p2")}
  45. , {S("p1/"), S("/p2"), S("p1//p2")}
  46. , {S(""), S("\\foo/bar/baz"), S("\\foo/bar/baz")}
  47. , {S("c:\\foo"), S(""), S("c:\\foo")}
  48. , {S(LONGSTR), S("foo"), S(LONGSTR "foo")}
  49. , {S("abcdefghijklmnopqrstuvwxyz/\\"), S("/\\123456789"), S("abcdefghijklmnopqrstuvwxyz/\\/\\123456789")}
  50. };
  51. const ConcatOperatorTestcase LongLHSCases[] =
  52. {
  53. {S(""), S(LONGSTR), S(LONGSTR)}
  54. , {S("p1/"), S(LONGSTR), S("p1/" LONGSTR)}
  55. };
  56. const ConcatOperatorTestcase CharTestCases[] =
  57. {
  58. {S(""), S("P"), S("P")}
  59. , {S("/fooba"), S("r"), S("/foobar")}
  60. };
  61. #undef S
  62. #undef LONGSTR
  63. // The concat operator may need to allocate a temporary buffer before a code_cvt
  64. // conversion. Test if this allocation occurs by:
  65. // 1. Create a path, `LHS`, and reserve enough space to append `RHS`.
  66. // This prevents `LHS` from allocating during the actual appending.
  67. // 2. Create a `Source` object `RHS`, which represents a "large" string.
  68. // (The string must not trigger the SSO)
  69. // 3. Concat `RHS` to `LHS` and check for the expected allocation behavior.
  70. template <class CharT>
  71. void doConcatSourceAllocTest(ConcatOperatorTestcase const& TC)
  72. {
  73. using namespace fs;
  74. using Ptr = CharT const*;
  75. using Str = std::basic_string<CharT>;
  76. using StrView = std::basic_string_view<CharT>;
  77. using InputIter = input_iterator<Ptr>;
  78. const Ptr L = TC.lhs;
  79. const Ptr R = TC.rhs;
  80. const Ptr E = TC.expect;
  81. std::size_t ReserveSize = StrLen(E) + 1;
  82. // basic_string
  83. {
  84. path LHS(L); PathReserve(LHS, ReserveSize);
  85. Str RHS(R);
  86. {
  87. DisableAllocationGuard g;
  88. LHS += RHS;
  89. }
  90. assert(LHS == E);
  91. }
  92. // basic_string_view
  93. {
  94. path LHS(L); PathReserve(LHS, ReserveSize);
  95. StrView RHS(R);
  96. {
  97. DisableAllocationGuard g;
  98. LHS += RHS;
  99. }
  100. assert(LHS == E);
  101. }
  102. // CharT*
  103. {
  104. path LHS(L); PathReserve(LHS, ReserveSize);
  105. Ptr RHS(R);
  106. {
  107. DisableAllocationGuard g;
  108. LHS += RHS;
  109. }
  110. assert(LHS == E);
  111. }
  112. {
  113. path LHS(L); PathReserve(LHS, ReserveSize);
  114. Ptr RHS(R);
  115. {
  116. DisableAllocationGuard g;
  117. LHS.concat(RHS, StrEnd(RHS));
  118. }
  119. assert(LHS == E);
  120. }
  121. // input iterator - For non-native char types, appends needs to copy the
  122. // iterator range into a contiguous block of memory before it can perform the
  123. // code_cvt conversions.
  124. // For "char" no allocations will be performed because no conversion is
  125. // required.
  126. bool DisableAllocations = std::is_same<CharT, char>::value;
  127. {
  128. path LHS(L); PathReserve(LHS, ReserveSize);
  129. InputIter RHS(R);
  130. {
  131. RequireAllocationGuard g; // requires 1 or more allocations occur by default
  132. if (DisableAllocations) g.requireExactly(0);
  133. LHS += RHS;
  134. }
  135. assert(LHS == E);
  136. }
  137. {
  138. path LHS(L); PathReserve(LHS, ReserveSize);
  139. InputIter RHS(R);
  140. InputIter REnd(StrEnd(R));
  141. {
  142. RequireAllocationGuard g;
  143. if (DisableAllocations) g.requireExactly(0);
  144. LHS.concat(RHS, REnd);
  145. }
  146. assert(LHS == E);
  147. }
  148. }
  149. template <class CharT>
  150. void doConcatSourceTest(ConcatOperatorTestcase const& TC)
  151. {
  152. using namespace fs;
  153. using Ptr = CharT const*;
  154. using Str = std::basic_string<CharT>;
  155. using StrView = std::basic_string_view<CharT>;
  156. using InputIter = input_iterator<Ptr>;
  157. const Ptr L = TC.lhs;
  158. const Ptr R = TC.rhs;
  159. const Ptr E = TC.expect;
  160. // basic_string
  161. {
  162. path LHS(L);
  163. Str RHS(R);
  164. path& Ref = (LHS += RHS);
  165. assert(LHS == E);
  166. assert(&Ref == &LHS);
  167. }
  168. {
  169. path LHS(L);
  170. Str RHS(R);
  171. path& Ref = LHS.concat(RHS);
  172. assert(LHS == E);
  173. assert(&Ref == &LHS);
  174. }
  175. // basic_string_view
  176. {
  177. path LHS(L);
  178. StrView RHS(R);
  179. path& Ref = (LHS += RHS);
  180. assert(LHS == E);
  181. assert(&Ref == &LHS);
  182. }
  183. {
  184. path LHS(L);
  185. StrView RHS(R);
  186. path& Ref = LHS.concat(RHS);
  187. assert(LHS == E);
  188. assert(&Ref == &LHS);
  189. }
  190. // Char*
  191. {
  192. path LHS(L);
  193. Str RHS(R);
  194. path& Ref = (LHS += RHS);
  195. assert(LHS == E);
  196. assert(&Ref == &LHS);
  197. }
  198. {
  199. path LHS(L);
  200. Ptr RHS(R);
  201. path& Ref = LHS.concat(RHS);
  202. assert(LHS == E);
  203. assert(&Ref == &LHS);
  204. }
  205. {
  206. path LHS(L);
  207. Ptr RHS(R);
  208. path& Ref = LHS.concat(RHS, StrEnd(RHS));
  209. assert(LHS == E);
  210. assert(&Ref == &LHS);
  211. }
  212. // iterators
  213. {
  214. path LHS(L);
  215. InputIter RHS(R);
  216. path& Ref = (LHS += RHS);
  217. assert(LHS == E);
  218. assert(&Ref == &LHS);
  219. }
  220. {
  221. path LHS(L); InputIter RHS(R);
  222. path& Ref = LHS.concat(RHS);
  223. assert(LHS == E);
  224. assert(&Ref == &LHS);
  225. }
  226. {
  227. path LHS(L);
  228. InputIter RHS(R);
  229. InputIter REnd(StrEnd(R));
  230. path& Ref = LHS.concat(RHS, REnd);
  231. assert(LHS == E);
  232. assert(&Ref == &LHS);
  233. }
  234. }
  235. template <class CharT>
  236. void doConcatECharTest(ConcatOperatorTestcase const& TC)
  237. {
  238. using namespace fs;
  239. using Ptr = CharT const*;
  240. const Ptr RStr = TC.rhs;
  241. assert(StrLen(RStr) == 1);
  242. const Ptr L = TC.lhs;
  243. const CharT R = RStr[0];
  244. const Ptr E = TC.expect;
  245. {
  246. path LHS(L);
  247. path& Ref = (LHS += R);
  248. assert(LHS == E);
  249. assert(&Ref == &LHS);
  250. }
  251. }
  252. template <class It, class = decltype(fs::path{}.concat(std::declval<It>()))>
  253. constexpr bool has_concat(int) { return true; }
  254. template <class It>
  255. constexpr bool has_concat(long) { return false; }
  256. template <class It, class = decltype(fs::path{}.operator+=(std::declval<It>()))>
  257. constexpr bool has_concat_op(int) { return true; }
  258. template <class It>
  259. constexpr bool has_concat_op(long) { return false; }
  260. template <class It>
  261. constexpr bool has_concat_op() { return has_concat_op<It>(0); }
  262. template <class It>
  263. constexpr bool has_concat() {
  264. static_assert(has_concat<It>(0) == has_concat_op<It>(0), "must be same");
  265. return has_concat<It>(0) && has_concat_op<It>(0);
  266. }
  267. void test_sfinae() {
  268. using namespace fs;
  269. {
  270. static_assert(has_concat_op<char>(), "");
  271. static_assert(has_concat_op<const char>(), "");
  272. static_assert(has_concat_op<char16_t>(), "");
  273. static_assert(has_concat_op<const char16_t>(), "");
  274. }
  275. {
  276. using It = const char* const;
  277. static_assert(has_concat<It>(), "");
  278. }
  279. {
  280. using It = input_iterator<const char*>;
  281. static_assert(has_concat<It>(), "");
  282. }
  283. {
  284. struct Traits {
  285. using iterator_category = std::input_iterator_tag;
  286. using value_type = const char;
  287. using pointer = const char*;
  288. using reference = const char&;
  289. using difference_type = std::ptrdiff_t;
  290. };
  291. using It = input_iterator<const char*, Traits>;
  292. static_assert(has_concat<It>(), "");
  293. }
  294. {
  295. using It = output_iterator<const char*>;
  296. static_assert(!has_concat<It>(), "");
  297. }
  298. {
  299. static_assert(!has_concat<int>(0), "");
  300. // operator+=(int) is well formed since it converts to operator+=(value_type)
  301. // but concat(int) isn't valid because there is no concat(value_type).
  302. // This should probably be addressed by a LWG issue.
  303. static_assert(has_concat_op<int>(), "");
  304. }
  305. {
  306. static_assert(!has_concat<int*>(), "");
  307. }
  308. }
  309. int main()
  310. {
  311. using namespace fs;
  312. for (auto const & TC : Cases) {
  313. {
  314. path LHS((const char*)TC.lhs);
  315. path RHS((const char*)TC.rhs);
  316. path& Ref = (LHS += RHS);
  317. assert(LHS == (const char*)TC.expect);
  318. assert(&Ref == &LHS);
  319. }
  320. {
  321. path LHS((const char*)TC.lhs);
  322. std::string_view RHS((const char*)TC.rhs);
  323. path& Ref = (LHS += RHS);
  324. assert(LHS == (const char*)TC.expect);
  325. assert(&Ref == &LHS);
  326. }
  327. doConcatSourceTest<char> (TC);
  328. doConcatSourceTest<wchar_t> (TC);
  329. doConcatSourceTest<char16_t>(TC);
  330. doConcatSourceTest<char32_t>(TC);
  331. }
  332. for (auto const & TC : LongLHSCases) {
  333. // Do path test
  334. {
  335. path LHS((const char*)TC.lhs);
  336. path RHS((const char*)TC.rhs);
  337. const char* E = TC.expect;
  338. PathReserve(LHS, StrLen(E) + 5);
  339. {
  340. DisableAllocationGuard g;
  341. path& Ref = (LHS += RHS);
  342. assert(&Ref == &LHS);
  343. }
  344. assert(LHS == E);
  345. }
  346. {
  347. path LHS((const char*)TC.lhs);
  348. std::string_view RHS((const char*)TC.rhs);
  349. const char* E = TC.expect;
  350. PathReserve(LHS, StrLen(E) + 5);
  351. {
  352. DisableAllocationGuard g;
  353. path& Ref = (LHS += RHS);
  354. assert(&Ref == &LHS);
  355. }
  356. assert(LHS == E);
  357. }
  358. doConcatSourceAllocTest<char>(TC);
  359. doConcatSourceAllocTest<wchar_t>(TC);
  360. }
  361. for (auto const& TC : CharTestCases) {
  362. doConcatECharTest<char>(TC);
  363. doConcatECharTest<wchar_t>(TC);
  364. doConcatECharTest<char16_t>(TC);
  365. doConcatECharTest<char32_t>(TC);
  366. }
  367. test_sfinae();
  368. }