Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 9 | // <string> |
| 10 | |
| 11 | // int stoi(const string& str, size_t *idx = 0, int base = 10); |
| 12 | // int stoi(const wstring& str, size_t *idx = 0, int base = 10); |
| 13 | |
Louis Dionne | 25838c6 | 2019-02-27 00:57:57 +0000 | [diff] [blame] | 14 | // When back-deploying to macosx10.7, the RTTI for exception classes |
| 15 | // incorrectly provided by libc++.dylib is mixed with the one in |
| 16 | // libc++abi.dylib and exceptions are not caught properly. |
| 17 | // XFAIL: with_system_cxx_lib=macosx10.7 |
| 18 | |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <cassert> |
Billy Robert O'Neal III | ed2f9a6 | 2018-08-08 00:40:32 +0000 | [diff] [blame] | 21 | #include <stdexcept> |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 22 | |
Roger Ferrer Ibanez | 84c152a | 2016-11-14 10:44:26 +0000 | [diff] [blame] | 23 | #include "test_macros.h" |
| 24 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 25 | int main(int, char**) |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 26 | { |
| 27 | assert(std::stoi("0") == 0); |
| 28 | assert(std::stoi(L"0") == 0); |
| 29 | assert(std::stoi("-0") == 0); |
| 30 | assert(std::stoi(L"-0") == 0); |
| 31 | assert(std::stoi("-10") == -10); |
| 32 | assert(std::stoi(L"-10") == -10); |
| 33 | assert(std::stoi(" 10") == 10); |
| 34 | assert(std::stoi(L" 10") == 10); |
| 35 | size_t idx = 0; |
| 36 | assert(std::stoi("10g", &idx, 16) == 16); |
| 37 | assert(idx == 2); |
| 38 | idx = 0; |
| 39 | assert(std::stoi(L"10g", &idx, 16) == 16); |
| 40 | assert(idx == 2); |
Roger Ferrer Ibanez | 84c152a | 2016-11-14 10:44:26 +0000 | [diff] [blame] | 41 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 42 | if (std::numeric_limits<long>::max() > std::numeric_limits<int>::max()) |
| 43 | { |
| 44 | try |
| 45 | { |
| 46 | std::stoi("0x100000000", &idx, 16); |
| 47 | assert(false); |
| 48 | } |
| 49 | catch (const std::out_of_range&) |
| 50 | { |
| 51 | } |
| 52 | try |
| 53 | { |
| 54 | std::stoi(L"0x100000000", &idx, 16); |
| 55 | assert(false); |
| 56 | } |
| 57 | catch (const std::out_of_range&) |
| 58 | { |
| 59 | } |
| 60 | } |
| 61 | idx = 0; |
| 62 | try |
| 63 | { |
| 64 | std::stoi("", &idx); |
| 65 | assert(false); |
| 66 | } |
| 67 | catch (const std::invalid_argument&) |
| 68 | { |
| 69 | assert(idx == 0); |
| 70 | } |
| 71 | try |
| 72 | { |
| 73 | std::stoi(L"", &idx); |
| 74 | assert(false); |
| 75 | } |
| 76 | catch (const std::invalid_argument&) |
| 77 | { |
| 78 | assert(idx == 0); |
| 79 | } |
| 80 | try |
| 81 | { |
| 82 | std::stoi(" - 8", &idx); |
| 83 | assert(false); |
| 84 | } |
| 85 | catch (const std::invalid_argument&) |
| 86 | { |
| 87 | assert(idx == 0); |
| 88 | } |
| 89 | try |
| 90 | { |
| 91 | std::stoi(L" - 8", &idx); |
| 92 | assert(false); |
| 93 | } |
| 94 | catch (const std::invalid_argument&) |
| 95 | { |
| 96 | assert(idx == 0); |
| 97 | } |
| 98 | try |
| 99 | { |
| 100 | std::stoi("a1", &idx); |
| 101 | assert(false); |
| 102 | } |
| 103 | catch (const std::invalid_argument&) |
| 104 | { |
| 105 | assert(idx == 0); |
| 106 | } |
| 107 | try |
| 108 | { |
| 109 | std::stoi(L"a1", &idx); |
| 110 | assert(false); |
| 111 | } |
| 112 | catch (const std::invalid_argument&) |
| 113 | { |
| 114 | assert(idx == 0); |
| 115 | } |
Roger Ferrer Ibanez | 84c152a | 2016-11-14 10:44:26 +0000 | [diff] [blame] | 116 | #endif |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 117 | |
| 118 | return 0; |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 119 | } |