1 | //===----------------------------------------------------------------------===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // XFAIL: no-wide-characters |
10 | |
11 | // <string> |
12 | |
13 | // wstring to_wstring(int val); |
14 | // wstring to_wstring(unsigned val); |
15 | // wstring to_wstring(long val); |
16 | // wstring to_wstring(unsigned long val); |
17 | // wstring to_wstring(long long val); |
18 | // wstring to_wstring(unsigned long long val); |
19 | // wstring to_wstring(float val); |
20 | // wstring to_wstring(double val); |
21 | // wstring to_wstring(long double val); |
22 | |
23 | #include <string> |
24 | #include <cassert> |
25 | #include <limits> |
26 | |
27 | #include "parse_integer.h" |
28 | #include "test_macros.h" |
29 | |
30 | template <class T> |
31 | void test_signed() { |
32 | { |
33 | std::wstring s = std::to_wstring(T(0)); |
34 | assert(s.size() == 1); |
35 | assert(s[s.size()] == 0); |
36 | assert(s == L"0"); |
37 | } |
38 | { |
39 | std::wstring s = std::to_wstring(T(12345)); |
40 | assert(s.size() == 5); |
41 | assert(s[s.size()] == 0); |
42 | assert(s == L"12345"); |
43 | } |
44 | { |
45 | std::wstring s = std::to_wstring(T(-12345)); |
46 | assert(s.size() == 6); |
47 | assert(s[s.size()] == 0); |
48 | assert(s == L"-12345"); |
49 | } |
50 | { |
51 | std::wstring s = std::to_wstring(std::numeric_limits<T>::max()); |
52 | assert(s.size() == std::numeric_limits<T>::digits10 + 1); |
53 | T t = parse_integer<T>(s); |
54 | assert(t == std::numeric_limits<T>::max()); |
55 | } |
56 | { |
57 | std::wstring s = std::to_wstring(std::numeric_limits<T>::min()); |
58 | T t = parse_integer<T>(s); |
59 | assert(t == std::numeric_limits<T>::min()); |
60 | } |
61 | } |
62 | |
63 | template <class T> |
64 | void test_unsigned() { |
65 | { |
66 | std::wstring s = std::to_wstring(T(0)); |
67 | assert(s.size() == 1); |
68 | assert(s[s.size()] == 0); |
69 | assert(s == L"0"); |
70 | } |
71 | { |
72 | std::wstring s = std::to_wstring(T(12345)); |
73 | assert(s.size() == 5); |
74 | assert(s[s.size()] == 0); |
75 | assert(s == L"12345"); |
76 | } |
77 | { |
78 | std::wstring s = std::to_wstring(std::numeric_limits<T>::max()); |
79 | assert(s.size() == std::numeric_limits<T>::digits10 + 1); |
80 | T t = parse_integer<T>(s); |
81 | assert(t == std::numeric_limits<T>::max()); |
82 | } |
83 | } |
84 | |
85 | template <class T> |
86 | void test_float() { |
87 | { |
88 | std::wstring s = std::to_wstring(T(0)); |
89 | assert(s.size() == 8); |
90 | assert(s[s.size()] == 0); |
91 | assert(s == L"0.000000"); |
92 | } |
93 | { |
94 | std::wstring s = std::to_wstring(T(12345)); |
95 | assert(s.size() == 12); |
96 | assert(s[s.size()] == 0); |
97 | assert(s == L"12345.000000"); |
98 | } |
99 | { |
100 | std::wstring s = std::to_wstring(T(-12345)); |
101 | assert(s.size() == 13); |
102 | assert(s[s.size()] == 0); |
103 | assert(s == L"-12345.000000"); |
104 | } |
105 | } |
106 | |
107 | int main(int, char**) { |
108 | test_signed<int>(); |
109 | test_signed<long>(); |
110 | test_signed<long long>(); |
111 | test_unsigned<unsigned>(); |
112 | test_unsigned<unsigned long>(); |
113 | test_unsigned<unsigned long long>(); |
114 | test_float<float>(); |
115 | test_float<double>(); |
116 | test_float<long double>(); |
117 | |
118 | return 0; |
119 | } |
120 |