xref: /llvm-project/libcxx/test/std/strings/string.conversions/stoi.pass.cpp (revision 25838c6dacca8203b4478ac2c16e563b51c3708b)
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 // int stoi(const string& str, size_t *idx = 0, int base = 10);
12 // int stoi(const wstring& str, size_t *idx = 0, int base = 10);
13 
14 // When back-deploying to macosx10.7, the RTTI for exception classes
15 // incorrectly provided by libc++.dylib is mixed with the one in
16 // libc++abi.dylib and exceptions are not caught properly.
17 // XFAIL: with_system_cxx_lib=macosx10.7
18 
19 #include <string>
20 #include <cassert>
21 #include <stdexcept>
22 
23 #include "test_macros.h"
24 
25 int main(int, char**)
26 {
27     assert(std::stoi("0") == 0);
28     assert(std::stoi(L"0") == 0);
29     assert(std::stoi("-0") == 0);
30     assert(std::stoi(L"-0") == 0);
31     assert(std::stoi("-10") == -10);
32     assert(std::stoi(L"-10") == -10);
33     assert(std::stoi(" 10") == 10);
34     assert(std::stoi(L" 10") == 10);
35     size_t idx = 0;
36     assert(std::stoi("10g", &idx, 16) == 16);
37     assert(idx == 2);
38     idx = 0;
39     assert(std::stoi(L"10g", &idx, 16) == 16);
40     assert(idx == 2);
41 #ifndef TEST_HAS_NO_EXCEPTIONS
42     if (std::numeric_limits<long>::max() > std::numeric_limits<int>::max())
43     {
44         try
45         {
46             std::stoi("0x100000000", &idx, 16);
47             assert(false);
48         }
49         catch (const std::out_of_range&)
50         {
51         }
52         try
53         {
54             std::stoi(L"0x100000000", &idx, 16);
55             assert(false);
56         }
57         catch (const std::out_of_range&)
58         {
59         }
60     }
61     idx = 0;
62     try
63     {
64         std::stoi("", &idx);
65         assert(false);
66     }
67     catch (const std::invalid_argument&)
68     {
69         assert(idx == 0);
70     }
71     try
72     {
73         std::stoi(L"", &idx);
74         assert(false);
75     }
76     catch (const std::invalid_argument&)
77     {
78         assert(idx == 0);
79     }
80     try
81     {
82         std::stoi("  - 8", &idx);
83         assert(false);
84     }
85     catch (const std::invalid_argument&)
86     {
87         assert(idx == 0);
88     }
89     try
90     {
91         std::stoi(L"  - 8", &idx);
92         assert(false);
93     }
94     catch (const std::invalid_argument&)
95     {
96         assert(idx == 0);
97     }
98     try
99     {
100         std::stoi("a1", &idx);
101         assert(false);
102     }
103     catch (const std::invalid_argument&)
104     {
105         assert(idx == 0);
106     }
107     try
108     {
109         std::stoi(L"a1", &idx);
110         assert(false);
111     }
112     catch (const std::invalid_argument&)
113     {
114         assert(idx == 0);
115     }
116 #endif
117 
118   return 0;
119 }
120