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