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