Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Howard Hinnant | 56c917c | 2013-08-22 00:04:22 +0000 | [diff] [blame] | 9 | // |
Jonathan Roelofs | eb7b5e7 | 2015-01-14 23:38:12 +0000 | [diff] [blame] | 10 | // XFAIL: with_system_cxx_lib=x86_64-apple-darwin11 |
| 11 | // XFAIL: with_system_cxx_lib=x86_64-apple-darwin12 |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 12 | |
| 13 | // <string> |
| 14 | |
| 15 | // unsigned long stoul(const string& str, size_t *idx = 0, int base = 10); |
| 16 | // unsigned long stoul(const wstring& str, size_t *idx = 0, int base = 10); |
| 17 | |
| 18 | #include <string> |
| 19 | #include <cassert> |
| 20 | |
Roger Ferrer Ibanez | 84c152a | 2016-11-14 10:44:26 +0000 | [diff] [blame^] | 21 | #include "test_macros.h" |
| 22 | |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 23 | int main() |
| 24 | { |
| 25 | assert(std::stoul("0") == 0); |
| 26 | assert(std::stoul(L"0") == 0); |
| 27 | assert(std::stoul("-0") == 0); |
| 28 | assert(std::stoul(L"-0") == 0); |
| 29 | assert(std::stoul(" 10") == 10); |
| 30 | assert(std::stoul(L" 10") == 10); |
| 31 | size_t idx = 0; |
| 32 | assert(std::stoul("10g", &idx, 16) == 16); |
| 33 | assert(idx == 2); |
| 34 | idx = 0; |
| 35 | assert(std::stoul(L"10g", &idx, 16) == 16); |
| 36 | assert(idx == 2); |
Roger Ferrer Ibanez | 84c152a | 2016-11-14 10:44:26 +0000 | [diff] [blame^] | 37 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 38 | idx = 0; |
| 39 | try |
| 40 | { |
| 41 | std::stoul("", &idx); |
| 42 | assert(false); |
| 43 | } |
| 44 | catch (const std::invalid_argument&) |
| 45 | { |
| 46 | assert(idx == 0); |
| 47 | } |
| 48 | try |
| 49 | { |
| 50 | std::stoul(L"", &idx); |
| 51 | assert(false); |
| 52 | } |
| 53 | catch (const std::invalid_argument&) |
| 54 | { |
| 55 | assert(idx == 0); |
| 56 | } |
| 57 | try |
| 58 | { |
| 59 | std::stoul(" - 8", &idx); |
| 60 | assert(false); |
| 61 | } |
| 62 | catch (const std::invalid_argument&) |
| 63 | { |
| 64 | assert(idx == 0); |
| 65 | } |
| 66 | try |
| 67 | { |
| 68 | std::stoul(L" - 8", &idx); |
| 69 | assert(false); |
| 70 | } |
| 71 | catch (const std::invalid_argument&) |
| 72 | { |
| 73 | assert(idx == 0); |
| 74 | } |
| 75 | try |
| 76 | { |
| 77 | std::stoul("a1", &idx); |
| 78 | assert(false); |
| 79 | } |
| 80 | catch (const std::invalid_argument&) |
| 81 | { |
| 82 | assert(idx == 0); |
| 83 | } |
| 84 | try |
| 85 | { |
| 86 | std::stoul(L"a1", &idx); |
| 87 | assert(false); |
| 88 | } |
| 89 | catch (const std::invalid_argument&) |
| 90 | { |
| 91 | assert(idx == 0); |
| 92 | } |
Marshall Clow | e4fa0de | 2013-08-13 22:22:40 +0000 | [diff] [blame] | 93 | // LWG issue #2009 |
Marshall Clow | 914993d | 2013-08-13 15:52:51 +0000 | [diff] [blame] | 94 | try |
| 95 | { |
| 96 | std::stoul("9999999999999999999999999999999999999999999999999", &idx); |
| 97 | assert(false); |
| 98 | } |
| 99 | catch (const std::out_of_range&) |
| 100 | { |
| 101 | assert(idx == 0); |
| 102 | } |
| 103 | try |
| 104 | { |
| 105 | std::stoul(L"9999999999999999999999999999999999999999999999999", &idx); |
| 106 | assert(false); |
| 107 | } |
| 108 | catch (const std::out_of_range&) |
| 109 | { |
| 110 | assert(idx == 0); |
| 111 | } |
Roger Ferrer Ibanez | 84c152a | 2016-11-14 10:44:26 +0000 | [diff] [blame^] | 112 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 113 | } |