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