stoul.pass.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // PR14919 was fixed in r172447, out_of_range wasn't thrown before.
  10. // XFAIL: with_system_cxx_lib=macosx10.7
  11. // XFAIL: with_system_cxx_lib=macosx10.8
  12. // <string>
  13. // unsigned long stoul(const string& str, size_t *idx = 0, int base = 10);
  14. // unsigned long stoul(const wstring& str, size_t *idx = 0, int base = 10);
  15. #include <string>
  16. #include <cassert>
  17. #include <stdexcept>
  18. #include "test_macros.h"
  19. int main(int, char**)
  20. {
  21. assert(std::stoul("0") == 0);
  22. assert(std::stoul(L"0") == 0);
  23. assert(std::stoul("-0") == 0);
  24. assert(std::stoul(L"-0") == 0);
  25. assert(std::stoul(" 10") == 10);
  26. assert(std::stoul(L" 10") == 10);
  27. size_t idx = 0;
  28. assert(std::stoul("10g", &idx, 16) == 16);
  29. assert(idx == 2);
  30. idx = 0;
  31. assert(std::stoul(L"10g", &idx, 16) == 16);
  32. assert(idx == 2);
  33. #ifndef TEST_HAS_NO_EXCEPTIONS
  34. idx = 0;
  35. try
  36. {
  37. std::stoul("", &idx);
  38. assert(false);
  39. }
  40. catch (const std::invalid_argument&)
  41. {
  42. assert(idx == 0);
  43. }
  44. try
  45. {
  46. std::stoul(L"", &idx);
  47. assert(false);
  48. }
  49. catch (const std::invalid_argument&)
  50. {
  51. assert(idx == 0);
  52. }
  53. try
  54. {
  55. std::stoul(" - 8", &idx);
  56. assert(false);
  57. }
  58. catch (const std::invalid_argument&)
  59. {
  60. assert(idx == 0);
  61. }
  62. try
  63. {
  64. std::stoul(L" - 8", &idx);
  65. assert(false);
  66. }
  67. catch (const std::invalid_argument&)
  68. {
  69. assert(idx == 0);
  70. }
  71. try
  72. {
  73. std::stoul("a1", &idx);
  74. assert(false);
  75. }
  76. catch (const std::invalid_argument&)
  77. {
  78. assert(idx == 0);
  79. }
  80. try
  81. {
  82. std::stoul(L"a1", &idx);
  83. assert(false);
  84. }
  85. catch (const std::invalid_argument&)
  86. {
  87. assert(idx == 0);
  88. }
  89. // LWG issue #2009
  90. try
  91. {
  92. std::stoul("9999999999999999999999999999999999999999999999999", &idx);
  93. assert(false);
  94. }
  95. catch (const std::out_of_range&)
  96. {
  97. assert(idx == 0);
  98. }
  99. try
  100. {
  101. std::stoul(L"9999999999999999999999999999999999999999999999999", &idx);
  102. assert(false);
  103. }
  104. catch (const std::out_of_range&)
  105. {
  106. assert(idx == 0);
  107. }
  108. #endif
  109. return 0;
  110. }