resize_size.pass.cpp 2.5 KB

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