Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 5ce9a53 | 2013-02-05 22:10:27 +0000 | [diff] [blame] | 9 | // |
Asiri Rathnayake | f520c14 | 2015-11-10 11:41:22 +0000 | [diff] [blame] | 10 | // XFAIL: libcpp-no-exceptions |
Jonathan Roelofs | eb7b5e7 | 2015-01-14 23:38:12 +0000 | [diff] [blame] | 11 | // XFAIL: with_system_cxx_lib=x86_64-apple-darwin11 |
| 12 | // XFAIL: with_system_cxx_lib=x86_64-apple-darwin12 |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 13 | |
| 14 | // <string> |
| 15 | |
| 16 | // float stof(const string& str, size_t *idx = 0); |
| 17 | // float stof(const wstring& str, size_t *idx = 0); |
| 18 | |
| 19 | #include <string> |
| 20 | #include <cmath> |
| 21 | #include <cassert> |
| 22 | |
| 23 | int main() |
| 24 | { |
| 25 | assert(std::stof("0") == 0); |
| 26 | assert(std::stof(L"0") == 0); |
| 27 | assert(std::stof("-0") == 0); |
| 28 | assert(std::stof(L"-0") == 0); |
| 29 | assert(std::stof("-10") == -10); |
| 30 | assert(std::stof(L"-10.5") == -10.5); |
| 31 | assert(std::stof(" 10") == 10); |
| 32 | assert(std::stof(L" 10") == 10); |
| 33 | size_t idx = 0; |
| 34 | assert(std::stof("10g", &idx) == 10); |
| 35 | assert(idx == 2); |
| 36 | idx = 0; |
| 37 | assert(std::stof(L"10g", &idx) == 10); |
| 38 | assert(idx == 2); |
Howard Hinnant | 1afbaba | 2013-01-14 18:59:43 +0000 | [diff] [blame] | 39 | idx = 0; |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 40 | try |
| 41 | { |
| 42 | assert(std::stof("1.e60", &idx) == INFINITY); |
Howard Hinnant | 1afbaba | 2013-01-14 18:59:43 +0000 | [diff] [blame] | 43 | assert(false); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 44 | } |
| 45 | catch (const std::out_of_range&) |
| 46 | { |
Howard Hinnant | 1afbaba | 2013-01-14 18:59:43 +0000 | [diff] [blame] | 47 | assert(idx == 0); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 48 | } |
| 49 | try |
| 50 | { |
| 51 | assert(std::stof(L"1.e60", &idx) == INFINITY); |
Howard Hinnant | 1afbaba | 2013-01-14 18:59:43 +0000 | [diff] [blame] | 52 | assert(false); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 53 | } |
| 54 | catch (const std::out_of_range&) |
| 55 | { |
Howard Hinnant | 1afbaba | 2013-01-14 18:59:43 +0000 | [diff] [blame] | 56 | assert(idx == 0); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 57 | } |
| 58 | idx = 0; |
| 59 | try |
| 60 | { |
| 61 | assert(std::stof("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(L"1.e360", &idx) == INFINITY); |
| 71 | assert(false); |
| 72 | } |
| 73 | catch (const std::out_of_range&) |
| 74 | { |
| 75 | assert(idx == 0); |
| 76 | } |
| 77 | try |
| 78 | { |
| 79 | assert(std::stof("INF", &idx) == INFINITY); |
| 80 | assert(idx == 3); |
| 81 | } |
| 82 | catch (const std::out_of_range&) |
| 83 | { |
| 84 | assert(false); |
| 85 | } |
| 86 | idx = 0; |
| 87 | try |
| 88 | { |
| 89 | assert(std::stof(L"INF", &idx) == INFINITY); |
| 90 | assert(idx == 3); |
| 91 | } |
| 92 | catch (const std::out_of_range&) |
| 93 | { |
| 94 | assert(false); |
| 95 | } |
| 96 | idx = 0; |
| 97 | try |
| 98 | { |
| 99 | assert(std::isnan(std::stof("NAN", &idx))); |
| 100 | assert(idx == 3); |
| 101 | } |
| 102 | catch (const std::out_of_range&) |
| 103 | { |
| 104 | assert(false); |
| 105 | } |
| 106 | idx = 0; |
| 107 | try |
| 108 | { |
| 109 | assert(std::isnan(std::stof(L"NAN", &idx))); |
| 110 | assert(idx == 3); |
| 111 | } |
| 112 | catch (const std::out_of_range&) |
| 113 | { |
| 114 | assert(false); |
| 115 | } |
| 116 | idx = 0; |
| 117 | try |
| 118 | { |
| 119 | std::stof("", &idx); |
| 120 | assert(false); |
| 121 | } |
| 122 | catch (const std::invalid_argument&) |
| 123 | { |
| 124 | assert(idx == 0); |
| 125 | } |
| 126 | try |
| 127 | { |
| 128 | std::stof(L"", &idx); |
| 129 | assert(false); |
| 130 | } |
| 131 | catch (const std::invalid_argument&) |
| 132 | { |
| 133 | assert(idx == 0); |
| 134 | } |
| 135 | try |
| 136 | { |
| 137 | std::stof(" - 8", &idx); |
| 138 | assert(false); |
| 139 | } |
| 140 | catch (const std::invalid_argument&) |
| 141 | { |
| 142 | assert(idx == 0); |
| 143 | } |
| 144 | try |
| 145 | { |
| 146 | std::stof(L" - 8", &idx); |
| 147 | assert(false); |
| 148 | } |
| 149 | catch (const std::invalid_argument&) |
| 150 | { |
| 151 | assert(idx == 0); |
| 152 | } |
| 153 | try |
| 154 | { |
| 155 | std::stof("a1", &idx); |
| 156 | assert(false); |
| 157 | } |
| 158 | catch (const std::invalid_argument&) |
| 159 | { |
| 160 | assert(idx == 0); |
| 161 | } |
| 162 | try |
| 163 | { |
| 164 | std::stof(L"a1", &idx); |
| 165 | assert(false); |
| 166 | } |
| 167 | catch (const std::invalid_argument&) |
| 168 | { |
| 169 | assert(idx == 0); |
| 170 | } |
| 171 | } |