stold.pass.cpp 4.0 KB

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