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