stof.pass.cpp 3.8 KB

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