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