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