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