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