pointer_size_alloc.pass.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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(const charT* s, size_type n, 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(const charT* s, unsigned n)
  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(s, n);
  26. LIBCPP_ASSERT(s2.__invariants());
  27. assert(s2.size() == n);
  28. assert(T::compare(s2.data(), s, n) == 0);
  29. assert(s2.get_allocator() == A());
  30. assert(s2.capacity() >= s2.size());
  31. }
  32. template <class charT, class A>
  33. void
  34. test(const charT* s, unsigned n, const A& a)
  35. {
  36. typedef std::basic_string<charT, std::char_traits<charT>, A> S;
  37. typedef typename S::traits_type T;
  38. S s2(s, n, a);
  39. LIBCPP_ASSERT(s2.__invariants());
  40. assert(s2.size() == n);
  41. assert(T::compare(s2.data(), s, n) == 0);
  42. assert(s2.get_allocator() == a);
  43. assert(s2.capacity() >= s2.size());
  44. }
  45. int main()
  46. {
  47. {
  48. typedef test_allocator<char> A;
  49. typedef std::basic_string<char, std::char_traits<char>, A> S;
  50. test("", 0);
  51. test("", 0, A(2));
  52. test("1", 1);
  53. test("1", 1, A(2));
  54. test("1234567980", 10);
  55. test("1234567980", 10, A(2));
  56. test("123456798012345679801234567980123456798012345679801234567980", 60);
  57. test("123456798012345679801234567980123456798012345679801234567980", 60, A(2));
  58. }
  59. #if TEST_STD_VER >= 11
  60. {
  61. typedef min_allocator<char> A;
  62. typedef std::basic_string<char, std::char_traits<char>, A> S;
  63. test("", 0);
  64. test("", 0, A());
  65. test("1", 1);
  66. test("1", 1, A());
  67. test("1234567980", 10);
  68. test("1234567980", 10, A());
  69. test("123456798012345679801234567980123456798012345679801234567980", 60);
  70. test("123456798012345679801234567980123456798012345679801234567980", 60, A());
  71. }
  72. #endif
  73. }