iter_alloc.pass.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // template<class InputIterator>
  11. // basic_string(InputIterator begin, InputIterator end,
  12. // const Allocator& a = Allocator());
  13. #include <string>
  14. #include <iterator>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. #include "test_allocator.h"
  18. #include "../input_iterator.h"
  19. #include "min_allocator.h"
  20. template <class It>
  21. void
  22. test(It first, It last)
  23. {
  24. typedef typename std::iterator_traits<It>::value_type charT;
  25. typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
  26. typedef typename S::traits_type T;
  27. typedef typename S::allocator_type A;
  28. S s2(first, last);
  29. LIBCPP_ASSERT(s2.__invariants());
  30. assert(s2.size() == std::distance(first, last));
  31. unsigned i = 0;
  32. for (It it = first; it != last; ++it, ++i)
  33. assert(s2[i] == *it);
  34. assert(s2.get_allocator() == A());
  35. assert(s2.capacity() >= s2.size());
  36. }
  37. template <class It, class A>
  38. void
  39. test(It first, It last, const A& a)
  40. {
  41. typedef typename std::iterator_traits<It>::value_type charT;
  42. typedef std::basic_string<charT, std::char_traits<charT>, A> S;
  43. typedef typename S::traits_type T;
  44. S s2(first, last, a);
  45. LIBCPP_ASSERT(s2.__invariants());
  46. assert(s2.size() == std::distance(first, last));
  47. unsigned i = 0;
  48. for (It it = first; it != last; ++it, ++i)
  49. assert(s2[i] == *it);
  50. assert(s2.get_allocator() == a);
  51. assert(s2.capacity() >= s2.size());
  52. }
  53. int main()
  54. {
  55. {
  56. typedef test_allocator<char> A;
  57. const char* s = "12345678901234567890123456789012345678901234567890";
  58. test(s, s);
  59. test(s, s, A(2));
  60. test(s, s+1);
  61. test(s, s+1, A(2));
  62. test(s, s+10);
  63. test(s, s+10, A(2));
  64. test(s, s+50);
  65. test(s, s+50, A(2));
  66. test(input_iterator<const char*>(s), input_iterator<const char*>(s));
  67. test(input_iterator<const char*>(s), input_iterator<const char*>(s), A(2));
  68. test(input_iterator<const char*>(s), input_iterator<const char*>(s+1));
  69. test(input_iterator<const char*>(s), input_iterator<const char*>(s+1), A(2));
  70. test(input_iterator<const char*>(s), input_iterator<const char*>(s+10));
  71. test(input_iterator<const char*>(s), input_iterator<const char*>(s+10), A(2));
  72. test(input_iterator<const char*>(s), input_iterator<const char*>(s+50));
  73. test(input_iterator<const char*>(s), input_iterator<const char*>(s+50), A(2));
  74. }
  75. #if TEST_STD_VER >= 11
  76. {
  77. typedef min_allocator<char> A;
  78. const char* s = "12345678901234567890123456789012345678901234567890";
  79. test(s, s);
  80. test(s, s, A());
  81. test(s, s+1);
  82. test(s, s+1, A());
  83. test(s, s+10);
  84. test(s, s+10, A());
  85. test(s, s+50);
  86. test(s, s+50, A());
  87. test(input_iterator<const char*>(s), input_iterator<const char*>(s));
  88. test(input_iterator<const char*>(s), input_iterator<const char*>(s), A());
  89. test(input_iterator<const char*>(s), input_iterator<const char*>(s+1));
  90. test(input_iterator<const char*>(s), input_iterator<const char*>(s+1), A());
  91. test(input_iterator<const char*>(s), input_iterator<const char*>(s+10));
  92. test(input_iterator<const char*>(s), input_iterator<const char*>(s+10), A());
  93. test(input_iterator<const char*>(s), input_iterator<const char*>(s+50));
  94. test(input_iterator<const char*>(s), input_iterator<const char*>(s+50), A());
  95. }
  96. #endif
  97. }