blob: 6f273321de7cbb0ca8ad8dd00f26c7b8a6ad39f2 [file] [log] [blame]
Howard Hinnantcbbf6332010-06-02 18:20:39 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Howard Hinnantcbbf6332010-06-02 18:20:39 +00006//
7//===----------------------------------------------------------------------===//
8
Howard Hinnantcbbf6332010-06-02 18:20:39 +00009// <string>
10
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);
13
Louis Dionne25838c62019-02-27 00:57:57 +000014// When back-deploying to macosx10.7, the RTTI for exception classes
15// incorrectly provided by libc++.dylib is mixed with the one in
16// libc++abi.dylib and exceptions are not caught properly.
17// XFAIL: with_system_cxx_lib=macosx10.7
18
Howard Hinnantcbbf6332010-06-02 18:20:39 +000019#include <string>
20#include <cassert>
Billy Robert O'Neal IIIed2f9a62018-08-08 00:40:32 +000021#include <stdexcept>
Howard Hinnantcbbf6332010-06-02 18:20:39 +000022
Roger Ferrer Ibanez84c152a2016-11-14 10:44:26 +000023#include "test_macros.h"
24
JF Bastien2df59c52019-02-04 20:31:13 +000025int main(int, char**)
Howard Hinnantcbbf6332010-06-02 18:20:39 +000026{
27 assert(std::stoi("0") == 0);
28 assert(std::stoi(L"0") == 0);
29 assert(std::stoi("-0") == 0);
30 assert(std::stoi(L"-0") == 0);
31 assert(std::stoi("-10") == -10);
32 assert(std::stoi(L"-10") == -10);
33 assert(std::stoi(" 10") == 10);
34 assert(std::stoi(L" 10") == 10);
35 size_t idx = 0;
36 assert(std::stoi("10g", &idx, 16) == 16);
37 assert(idx == 2);
38 idx = 0;
39 assert(std::stoi(L"10g", &idx, 16) == 16);
40 assert(idx == 2);
Roger Ferrer Ibanez84c152a2016-11-14 10:44:26 +000041#ifndef TEST_HAS_NO_EXCEPTIONS
Howard Hinnantcbbf6332010-06-02 18:20:39 +000042 if (std::numeric_limits<long>::max() > std::numeric_limits<int>::max())
43 {
44 try
45 {
46 std::stoi("0x100000000", &idx, 16);
47 assert(false);
48 }
49 catch (const std::out_of_range&)
50 {
51 }
52 try
53 {
54 std::stoi(L"0x100000000", &idx, 16);
55 assert(false);
56 }
57 catch (const std::out_of_range&)
58 {
59 }
60 }
61 idx = 0;
62 try
63 {
64 std::stoi("", &idx);
65 assert(false);
66 }
67 catch (const std::invalid_argument&)
68 {
69 assert(idx == 0);
70 }
71 try
72 {
73 std::stoi(L"", &idx);
74 assert(false);
75 }
76 catch (const std::invalid_argument&)
77 {
78 assert(idx == 0);
79 }
80 try
81 {
82 std::stoi(" - 8", &idx);
83 assert(false);
84 }
85 catch (const std::invalid_argument&)
86 {
87 assert(idx == 0);
88 }
89 try
90 {
91 std::stoi(L" - 8", &idx);
92 assert(false);
93 }
94 catch (const std::invalid_argument&)
95 {
96 assert(idx == 0);
97 }
98 try
99 {
100 std::stoi("a1", &idx);
101 assert(false);
102 }
103 catch (const std::invalid_argument&)
104 {
105 assert(idx == 0);
106 }
107 try
108 {
109 std::stoi(L"a1", &idx);
110 assert(false);
111 }
112 catch (const std::invalid_argument&)
113 {
114 assert(idx == 0);
115 }
Roger Ferrer Ibanez84c152a2016-11-14 10:44:26 +0000116#endif
JF Bastien2df59c52019-02-04 20:31:13 +0000117
118 return 0;
Howard Hinnantcbbf6332010-06-02 18:20:39 +0000119}