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