stod.pass.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // double stod(const string& str, size_t *idx = 0);
  10. // double stod(const wstring& str, size_t *idx = 0);
  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 <cmath>
  17. #include <cassert>
  18. #include <stdexcept>
  19. #include "test_macros.h"
  20. int main(int, char**)
  21. {
  22. assert(std::stod("0") == 0);
  23. assert(std::stod(L"0") == 0);
  24. assert(std::stod("-0") == 0);
  25. assert(std::stod(L"-0") == 0);
  26. assert(std::stod("-10") == -10);
  27. assert(std::stod(L"-10.5") == -10.5);
  28. assert(std::stod(" 10") == 10);
  29. assert(std::stod(L" 10") == 10);
  30. size_t idx = 0;
  31. assert(std::stod("10g", &idx) == 10);
  32. assert(idx == 2);
  33. idx = 0;
  34. assert(std::stod(L"10g", &idx) == 10);
  35. assert(idx == 2);
  36. #ifndef TEST_HAS_NO_EXCEPTIONS
  37. try
  38. #endif
  39. {
  40. assert(std::stod("1.e60", &idx) == 1.e60);
  41. assert(idx == 5);
  42. }
  43. #ifndef TEST_HAS_NO_EXCEPTIONS
  44. catch (const std::out_of_range&)
  45. {
  46. assert(false);
  47. }
  48. try
  49. #endif
  50. {
  51. assert(std::stod(L"1.e60", &idx) == 1.e60);
  52. assert(idx == 5);
  53. }
  54. #ifndef TEST_HAS_NO_EXCEPTIONS
  55. catch (const std::out_of_range&)
  56. {
  57. assert(false);
  58. }
  59. idx = 0;
  60. try
  61. {
  62. assert(std::stod("1.e360", &idx) == INFINITY);
  63. assert(false);
  64. }
  65. catch (const std::out_of_range&)
  66. {
  67. assert(idx == 0);
  68. }
  69. try
  70. {
  71. assert(std::stod(L"1.e360", &idx) == INFINITY);
  72. assert(false);
  73. }
  74. catch (const std::out_of_range&)
  75. {
  76. assert(idx == 0);
  77. }
  78. try
  79. #endif
  80. {
  81. assert(std::stod("INF", &idx) == INFINITY);
  82. assert(idx == 3);
  83. }
  84. #ifndef TEST_HAS_NO_EXCEPTIONS
  85. catch (const std::out_of_range&)
  86. {
  87. assert(false);
  88. }
  89. #endif
  90. idx = 0;
  91. #ifndef TEST_HAS_NO_EXCEPTIONS
  92. try
  93. #endif
  94. {
  95. assert(std::stod(L"INF", &idx) == INFINITY);
  96. assert(idx == 3);
  97. }
  98. #ifndef TEST_HAS_NO_EXCEPTIONS
  99. catch (const std::out_of_range&)
  100. {
  101. assert(false);
  102. }
  103. #endif
  104. idx = 0;
  105. #ifndef TEST_HAS_NO_EXCEPTIONS
  106. try
  107. #endif
  108. {
  109. assert(std::isnan(std::stod("NAN", &idx)));
  110. assert(idx == 3);
  111. }
  112. #ifndef TEST_HAS_NO_EXCEPTIONS
  113. catch (const std::out_of_range&)
  114. {
  115. assert(false);
  116. }
  117. #endif
  118. idx = 0;
  119. #ifndef TEST_HAS_NO_EXCEPTIONS
  120. try
  121. #endif
  122. {
  123. assert(std::isnan(std::stod(L"NAN", &idx)));
  124. assert(idx == 3);
  125. }
  126. #ifndef TEST_HAS_NO_EXCEPTIONS
  127. catch (const std::out_of_range&)
  128. {
  129. assert(false);
  130. }
  131. idx = 0;
  132. try
  133. {
  134. std::stod("", &idx);
  135. assert(false);
  136. }
  137. catch (const std::invalid_argument&)
  138. {
  139. assert(idx == 0);
  140. }
  141. try
  142. {
  143. std::stod(L"", &idx);
  144. assert(false);
  145. }
  146. catch (const std::invalid_argument&)
  147. {
  148. assert(idx == 0);
  149. }
  150. try
  151. {
  152. std::stod(" - 8", &idx);
  153. assert(false);
  154. }
  155. catch (const std::invalid_argument&)
  156. {
  157. assert(idx == 0);
  158. }
  159. try
  160. {
  161. std::stod(L" - 8", &idx);
  162. assert(false);
  163. }
  164. catch (const std::invalid_argument&)
  165. {
  166. assert(idx == 0);
  167. }
  168. try
  169. {
  170. std::stod("a1", &idx);
  171. assert(false);
  172. }
  173. catch (const std::invalid_argument&)
  174. {
  175. assert(idx == 0);
  176. }
  177. try
  178. {
  179. std::stod(L"a1", &idx);
  180. assert(false);
  181. }
  182. catch (const std::invalid_argument&)
  183. {
  184. assert(idx == 0);
  185. }
  186. #endif
  187. return 0;
  188. }