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 | |
| 14 | #include <string> |
| 15 | #include <cassert> |
Billy Robert O'Neal III | ed2f9a6 | 2018-08-08 00:40:32 +0000 | [diff] [blame] | 16 | #include <stdexcept> |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 17 | |
Roger Ferrer Ibanez | 84c152a | 2016-11-14 10:44:26 +0000 | [diff] [blame] | 18 | #include "test_macros.h" |
| 19 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 20 | int main(int, char**) |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 21 | { |
| 22 | assert(std::stoi("0") == 0); |
| 23 | assert(std::stoi(L"0") == 0); |
| 24 | assert(std::stoi("-0") == 0); |
| 25 | assert(std::stoi(L"-0") == 0); |
| 26 | assert(std::stoi("-10") == -10); |
| 27 | assert(std::stoi(L"-10") == -10); |
| 28 | assert(std::stoi(" 10") == 10); |
| 29 | assert(std::stoi(L" 10") == 10); |
| 30 | size_t idx = 0; |
| 31 | assert(std::stoi("10g", &idx, 16) == 16); |
| 32 | assert(idx == 2); |
| 33 | idx = 0; |
| 34 | assert(std::stoi(L"10g", &idx, 16) == 16); |
| 35 | assert(idx == 2); |
Roger Ferrer Ibanez | 84c152a | 2016-11-14 10:44:26 +0000 | [diff] [blame] | 36 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 37 | if (std::numeric_limits<long>::max() > std::numeric_limits<int>::max()) |
| 38 | { |
| 39 | try |
| 40 | { |
| 41 | std::stoi("0x100000000", &idx, 16); |
| 42 | assert(false); |
| 43 | } |
| 44 | catch (const std::out_of_range&) |
| 45 | { |
| 46 | } |
| 47 | try |
| 48 | { |
| 49 | std::stoi(L"0x100000000", &idx, 16); |
| 50 | assert(false); |
| 51 | } |
| 52 | catch (const std::out_of_range&) |
| 53 | { |
| 54 | } |
| 55 | } |
| 56 | idx = 0; |
| 57 | try |
| 58 | { |
| 59 | std::stoi("", &idx); |
| 60 | assert(false); |
| 61 | } |
| 62 | catch (const std::invalid_argument&) |
| 63 | { |
| 64 | assert(idx == 0); |
| 65 | } |
| 66 | try |
| 67 | { |
| 68 | std::stoi(L"", &idx); |
| 69 | assert(false); |
| 70 | } |
| 71 | catch (const std::invalid_argument&) |
| 72 | { |
| 73 | assert(idx == 0); |
| 74 | } |
| 75 | try |
| 76 | { |
| 77 | std::stoi(" - 8", &idx); |
| 78 | assert(false); |
| 79 | } |
| 80 | catch (const std::invalid_argument&) |
| 81 | { |
| 82 | assert(idx == 0); |
| 83 | } |
| 84 | try |
| 85 | { |
| 86 | std::stoi(L" - 8", &idx); |
| 87 | assert(false); |
| 88 | } |
| 89 | catch (const std::invalid_argument&) |
| 90 | { |
| 91 | assert(idx == 0); |
| 92 | } |
| 93 | try |
| 94 | { |
| 95 | std::stoi("a1", &idx); |
| 96 | assert(false); |
| 97 | } |
| 98 | catch (const std::invalid_argument&) |
| 99 | { |
| 100 | assert(idx == 0); |
| 101 | } |
| 102 | try |
| 103 | { |
| 104 | std::stoi(L"a1", &idx); |
| 105 | assert(false); |
| 106 | } |
| 107 | catch (const std::invalid_argument&) |
| 108 | { |
| 109 | assert(idx == 0); |
| 110 | } |
Roger Ferrer Ibanez | 84c152a | 2016-11-14 10:44:26 +0000 | [diff] [blame] | 111 | #endif |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 112 | |
| 113 | return 0; |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 114 | } |