• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <limits.h>
2 #include <stddef.h>
3 #include <wchar.h>
4 
5 #include <minitest/minitest.h>
6 
7 namespace {
8 
to_cstr(const wchar_t * wcs)9 const char* to_cstr(const wchar_t* wcs) {
10     static char buffer[256];
11     size_t n;
12     for (n = 0; n + 1U < sizeof(buffer); ++n) {
13         wchar_t ch = wcs[n];
14         if (!ch)
15             break;
16         buffer[n] = (ch < 128) ? ch : '@';
17     }
18     buffer[n] = '\0';
19     return buffer;
20 }
21 
22 #define ARRAY_SIZE(x)  (sizeof(x) / sizeof(x[0]))
23 
24 }
25 
TEST(wchar,wchar_limits)26 TEST(wchar, wchar_limits) {
27   ASSERT_EQ(sizeof(__WCHAR_TYPE__), sizeof(wchar_t));
28   ASSERT_EQ(sizeof(int), sizeof(wint_t));
29 #ifdef __arm__
30   ASSERT_GT(wchar_t(0), wchar_t(-1));
31   ASSERT_EQ(wchar_t(0), WCHAR_MIN);
32   ASSERT_EQ(wchar_t(0xffffffff), WCHAR_MAX);
33 #else
34   ASSERT_LT(wchar_t(0), wchar_t(-1));
35   ASSERT_EQ(wchar_t(0x80000000), WCHAR_MIN);
36   ASSERT_EQ(wchar_t(0x7fffffff), WCHAR_MAX);
37 #endif
38 }
39 
TEST(wchar,wcschr)40 TEST(wchar, wcschr) {
41   static const wchar_t kString[] = L"abcda";
42   EXPECT_EQ(kString + 0, wcschr(kString, L'a'));
43   EXPECT_EQ(kString + 1, wcschr(kString, L'b'));
44   EXPECT_EQ(kString + 2, wcschr(kString, L'c'));
45   EXPECT_EQ(kString + 3, wcschr(kString, L'd'));
46   EXPECT_FALSE(wcschr(kString, L'e'));
47   EXPECT_EQ(kString + 5, wcschr(kString, L'\0'));
48 }
49 
TEST(wchar,wcsrchr)50 TEST(wchar, wcsrchr) {
51   static const wchar_t kString[] = L"abcda";
52   EXPECT_EQ(kString + 4, wcsrchr(kString, L'a'));
53   EXPECT_EQ(kString + 1, wcsrchr(kString, L'b'));
54   EXPECT_EQ(kString + 2, wcsrchr(kString, L'c'));
55   EXPECT_EQ(kString + 3, wcsrchr(kString, L'd'));
56   EXPECT_FALSE(wcsrchr(kString, L'e'));
57   EXPECT_EQ(kString + 5, wcsrchr(kString, L'\0'));
58 }
59 
TEST(wchar,wcstof)60 TEST(wchar, wcstof) {
61     static const struct {
62         const wchar_t* input;
63         float expected;
64         int expected_len;
65     } kData[] = {
66         { L"123", 123., 3 },
67         { L"123#", 123., 3 },
68         { L"   123 45", 123., 6 },
69         { L"0.2", 0.2, 3 },
70         { L"-0.2", -0.2, 4 },
71         { L"-3.1415926535", -3.1415926535, 13 },
72         { L"+1e+100", 1e100, 7 },
73         { L"0x10000.80", 65536.50, 10 },
74     };
75     for (size_t n = 0; n < ARRAY_SIZE(kData); ++n) {
76         const char* text = to_cstr(kData[n].input);
77         wchar_t* end;
78         TEST_TEXT << text;
79         EXPECT_EQ(kData[n].expected, wcstof(kData[n].input, &end));
80         TEST_TEXT << text;
81         EXPECT_EQ(kData[n].expected_len, (int)(end - kData[n].input));
82     }
83 }
84 
TEST(wchar,wcstod)85 TEST(wchar, wcstod) {
86     static const struct {
87         const wchar_t* input;
88         double expected;
89         int expected_len;
90     } kData[] = {
91         { L"123", 123., 3 },
92         { L"123#", 123., 3 },
93         { L"   123 45", 123., 6 },
94         { L"0.2", 0.2, 3 },
95         { L"-0.2", -0.2, 4 },
96         { L"-3.1415926535", -3.1415926535, 13 },
97         { L"+1e+100", 1e100, 7 },
98         { L"0x10000.80", 65536.50, 10 },
99         { L"1.e60", 1e60, 5 },
100     };
101     for (size_t n = 0; n < ARRAY_SIZE(kData); ++n) {
102         const char* text = to_cstr(kData[n].input);
103         wchar_t* end;
104         TEST_TEXT << text;
105         EXPECT_EQ(kData[n].expected, wcstod(kData[n].input, &end));
106         TEST_TEXT << text;
107         EXPECT_EQ(kData[n].expected_len, (int)(end - kData[n].input));
108     }
109 }
110 
TEST(wchar,wcstold)111 TEST(wchar, wcstold) {
112     static const struct {
113         const wchar_t* input;
114         long double expected;
115         int expected_len;
116     } kData[] = {
117         { L"123", 123., 3 },
118         { L"123#", 123., 3 },
119         { L"   123 45", 123., 6 },
120         { L"0.2", 0.2L, 3 },
121         { L"-0.2", -0.2L, 4 },
122         { L"-3.1415926535", -3.1415926535L, 13 },
123         { L"+1e+100", 1e100L, 7 },
124         { L"0x10000.80", 65536.50L, 10 },
125         { L"+1.e+100", 1e100L, 8 },
126     };
127     for (size_t n = 0; n < ARRAY_SIZE(kData); ++n) {
128         const char* text = to_cstr(kData[n].input);
129         wchar_t* end;
130         TEST_TEXT << text;
131         EXPECT_EQ(kData[n].expected, wcstold(kData[n].input, &end));
132         TEST_TEXT << text;
133         EXPECT_EQ(kData[n].expected_len, (int)(end - kData[n].input));
134     }
135 }
136 
TEST(wchar,wcstol)137 TEST(wchar, wcstol) {
138     static const struct {
139         const wchar_t* input;
140         int base;
141         long expected;
142         int expected_len;
143     } kData[] = {
144         { L"123", 10, 123, 3 },
145         { L"123#", 10, 123, 3 },
146         { L"01000", 0, 512, 5 },
147         { L"   123 45", 0, 123, 6 },
148         { L"  -123", 0, -123, 6 },
149         { L"0x10000", 0, 65536, 7 },
150         { L"12222222222222222222222222222222222", 10, LONG_MAX, 35 },
151         { L"-12222222222222222222222222222222222", 10, LONG_MIN, 36 },
152     };
153     for (size_t n = 0; n < ARRAY_SIZE(kData); ++n) {
154         const char* text = to_cstr(kData[n].input);
155         wchar_t* end;
156         TEST_TEXT << text;
157         EXPECT_EQ(kData[n].expected, wcstol(kData[n].input, &end, kData[n].base));
158         TEST_TEXT << text;
159         EXPECT_EQ(kData[n].expected_len, (int)(end - kData[n].input));
160     }
161 }
162 
TEST(wchar,wcstoul)163 TEST(wchar, wcstoul) {
164     static const struct {
165         const wchar_t* input;
166         int base;
167         unsigned long expected;
168         int expected_len;
169     } kData[] = {
170         { L"123", 10, 123, 3 },
171         { L"123#", 10, 123, 3 },
172         { L"01000", 0, 512, 5 },
173         { L"   123 45", 0, 123, 6 },
174         { L"  -123", 0, ULONG_MAX - 123 + 1, 6 },
175         { L"0x10000", 0, 65536, 7 },
176         { L"12222222222222222222222222222222222", 10, ULONG_MAX, 35 },
177         { L"-1", 10, ULONG_MAX, 2 },
178     };
179     for (size_t n = 0; n < ARRAY_SIZE(kData); ++n) {
180         const char* text = to_cstr(kData[n].input);
181         wchar_t* end;
182         TEST_TEXT << text;
183         EXPECT_EQ(kData[n].expected, wcstoul(kData[n].input, &end, kData[n].base));
184         TEST_TEXT << text;
185         EXPECT_EQ(kData[n].expected_len, (int)(end - kData[n].input));
186     }
187 }
188 
TEST(wchar,wcstoll)189 TEST(wchar, wcstoll) {
190     static const struct {
191         const wchar_t* input;
192         int base;
193         long long expected;
194         int expected_len;
195     } kData[] = {
196         { L"123", 10, 123, 3 },
197         { L"123#", 10, 123, 3 },
198         { L"01000", 0, 512, 5 },
199         { L"   123 45", 0, 123, 6 },
200         { L"  -123", 0, -123, 6 },
201         { L"0x10000", 0, 65536, 7 },
202         { L"12222222222222222222222222222222222", 10, LLONG_MAX, 35 },
203         { L"-12222222222222222222222222222222222", 10, LLONG_MIN, 36 },
204     };
205     for (size_t n = 0; n < ARRAY_SIZE(kData); ++n) {
206         const char* text = to_cstr(kData[n].input);
207         wchar_t* end;
208         TEST_TEXT << text;
209         EXPECT_EQ(kData[n].expected, wcstoll(kData[n].input, &end, kData[n].base));
210         TEST_TEXT << text;
211         EXPECT_EQ(kData[n].expected_len, (int)(end - kData[n].input));
212     }
213 }
214 
TEST(wchar,wcstoull)215 TEST(wchar, wcstoull) {
216     static const struct {
217         const wchar_t* input;
218         int base;
219         unsigned long long expected;
220         int expected_len;
221     } kData[] = {
222         { L"123", 10, 123, 3 },
223         { L"123#", 10, 123, 3 },
224         { L"01000", 0, 512, 5 },
225         { L"   123 45", 0, 123, 6 },
226         { L"  -123", 0, ULLONG_MAX - 123 + 1, 6 },
227         { L"0x10000", 0, 65536, 7 },
228         { L"12222222222222222222222222222222222", 10, ULLONG_MAX, 35 },
229         { L"-1", 10, ULLONG_MAX, 2 },
230     };
231     for (size_t n = 0; n < ARRAY_SIZE(kData); ++n) {
232         const char* text = to_cstr(kData[n].input);
233         wchar_t* end;
234         TEST_TEXT << text;
235         EXPECT_EQ(kData[n].expected, wcstoull(kData[n].input, &end, kData[n].base));
236         TEST_TEXT << text;
237         EXPECT_EQ(kData[n].expected_len, (int)(end - kData[n].input));
238     }
239 }
240