size_char_alloc.pass.cpp 3.0 KB

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