Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / strings / string.conversions / stol.pass.cpp
blob083a6aaff28c97d0ebdc115e8ba28c1b3919ee18
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 //===----------------------------------------------------------------------===//
9 // <string>
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);
14 #include <string>
15 #include <cassert>
16 #include <limits>
17 #include <stdexcept>
19 #include "test_macros.h"
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);
27 std::size_t idx = 0;
28 assert(std::stol("10g", &idx, 16) == 16);
29 assert(idx == 2);
31 #ifndef TEST_HAS_NO_EXCEPTIONS
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);
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);
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);
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);
69 #endif // TEST_HAS_NO_EXCEPTIONS
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);
77 std::size_t idx = 0;
78 assert(std::stol(L"10g", &idx, 16) == 16);
79 assert(idx == 2);
81 # ifndef TEST_HAS_NO_EXCEPTIONS
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);
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);
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);
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);
119 # endif // TEST_HAS_NO_EXCEPTIONS
120 #endif // TEST_HAS_NO_WIDE_CHARACTERS
122 return 0;