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