path.concat.pass.cpp 10 KB

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