| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <string> |
| 10 | |
| 11 | // unsigned long stoul(const string& str, size_t *idx = 0, int base = 10); |
| 12 | // unsigned long stoul(const wstring& str, size_t *idx = 0, int base = 10); |
| 13 | |
| 14 | #include <string> |
| 15 | #include <cassert> |
| 16 | #include <stdexcept> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | |
| 20 | int main(int, char**) { |
| 21 | assert(std::stoul("0" ) == 0); |
| 22 | assert(std::stoul("-0" ) == 0); |
| 23 | assert(std::stoul(" 10" ) == 10); |
| 24 | { |
| 25 | std::size_t idx = 0; |
| 26 | assert(std::stoul("10g" , &idx, 16) == 16); |
| 27 | assert(idx == 2); |
| 28 | } |
| 29 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 30 | { |
| 31 | std::size_t idx = 0; |
| 32 | try { |
| 33 | (void)std::stoul("" , &idx); |
| 34 | assert(false); |
| 35 | } catch (const std::invalid_argument&) { |
| 36 | assert(idx == 0); |
| 37 | } |
| 38 | } |
| 39 | { |
| 40 | std::size_t idx = 0; |
| 41 | try { |
| 42 | (void)std::stoul(" - 8" , &idx); |
| 43 | assert(false); |
| 44 | } catch (const std::invalid_argument&) { |
| 45 | assert(idx == 0); |
| 46 | } |
| 47 | } |
| 48 | { |
| 49 | std::size_t idx = 0; |
| 50 | try { |
| 51 | (void)std::stoul("a1" , &idx); |
| 52 | assert(false); |
| 53 | } catch (const std::invalid_argument&) { |
| 54 | assert(idx == 0); |
| 55 | } |
| 56 | } |
| 57 | { |
| 58 | std::size_t idx = 0; |
| 59 | try { |
| 60 | // LWG#2009 and PR14919 |
| 61 | (void)std::stoul("9999999999999999999999999999999999999999999999999" , &idx); |
| 62 | assert(false); |
| 63 | } catch (const std::out_of_range&) { |
| 64 | assert(idx == 0); |
| 65 | } |
| 66 | } |
| 67 | #endif // TEST_HAS_NO_EXCEPTIONS |
| 68 | |
| 69 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 70 | assert(std::stoul(L"0" ) == 0); |
| 71 | assert(std::stoul(L"-0" ) == 0); |
| 72 | assert(std::stoul(L" 10" ) == 10); |
| 73 | { |
| 74 | std::size_t idx = 0; |
| 75 | assert(std::stoul(L"10g" , &idx, 16) == 16); |
| 76 | assert(idx == 2); |
| 77 | } |
| 78 | # ifndef TEST_HAS_NO_EXCEPTIONS |
| 79 | { |
| 80 | std::size_t idx = 0; |
| 81 | try { |
| 82 | (void)std::stoul(L"" , &idx); |
| 83 | assert(false); |
| 84 | } catch (const std::invalid_argument&) { |
| 85 | assert(idx == 0); |
| 86 | } |
| 87 | } |
| 88 | { |
| 89 | std::size_t idx = 0; |
| 90 | try { |
| 91 | (void)std::stoul(L" - 8" , &idx); |
| 92 | assert(false); |
| 93 | } catch (const std::invalid_argument&) { |
| 94 | assert(idx == 0); |
| 95 | } |
| 96 | } |
| 97 | { |
| 98 | std::size_t idx = 0; |
| 99 | try { |
| 100 | (void)std::stoul(L"a1" , &idx); |
| 101 | assert(false); |
| 102 | } catch (const std::invalid_argument&) { |
| 103 | assert(idx == 0); |
| 104 | } |
| 105 | } |
| 106 | { |
| 107 | std::size_t idx = 0; |
| 108 | try { |
| 109 | // LWG#2009 and PR14919 |
| 110 | (void)std::stoul(L"9999999999999999999999999999999999999999999999999" , &idx); |
| 111 | assert(false); |
| 112 | } catch (const std::out_of_range&) { |
| 113 | assert(idx == 0); |
| 114 | } |
| 115 | } |
| 116 | # endif // TEST_HAS_NO_EXCEPTIONS |
| 117 | #endif // TEST_HAS_NO_WIDE_CHARACTERS |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |