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