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