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