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