Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <string> |
| 11 | |
| 12 | // wstring to_wstring(int val); |
| 13 | // wstring to_wstring(unsigned val); |
| 14 | // wstring to_wstring(long val); |
| 15 | // wstring to_wstring(unsigned long val); |
| 16 | // wstring to_wstring(long long val); |
| 17 | // wstring to_wstring(unsigned long long val); |
| 18 | // wstring to_wstring(float val); |
| 19 | // wstring to_wstring(double val); |
| 20 | // wstring to_wstring(long double val); |
| 21 | |
Marshall Clow | 5a127cd | 2018-12-18 23:19:00 +0000 | [diff] [blame] | 22 | #include <limits> |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <cassert> |
| 25 | #include <sstream> |
| 26 | |
| 27 | template <class T> |
| 28 | void |
| 29 | test_signed() |
| 30 | { |
| 31 | { |
| 32 | std::wstring s = std::to_wstring(T(0)); |
| 33 | assert(s.size() == 1); |
| 34 | assert(s[s.size()] == 0); |
| 35 | assert(s == L"0"); |
| 36 | } |
| 37 | { |
| 38 | std::wstring s = std::to_wstring(T(12345)); |
| 39 | assert(s.size() == 5); |
| 40 | assert(s[s.size()] == 0); |
| 41 | assert(s == L"12345"); |
| 42 | } |
| 43 | { |
| 44 | std::wstring s = std::to_wstring(T(-12345)); |
| 45 | assert(s.size() == 6); |
| 46 | assert(s[s.size()] == 0); |
| 47 | assert(s == L"-12345"); |
| 48 | } |
| 49 | { |
| 50 | std::wstring s = std::to_wstring(std::numeric_limits<T>::max()); |
| 51 | assert(s.size() == std::numeric_limits<T>::digits10 + 1); |
| 52 | std::wistringstream is(s); |
| 53 | T t(0); |
| 54 | is >> t; |
| 55 | assert(t == std::numeric_limits<T>::max()); |
| 56 | } |
| 57 | { |
| 58 | std::wstring s = std::to_wstring(std::numeric_limits<T>::min()); |
| 59 | std::wistringstream 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 |
| 68 | test_unsigned() |
| 69 | { |
| 70 | { |
| 71 | std::wstring s = std::to_wstring(T(0)); |
| 72 | assert(s.size() == 1); |
| 73 | assert(s[s.size()] == 0); |
| 74 | assert(s == L"0"); |
| 75 | } |
| 76 | { |
| 77 | std::wstring s = std::to_wstring(T(12345)); |
| 78 | assert(s.size() == 5); |
| 79 | assert(s[s.size()] == 0); |
| 80 | assert(s == L"12345"); |
| 81 | } |
| 82 | { |
| 83 | std::wstring s = std::to_wstring(std::numeric_limits<T>::max()); |
| 84 | assert(s.size() == std::numeric_limits<T>::digits10 + 1); |
| 85 | std::wistringstream 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 |
| 94 | test_float() |
| 95 | { |
| 96 | { |
| 97 | std::wstring s = std::to_wstring(T(0)); |
| 98 | assert(s.size() == 8); |
| 99 | assert(s[s.size()] == 0); |
| 100 | assert(s == L"0.000000"); |
| 101 | } |
| 102 | { |
| 103 | std::wstring s = std::to_wstring(T(12345)); |
| 104 | assert(s.size() == 12); |
| 105 | assert(s[s.size()] == 0); |
| 106 | assert(s == L"12345.000000"); |
| 107 | } |
| 108 | { |
| 109 | std::wstring s = std::to_wstring(T(-12345)); |
| 110 | assert(s.size() == 13); |
| 111 | assert(s[s.size()] == 0); |
| 112 | assert(s == L"-12345.000000"); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | int 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 | } |