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