pointer_alloc.pass.cpp 2.3 KB

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