libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

stod.pass.cpp (3779B)


      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 // double stod(const string& str, size_t *idx = 0);
     13 // double stod(const wstring& str, size_t *idx = 0);
     14 
     15 #include <string>
     16 #include <cmath>
     17 #include <cassert>
     18 #include <stdexcept>
     19 
     20 #include "test_macros.h"
     21 
     22 int main()
     23 {
     24     assert(std::stod("0") == 0);
     25     assert(std::stod(L"0") == 0);
     26     assert(std::stod("-0") == 0);
     27     assert(std::stod(L"-0") == 0);
     28     assert(std::stod("-10") == -10);
     29     assert(std::stod(L"-10.5") == -10.5);
     30     assert(std::stod(" 10") == 10);
     31     assert(std::stod(L" 10") == 10);
     32     size_t idx = 0;
     33     assert(std::stod("10g", &idx) == 10);
     34     assert(idx == 2);
     35     idx = 0;
     36     assert(std::stod(L"10g", &idx) == 10);
     37     assert(idx == 2);
     38 #ifndef TEST_HAS_NO_EXCEPTIONS
     39     try
     40 #endif
     41     {
     42         assert(std::stod("1.e60", &idx) == 1.e60);
     43         assert(idx == 5);
     44     }
     45 #ifndef TEST_HAS_NO_EXCEPTIONS
     46     catch (const std::out_of_range&)
     47     {
     48         assert(false);
     49     }
     50     try
     51 #endif
     52     {
     53         assert(std::stod(L"1.e60", &idx) == 1.e60);
     54         assert(idx == 5);
     55     }
     56 #ifndef TEST_HAS_NO_EXCEPTIONS
     57     catch (const std::out_of_range&)
     58     {
     59         assert(false);
     60     }
     61     idx = 0;
     62     try
     63     {
     64         assert(std::stod("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::stod(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::stod("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::stod(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::stod("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::stod(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::stod("", &idx);
    137         assert(false);
    138     }
    139     catch (const std::invalid_argument&)
    140     {
    141         assert(idx == 0);
    142     }
    143     try
    144     {
    145         std::stod(L"", &idx);
    146         assert(false);
    147     }
    148     catch (const std::invalid_argument&)
    149     {
    150         assert(idx == 0);
    151     }
    152     try
    153     {
    154         std::stod("  - 8", &idx);
    155         assert(false);
    156     }
    157     catch (const std::invalid_argument&)
    158     {
    159         assert(idx == 0);
    160     }
    161     try
    162     {
    163         std::stod(L"  - 8", &idx);
    164         assert(false);
    165     }
    166     catch (const std::invalid_argument&)
    167     {
    168         assert(idx == 0);
    169     }
    170     try
    171     {
    172         std::stod("a1", &idx);
    173         assert(false);
    174     }
    175     catch (const std::invalid_argument&)
    176     {
    177         assert(idx == 0);
    178     }
    179     try
    180     {
    181         std::stod(L"a1", &idx);
    182         assert(false);
    183     }
    184     catch (const std::invalid_argument&)
    185     {
    186         assert(idx == 0);
    187     }
    188 #endif
    189 }