at.pass.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // const_reference at(size_type pos) const;
  11. // reference at(size_type pos);
  12. #include <string>
  13. #include <stdexcept>
  14. #include <cassert>
  15. #include "min_allocator.h"
  16. #include "test_macros.h"
  17. template <class S>
  18. void
  19. test(S s, typename S::size_type pos)
  20. {
  21. const S& cs = s;
  22. if (pos < s.size())
  23. {
  24. assert(s.at(pos) == s[pos]);
  25. assert(cs.at(pos) == cs[pos]);
  26. }
  27. #ifndef TEST_HAS_NO_EXCEPTIONS
  28. else
  29. {
  30. try
  31. {
  32. TEST_IGNORE_NODISCARD s.at(pos);
  33. assert(false);
  34. }
  35. catch (std::out_of_range&)
  36. {
  37. assert(pos >= s.size());
  38. }
  39. try
  40. {
  41. TEST_IGNORE_NODISCARD cs.at(pos);
  42. assert(false);
  43. }
  44. catch (std::out_of_range&)
  45. {
  46. assert(pos >= s.size());
  47. }
  48. }
  49. #endif
  50. }
  51. int main()
  52. {
  53. {
  54. typedef std::string S;
  55. test(S(), 0);
  56. test(S("123"), 0);
  57. test(S("123"), 1);
  58. test(S("123"), 2);
  59. test(S("123"), 3);
  60. }
  61. #if TEST_STD_VER >= 11
  62. {
  63. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  64. test(S(), 0);
  65. test(S("123"), 0);
  66. test(S("123"), 1);
  67. test(S("123"), 2);
  68. test(S("123"), 3);
  69. }
  70. #endif
  71. }