stof.pass.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <string>
  10. // float stof(const string& str, size_t *idx = 0);
  11. // float stof(const wstring& str, size_t *idx = 0);
  12. #include <string>
  13. #include <cmath>
  14. #include <cassert>
  15. int main()
  16. {
  17. assert(std::stof("0") == 0);
  18. assert(std::stof(L"0") == 0);
  19. assert(std::stof("-0") == 0);
  20. assert(std::stof(L"-0") == 0);
  21. assert(std::stof("-10") == -10);
  22. assert(std::stof(L"-10.5") == -10.5);
  23. assert(std::stof(" 10") == 10);
  24. assert(std::stof(L" 10") == 10);
  25. size_t idx = 0;
  26. assert(std::stof("10g", &idx) == 10);
  27. assert(idx == 2);
  28. idx = 0;
  29. assert(std::stof(L"10g", &idx) == 10);
  30. assert(idx == 2);
  31. try
  32. {
  33. assert(std::stof("1.e60", &idx) == INFINITY);
  34. assert(idx == 5);
  35. }
  36. catch (const std::out_of_range&)
  37. {
  38. assert(false);
  39. }
  40. try
  41. {
  42. assert(std::stof(L"1.e60", &idx) == INFINITY);
  43. assert(idx == 5);
  44. }
  45. catch (const std::out_of_range&)
  46. {
  47. assert(false);
  48. }
  49. idx = 0;
  50. try
  51. {
  52. assert(std::stof("1.e360", &idx) == INFINITY);
  53. assert(false);
  54. }
  55. catch (const std::out_of_range&)
  56. {
  57. assert(idx == 0);
  58. }
  59. try
  60. {
  61. assert(std::stof(L"1.e360", &idx) == INFINITY);
  62. assert(false);
  63. }
  64. catch (const std::out_of_range&)
  65. {
  66. assert(idx == 0);
  67. }
  68. try
  69. {
  70. assert(std::stof("INF", &idx) == INFINITY);
  71. assert(idx == 3);
  72. }
  73. catch (const std::out_of_range&)
  74. {
  75. assert(false);
  76. }
  77. idx = 0;
  78. try
  79. {
  80. assert(std::stof(L"INF", &idx) == INFINITY);
  81. assert(idx == 3);
  82. }
  83. catch (const std::out_of_range&)
  84. {
  85. assert(false);
  86. }
  87. idx = 0;
  88. try
  89. {
  90. assert(std::isnan(std::stof("NAN", &idx)));
  91. assert(idx == 3);
  92. }
  93. catch (const std::out_of_range&)
  94. {
  95. assert(false);
  96. }
  97. idx = 0;
  98. try
  99. {
  100. assert(std::isnan(std::stof(L"NAN", &idx)));
  101. assert(idx == 3);
  102. }
  103. catch (const std::out_of_range&)
  104. {
  105. assert(false);
  106. }
  107. idx = 0;
  108. try
  109. {
  110. std::stof("", &idx);
  111. assert(false);
  112. }
  113. catch (const std::invalid_argument&)
  114. {
  115. assert(idx == 0);
  116. }
  117. try
  118. {
  119. std::stof(L"", &idx);
  120. assert(false);
  121. }
  122. catch (const std::invalid_argument&)
  123. {
  124. assert(idx == 0);
  125. }
  126. try
  127. {
  128. std::stof(" - 8", &idx);
  129. assert(false);
  130. }
  131. catch (const std::invalid_argument&)
  132. {
  133. assert(idx == 0);
  134. }
  135. try
  136. {
  137. std::stof(L" - 8", &idx);
  138. assert(false);
  139. }
  140. catch (const std::invalid_argument&)
  141. {
  142. assert(idx == 0);
  143. }
  144. try
  145. {
  146. std::stof("a1", &idx);
  147. assert(false);
  148. }
  149. catch (const std::invalid_argument&)
  150. {
  151. assert(idx == 0);
  152. }
  153. try
  154. {
  155. std::stof(L"a1", &idx);
  156. assert(false);
  157. }
  158. catch (const std::invalid_argument&)
  159. {
  160. assert(idx == 0);
  161. }
  162. }