blob: c79c949413db7c1d3051eb2e946c0c7e737e8a87 [file] [log] [blame]
Howard Hinnantcbbf6332010-06-02 18:20:39 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantcbbf6332010-06-02 18:20:39 +00007//
8//===----------------------------------------------------------------------===//
Howard Hinnant56c917c2013-08-22 00:04:22 +00009//
Mehdi Aminie9c66ad2017-05-04 17:08:54 +000010// PR14919 was fixed in r172447, out_of_range wasn't thrown before.
11// XFAIL: with_system_cxx_lib=macosx10.7
12// XFAIL: with_system_cxx_lib=macosx10.8
Howard Hinnantcbbf6332010-06-02 18:20:39 +000013
14// <string>
15
16// unsigned long stoul(const string& str, size_t *idx = 0, int base = 10);
17// unsigned long stoul(const wstring& str, size_t *idx = 0, int base = 10);
18
19#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
Howard Hinnantcbbf6332010-06-02 18:20:39 +000025int main()
26{
27 assert(std::stoul("0") == 0);
28 assert(std::stoul(L"0") == 0);
29 assert(std::stoul("-0") == 0);
30 assert(std::stoul(L"-0") == 0);
31 assert(std::stoul(" 10") == 10);
32 assert(std::stoul(L" 10") == 10);
33 size_t idx = 0;
34 assert(std::stoul("10g", &idx, 16) == 16);
35 assert(idx == 2);
36 idx = 0;
37 assert(std::stoul(L"10g", &idx, 16) == 16);
38 assert(idx == 2);
Roger Ferrer Ibanez84c152a2016-11-14 10:44:26 +000039#ifndef TEST_HAS_NO_EXCEPTIONS
Howard Hinnantcbbf6332010-06-02 18:20:39 +000040 idx = 0;
41 try
42 {
43 std::stoul("", &idx);
44 assert(false);
45 }
46 catch (const std::invalid_argument&)
47 {
48 assert(idx == 0);
49 }
50 try
51 {
52 std::stoul(L"", &idx);
53 assert(false);
54 }
55 catch (const std::invalid_argument&)
56 {
57 assert(idx == 0);
58 }
59 try
60 {
61 std::stoul(" - 8", &idx);
62 assert(false);
63 }
64 catch (const std::invalid_argument&)
65 {
66 assert(idx == 0);
67 }
68 try
69 {
70 std::stoul(L" - 8", &idx);
71 assert(false);
72 }
73 catch (const std::invalid_argument&)
74 {
75 assert(idx == 0);
76 }
77 try
78 {
79 std::stoul("a1", &idx);
80 assert(false);
81 }
82 catch (const std::invalid_argument&)
83 {
84 assert(idx == 0);
85 }
86 try
87 {
88 std::stoul(L"a1", &idx);
89 assert(false);
90 }
91 catch (const std::invalid_argument&)
92 {
93 assert(idx == 0);
94 }
Marshall Clowe4fa0de2013-08-13 22:22:40 +000095// LWG issue #2009
Marshall Clow914993d2013-08-13 15:52:51 +000096 try
97 {
98 std::stoul("9999999999999999999999999999999999999999999999999", &idx);
99 assert(false);
100 }
101 catch (const std::out_of_range&)
102 {
103 assert(idx == 0);
104 }
105 try
106 {
107 std::stoul(L"9999999999999999999999999999999999999999999999999", &idx);
108 assert(false);
109 }
110 catch (const std::out_of_range&)
111 {
112 assert(idx == 0);
113 }
Roger Ferrer Ibanez84c152a2016-11-14 10:44:26 +0000114#endif
Howard Hinnantcbbf6332010-06-02 18:20:39 +0000115}