Home | History | Annotate | Download | only in string.conversions
      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 // long stol(const string& str, size_t *idx = 0, int base = 10);
     13 // long stol(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::stol("0") == 0);
     21     assert(std::stol(L"0") == 0);
     22     assert(std::stol("-0") == 0);
     23     assert(std::stol(L"-0") == 0);
     24     assert(std::stol("-10") == -10);
     25     assert(std::stol(L"-10") == -10);
     26     assert(std::stol(" 10") == 10);
     27     assert(std::stol(L" 10") == 10);
     28     size_t idx = 0;
     29     assert(std::stol("10g", &idx, 16) == 16);
     30     assert(idx == 2);
     31     idx = 0;
     32     assert(std::stol(L"10g", &idx, 16) == 16);
     33     assert(idx == 2);
     34     idx = 0;
     35     try
     36     {
     37         std::stol("", &idx);
     38         assert(false);
     39     }
     40     catch (const std::invalid_argument&)
     41     {
     42         assert(idx == 0);
     43     }
     44     try
     45     {
     46         std::stol(L"", &idx);
     47         assert(false);
     48     }
     49     catch (const std::invalid_argument&)
     50     {
     51         assert(idx == 0);
     52     }
     53     try
     54     {
     55         std::stol("  - 8", &idx);
     56         assert(false);
     57     }
     58     catch (const std::invalid_argument&)
     59     {
     60         assert(idx == 0);
     61     }
     62     try
     63     {
     64         std::stol(L"  - 8", &idx);
     65         assert(false);
     66     }
     67     catch (const std::invalid_argument&)
     68     {
     69         assert(idx == 0);
     70     }
     71     try
     72     {
     73         std::stol("a1", &idx);
     74         assert(false);
     75     }
     76     catch (const std::invalid_argument&)
     77     {
     78         assert(idx == 0);
     79     }
     80     try
     81     {
     82         std::stol(L"a1", &idx);
     83         assert(false);
     84     }
     85     catch (const std::invalid_argument&)
     86     {
     87         assert(idx == 0);
     88     }
     89 }
     90