stoll.pass.cpp 2.5 KB

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