size_char_alloc.pass.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // basic_string(size_type n, charT c, const Allocator& a = Allocator());
  10. #include <string>
  11. #include <stdexcept>
  12. #include <algorithm>
  13. #include <cassert>
  14. #include <cstddef>
  15. #include "test_macros.h"
  16. #include "test_allocator.h"
  17. #include "min_allocator.h"
  18. template <class charT>
  19. void
  20. test(unsigned n, charT c)
  21. {
  22. typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
  23. typedef typename S::allocator_type A;
  24. S s2(n, c);
  25. LIBCPP_ASSERT(s2.__invariants());
  26. assert(s2.size() == n);
  27. for (unsigned i = 0; i < n; ++i)
  28. assert(s2[i] == c);
  29. assert(s2.get_allocator() == A());
  30. assert(s2.capacity() >= s2.size());
  31. }
  32. template <class charT, class A>
  33. void
  34. test(unsigned n, charT c, const A& a)
  35. {
  36. typedef std::basic_string<charT, std::char_traits<charT>, A> S;
  37. S s2(n, c, a);
  38. LIBCPP_ASSERT(s2.__invariants());
  39. assert(s2.size() == n);
  40. for (unsigned i = 0; i < n; ++i)
  41. assert(s2[i] == c);
  42. assert(s2.get_allocator() == a);
  43. assert(s2.capacity() >= s2.size());
  44. }
  45. template <class Tp>
  46. void
  47. test(Tp n, Tp c)
  48. {
  49. typedef char charT;
  50. typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
  51. typedef typename S::allocator_type A;
  52. S s2(n, c);
  53. LIBCPP_ASSERT(s2.__invariants());
  54. assert(s2.size() == static_cast<std::size_t>(n));
  55. for (int i = 0; i < n; ++i)
  56. assert(s2[i] == c);
  57. assert(s2.get_allocator() == A());
  58. assert(s2.capacity() >= s2.size());
  59. }
  60. template <class Tp, class A>
  61. void
  62. test(Tp n, Tp c, const A& a)
  63. {
  64. typedef char charT;
  65. typedef std::basic_string<charT, std::char_traits<charT>, A> S;
  66. S s2(n, c, a);
  67. LIBCPP_ASSERT(s2.__invariants());
  68. assert(s2.size() == static_cast<std::size_t>(n));
  69. for (int i = 0; i < n; ++i)
  70. assert(s2[i] == c);
  71. assert(s2.get_allocator() == a);
  72. assert(s2.capacity() >= s2.size());
  73. }
  74. int main(int, char**)
  75. {
  76. {
  77. typedef test_allocator<char> A;
  78. test(0, 'a');
  79. test(0, 'a', A(2));
  80. test(1, 'a');
  81. test(1, 'a', A(2));
  82. test(10, 'a');
  83. test(10, 'a', A(2));
  84. test(100, 'a');
  85. test(100, 'a', A(2));
  86. test(static_cast<char>(100), static_cast<char>(65));
  87. test(static_cast<char>(100), static_cast<char>(65), A(3));
  88. }
  89. #if TEST_STD_VER >= 11
  90. {
  91. typedef min_allocator<char> A;
  92. test(0, 'a');
  93. test(0, 'a', A());
  94. test(1, 'a');
  95. test(1, 'a', A());
  96. test(10, 'a');
  97. test(10, 'a', A());
  98. test(100, 'a');
  99. test(100, 'a', A());
  100. test(static_cast<char>(100), static_cast<char>(65));
  101. test(static_cast<char>(100), static_cast<char>(65), A());
  102. }
  103. #endif
  104. return 0;
  105. }