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