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