1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // XFAIL: libcpp-no-exceptions 11 // XFAIL: with_system_cxx_lib=x86_64-apple-darwin11 12 // XFAIL: with_system_cxx_lib=x86_64-apple-darwin12 13 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> 21 22 int main() 23 { 24 assert(std::stoul("0") == 0); 25 assert(std::stoul(L"0") == 0); 26 assert(std::stoul("-0") == 0); 27 assert(std::stoul(L"-0") == 0); 28 assert(std::stoul(" 10") == 10); 29 assert(std::stoul(L" 10") == 10); 30 size_t idx = 0; 31 assert(std::stoul("10g", &idx, 16) == 16); 32 assert(idx == 2); 33 idx = 0; 34 assert(std::stoul(L"10g", &idx, 16) == 16); 35 assert(idx == 2); 36 idx = 0; 37 try 38 { 39 std::stoul("", &idx); 40 assert(false); 41 } 42 catch (const std::invalid_argument&) 43 { 44 assert(idx == 0); 45 } 46 try 47 { 48 std::stoul(L"", &idx); 49 assert(false); 50 } 51 catch (const std::invalid_argument&) 52 { 53 assert(idx == 0); 54 } 55 try 56 { 57 std::stoul(" - 8", &idx); 58 assert(false); 59 } 60 catch (const std::invalid_argument&) 61 { 62 assert(idx == 0); 63 } 64 try 65 { 66 std::stoul(L" - 8", &idx); 67 assert(false); 68 } 69 catch (const std::invalid_argument&) 70 { 71 assert(idx == 0); 72 } 73 try 74 { 75 std::stoul("a1", &idx); 76 assert(false); 77 } 78 catch (const std::invalid_argument&) 79 { 80 assert(idx == 0); 81 } 82 try 83 { 84 std::stoul(L"a1", &idx); 85 assert(false); 86 } 87 catch (const std::invalid_argument&) 88 { 89 assert(idx == 0); 90 } 91 // LWG issue #2009 92 try 93 { 94 std::stoul("9999999999999999999999999999999999999999999999999", &idx); 95 assert(false); 96 } 97 catch (const std::out_of_range&) 98 { 99 assert(idx == 0); 100 } 101 try 102 { 103 std::stoul(L"9999999999999999999999999999999999999999999999999", &idx); 104 assert(false); 105 } 106 catch (const std::out_of_range&) 107 { 108 assert(idx == 0); 109 } 110 } 111