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