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