xref: /llvm-project/libcxx/test/std/strings/string.conversions/stoll.pass.cpp (revision f4c1258d5633fcf06385ff3fd1f4bf57ab971964)
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 
20 int main(int, char**)
21 {
22     assert(std::stoll("0") == 0);
23     assert(std::stoll("-0") == 0);
24     assert(std::stoll("-10") == -10);
25     assert(std::stoll(" 10") == 10);
26     {
27         size_t idx = 0;
28         assert(std::stoll("10g", &idx, 16) == 16);
29         assert(idx == 2);
30     }
31 #ifndef TEST_HAS_NO_EXCEPTIONS
32     {
33         size_t idx = 0;
34         try {
35             std::stoll("", &idx);
36             assert(false);
37         } catch (const std::invalid_argument&) {
38             assert(idx == 0);
39         }
40     }
41     {
42         size_t idx = 0;
43         try {
44             std::stoll("  - 8", &idx);
45             assert(false);
46         } catch (const std::invalid_argument&) {
47             assert(idx == 0);
48         }
49     }
50     {
51         size_t idx = 0;
52         try {
53             std::stoll("a1", &idx);
54             assert(false);
55         } catch (const std::invalid_argument&) {
56             assert(idx == 0);
57         }
58     }
59     {
60         size_t idx = 0;
61         try {
62             // LWG#2009 and PR14919
63             std::stoll("99999999999999999999999999", &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::stoll(L"0") == 0);
73     assert(std::stoll(L"-0") == 0);
74     assert(std::stoll(L"-10") == -10);
75     assert(std::stoll(L" 10") == 10);
76     {
77         size_t idx = 0;
78         assert(std::stoll(L"10g", &idx, 16) == 16);
79         assert(idx == 2);
80     }
81 #ifndef TEST_HAS_NO_EXCEPTIONS
82     {
83         size_t idx = 0;
84         try {
85             std::stoll(L"", &idx);
86             assert(false);
87         } catch (const std::invalid_argument&) {
88             assert(idx == 0);
89         }
90     }
91     {
92         size_t idx = 0;
93         try {
94             std::stoll(L"  - 8", &idx);
95             assert(false);
96         } catch (const std::invalid_argument&) {
97             assert(idx == 0);
98         }
99     }
100     {
101         size_t idx = 0;
102         try {
103             std::stoll(L"a1", &idx);
104             assert(false);
105         } catch (const std::invalid_argument&) {
106             assert(idx == 0);
107         }
108     }
109     {
110         size_t idx = 0;
111         try {
112             // LWG#2009 and PR14919
113             std::stoll(L"99999999999999999999999999", &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