T_size_size.pass.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. // <string>
  9. // template<class _Tp>
  10. // basic_string(const _Tp& __t, size_type __pos, size_type __n,
  11. // const allocator_type& __a = allocator_type());
  12. //
  13. // Mostly we're testing string_view here
  14. #include <string>
  15. #include <string_view>
  16. #include <stdexcept>
  17. #include <algorithm>
  18. #include <cassert>
  19. #include "test_macros.h"
  20. #include "test_allocator.h"
  21. #include "min_allocator.h"
  22. template <class S, class SV>
  23. void
  24. test(SV sv, std::size_t pos, std::size_t n)
  25. {
  26. typedef typename S::traits_type T;
  27. typedef typename S::allocator_type A;
  28. typedef typename S::size_type Size;
  29. if (pos <= sv.size())
  30. {
  31. S s2(sv, static_cast<Size>(pos), static_cast<Size>(n));
  32. LIBCPP_ASSERT(s2.__invariants());
  33. assert(pos <= sv.size());
  34. std::size_t rlen = std::min(sv.size() - pos, n);
  35. assert(s2.size() == rlen);
  36. assert(T::compare(s2.data(), sv.data() + pos, rlen) == 0);
  37. assert(s2.get_allocator() == A());
  38. assert(s2.capacity() >= s2.size());
  39. }
  40. #ifndef TEST_HAS_NO_EXCEPTIONS
  41. else
  42. {
  43. try
  44. {
  45. S s2(sv, static_cast<Size>(pos), static_cast<Size>(n));
  46. assert(false);
  47. }
  48. catch (std::out_of_range&)
  49. {
  50. assert(pos > sv.size());
  51. }
  52. }
  53. #endif
  54. }
  55. template <class S, class SV>
  56. void
  57. test(SV sv, std::size_t pos, std::size_t n, const typename S::allocator_type& a)
  58. {
  59. typedef typename S::traits_type T;
  60. typedef typename S::size_type Size;
  61. if (pos <= sv.size())
  62. {
  63. S s2(sv, static_cast<Size>(pos), static_cast<Size>(n), a);
  64. LIBCPP_ASSERT(s2.__invariants());
  65. assert(pos <= sv.size());
  66. std::size_t rlen = std::min(sv.size() - pos, n);
  67. assert(s2.size() == rlen);
  68. assert(T::compare(s2.data(), sv.data() + pos, rlen) == 0);
  69. assert(s2.get_allocator() == a);
  70. assert(s2.capacity() >= s2.size());
  71. }
  72. #ifndef TEST_HAS_NO_EXCEPTIONS
  73. else
  74. {
  75. try
  76. {
  77. S s2(sv, static_cast<Size>(pos), static_cast<Size>(n), a);
  78. assert(false);
  79. }
  80. catch (std::out_of_range&)
  81. {
  82. assert(pos > sv.size());
  83. }
  84. }
  85. #endif
  86. }
  87. int main(int, char**)
  88. {
  89. {
  90. typedef test_allocator<char> A;
  91. typedef std::basic_string_view<char, std::char_traits<char> > SV;
  92. typedef std::basic_string <char, std::char_traits<char>, A> S;
  93. test<S,SV>(SV(), 0, 0);
  94. test<S,SV>(SV(), 0, 1);
  95. test<S,SV>(SV(), 1, 0);
  96. test<S,SV>(SV(), 1, 1);
  97. test<S,SV>(SV(), 1, 2);
  98. test<S,SV>(SV("1"), 0, 0);
  99. test<S,SV>(SV("1"), 0, 1);
  100. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 0);
  101. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 1);
  102. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 10);
  103. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 100);
  104. test<S,SV>(SV(), 0, 0, A(4));
  105. test<S,SV>(SV(), 0, 1, A(4));
  106. test<S,SV>(SV(), 1, 0, A(4));
  107. test<S,SV>(SV(), 1, 1, A(4));
  108. test<S,SV>(SV(), 1, 2, A(4));
  109. test<S,SV>(SV("1"), 0, 0, A(6));
  110. test<S,SV>(SV("1"), 0, 1, A(6));
  111. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 0, A(8));
  112. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 1, A(8));
  113. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 10, A(8));
  114. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 100, A(8));
  115. }
  116. #if TEST_STD_VER >= 11
  117. {
  118. typedef min_allocator<char> A;
  119. typedef std::basic_string_view<char, std::char_traits<char> > SV;
  120. typedef std::basic_string <char, std::char_traits<char>, A> S;
  121. test<S,SV>(SV(), 0, 0);
  122. test<S,SV>(SV(), 0, 1);
  123. test<S,SV>(SV(), 1, 0);
  124. test<S,SV>(SV(), 1, 1);
  125. test<S,SV>(SV(), 1, 2);
  126. test<S,SV>(SV("1"), 0, 0);
  127. test<S,SV>(SV("1"), 0, 1);
  128. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 0);
  129. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 1);
  130. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 10);
  131. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 100);
  132. test<S,SV>(SV(), 0, 0, A());
  133. test<S,SV>(SV(), 0, 1, A());
  134. test<S,SV>(SV(), 1, 0, A());
  135. test<S,SV>(SV(), 1, 1, A());
  136. test<S,SV>(SV(), 1, 2, A());
  137. test<S,SV>(SV("1"), 0, 0, A());
  138. test<S,SV>(SV("1"), 0, 1, A());
  139. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 0, A());
  140. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 1, A());
  141. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 10, A());
  142. test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 100, A());
  143. }
  144. #endif
  145. {
  146. typedef std::string S;
  147. typedef std::string_view SV;
  148. S s = "ABCD";
  149. SV sv = "EFGH";
  150. char arr[] = "IJKL";
  151. S s1("CDEF", 4); // calls ctor(const char *, len)
  152. assert(s1 == "CDEF");
  153. S s2("QRST", 0, 3); // calls ctor(string("QRST", pos, len)
  154. assert(s2 == "QRS");
  155. S s3(sv, 0, std::string::npos); // calls ctor(T, pos, npos)
  156. assert(s3 == sv);
  157. S s4(sv, 0, 3); // calls ctor(T, pos, len)
  158. assert(s4 == "EFG");
  159. S s5(arr, 0, 2); // calls ctor(const char *, len)
  160. assert(s5 == "IJ");
  161. S s6(arr, 0); // calls ctor(const char *, len)
  162. assert(s6 == "");
  163. S s7(s.data(), 2); // calls ctor(const char *, len)
  164. assert(s7 == "AB");
  165. }
  166. return 0;
  167. }