xref: /aosp_15_r20/external/libcxx/test/std/strings/string.conversions/stoi.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 // <string>
11*58b9f456SAndroid Build Coastguard Worker 
12*58b9f456SAndroid Build Coastguard Worker // int stoi(const string& str, size_t *idx = 0, int base = 10);
13*58b9f456SAndroid Build Coastguard Worker // int stoi(const wstring& str, size_t *idx = 0, int base = 10);
14*58b9f456SAndroid Build Coastguard Worker 
15*58b9f456SAndroid Build Coastguard Worker #include <string>
16*58b9f456SAndroid Build Coastguard Worker #include <cassert>
17*58b9f456SAndroid Build Coastguard Worker #include <stdexcept>
18*58b9f456SAndroid Build Coastguard Worker 
19*58b9f456SAndroid Build Coastguard Worker #include "test_macros.h"
20*58b9f456SAndroid Build Coastguard Worker 
main()21*58b9f456SAndroid Build Coastguard Worker int main()
22*58b9f456SAndroid Build Coastguard Worker {
23*58b9f456SAndroid Build Coastguard Worker     assert(std::stoi("0") == 0);
24*58b9f456SAndroid Build Coastguard Worker     assert(std::stoi(L"0") == 0);
25*58b9f456SAndroid Build Coastguard Worker     assert(std::stoi("-0") == 0);
26*58b9f456SAndroid Build Coastguard Worker     assert(std::stoi(L"-0") == 0);
27*58b9f456SAndroid Build Coastguard Worker     assert(std::stoi("-10") == -10);
28*58b9f456SAndroid Build Coastguard Worker     assert(std::stoi(L"-10") == -10);
29*58b9f456SAndroid Build Coastguard Worker     assert(std::stoi(" 10") == 10);
30*58b9f456SAndroid Build Coastguard Worker     assert(std::stoi(L" 10") == 10);
31*58b9f456SAndroid Build Coastguard Worker     size_t idx = 0;
32*58b9f456SAndroid Build Coastguard Worker     assert(std::stoi("10g", &idx, 16) == 16);
33*58b9f456SAndroid Build Coastguard Worker     assert(idx == 2);
34*58b9f456SAndroid Build Coastguard Worker     idx = 0;
35*58b9f456SAndroid Build Coastguard Worker     assert(std::stoi(L"10g", &idx, 16) == 16);
36*58b9f456SAndroid Build Coastguard Worker     assert(idx == 2);
37*58b9f456SAndroid Build Coastguard Worker #ifndef TEST_HAS_NO_EXCEPTIONS
38*58b9f456SAndroid Build Coastguard Worker     if (std::numeric_limits<long>::max() > std::numeric_limits<int>::max())
39*58b9f456SAndroid Build Coastguard Worker     {
40*58b9f456SAndroid Build Coastguard Worker         try
41*58b9f456SAndroid Build Coastguard Worker         {
42*58b9f456SAndroid Build Coastguard Worker             std::stoi("0x100000000", &idx, 16);
43*58b9f456SAndroid Build Coastguard Worker             assert(false);
44*58b9f456SAndroid Build Coastguard Worker         }
45*58b9f456SAndroid Build Coastguard Worker         catch (const std::out_of_range&)
46*58b9f456SAndroid Build Coastguard Worker         {
47*58b9f456SAndroid Build Coastguard Worker         }
48*58b9f456SAndroid Build Coastguard Worker         try
49*58b9f456SAndroid Build Coastguard Worker         {
50*58b9f456SAndroid Build Coastguard Worker             std::stoi(L"0x100000000", &idx, 16);
51*58b9f456SAndroid Build Coastguard Worker             assert(false);
52*58b9f456SAndroid Build Coastguard Worker         }
53*58b9f456SAndroid Build Coastguard Worker         catch (const std::out_of_range&)
54*58b9f456SAndroid Build Coastguard Worker         {
55*58b9f456SAndroid Build Coastguard Worker         }
56*58b9f456SAndroid Build Coastguard Worker     }
57*58b9f456SAndroid Build Coastguard Worker     idx = 0;
58*58b9f456SAndroid Build Coastguard Worker     try
59*58b9f456SAndroid Build Coastguard Worker     {
60*58b9f456SAndroid Build Coastguard Worker         std::stoi("", &idx);
61*58b9f456SAndroid Build Coastguard Worker         assert(false);
62*58b9f456SAndroid Build Coastguard Worker     }
63*58b9f456SAndroid Build Coastguard Worker     catch (const std::invalid_argument&)
64*58b9f456SAndroid Build Coastguard Worker     {
65*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
66*58b9f456SAndroid Build Coastguard Worker     }
67*58b9f456SAndroid Build Coastguard Worker     try
68*58b9f456SAndroid Build Coastguard Worker     {
69*58b9f456SAndroid Build Coastguard Worker         std::stoi(L"", &idx);
70*58b9f456SAndroid Build Coastguard Worker         assert(false);
71*58b9f456SAndroid Build Coastguard Worker     }
72*58b9f456SAndroid Build Coastguard Worker     catch (const std::invalid_argument&)
73*58b9f456SAndroid Build Coastguard Worker     {
74*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
75*58b9f456SAndroid Build Coastguard Worker     }
76*58b9f456SAndroid Build Coastguard Worker     try
77*58b9f456SAndroid Build Coastguard Worker     {
78*58b9f456SAndroid Build Coastguard Worker         std::stoi("  - 8", &idx);
79*58b9f456SAndroid Build Coastguard Worker         assert(false);
80*58b9f456SAndroid Build Coastguard Worker     }
81*58b9f456SAndroid Build Coastguard Worker     catch (const std::invalid_argument&)
82*58b9f456SAndroid Build Coastguard Worker     {
83*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
84*58b9f456SAndroid Build Coastguard Worker     }
85*58b9f456SAndroid Build Coastguard Worker     try
86*58b9f456SAndroid Build Coastguard Worker     {
87*58b9f456SAndroid Build Coastguard Worker         std::stoi(L"  - 8", &idx);
88*58b9f456SAndroid Build Coastguard Worker         assert(false);
89*58b9f456SAndroid Build Coastguard Worker     }
90*58b9f456SAndroid Build Coastguard Worker     catch (const std::invalid_argument&)
91*58b9f456SAndroid Build Coastguard Worker     {
92*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
93*58b9f456SAndroid Build Coastguard Worker     }
94*58b9f456SAndroid Build Coastguard Worker     try
95*58b9f456SAndroid Build Coastguard Worker     {
96*58b9f456SAndroid Build Coastguard Worker         std::stoi("a1", &idx);
97*58b9f456SAndroid Build Coastguard Worker         assert(false);
98*58b9f456SAndroid Build Coastguard Worker     }
99*58b9f456SAndroid Build Coastguard Worker     catch (const std::invalid_argument&)
100*58b9f456SAndroid Build Coastguard Worker     {
101*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
102*58b9f456SAndroid Build Coastguard Worker     }
103*58b9f456SAndroid Build Coastguard Worker     try
104*58b9f456SAndroid Build Coastguard Worker     {
105*58b9f456SAndroid Build Coastguard Worker         std::stoi(L"a1", &idx);
106*58b9f456SAndroid Build Coastguard Worker         assert(false);
107*58b9f456SAndroid Build Coastguard Worker     }
108*58b9f456SAndroid Build Coastguard Worker     catch (const std::invalid_argument&)
109*58b9f456SAndroid Build Coastguard Worker     {
110*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
111*58b9f456SAndroid Build Coastguard Worker     }
112*58b9f456SAndroid Build Coastguard Worker #endif
113*58b9f456SAndroid Build Coastguard Worker }
114