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