stoul.pass.cpp 2.4 KB

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