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 // XFAIL: with_system_lib=x86_64-apple-darwin11
     11 // XFAIL: with_system_lib=x86_64-apple-darwin12
     12 
     13 // <string>
     14 
     15 // float stof(const string& str, size_t *idx = 0);
     16 // float stof(const wstring& str, size_t *idx = 0);
     17 
     18 #include <string>
     19 #include <cmath>
     20 #include <cassert>
     21 
     22 int main()
     23 {
     24     assert(std::stof("0") == 0);
     25     assert(std::stof(L"0") == 0);
     26     assert(std::stof("-0") == 0);
     27     assert(std::stof(L"-0") == 0);
     28     assert(std::stof("-10") == -10);
     29     assert(std::stof(L"-10.5") == -10.5);
     30     assert(std::stof(" 10") == 10);
     31     assert(std::stof(L" 10") == 10);
     32     size_t idx = 0;
     33     assert(std::stof("10g", &idx) == 10);
     34     assert(idx == 2);
     35     idx = 0;
     36     assert(std::stof(L"10g", &idx) == 10);
     37     assert(idx == 2);
     38     idx = 0;
     39     try
     40     {
     41         assert(std::stof("1.e60", &idx) == INFINITY);
     42         assert(false);
     43     }
     44     catch (const std::out_of_range&)
     45     {
     46         assert(idx == 0);
     47     }
     48     try
     49     {
     50         assert(std::stof(L"1.e60", &idx) == INFINITY);
     51         assert(false);
     52     }
     53     catch (const std::out_of_range&)
     54     {
     55         assert(idx == 0);
     56     }
     57     idx = 0;
     58     try
     59     {
     60         assert(std::stof("1.e360", &idx) == INFINITY);
     61         assert(false);
     62     }
     63     catch (const std::out_of_range&)
     64     {
     65         assert(idx == 0);
     66     }
     67     try
     68     {
     69         assert(std::stof(L"1.e360", &idx) == INFINITY);
     70         assert(false);
     71     }
     72     catch (const std::out_of_range&)
     73     {
     74         assert(idx == 0);
     75     }
     76     try
     77     {
     78         assert(std::stof("INF", &idx) == INFINITY);
     79         assert(idx == 3);
     80     }
     81     catch (const std::out_of_range&)
     82     {
     83         assert(false);
     84     }
     85     idx = 0;
     86     try
     87     {
     88         assert(std::stof(L"INF", &idx) == INFINITY);
     89         assert(idx == 3);
     90     }
     91     catch (const std::out_of_range&)
     92     {
     93         assert(false);
     94     }
     95     idx = 0;
     96     try
     97     {
     98         assert(std::isnan(std::stof("NAN", &idx)));
     99         assert(idx == 3);
    100     }
    101     catch (const std::out_of_range&)
    102     {
    103         assert(false);
    104     }
    105     idx = 0;
    106     try
    107     {
    108         assert(std::isnan(std::stof(L"NAN", &idx)));
    109         assert(idx == 3);
    110     }
    111     catch (const std::out_of_range&)
    112     {
    113         assert(false);
    114     }
    115     idx = 0;
    116     try
    117     {
    118         std::stof("", &idx);
    119         assert(false);
    120     }
    121     catch (const std::invalid_argument&)
    122     {
    123         assert(idx == 0);
    124     }
    125     try
    126     {
    127         std::stof(L"", &idx);
    128         assert(false);
    129     }
    130     catch (const std::invalid_argument&)
    131     {
    132         assert(idx == 0);
    133     }
    134     try
    135     {
    136         std::stof("  - 8", &idx);
    137         assert(false);
    138     }
    139     catch (const std::invalid_argument&)
    140     {
    141         assert(idx == 0);
    142     }
    143     try
    144     {
    145         std::stof(L"  - 8", &idx);
    146         assert(false);
    147     }
    148     catch (const std::invalid_argument&)
    149     {
    150         assert(idx == 0);
    151     }
    152     try
    153     {
    154         std::stof("a1", &idx);
    155         assert(false);
    156     }
    157     catch (const std::invalid_argument&)
    158     {
    159         assert(idx == 0);
    160     }
    161     try
    162     {
    163         std::stof(L"a1", &idx);
    164         assert(false);
    165     }
    166     catch (const std::invalid_argument&)
    167     {
    168         assert(idx == 0);
    169     }
    170 }
    171