path.concat.pass.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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); // Implemented as Source template
  14. // path& operator+=(const value_type* x); // Implemented as Source template
  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 <experimental/filesystem>
  25. #include <type_traits>
  26. #include <cassert>
  27. #include "test_macros.h"
  28. #include "test_iterators.h"
  29. #include "count_new.hpp"
  30. #include "filesystem_test_helper.hpp"
  31. namespace fs = std::experimental::filesystem;
  32. struct ConcatOperatorTestcase {
  33. MultiStringType lhs;
  34. MultiStringType rhs;
  35. MultiStringType expect;
  36. };
  37. #define LONGSTR "LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR_LONGSTR"
  38. #define S(Str) MKSTR(Str)
  39. const ConcatOperatorTestcase Cases[] =
  40. {
  41. {S(""), S(""), S("")}
  42. , {S("p1"), S("p2"), S("p1p2")}
  43. , {S("p1/"), S("/p2"), S("p1//p2")}
  44. , {S(""), S("\\foo/bar/baz"), S("\\foo/bar/baz")}
  45. , {S("c:\\foo"), S(""), S("c:\\foo")}
  46. , {S(LONGSTR), S("foo"), S(LONGSTR "foo")}
  47. , {S("abcdefghijklmnopqrstuvwxyz/\\"), S("/\\123456789"), S("abcdefghijklmnopqrstuvwxyz/\\/\\123456789")}
  48. };
  49. const ConcatOperatorTestcase LongLHSCases[] =
  50. {
  51. {S(""), S(LONGSTR), S(LONGSTR)}
  52. , {S("p1/"), S(LONGSTR), S("p1/" LONGSTR)}
  53. };
  54. const ConcatOperatorTestcase CharTestCases[] =
  55. {
  56. {S(""), S("P"), S("P")}
  57. , {S("/fooba"), S("r"), S("/foobar")}
  58. };
  59. #undef S
  60. #undef LONGSTR
  61. // The concat operator may need to allocate a temporary buffer before a code_cvt
  62. // conversion. Test if this allocation occurs by:
  63. // 1. Create a path, `LHS`, and reserve enough space to append `RHS`.
  64. // This prevents `LHS` from allocating during the actual appending.
  65. // 2. Create a `Source` object `RHS`, which represents a "large" string.
  66. // (The string must not trigger the SSO)
  67. // 3. Concat `RHS` to `LHS` and check for the expected allocation behavior.
  68. template <class CharT>
  69. void doConcatSourceAllocTest(ConcatOperatorTestcase const& TC)
  70. {
  71. using namespace fs;
  72. using Ptr = CharT const*;
  73. using Str = std::basic_string<CharT>;
  74. using InputIter = input_iterator<Ptr>;
  75. const Ptr L = TC.lhs;
  76. const Ptr R = TC.rhs;
  77. const Ptr E = TC.expect;
  78. std::size_t ReserveSize = StrLen(E) + 1;
  79. // basic_string
  80. {
  81. path LHS(L); PathReserve(LHS, ReserveSize);
  82. Str RHS(R);
  83. {
  84. DisableAllocationGuard g;
  85. LHS += RHS;
  86. }
  87. assert(LHS == E);
  88. }
  89. // CharT*
  90. {
  91. path LHS(L); PathReserve(LHS, ReserveSize);
  92. Ptr RHS(R);
  93. {
  94. DisableAllocationGuard g;
  95. LHS += RHS;
  96. }
  97. assert(LHS == E);
  98. }
  99. {
  100. path LHS(L); PathReserve(LHS, ReserveSize);
  101. Ptr RHS(R);
  102. {
  103. DisableAllocationGuard g;
  104. LHS.concat(RHS, StrEnd(RHS));
  105. }
  106. assert(LHS == E);
  107. }
  108. // input iterator - For non-native char types, appends needs to copy the
  109. // iterator range into a contigious block of memory before it can perform the
  110. // code_cvt conversions.
  111. // For "char" no allocations will be performed because no conversion is
  112. // required.
  113. bool DisableAllocations = std::is_same<CharT, char>::value;
  114. {
  115. path LHS(L); PathReserve(LHS, ReserveSize);
  116. InputIter RHS(R);
  117. {
  118. RequireAllocationGuard g; // requires 1 or more allocations occur by default
  119. if (DisableAllocations) g.requireExactly(0);
  120. LHS += RHS;
  121. }
  122. assert(LHS == E);
  123. }
  124. {
  125. path LHS(L); PathReserve(LHS, ReserveSize);
  126. InputIter RHS(R);
  127. InputIter REnd(StrEnd(R));
  128. {
  129. RequireAllocationGuard g;
  130. if (DisableAllocations) g.requireExactly(0);
  131. LHS.concat(RHS, REnd);
  132. }
  133. assert(LHS == E);
  134. }
  135. }
  136. template <class CharT>
  137. void doConcatSourceTest(ConcatOperatorTestcase const& TC)
  138. {
  139. using namespace fs;
  140. using Ptr = CharT const*;
  141. using Str = std::basic_string<CharT>;
  142. using InputIter = input_iterator<Ptr>;
  143. const Ptr L = TC.lhs;
  144. const Ptr R = TC.rhs;
  145. const Ptr E = TC.expect;
  146. // basic_string
  147. {
  148. path LHS(L);
  149. Str RHS(R);
  150. path& Ref = (LHS += RHS);
  151. assert(LHS == E);
  152. assert(&Ref == &LHS);
  153. }
  154. {
  155. path LHS(L);
  156. Str RHS(R);
  157. path& Ref = LHS.concat(RHS);
  158. assert(LHS == E);
  159. assert(&Ref == &LHS);
  160. }
  161. // Char*
  162. {
  163. path LHS(L);
  164. Str RHS(R);
  165. path& Ref = (LHS += RHS);
  166. assert(LHS == E);
  167. assert(&Ref == &LHS);
  168. }
  169. {
  170. path LHS(L);
  171. Ptr RHS(R);
  172. path& Ref = LHS.concat(RHS);
  173. assert(LHS == E);
  174. assert(&Ref == &LHS);
  175. }
  176. {
  177. path LHS(L);
  178. Ptr RHS(R);
  179. path& Ref = LHS.concat(RHS, StrEnd(RHS));
  180. assert(LHS == E);
  181. assert(&Ref == &LHS);
  182. }
  183. // iterators
  184. {
  185. path LHS(L);
  186. InputIter RHS(R);
  187. path& Ref = (LHS += RHS);
  188. assert(LHS == E);
  189. assert(&Ref == &LHS);
  190. }
  191. {
  192. path LHS(L); InputIter RHS(R);
  193. path& Ref = LHS.concat(RHS);
  194. assert(LHS == E);
  195. assert(&Ref == &LHS);
  196. }
  197. {
  198. path LHS(L);
  199. InputIter RHS(R);
  200. InputIter REnd(StrEnd(R));
  201. path& Ref = LHS.concat(RHS, REnd);
  202. assert(LHS == E);
  203. assert(&Ref == &LHS);
  204. }
  205. }
  206. template <class CharT>
  207. void doConcatECharTest(ConcatOperatorTestcase const& TC)
  208. {
  209. using namespace fs;
  210. using Ptr = CharT const*;
  211. const Ptr RStr = TC.rhs;
  212. assert(StrLen(RStr) == 1);
  213. const Ptr L = TC.lhs;
  214. const CharT R = RStr[0];
  215. const Ptr E = TC.expect;
  216. {
  217. path LHS(L);
  218. path& Ref = (LHS += R);
  219. assert(LHS == E);
  220. assert(&Ref == &LHS);
  221. }
  222. }
  223. int main()
  224. {
  225. using namespace fs;
  226. for (auto const & TC : Cases) {
  227. {
  228. path LHS((const char*)TC.lhs);
  229. path RHS((const char*)TC.rhs);
  230. path& Ref = (LHS += RHS);
  231. assert(LHS == (const char*)TC.expect);
  232. assert(&Ref == &LHS);
  233. }
  234. doConcatSourceTest<char> (TC);
  235. doConcatSourceTest<wchar_t> (TC);
  236. doConcatSourceTest<char16_t>(TC);
  237. doConcatSourceTest<char32_t>(TC);
  238. }
  239. for (auto const & TC : LongLHSCases) {
  240. // Do path test
  241. {
  242. path LHS((const char*)TC.lhs);
  243. path RHS((const char*)TC.rhs);
  244. const char* E = TC.expect;
  245. PathReserve(LHS, StrLen(E) + 5);
  246. {
  247. DisableAllocationGuard g;
  248. path& Ref = (LHS += RHS);
  249. assert(&Ref == &LHS);
  250. }
  251. assert(LHS == E);
  252. }
  253. doConcatSourceAllocTest<char>(TC);
  254. doConcatSourceAllocTest<wchar_t>(TC);
  255. }
  256. for (auto const& TC : CharTestCases) {
  257. doConcatECharTest<char>(TC);
  258. doConcatECharTest<wchar_t>(TC);
  259. doConcatECharTest<char16_t>(TC);
  260. doConcatECharTest<char32_t>(TC);
  261. }
  262. }