stoi.pass.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // int stoi(const string& str, size_t *idx = 0, int base = 10);
  12. // int stoi(const wstring& str, size_t *idx = 0, int base = 10);
  13. #include <string>
  14. #include <cassert>
  15. int main()
  16. {
  17. assert(std::stoi("0") == 0);
  18. assert(std::stoi(L"0") == 0);
  19. assert(std::stoi("-0") == 0);
  20. assert(std::stoi(L"-0") == 0);
  21. assert(std::stoi("-10") == -10);
  22. assert(std::stoi(L"-10") == -10);
  23. assert(std::stoi(" 10") == 10);
  24. assert(std::stoi(L" 10") == 10);
  25. size_t idx = 0;
  26. assert(std::stoi("10g", &idx, 16) == 16);
  27. assert(idx == 2);
  28. idx = 0;
  29. assert(std::stoi(L"10g", &idx, 16) == 16);
  30. assert(idx == 2);
  31. if (std::numeric_limits<long>::max() > std::numeric_limits<int>::max())
  32. {
  33. try
  34. {
  35. std::stoi("0x100000000", &idx, 16);
  36. assert(false);
  37. }
  38. catch (const std::out_of_range&)
  39. {
  40. }
  41. try
  42. {
  43. std::stoi(L"0x100000000", &idx, 16);
  44. assert(false);
  45. }
  46. catch (const std::out_of_range&)
  47. {
  48. }
  49. }
  50. idx = 0;
  51. try
  52. {
  53. std::stoi("", &idx);
  54. assert(false);
  55. }
  56. catch (const std::invalid_argument&)
  57. {
  58. assert(idx == 0);
  59. }
  60. try
  61. {
  62. std::stoi(L"", &idx);
  63. assert(false);
  64. }
  65. catch (const std::invalid_argument&)
  66. {
  67. assert(idx == 0);
  68. }
  69. try
  70. {
  71. std::stoi(" - 8", &idx);
  72. assert(false);
  73. }
  74. catch (const std::invalid_argument&)
  75. {
  76. assert(idx == 0);
  77. }
  78. try
  79. {
  80. std::stoi(L" - 8", &idx);
  81. assert(false);
  82. }
  83. catch (const std::invalid_argument&)
  84. {
  85. assert(idx == 0);
  86. }
  87. try
  88. {
  89. std::stoi("a1", &idx);
  90. assert(false);
  91. }
  92. catch (const std::invalid_argument&)
  93. {
  94. assert(idx == 0);
  95. }
  96. try
  97. {
  98. std::stoi(L"a1", &idx);
  99. assert(false);
  100. }
  101. catch (const std::invalid_argument&)
  102. {
  103. assert(idx == 0);
  104. }
  105. }