1 // { dg-do run { target c++11 } } 2 // { dg-require-string-conversions "" } 3 // { dg-xfail-run-if "broken long double IO" { newlib_broken_long_double_io } } 4 5 // 2008-06-15 Paolo Carlini <paolo.carlini@oracle.com> 6 7 // Copyright (C) 2008-2019 Free Software Foundation, Inc. 8 // 9 // This file is part of the GNU ISO C++ Library. This library is free 10 // software; you can redistribute it and/or modify it under the 11 // terms of the GNU General Public License as published by the 12 // Free Software Foundation; either version 3, or (at your option) 13 // any later version. 14 15 // This library is distributed in the hope that it will be useful, 16 // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 // GNU General Public License for more details. 19 20 // You should have received a copy of the GNU General Public License along 21 // with this library; see the file COPYING3. If not see 22 // <http://www.gnu.org/licenses/>. 23 24 // 21.4 Numeric Conversions [string.conversions] 25 26 #include <string> 27 #include <limits> 28 #include <stdexcept> 29 #include <testsuite_hooks.h> 30 31 void test01()32test01() 33 { 34 #if _GLIBCXX_USE_C99_WCHAR 35 36 bool test = false; 37 using namespace std; 38 39 try 40 { 41 wstring one; 42 stod(one); 43 } 44 catch(std::invalid_argument) 45 { 46 test = true; 47 } 48 catch(...) 49 { 50 } 51 VERIFY( test ); 52 53 test = false; 54 try 55 { 56 wstring one(L"a"); 57 stod(one); 58 } 59 catch(std::invalid_argument) 60 { 61 test = true; 62 } 63 catch(...) 64 { 65 } 66 VERIFY( test ); 67 68 double d1 = 0.0; 69 size_t idx1 = 0; 70 try 71 { 72 wstring one(L"2.0a"); 73 d1 = stod(one, &idx1); 74 } 75 catch(...) 76 { 77 test = false; 78 } 79 VERIFY( test ); 80 VERIFY( d1 == 2.0 ); 81 VERIFY( idx1 == 3 ); 82 83 test = false; 84 try 85 { 86 wstring one(L"1e"); 87 one.append(2 * numeric_limits<double>::max_exponent10, L'9'); 88 d1 = stod(one); 89 } 90 catch(std::out_of_range) 91 { 92 test = true; 93 } 94 catch(...) 95 { 96 } 97 VERIFY( test ); 98 VERIFY( d1 == 2.0 ); 99 100 try 101 { 102 long double ld0 = numeric_limits<double>::max() / 100.0; 103 wstring one(to_wstring(ld0)); 104 stod(one); 105 } 106 catch(...) 107 { 108 test = false; 109 } 110 VERIFY( test ); 111 112 if ((numeric_limits<long double>::max() / 10000.0L) 113 > numeric_limits<double>::max()) 114 { 115 test = false; 116 d1 = -1.0; 117 try 118 { 119 long double ld1 = numeric_limits<double>::max(); 120 ld1 *= 100.0; 121 wstring one(to_wstring(ld1)); 122 d1 = stod(one); 123 } 124 catch(std::out_of_range) 125 { 126 test = true; 127 } 128 catch(...) 129 { 130 } 131 VERIFY( test ); 132 VERIFY( d1 == -1.0 ); 133 } 134 135 #endif 136 } 137 main()138int main() 139 { 140 test01(); 141 return 0; 142 } 143