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