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