xref: /llvm-project/libcxx/test/std/strings/string.conversions/stoul.pass.cpp (revision e9c66ad9fa9f1c1138c2a4868610e1d77271216c)
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // PR14919 was fixed in r172447, out_of_range wasn't thrown before.
11 // XFAIL: with_system_cxx_lib=macosx10.7
12 // XFAIL: with_system_cxx_lib=macosx10.8
13 
14 // <string>
15 
16 // unsigned long stoul(const string& str, size_t *idx = 0, int base = 10);
17 // unsigned long stoul(const wstring& str, size_t *idx = 0, int base = 10);
18 
19 #include <string>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 
24 int main()
25 {
26     assert(std::stoul("0") == 0);
27     assert(std::stoul(L"0") == 0);
28     assert(std::stoul("-0") == 0);
29     assert(std::stoul(L"-0") == 0);
30     assert(std::stoul(" 10") == 10);
31     assert(std::stoul(L" 10") == 10);
32     size_t idx = 0;
33     assert(std::stoul("10g", &idx, 16) == 16);
34     assert(idx == 2);
35     idx = 0;
36     assert(std::stoul(L"10g", &idx, 16) == 16);
37     assert(idx == 2);
38 #ifndef TEST_HAS_NO_EXCEPTIONS
39     idx = 0;
40     try
41     {
42         std::stoul("", &idx);
43         assert(false);
44     }
45     catch (const std::invalid_argument&)
46     {
47         assert(idx == 0);
48     }
49     try
50     {
51         std::stoul(L"", &idx);
52         assert(false);
53     }
54     catch (const std::invalid_argument&)
55     {
56         assert(idx == 0);
57     }
58     try
59     {
60         std::stoul("  - 8", &idx);
61         assert(false);
62     }
63     catch (const std::invalid_argument&)
64     {
65         assert(idx == 0);
66     }
67     try
68     {
69         std::stoul(L"  - 8", &idx);
70         assert(false);
71     }
72     catch (const std::invalid_argument&)
73     {
74         assert(idx == 0);
75     }
76     try
77     {
78         std::stoul("a1", &idx);
79         assert(false);
80     }
81     catch (const std::invalid_argument&)
82     {
83         assert(idx == 0);
84     }
85     try
86     {
87         std::stoul(L"a1", &idx);
88         assert(false);
89     }
90     catch (const std::invalid_argument&)
91     {
92         assert(idx == 0);
93     }
94 //  LWG issue #2009
95     try
96     {
97         std::stoul("9999999999999999999999999999999999999999999999999", &idx);
98         assert(false);
99     }
100     catch (const std::out_of_range&)
101     {
102         assert(idx == 0);
103     }
104     try
105     {
106         std::stoul(L"9999999999999999999999999999999999999999999999999", &idx);
107         assert(false);
108     }
109     catch (const std::out_of_range&)
110     {
111         assert(idx == 0);
112     }
113 #endif
114 }
115