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 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 5ce9a53 | 2013-02-05 22:10:27 +0000 | [diff] [blame] | 8 | // |
Mehdi Amini | e9c66ad | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 9 | // PR14919 was fixed in r172447, out_of_range wasn't thrown before. |
| 10 | // XFAIL: with_system_cxx_lib=macosx10.7 |
| 11 | // XFAIL: with_system_cxx_lib=macosx10.8 |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 12 | |
| 13 | // <string> |
| 14 | |
| 15 | // long long stoll(const string& str, size_t *idx = 0, int base = 10); |
| 16 | // long long stoll(const wstring& str, size_t *idx = 0, int base = 10); |
| 17 | |
| 18 | #include <string> |
| 19 | #include <cassert> |
Billy Robert O'Neal III | ed2f9a6 | 2018-08-08 00:40:32 +0000 | [diff] [blame] | 20 | #include <stdexcept> |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 21 | |
Roger Ferrer Ibanez | 84c152a | 2016-11-14 10:44:26 +0000 | [diff] [blame] | 22 | #include "test_macros.h" |
| 23 | |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 24 | int main() |
| 25 | { |
| 26 | assert(std::stoll("0") == 0); |
| 27 | assert(std::stoll(L"0") == 0); |
| 28 | assert(std::stoll("-0") == 0); |
| 29 | assert(std::stoll(L"-0") == 0); |
| 30 | assert(std::stoll("-10") == -10); |
| 31 | assert(std::stoll(L"-10") == -10); |
| 32 | assert(std::stoll(" 10") == 10); |
| 33 | assert(std::stoll(L" 10") == 10); |
| 34 | size_t idx = 0; |
| 35 | assert(std::stoll("10g", &idx, 16) == 16); |
| 36 | assert(idx == 2); |
| 37 | idx = 0; |
| 38 | assert(std::stoll(L"10g", &idx, 16) == 16); |
| 39 | assert(idx == 2); |
Roger Ferrer Ibanez | 84c152a | 2016-11-14 10:44:26 +0000 | [diff] [blame] | 40 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 41 | idx = 0; |
| 42 | try |
| 43 | { |
| 44 | std::stoll("", &idx); |
| 45 | assert(false); |
| 46 | } |
| 47 | catch (const std::invalid_argument&) |
| 48 | { |
| 49 | assert(idx == 0); |
| 50 | } |
| 51 | try |
| 52 | { |
| 53 | std::stoll(L"", &idx); |
| 54 | assert(false); |
| 55 | } |
| 56 | catch (const std::invalid_argument&) |
| 57 | { |
| 58 | assert(idx == 0); |
| 59 | } |
| 60 | try |
| 61 | { |
| 62 | std::stoll(" - 8", &idx); |
| 63 | assert(false); |
| 64 | } |
| 65 | catch (const std::invalid_argument&) |
| 66 | { |
| 67 | assert(idx == 0); |
| 68 | } |
| 69 | try |
| 70 | { |
| 71 | std::stoll(L" - 8", &idx); |
| 72 | assert(false); |
| 73 | } |
| 74 | catch (const std::invalid_argument&) |
| 75 | { |
| 76 | assert(idx == 0); |
| 77 | } |
| 78 | try |
| 79 | { |
| 80 | std::stoll("a1", &idx); |
| 81 | assert(false); |
| 82 | } |
| 83 | catch (const std::invalid_argument&) |
| 84 | { |
| 85 | assert(idx == 0); |
| 86 | } |
| 87 | try |
| 88 | { |
| 89 | std::stoll(L"a1", &idx); |
| 90 | assert(false); |
| 91 | } |
| 92 | catch (const std::invalid_argument&) |
| 93 | { |
| 94 | assert(idx == 0); |
| 95 | } |
Howard Hinnant | 1afbaba | 2013-01-14 18:59:43 +0000 | [diff] [blame] | 96 | try |
| 97 | { |
| 98 | std::stoll("99999999999999999999999999", &idx); |
| 99 | assert(false); |
| 100 | } |
| 101 | catch (const std::out_of_range&) |
| 102 | { |
| 103 | assert(idx == 0); |
| 104 | } |
Marshall Clow | 914993d | 2013-08-13 15:52:51 +0000 | [diff] [blame] | 105 | try |
| 106 | { |
| 107 | std::stoll(L"99999999999999999999999999", &idx); |
| 108 | assert(false); |
| 109 | } |
| 110 | catch (const std::out_of_range&) |
| 111 | { |
| 112 | assert(idx == 0); |
| 113 | } |
Roger Ferrer Ibanez | 84c152a | 2016-11-14 10:44:26 +0000 | [diff] [blame] | 114 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 115 | } |