]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/winnmtst.cpp
ICU-59117.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / winnmtst.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
73c04bcf
A
3/*
4********************************************************************************
2ca993e8 5* Copyright (C) 2005-2016, International Business Machines
73c04bcf
A
6* Corporation and others. All Rights Reserved.
7********************************************************************************
8*
9* File WINNMTST.CPP
10*
11********************************************************************************
12*/
13
14#include "unicode/utypes.h"
15
4388f060 16#if U_PLATFORM_USES_ONLY_WIN32_API
73c04bcf
A
17
18#if !UCONFIG_NO_FORMATTING
19
20#include "unicode/format.h"
21#include "unicode/numfmt.h"
22#include "unicode/locid.h"
23#include "unicode/ustring.h"
24#include "unicode/testlog.h"
25#include "unicode/utmscale.h"
26
27#include "winnmfmt.h"
28#include "winutil.h"
29#include "winnmtst.h"
30
f3c0d7a5
A
31#include "numfmtst.h"
32
73c04bcf
A
33#include "cmemory.h"
34#include "cstring.h"
35#include "locmap.h"
36#include "wintz.h"
37#include "uassert.h"
38
39# define WIN32_LEAN_AND_MEAN
40# define VC_EXTRALEAN
41# define NOUSER
42# define NOSERVICE
43# define NOIME
44# define NOMCX
45# include <windows.h>
46# include <stdio.h>
47# include <time.h>
48# include <float.h>
49# include <locale.h>
50
73c04bcf
A
51#define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type))
52#define DELETE_ARRAY(array) uprv_free((void *) (array))
53
54#define STACK_BUFFER_SIZE 32
55
56#define LOOP_COUNT 1000
57
58static UBool initialized = FALSE;
59
60/**
61 * Return a random int64_t where U_INT64_MIN <= ran <= U_INT64_MAX.
62 */
63static uint64_t randomInt64(void)
64{
65 int64_t ran = 0;
66 int32_t i;
67
68 if (!initialized) {
69 srand((unsigned)time(NULL));
70 initialized = TRUE;
71 }
72
73 /* Assume rand has at least 12 bits of precision */
74 for (i = 0; i < sizeof(ran); i += 1) {
75 ((char*)&ran)[i] = (char)((rand() & 0x0FF0) >> 4);
76 }
77
78 return ran;
79}
80
81/**
82 * Return a random double where U_DOUBLE_MIN <= ran <= U_DOUBLE_MAX.
83 */
84static double randomDouble(void)
85{
86 double ran = 0;
87
88 if (!initialized) {
89 srand((unsigned)time(NULL));
90 initialized = TRUE;
91 }
92#if 0
93 int32_t i;
94 do {
95 /* Assume rand has at least 12 bits of precision */
96 for (i = 0; i < sizeof(ran); i += 1) {
97 ((char*)&ran)[i] = (char)((rand() & 0x0FF0) >> 4);
98 }
99 } while (_isnan(ran));
100#else
101 int64_t numerator = randomInt64();
102 int64_t denomenator;
103 do {
104 denomenator = randomInt64();
105 }
106 while (denomenator == 0);
107
108 ran = (double)numerator / (double)denomenator;
109#endif
110
111 return ran;
112}
113
114/**
115 * Return a random int32_t where U_INT32_MIN <= ran <= U_INT32_MAX.
116 */
117static uint32_t randomInt32(void)
118{
119 int32_t ran = 0;
120 int32_t i;
121
122 if (!initialized) {
123 srand((unsigned)time(NULL));
124 initialized = TRUE;
125 }
126
127 /* Assume rand has at least 12 bits of precision */
128 for (i = 0; i < sizeof(ran); i += 1) {
129 ((char*)&ran)[i] = (char)((rand() & 0x0FF0) >> 4);
130 }
131
132 return ran;
133}
134
135static UnicodeString &getWindowsFormat(int32_t lcid, UBool currency, UnicodeString &appendTo, const wchar_t *fmt, ...)
136{
137 wchar_t nStackBuffer[STACK_BUFFER_SIZE];
138 wchar_t *nBuffer = nStackBuffer;
139 va_list args;
140 int result;
141
142 nBuffer[0] = 0x0000;
143
144 /* Due to the arguments causing a result to be <= 23 characters (+2 for NULL and minus),
145 we don't need to reallocate the buffer. */
146 va_start(args, fmt);
147 result = _vsnwprintf(nBuffer, STACK_BUFFER_SIZE, fmt, args);
148 va_end(args);
149
150 /* Just to make sure of the above statement, we add this assert */
151 U_ASSERT(result >=0);
152 // The following code is not used because _vscwprintf isn't available on MinGW at the moment.
153 /*if (result < 0) {
154 int newLength;
155
156 va_start(args, fmt);
157 newLength = _vscwprintf(fmt, args);
158 va_end(args);
159
160 nBuffer = NEW_ARRAY(UChar, newLength + 1);
161
162 va_start(args, fmt);
163 result = _vsnwprintf(nBuffer, newLength + 1, fmt, args);
164 va_end(args);
165 }*/
166
167
168 // vswprintf is sensitive to the locale set by setlocale. For some locales
169 // it doesn't use "." as the decimal separator, which is what GetNumberFormatW
170 // and GetCurrencyFormatW both expect to see.
171 //
172 // To fix this, we scan over the string and replace the first non-digits, except
173 // for a leading "-", with a "."
174 //
175 // Note: (nBuffer[0] == L'-') will evaluate to 1 if there is a leading '-' in the
176 // number, and 0 otherwise.
177 for (wchar_t *p = &nBuffer[nBuffer[0] == L'-']; *p != L'\0'; p += 1) {
178 if (*p < L'0' || *p > L'9') {
179 *p = L'.';
180 break;
181 }
182 }
183
184 wchar_t stackBuffer[STACK_BUFFER_SIZE];
185 wchar_t *buffer = stackBuffer;
186
187 buffer[0] = 0x0000;
188
189 if (currency) {
190 result = GetCurrencyFormatW(lcid, 0, nBuffer, NULL, buffer, STACK_BUFFER_SIZE);
191
192 if (result == 0) {
193 DWORD lastError = GetLastError();
194
195 if (lastError == ERROR_INSUFFICIENT_BUFFER) {
196 int newLength = GetCurrencyFormatW(lcid, 0, nBuffer, NULL, NULL, 0);
197
f3c0d7a5 198 buffer = NEW_ARRAY(wchar_t, newLength);
73c04bcf
A
199 buffer[0] = 0x0000;
200 GetCurrencyFormatW(lcid, 0, nBuffer, NULL, buffer, newLength);
201 }
202 }
203 } else {
204 result = GetNumberFormatW(lcid, 0, nBuffer, NULL, buffer, STACK_BUFFER_SIZE);
205
206 if (result == 0) {
207 DWORD lastError = GetLastError();
208
209 if (lastError == ERROR_INSUFFICIENT_BUFFER) {
210 int newLength = GetNumberFormatW(lcid, 0, nBuffer, NULL, NULL, 0);
211
f3c0d7a5 212 buffer = NEW_ARRAY(wchar_t, newLength);
73c04bcf
A
213 buffer[0] = 0x0000;
214 GetNumberFormatW(lcid, 0, nBuffer, NULL, buffer, newLength);
215 }
216 }
217 }
218
f3c0d7a5 219 appendTo.append((const UChar *)buffer, (int32_t) wcslen(buffer));
73c04bcf
A
220
221 if (buffer != stackBuffer) {
222 DELETE_ARRAY(buffer);
223 }
224
225 /*if (nBuffer != nStackBuffer) {
226 DELETE_ARRAY(nBuffer);
227 }*/
228
229 return appendTo;
230}
231
232static void testLocale(const char *localeID, int32_t lcid, NumberFormat *wnf, UBool currency, TestLog *log)
233{
234 for (int n = 0; n < LOOP_COUNT; n += 1) {
235 UnicodeString u3Buffer, u6Buffer, udBuffer;
236 UnicodeString w3Buffer, w6Buffer, wdBuffer;
237 double d = randomDouble();
238 int32_t i32 = randomInt32();
239 int64_t i64 = randomInt64();
240
241 getWindowsFormat(lcid, currency, wdBuffer, L"%.16f", d);
242
243 getWindowsFormat(lcid, currency, w3Buffer, L"%I32d", i32);
244
245 getWindowsFormat(lcid, currency, w6Buffer, L"%I64d", i64);
246
247 wnf->format(d, udBuffer);
248 if (udBuffer.compare(wdBuffer) != 0) {
249 UnicodeString locale(localeID);
250
251 log->errln("Double format error for locale " + locale +
729e4ab9 252 ": got " + udBuffer + " expected " + wdBuffer);
73c04bcf
A
253 }
254
255 wnf->format(i32, u3Buffer);
256 if (u3Buffer.compare(w3Buffer) != 0) {
257 UnicodeString locale(localeID);
258
259 log->errln("int32_t format error for locale " + locale +
729e4ab9 260 ": got " + u3Buffer + " expected " + w3Buffer);
73c04bcf
A
261 }
262
263 wnf->format(i64, u6Buffer);
264 if (u6Buffer.compare(w6Buffer) != 0) {
265 UnicodeString locale(localeID);
266
267 log->errln("int64_t format error for locale " + locale +
729e4ab9 268 ": got " + u6Buffer + " expected " + w6Buffer);
73c04bcf
A
269 }
270 }
271}
272
f3c0d7a5 273void Win32NumberTest::testLocales(NumberFormatTest *log)
73c04bcf
A
274{
275 int32_t lcidCount = 0;
276 Win32Utilities::LCIDRecord *lcidRecords = Win32Utilities::getLocales(lcidCount);
277
278 for(int i = 0; i < lcidCount; i += 1) {
279 UErrorCode status = U_ZERO_ERROR;
280 char localeID[128];
281
729e4ab9
A
282 // NULL localeID means ICU didn't recognize the lcid
283 if (lcidRecords[i].localeID == NULL) {
284 continue;
285 }
73c04bcf 286
f3c0d7a5
A
287 // Some locales have had their names change over various OS releases; skip them in the test for now.
288 int32_t failingLocaleLCIDs[] = {
289 0x040a, /* es-ES_tradnl;es-ES-u-co-trad; */
290 0x048c, /* fa-AF;prs-AF;prs-Arab-AF; */
291 0x046b, /* qu-BO;quz-BO;quz-Latn-BO; */
292 0x086b, /* qu-EC;quz-EC;quz-Latn-EC; */
293 0x0c6b, /* qu-PE;quz-PE;quz-Latn-PE; */
294 0x0492 /* ckb-IQ;ku-Arab-IQ; */
295 };
296 bool skip = (std::find(std::begin(failingLocaleLCIDs), std::end(failingLocaleLCIDs), lcidRecords[i].lcid) != std::end(failingLocaleLCIDs));
297 if (skip && log->logKnownIssue("13119", "Windows '@compat=host' fails on down-level versions of the OS")) {
298 log->logln("ticket:13119 - Skipping LCID = 0x%04x", lcidRecords[i].lcid);
299 continue;
300 }
301
73c04bcf
A
302 strcpy(localeID, lcidRecords[i].localeID);
303
304 if (strchr(localeID, '@') > 0) {
305 strcat(localeID, ";");
306 } else {
307 strcat(localeID, "@");
308 }
309
310 strcat(localeID, "compat=host");
311
312 Locale ulocale(localeID);
313 NumberFormat *wnf = NumberFormat::createInstance(ulocale, status);
314 NumberFormat *wcf = NumberFormat::createCurrencyInstance(ulocale, status);
315
316 testLocale(lcidRecords[i].localeID, lcidRecords[i].lcid, wnf, FALSE, log);
317 testLocale(lcidRecords[i].localeID, lcidRecords[i].lcid, wcf, TRUE, log);
318
319#if 0
320 char *old_locale = strdup(setlocale(LC_ALL, NULL));
321
322 setlocale(LC_ALL, "German");
323
324 testLocale(lcidRecords[i].localeID, lcidRecords[i].lcid, wnf, FALSE, log);
325 testLocale(lcidRecords[i].localeID, lcidRecords[i].lcid, wcf, TRUE, log);
326
327 setlocale(LC_ALL, old_locale);
328
329 free(old_locale);
330#endif
331
332 delete wcf;
333 delete wnf;
334 }
335
336 Win32Utilities::freeLocales(lcidRecords);
337}
338
339#endif /* #if !UCONFIG_NO_FORMATTING */
340
4388f060 341#endif /* U_PLATFORM_USES_ONLY_WIN32_API */