[clang][test] Fix SemaCXX/msvc-pragma-function-no-builtin-attr.cpp for x86 (#119986)
[llvm-project.git] / libcxx / test / std / strings / string.conversions / stoi.pass.cpp
blob7fe0613b7fc58de31d706a5b0a779b7281c046c7
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 // int stoi(const string& str, size_t *idx = 0, int base = 10);
12 // int stoi(const wstring& str, size_t *idx = 0, int base = 10);
14 #include <string>
15 #include <cassert>
16 #include <stdexcept>
18 #include "test_macros.h"
20 int main(int, char**) {
21 assert(std::stoi("0") == 0);
22 assert(std::stoi("-0") == 0);
23 assert(std::stoi("-10") == -10);
24 assert(std::stoi(" 10") == 10);
26 std::size_t idx = 0;
27 assert(std::stoi("10g", &idx, 16) == 16);
28 assert(idx == 2);
30 #ifndef TEST_HAS_NO_EXCEPTIONS
31 if (std::numeric_limits<long>::max() > std::numeric_limits<int>::max()) {
32 std::size_t idx = 0;
33 try {
34 (void)std::stoi("0x100000000", &idx, 16);
35 assert(false);
36 } catch (const std::out_of_range&) {
40 std::size_t idx = 0;
41 try {
42 (void)std::stoi("", &idx);
43 assert(false);
44 } catch (const std::invalid_argument&) {
45 assert(idx == 0);
49 std::size_t idx = 0;
50 try {
51 (void)std::stoi(" - 8", &idx);
52 assert(false);
53 } catch (const std::invalid_argument&) {
54 assert(idx == 0);
58 std::size_t idx = 0;
59 try {
60 (void)std::stoi("a1", &idx);
61 assert(false);
62 } catch (const std::invalid_argument&) {
63 assert(idx == 0);
66 #endif // TEST_HAS_NO_EXCEPTIONS
68 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
69 assert(std::stoi(L"0") == 0);
70 assert(std::stoi(L"-0") == 0);
71 assert(std::stoi(L"-10") == -10);
72 assert(std::stoi(L" 10") == 10);
74 std::size_t idx = 0;
75 assert(std::stoi(L"10g", &idx, 16) == 16);
76 assert(idx == 2);
78 # ifndef TEST_HAS_NO_EXCEPTIONS
79 if (std::numeric_limits<long>::max() > std::numeric_limits<int>::max()) {
80 std::size_t idx = 0;
81 try {
82 (void)std::stoi(L"0x100000000", &idx, 16);
83 assert(false);
84 } catch (const std::out_of_range&) {
88 std::size_t idx = 0;
89 try {
90 (void)std::stoi(L"", &idx);
91 assert(false);
92 } catch (const std::invalid_argument&) {
93 assert(idx == 0);
97 std::size_t idx = 0;
98 try {
99 (void)std::stoi(L" - 8", &idx);
100 assert(false);
101 } catch (const std::invalid_argument&) {
102 assert(idx == 0);
106 std::size_t idx = 0;
107 try {
108 (void)std::stoi(L"a1", &idx);
109 assert(false);
110 } catch (const std::invalid_argument&) {
111 assert(idx == 0);
114 # endif // TEST_HAS_NO_EXCEPTIONS
115 #endif // TEST_HAS_NO_WIDE_CHARACTERS
117 return 0;