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 // 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 
22 #include <string>
23 #include <cassert>
24 #include <sstream>
25 
26 template <class T>
27 void
test_signed()28 test_signed()
29 {
30     {
31         std::wstring s = std::to_wstring(T(0));
32         assert(s.size() == 1);
33         assert(s[s.size()] == 0);
34         assert(s == L"0");
35     }
36     {
37         std::wstring s = std::to_wstring(T(12345));
38         assert(s.size() == 5);
39         assert(s[s.size()] == 0);
40         assert(s == L"12345");
41     }
42     {
43         std::wstring s = std::to_wstring(T(-12345));
44         assert(s.size() == 6);
45         assert(s[s.size()] == 0);
46         assert(s == L"-12345");
47     }
48     {
49         std::wstring s = std::to_wstring(std::numeric_limits<T>::max());
50         assert(s.size() == std::numeric_limits<T>::digits10 + 1);
51         std::wistringstream is(s);
52         T t(0);
53         is >> t;
54         assert(t == std::numeric_limits<T>::max());
55     }
56     {
57         std::wstring s = std::to_wstring(std::numeric_limits<T>::min());
58         std::wistringstream 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
test_unsigned()67 test_unsigned()
68 {
69     {
70         std::wstring s = std::to_wstring(T(0));
71         assert(s.size() == 1);
72         assert(s[s.size()] == 0);
73         assert(s == L"0");
74     }
75     {
76         std::wstring s = std::to_wstring(T(12345));
77         assert(s.size() == 5);
78         assert(s[s.size()] == 0);
79         assert(s == L"12345");
80     }
81     {
82         std::wstring s = std::to_wstring(std::numeric_limits<T>::max());
83         assert(s.size() == std::numeric_limits<T>::digits10 + 1);
84         std::wistringstream 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
test_float()93 test_float()
94 {
95     {
96         std::wstring s = std::to_wstring(T(0));
97         assert(s.size() == 8);
98         assert(s[s.size()] == 0);
99         assert(s == L"0.000000");
100     }
101     {
102         std::wstring s = std::to_wstring(T(12345));
103         assert(s.size() == 12);
104         assert(s[s.size()] == 0);
105         assert(s == L"12345.000000");
106     }
107     {
108         std::wstring s = std::to_wstring(T(-12345));
109         assert(s.size() == 13);
110         assert(s[s.size()] == 0);
111         assert(s == L"-12345.000000");
112     }
113 }
114 
main()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