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