blob: 2ca2c01cfb8c5f78f7be2478d02a9d9ba99ce70d [file] [log] [blame]
Howard Hinnantcbbf6332010-06-02 18:20:39 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantcbbf6332010-06-02 18:20:39 +00007//
8//===----------------------------------------------------------------------===//
Howard Hinnant56c917c2013-08-22 00:04:22 +00009//
Asiri Rathnayakef520c142015-11-10 11:41:22 +000010// XFAIL: libcpp-no-exceptions
Jonathan Roelofseb7b5e72015-01-14 23:38:12 +000011// XFAIL: with_system_cxx_lib=x86_64-apple-darwin11
12// XFAIL: with_system_cxx_lib=x86_64-apple-darwin12
Howard Hinnantcbbf6332010-06-02 18:20:39 +000013
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
22int main()
23{
24 assert(std::stol("0") == 0);
25 assert(std::stol(L"0") == 0);
26 assert(std::stol("-0") == 0);
27 assert(std::stol(L"-0") == 0);
28 assert(std::stol("-10") == -10);
29 assert(std::stol(L"-10") == -10);
30 assert(std::stol(" 10") == 10);
31 assert(std::stol(L" 10") == 10);
32 size_t idx = 0;
33 assert(std::stol("10g", &idx, 16) == 16);
34 assert(idx == 2);
35 idx = 0;
36 assert(std::stol(L"10g", &idx, 16) == 16);
37 assert(idx == 2);
38 idx = 0;
39 try
40 {
41 std::stol("", &idx);
42 assert(false);
43 }
44 catch (const std::invalid_argument&)
45 {
46 assert(idx == 0);
47 }
48 try
49 {
50 std::stol(L"", &idx);
51 assert(false);
52 }
53 catch (const std::invalid_argument&)
54 {
55 assert(idx == 0);
56 }
57 try
58 {
59 std::stol(" - 8", &idx);
60 assert(false);
61 }
62 catch (const std::invalid_argument&)
63 {
64 assert(idx == 0);
65 }
66 try
67 {
68 std::stol(L" - 8", &idx);
69 assert(false);
70 }
71 catch (const std::invalid_argument&)
72 {
73 assert(idx == 0);
74 }
75 try
76 {
77 std::stol("a1", &idx);
78 assert(false);
79 }
80 catch (const std::invalid_argument&)
81 {
82 assert(idx == 0);
83 }
84 try
85 {
86 std::stol(L"a1", &idx);
87 assert(false);
88 }
89 catch (const std::invalid_argument&)
90 {
91 assert(idx == 0);
92 }
Marshall Clowe4fa0de2013-08-13 22:22:40 +000093// LWG issue #2009
Marshall Clow914993d2013-08-13 15:52:51 +000094 try
95 {
96 std::stol("9999999999999999999999999999999999999999999999999", &idx);
97 assert(false);
98 }
99 catch (const std::out_of_range&)
100 {
101 assert(idx == 0);
102 }
103 try
104 {
105 std::stol(L"9999999999999999999999999999999999999999999999999", &idx);
106 assert(false);
107 }
108 catch (const std::out_of_range&)
109 {
110 assert(idx == 0);
111 }
Howard Hinnantcbbf6332010-06-02 18:20:39 +0000112}