stoi.pass.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // <string>
  9. // int stoi(const string& str, size_t *idx = 0, int base = 10);
  10. // int stoi(const wstring& str, size_t *idx = 0, int base = 10);
  11. // When back-deploying to macosx10.7, the RTTI for exception classes
  12. // incorrectly provided by libc++.dylib is mixed with the one in
  13. // libc++abi.dylib and exceptions are not caught properly.
  14. // XFAIL: with_system_cxx_lib=macosx10.7
  15. #include <string>
  16. #include <cassert>
  17. #include <stdexcept>
  18. #include "test_macros.h"
  19. int main(int, char**)
  20. {
  21. assert(std::stoi("0") == 0);
  22. assert(std::stoi(L"0") == 0);
  23. assert(std::stoi("-0") == 0);
  24. assert(std::stoi(L"-0") == 0);
  25. assert(std::stoi("-10") == -10);
  26. assert(std::stoi(L"-10") == -10);
  27. assert(std::stoi(" 10") == 10);
  28. assert(std::stoi(L" 10") == 10);
  29. size_t idx = 0;
  30. assert(std::stoi("10g", &idx, 16) == 16);
  31. assert(idx == 2);
  32. idx = 0;
  33. assert(std::stoi(L"10g", &idx, 16) == 16);
  34. assert(idx == 2);
  35. #ifndef TEST_HAS_NO_EXCEPTIONS
  36. if (std::numeric_limits<long>::max() > std::numeric_limits<int>::max())
  37. {
  38. try
  39. {
  40. std::stoi("0x100000000", &idx, 16);
  41. assert(false);
  42. }
  43. catch (const std::out_of_range&)
  44. {
  45. }
  46. try
  47. {
  48. std::stoi(L"0x100000000", &idx, 16);
  49. assert(false);
  50. }
  51. catch (const std::out_of_range&)
  52. {
  53. }
  54. }
  55. idx = 0;
  56. try
  57. {
  58. std::stoi("", &idx);
  59. assert(false);
  60. }
  61. catch (const std::invalid_argument&)
  62. {
  63. assert(idx == 0);
  64. }
  65. try
  66. {
  67. std::stoi(L"", &idx);
  68. assert(false);
  69. }
  70. catch (const std::invalid_argument&)
  71. {
  72. assert(idx == 0);
  73. }
  74. try
  75. {
  76. std::stoi(" - 8", &idx);
  77. assert(false);
  78. }
  79. catch (const std::invalid_argument&)
  80. {
  81. assert(idx == 0);
  82. }
  83. try
  84. {
  85. std::stoi(L" - 8", &idx);
  86. assert(false);
  87. }
  88. catch (const std::invalid_argument&)
  89. {
  90. assert(idx == 0);
  91. }
  92. try
  93. {
  94. std::stoi("a1", &idx);
  95. assert(false);
  96. }
  97. catch (const std::invalid_argument&)
  98. {
  99. assert(idx == 0);
  100. }
  101. try
  102. {
  103. std::stoi(L"a1", &idx);
  104. assert(false);
  105. }
  106. catch (const std::invalid_argument&)
  107. {
  108. assert(idx == 0);
  109. }
  110. #endif
  111. return 0;
  112. }