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