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 // string to_string(int val);
     13 // string to_string(unsigned val);
     14 // string to_string(long val);
     15 // string to_string(unsigned long val);
     16 // string to_string(long long val);
     17 // string to_string(unsigned long long val);
     18 // string to_string(float val);
     19 // string to_string(double val);
     20 // string to_string(long double val);
     21 
     22 #include <string>
     23 #include <cassert>
     24 #include <sstream>
     25 
     26 template <class T>
     27 void
     28 test_signed()
     29 {
     30     {
     31         std::string s = std::to_string(T(0));
     32         assert(s.size() == 1);
     33         assert(s[s.size()] == 0);
     34         assert(s == "0");
     35     }
     36     {
     37         std::string s = std::to_string(T(12345));
     38         assert(s.size() == 5);
     39         assert(s[s.size()] == 0);
     40         assert(s == "12345");
     41     }
     42     {
     43         std::string s = std::to_string(T(-12345));
     44         assert(s.size() == 6);
     45         assert(s[s.size()] == 0);
     46         assert(s == "-12345");
     47     }
     48     {
     49         std::string s = std::to_string(std::numeric_limits<T>::max());
     50         assert(s.size() == std::numeric_limits<T>::digits10 + 1);
     51         std::istringstream is(s);
     52         T t(0);
     53         is >> t;
     54         assert(t == std::numeric_limits<T>::max());
     55     }
     56     {
     57         std::string s = std::to_string(std::numeric_limits<T>::min());
     58         std::istringstream is(s);
     59         T t(0);
     60         is >> t;
     61         assert(t == std::numeric_limits<T>::min());
     62     }
     63 }
     64 
     65 template <class T>
     66 void
     67 test_unsigned()
     68 {
     69     {
     70         std::string s = std::to_string(T(0));
     71         assert(s.size() == 1);
     72         assert(s[s.size()] == 0);
     73         assert(s == "0");
     74     }
     75     {
     76         std::string s = std::to_string(T(12345));
     77         assert(s.size() == 5);
     78         assert(s[s.size()] == 0);
     79         assert(s == "12345");
     80     }
     81     {
     82         std::string s = std::to_string(std::numeric_limits<T>::max());
     83         assert(s.size() == std::numeric_limits<T>::digits10 + 1);
     84         std::istringstream is(s);
     85         T t(0);
     86         is >> t;
     87         assert(t == std::numeric_limits<T>::max());
     88     }
     89 }
     90 
     91 template <class T>
     92 void
     93 test_float()
     94 {
     95     {
     96         std::string s = std::to_string(T(0));
     97         assert(s.size() == 8);
     98         assert(s[s.size()] == 0);
     99         assert(s == "0.000000");
    100     }
    101     {
    102         std::string s = std::to_string(T(12345));
    103         assert(s.size() == 12);
    104         assert(s[s.size()] == 0);
    105         assert(s == "12345.000000");
    106     }
    107     {
    108         std::string s = std::to_string(T(-12345));
    109         assert(s.size() == 13);
    110         assert(s[s.size()] == 0);
    111         assert(s == "-12345.000000");
    112     }
    113 }
    114 
    115 int main()
    116 {
    117     test_signed<int>();
    118     test_signed<long>();
    119     test_signed<long long>();
    120     test_unsigned<unsigned>();
    121     test_unsigned<unsigned long>();
    122     test_unsigned<unsigned long long>();
    123     test_float<float>();
    124     test_float<double>();
    125     test_float<long double>();
    126 }
    127