resize_size.pass.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // XFAIL: libcpp-no-exceptions
  10. // <string>
  11. // void resize(size_type n);
  12. #include <string>
  13. #include <stdexcept>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "min_allocator.h"
  17. template <class S>
  18. void
  19. test(S s, typename S::size_type n, S expected)
  20. {
  21. try
  22. {
  23. s.resize(n);
  24. LIBCPP_ASSERT(s.__invariants());
  25. assert(n <= s.max_size());
  26. assert(s == expected);
  27. }
  28. catch (std::length_error&)
  29. {
  30. assert(n > s.max_size());
  31. }
  32. }
  33. int main()
  34. {
  35. {
  36. typedef std::string S;
  37. test(S(), 0, S());
  38. test(S(), 1, S(1, '\0'));
  39. test(S(), 10, S(10, '\0'));
  40. test(S(), 100, S(100, '\0'));
  41. test(S("12345"), 0, S());
  42. test(S("12345"), 2, S("12"));
  43. test(S("12345"), 5, S("12345"));
  44. test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
  45. test(S("12345678901234567890123456789012345678901234567890"), 0, S());
  46. test(S("12345678901234567890123456789012345678901234567890"), 10,
  47. S("1234567890"));
  48. test(S("12345678901234567890123456789012345678901234567890"), 50,
  49. S("12345678901234567890123456789012345678901234567890"));
  50. test(S("12345678901234567890123456789012345678901234567890"), 60,
  51. S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
  52. test(S(), S::npos, S("not going to happen"));
  53. }
  54. #if TEST_STD_VER >= 11
  55. {
  56. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  57. test(S(), 0, S());
  58. test(S(), 1, S(1, '\0'));
  59. test(S(), 10, S(10, '\0'));
  60. test(S(), 100, S(100, '\0'));
  61. test(S("12345"), 0, S());
  62. test(S("12345"), 2, S("12"));
  63. test(S("12345"), 5, S("12345"));
  64. test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
  65. test(S("12345678901234567890123456789012345678901234567890"), 0, S());
  66. test(S("12345678901234567890123456789012345678901234567890"), 10,
  67. S("1234567890"));
  68. test(S("12345678901234567890123456789012345678901234567890"), 50,
  69. S("12345678901234567890123456789012345678901234567890"));
  70. test(S("12345678901234567890123456789012345678901234567890"), 60,
  71. S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
  72. test(S(), S::npos, S("not going to happen"));
  73. }
  74. #endif
  75. }