path.append.pass.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/=(path const&)
  13. // template <class Source>
  14. // path& operator/=(Source const&);
  15. // template <class Source>
  16. // path& append(Source const&);
  17. // template <class InputIterator>
  18. // path& append(InputIterator first, InputIterator last);
  19. #include <experimental/filesystem>
  20. #include <type_traits>
  21. #include <cassert>
  22. #include "test_macros.h"
  23. #include "test_iterators.h"
  24. #include "count_new.hpp"
  25. #include "filesystem_test_helper.hpp"
  26. namespace fs = std::experimental::filesystem;
  27. struct AppendOperatorTestcase {
  28. MultiStringType lhs;
  29. MultiStringType rhs;
  30. MultiStringType expect;
  31. };
  32. #define S(Str) MKSTR(Str)
  33. const AppendOperatorTestcase Cases[] =
  34. {
  35. {S(""), S(""), S("")}
  36. , {S("p1"), S("p2"), S("p1/p2")}
  37. , {S("p1/"), S("p2"), S("p1/p2")}
  38. , {S("p1"), S("/p2"), S("p1/p2")}
  39. , {S("p1/"), S("/p2"), S("p1//p2")}
  40. , {S("p1"), S("\\p2"), S("p1/\\p2")}
  41. , {S("p1\\"), S("p2"), S("p1\\/p2")}
  42. , {S("p1\\"), S("\\p2"), S("p1\\/\\p2")}
  43. , {S("p1"), S(""), S("p1")}
  44. , {S(""), S("p2"), S("p2")}
  45. };
  46. const AppendOperatorTestcase LongLHSCases[] =
  47. {
  48. {S("p1"), S("p2"), S("p1/p2")}
  49. , {S("p1/"), S("p2"), S("p1/p2")}
  50. , {S("p1"), S("/p2"), S("p1/p2")}
  51. };
  52. #undef S
  53. // The append operator may need to allocate a temporary buffer before a code_cvt
  54. // conversion. Test if this allocation occurs by:
  55. // 1. Create a path, `LHS`, and reserve enough space to append `RHS`.
  56. // This prevents `LHS` from allocating during the actual appending.
  57. // 2. Create a `Source` object `RHS`, which represents a "large" string.
  58. // (The string must not trigger the SSO)
  59. // 3. Append `RHS` to `LHS` and check for the expected allocation behavior.
  60. template <class CharT>
  61. void doAppendSourceAllocTest(AppendOperatorTestcase const& TC)
  62. {
  63. using namespace fs;
  64. using Ptr = CharT const*;
  65. using Str = std::basic_string<CharT>;
  66. using InputIter = input_iterator<Ptr>;
  67. const Ptr L = TC.lhs;
  68. Str RShort = (Ptr)TC.rhs;
  69. Str EShort = (Ptr)TC.expect;
  70. assert(RShort.size() >= 2);
  71. CharT c = RShort.back();
  72. RShort.append(100, c);
  73. EShort.append(100, c);
  74. const Ptr R = RShort.data();
  75. const Str& E = EShort;
  76. std::size_t ReserveSize = E.size() + 3;
  77. // basic_string
  78. {
  79. path LHS(L); PathReserve(LHS, ReserveSize);
  80. Str RHS(R);
  81. {
  82. DisableAllocationGuard g;
  83. LHS /= RHS;
  84. }
  85. assert(LHS == E);
  86. }
  87. // CharT*
  88. {
  89. path LHS(L); PathReserve(LHS, ReserveSize);
  90. Ptr RHS(R);
  91. {
  92. DisableAllocationGuard g;
  93. LHS /= RHS;
  94. }
  95. assert(LHS == E);
  96. }
  97. {
  98. path LHS(L); PathReserve(LHS, ReserveSize);
  99. Ptr RHS(R);
  100. {
  101. DisableAllocationGuard g;
  102. LHS.append(RHS, StrEnd(RHS));
  103. }
  104. assert(LHS == E);
  105. }
  106. // input iterator - For non-native char types, appends needs to copy the
  107. // iterator range into a contigious block of memory before it can perform the
  108. // code_cvt conversions.
  109. // For "char" no allocations will be performed because no conversion is
  110. // required.
  111. bool DisableAllocations = std::is_same<CharT, char>::value;
  112. {
  113. path LHS(L); PathReserve(LHS, ReserveSize);
  114. InputIter RHS(R);
  115. {
  116. RequireAllocationGuard g; // requires 1 or more allocations occur by default
  117. if (DisableAllocations) g.requireExactly(0);
  118. LHS /= RHS;
  119. }
  120. assert(LHS == E);
  121. }
  122. {
  123. path LHS(L); PathReserve(LHS, ReserveSize);
  124. InputIter RHS(R);
  125. InputIter REnd(StrEnd(R));
  126. {
  127. RequireAllocationGuard g;
  128. if (DisableAllocations) g.requireExactly(0);
  129. LHS.append(RHS, REnd);
  130. }
  131. assert(LHS == E);
  132. }
  133. }
  134. template <class CharT>
  135. void doAppendSourceTest(AppendOperatorTestcase const& TC)
  136. {
  137. using namespace fs;
  138. using Ptr = CharT const*;
  139. using Str = std::basic_string<CharT>;
  140. using InputIter = input_iterator<Ptr>;
  141. const Ptr L = TC.lhs;
  142. const Ptr R = TC.rhs;
  143. const Ptr E = TC.expect;
  144. // basic_string
  145. {
  146. path LHS(L);
  147. Str RHS(R);
  148. path& Ref = (LHS /= RHS);
  149. assert(LHS == E);
  150. assert(&Ref == &LHS);
  151. }
  152. {
  153. path LHS(L);
  154. Str RHS(R);
  155. path& Ref = LHS.append(RHS);
  156. assert(LHS == E);
  157. assert(&Ref == &LHS);
  158. }
  159. // Char*
  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. Ptr RHS(R);
  170. path& Ref = LHS.append(RHS);
  171. assert(LHS == E);
  172. assert(&Ref == &LHS);
  173. }
  174. {
  175. path LHS(L);
  176. Ptr RHS(R);
  177. path& Ref = LHS.append(RHS, StrEnd(RHS));
  178. assert(LHS == E);
  179. assert(&Ref == &LHS);
  180. }
  181. // iterators
  182. {
  183. path LHS(L);
  184. InputIter RHS(R);
  185. path& Ref = (LHS /= RHS);
  186. assert(LHS == E);
  187. assert(&Ref == &LHS);
  188. }
  189. {
  190. path LHS(L); InputIter RHS(R);
  191. path& Ref = LHS.append(RHS);
  192. assert(LHS == E);
  193. assert(&Ref == &LHS);
  194. }
  195. {
  196. path LHS(L);
  197. InputIter RHS(R);
  198. InputIter REnd(StrEnd(R));
  199. path& Ref = LHS.append(RHS, REnd);
  200. assert(LHS == E);
  201. assert(&Ref == &LHS);
  202. }
  203. }
  204. int main()
  205. {
  206. using namespace fs;
  207. for (auto const & TC : Cases) {
  208. {
  209. path LHS((const char*)TC.lhs);
  210. path RHS((const char*)TC.rhs);
  211. path& Ref = (LHS /= RHS);
  212. assert(LHS == (const char*)TC.expect);
  213. assert(&Ref == &LHS);
  214. }
  215. doAppendSourceTest<char> (TC);
  216. doAppendSourceTest<wchar_t> (TC);
  217. doAppendSourceTest<char16_t>(TC);
  218. doAppendSourceTest<char32_t>(TC);
  219. }
  220. for (auto const & TC : LongLHSCases) {
  221. doAppendSourceAllocTest<char>(TC);
  222. doAppendSourceAllocTest<wchar_t>(TC);
  223. }
  224. }