blob: 41d11010783a8360fa94b0e824d9480066398c40 [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//===----------------------------------------------------------------------===//
9
10// <string>
11
12// unsigned long stoul(const string& str, size_t *idx = 0, int base = 10);
13// unsigned long stoul(const wstring& str, size_t *idx = 0, int base = 10);
14
15#include <string>
16#include <cassert>
17
18int main()
19{
20 assert(std::stoul("0") == 0);
21 assert(std::stoul(L"0") == 0);
22 assert(std::stoul("-0") == 0);
23 assert(std::stoul(L"-0") == 0);
24 assert(std::stoul(" 10") == 10);
25 assert(std::stoul(L" 10") == 10);
26 size_t idx = 0;
27 assert(std::stoul("10g", &idx, 16) == 16);
28 assert(idx == 2);
29 idx = 0;
30 assert(std::stoul(L"10g", &idx, 16) == 16);
31 assert(idx == 2);
32 idx = 0;
33 try
34 {
35 std::stoul("", &idx);
36 assert(false);
37 }
38 catch (const std::invalid_argument&)
39 {
40 assert(idx == 0);
41 }
42 try
43 {
44 std::stoul(L"", &idx);
45 assert(false);
46 }
47 catch (const std::invalid_argument&)
48 {
49 assert(idx == 0);
50 }
51 try
52 {
53 std::stoul(" - 8", &idx);
54 assert(false);
55 }
56 catch (const std::invalid_argument&)
57 {
58 assert(idx == 0);
59 }
60 try
61 {
62 std::stoul(L" - 8", &idx);
63 assert(false);
64 }
65 catch (const std::invalid_argument&)
66 {
67 assert(idx == 0);
68 }
69 try
70 {
71 std::stoul("a1", &idx);
72 assert(false);
73 }
74 catch (const std::invalid_argument&)
75 {
76 assert(idx == 0);
77 }
78 try
79 {
80 std::stoul(L"a1", &idx);
81 assert(false);
82 }
83 catch (const std::invalid_argument&)
84 {
85 assert(idx == 0);
86 }
Marshall Clow914993d2013-08-13 15:52:51 +000087// LWG issue #2009
88 try
89 {
90 std::stoul("9999999999999999999999999999999999999999999999999", &idx);
91 assert(false);
92 }
93 catch (const std::out_of_range&)
94 {
95 assert(idx == 0);
96 }
97 try
98 {
99 std::stoul(L"9999999999999999999999999999999999999999999999999", &idx);
100 assert(false);
101 }
102 catch (const std::out_of_range&)
103 {
104 assert(idx == 0);
105 }
Howard Hinnantcbbf6332010-06-02 18:20:39 +0000106}