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