ICU-4152 Initial checkin of tests. Aslo bug fixes for bugs that the tests found!

X-SVN-Rev: 19017
This commit is contained in:
Eric Mader 2006-01-23 23:45:12 +00:00
parent ed5ad9b463
commit bfd3e8b331
6 changed files with 650 additions and 0 deletions

View File

@ -0,0 +1,173 @@
/*
********************************************************************************
* Copyright (C) 2005-2006, International Business Machines
* Corporation and others. All Rights Reserved.
********************************************************************************
*
* File WINDTTST.CPP
*
********************************************************************************
*/
#include "unicode/utypes.h"
#ifdef U_WINDOWS
#if !UCONFIG_NO_FORMATTING
#include "unicode/format.h"
#include "unicode/numfmt.h"
#include "unicode/locid.h"
#include "unicode/ustring.h"
#include "unicode/testlog.h"
#include "unicode/utmscale.h"
#include "windtfmt.h"
#include "winutil.h"
#include "windttst.h"
#include "cmemory.h"
#include "cstring.h"
#include "locmap.h"
#include "wintz.h"
# define WIN32_LEAN_AND_MEAN
# define VC_EXTRALEAN
# define NOUSER
# define NOSERVICE
# define NOIME
# define NOMCX
# include <windows.h>
# include <stdio.h>
#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
static const char *getCalendarType(int32_t type)
{
switch (type)
{
case 1:
case 2:
return "@calendar=gregorian";
case 3:
return "@calendar=japanese";
case 6:
return "@calendar=islamic";
case 7:
return "@calendar=buddhist";
case 8:
return "@calendar=hebrew";
default:
return "";
}
}
void Win32DateTimeTest::testLocales(TestLog *log)
{
SYSTEMTIME winNow;
UDate icuNow = 0;
SYSTEMTIME st;
FILETIME ft;
UnicodeString zoneID;
const TimeZone *tz = TimeZone::createDefault();
TIME_ZONE_INFORMATION tzi;
tz->getID(zoneID);
uprv_getWindowsTimeZoneInfo(&tzi, zoneID.getBuffer(), zoneID.length());
GetSystemTime(&st);
SystemTimeToFileTime(&st, &ft);
SystemTimeToTzSpecificLocalTime(&tzi, &st, &winNow);
int64_t wftNow = ((int64_t) ft.dwHighDateTime << 32) + ft.dwLowDateTime;
UErrorCode status = U_ZERO_ERROR;
int64_t udtsNow = utmscale_fromInt64(wftNow, UDTS_WINDOWS_FILE_TIME, &status);
icuNow = (UDate) utmscale_toInt64(udtsNow, UDTS_ICU4C_TIME, &status);
int32_t lcidCount = 0;
Win32Utilities::LCIDRecord *lcidRecords = Win32Utilities::getLocales(lcidCount);
for(int i = 0; i < lcidCount; i += 1) {
UErrorCode status = U_ZERO_ERROR;
WCHAR longDateFormat[81], longTimeFormat[81], wdBuffer[256], wtBuffer[256];
int32_t calType = 0;
GetLocaleInfoW(lcidRecords[i].lcid, LOCALE_SLONGDATE, longDateFormat, 81);
GetLocaleInfoW(lcidRecords[i].lcid, LOCALE_STIMEFORMAT, longTimeFormat, 81);
GetLocaleInfoW(lcidRecords[i].lcid, LOCALE_RETURN_NUMBER|LOCALE_ICALENDARTYPE, (LPWSTR) calType, sizeof(int32_t));
char localeID[64];
uprv_strcpy(localeID, lcidRecords[i].localeID);
uprv_strcat(localeID, getCalendarType(calType));
UnicodeString ubBuffer, udBuffer, utBuffer;
Locale ulocale(localeID);
int32_t wdLength, wtLength;
wdLength = GetDateFormatW(lcidRecords[i].lcid, DATE_LONGDATE, &winNow, NULL, wdBuffer, ARRAY_SIZE(wdBuffer));
wtLength = GetTimeFormatW(lcidRecords[i].lcid, 0, &winNow, NULL, wtBuffer, ARRAY_SIZE(wtBuffer));
if (uprv_strchr(localeID, '@') > 0) {
uprv_strcat(localeID, ";");
} else {
uprv_strcat(localeID, "@");
}
uprv_strcat(localeID, "compat=host");
Locale wlocale(localeID);
DateFormat *wbf = DateFormat::createDateTimeInstance(DateFormat::kFull, DateFormat::kFull, wlocale);
DateFormat *wdf = DateFormat::createDateInstance(DateFormat::kFull, wlocale);
DateFormat *wtf = DateFormat::createTimeInstance(DateFormat::kFull, wlocale);
wbf->format(icuNow, ubBuffer);
wdf->format(icuNow, udBuffer);
wtf->format(icuNow, utBuffer);
if (ubBuffer.indexOf(wdBuffer, wdLength - 1, 0) < 0) {
UnicodeString baseName(wlocale.getBaseName());
UnicodeString expected(wdBuffer);
log->errln("DateTime format error for locale " + baseName + ": expected date \"" + expected +
"\" got \"" + ubBuffer + "\"");
}
if (ubBuffer.indexOf(wtBuffer, wtLength - 1, 0) < 0) {
UnicodeString baseName(wlocale.getBaseName());
UnicodeString expected(wtBuffer);
log->errln("DateTime format error for locale " + baseName + ": expected time \"" + expected +
"\" got \"" + ubBuffer + "\"");
}
if (udBuffer.compare(wdBuffer) != 0) {
UnicodeString baseName(wlocale.getBaseName());
UnicodeString expected(wdBuffer);
log->errln("Date format error for locale " + baseName + ": expected \"" + expected +
"\" got \"" + udBuffer + "\"");
}
if (utBuffer.compare(wtBuffer) != 0) {
UnicodeString baseName(wlocale.getBaseName());
UnicodeString expected(wtBuffer);
log->errln("Time format error for locale " + baseName + ": expected \"" + expected +
"\" got \"" + utBuffer + "\"");
}
}
Win32Utilities::freeLocales(lcidRecords);
}
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif /* #ifdef U_WINDOWS */

View File

@ -0,0 +1,41 @@
/*
********************************************************************************
* Copyright (C) 2005-2006, International Business Machines
* Corporation and others. All Rights Reserved.
********************************************************************************
*
* File WINDTTST.H
*
********************************************************************************
*/
#ifndef __WINDTTST
#define __WINDTTST
#include "unicode/utypes.h"
#ifdef U_WINDOWS
#if !UCONFIG_NO_FORMATTING
/**
* \file
* \brief C++ API: Format dates using Windows API.
*/
class TestLog;
class Win32DateTimeTest
{
public:
static void testLocales(TestLog *log);
private:
Win32DateTimeTest();
};
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif // #ifdef U_WINDOWS
#endif // __WINDTTST

View File

@ -0,0 +1,268 @@
/*
********************************************************************************
* Copyright (C) 2005-2006, International Business Machines
* Corporation and others. All Rights Reserved.
********************************************************************************
*
* File WINNMTST.CPP
*
********************************************************************************
*/
#include "unicode/utypes.h"
#ifdef U_WINDOWS
#if !UCONFIG_NO_FORMATTING
#include "unicode/format.h"
#include "unicode/numfmt.h"
#include "unicode/locid.h"
#include "unicode/ustring.h"
#include "unicode/testlog.h"
#include "unicode/utmscale.h"
#include "winnmfmt.h"
#include "winutil.h"
#include "winnmtst.h"
#include "cmemory.h"
#include "cstring.h"
#include "locmap.h"
#include "wintz.h"
# define WIN32_LEAN_AND_MEAN
# define VC_EXTRALEAN
# define NOUSER
# define NOSERVICE
# define NOIME
# define NOMCX
# include <windows.h>
# include <stdio.h>
# include <time.h>
#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
#define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type))
#define DELETE_ARRAY(array) uprv_free((void *) (array))
#define STACK_BUFFER_SIZE 32
#define LOOP_COUNT 1000
static UBool initialized = FALSE;
/**
* Return a random int64_t where U_INT64_MIN <= ran <= U_INT64_MAX.
*/
static uint64_t randomInt64(void)
{
int64_t ran = 0;
int32_t i;
if (!initialized) {
srand((unsigned)time(NULL));
initialized = TRUE;
}
/* Assume rand has at least 12 bits of precision */
for (i = 0; i < sizeof(ran); i += 1) {
((char*)&ran)[i] = (char)((rand() & 0x0FF0) >> 4);
}
return ran;
}
/**
* Return a random double where U_DOUBLE_MIN <= ran <= U_DOUBLE_MAX.
*/
static double randomDouble(void)
{
double ran = 0;
int32_t i;
if (!initialized) {
srand((unsigned)time(NULL));
initialized = TRUE;
}
/* Assume rand has at least 12 bits of precision */
for (i = 0; i < sizeof(ran); i += 1) {
((char*)&ran)[i] = (char)((rand() & 0x0FF0) >> 4);
}
return ran;
}
/**
* Return a random int32_t where U_INT32_MIN <= ran <= U_INT32_MAX.
*/
static uint32_t randomInt32(void)
{
int32_t ran = 0;
int32_t i;
if (!initialized) {
srand((unsigned)time(NULL));
initialized = TRUE;
}
/* Assume rand has at least 12 bits of precision */
for (i = 0; i < sizeof(ran); i += 1) {
((char*)&ran)[i] = (char)((rand() & 0x0FF0) >> 4);
}
return ran;
}
static UnicodeString &getWindowsFormat(int32_t lcid, UBool currency, UnicodeString &appendTo, const wchar_t *fmt, ...)
{
wchar_t nStackBuffer[STACK_BUFFER_SIZE];
wchar_t *nBuffer = nStackBuffer;
va_list args;
int result;
nBuffer[0] = 0x0000;
va_start(args, fmt);
result = vswprintf(nBuffer, STACK_BUFFER_SIZE, fmt, args);
va_end(args);
if (result < 0) {
int newLength;
va_start(args, fmt);
newLength = _vscwprintf(fmt, args);
va_end(args);
nBuffer = NEW_ARRAY(UChar, newLength + 1);
va_start(args, fmt);
result = vswprintf(nBuffer, newLength + 1, fmt, args);
va_end(args);
}
wchar_t stackBuffer[STACK_BUFFER_SIZE];
wchar_t *buffer = stackBuffer;
buffer[0] = 0x0000;
if (currency) {
result = GetCurrencyFormatW(lcid, 0, nBuffer, NULL, buffer, STACK_BUFFER_SIZE);
if (result == 0) {
DWORD lastError = GetLastError();
if (lastError == ERROR_INSUFFICIENT_BUFFER) {
int newLength = GetCurrencyFormatW(lcid, 0, nBuffer, NULL, NULL, 0);
buffer = NEW_ARRAY(UChar, newLength);
buffer[0] = 0x0000;
GetCurrencyFormatW(lcid, 0, nBuffer, NULL, buffer, newLength);
}
}
} else {
result = GetNumberFormatW(lcid, 0, nBuffer, NULL, buffer, STACK_BUFFER_SIZE);
if (result == 0) {
DWORD lastError = GetLastError();
if (lastError == ERROR_INSUFFICIENT_BUFFER) {
int newLength = GetNumberFormatW(lcid, 0, nBuffer, NULL, NULL, 0);
buffer = NEW_ARRAY(UChar, newLength);
buffer[0] = 0x0000;
GetNumberFormatW(lcid, 0, nBuffer, NULL, buffer, newLength);
}
}
}
appendTo.append(buffer, (int32_t) wcslen(buffer));
if (buffer != stackBuffer) {
DELETE_ARRAY(buffer);
}
if (nBuffer != nStackBuffer) {
DELETE_ARRAY(nBuffer);
}
return appendTo;
}
static void testLocale(const char *localeID, int32_t lcid, NumberFormat *wnf, UBool currency, TestLog *log)
{
for (int n = 0; n < LOOP_COUNT; n += 1) {
UnicodeString u3Buffer, u6Buffer, udBuffer;
UnicodeString w3Buffer, w6Buffer, wdBuffer;
double d = randomDouble();
int32_t i32 = randomInt32();
int64_t i64 = randomInt64();
getWindowsFormat(lcid, currency, wdBuffer, L"%f", d);
getWindowsFormat(lcid, currency, w3Buffer, L"%I32d", i32);
getWindowsFormat(lcid, currency, w6Buffer, L"%I64d", i64);
wnf->format(d, udBuffer);
if (udBuffer.compare(wdBuffer) != 0) {
UnicodeString locale(localeID);
log->errln("Double format error for locale " + locale +
": got " + udBuffer + " expected " + wdBuffer);
}
wnf->format(i32, u3Buffer);
if (u3Buffer.compare(w3Buffer) != 0) {
UnicodeString locale(localeID);
log->errln("int32_t format error for locale " + locale +
": got " + u3Buffer + " expected " + w3Buffer);
}
wnf->format(i64, u6Buffer);
if (u6Buffer.compare(w6Buffer) != 0) {
UnicodeString locale(localeID);
log->errln("int64_t format error for locale " + locale +
": got " + u6Buffer + " expected " + w6Buffer);
}
}
}
void Win32NumberTest::testLocales(TestLog *log)
{
int32_t lcidCount = 0;
Win32Utilities::LCIDRecord *lcidRecords = Win32Utilities::getLocales(lcidCount);
for(int i = 0; i < lcidCount; i += 1) {
UErrorCode status = U_ZERO_ERROR;
char localeID[128];
strcpy(localeID, lcidRecords[i].localeID);
if (strchr(localeID, '@') > 0) {
strcat(localeID, ";");
} else {
strcat(localeID, "@");
}
strcat(localeID, "compat=host");
Locale ulocale(localeID);
NumberFormat *wnf = NumberFormat::createInstance(ulocale, status);
NumberFormat *wcf = NumberFormat::createCurrencyInstance(ulocale, status);
testLocale(lcidRecords[i].localeID, lcidRecords[i].lcid, wnf, FALSE, log);
testLocale(lcidRecords[i].localeID, lcidRecords[i].lcid, wcf, TRUE, log);
delete wcf;
delete wnf;
}
Win32Utilities::freeLocales(lcidRecords);
}
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif /* #ifdef U_WINDOWS */

View File

@ -0,0 +1,41 @@
/*
********************************************************************************
* Copyright (C) 2005-2006, International Business Machines
* Corporation and others. All Rights Reserved.
********************************************************************************
*
* File WINNMTST.H
*
********************************************************************************
*/
#ifndef __WINNMTST
#define __WINNMTST
#include "unicode/utypes.h"
#ifdef U_WINDOWS
#if !UCONFIG_NO_FORMATTING
/**
* \file
* \brief C++ API: Format dates using Windows API.
*/
class TestLog;
class Win32NumberTest
{
public:
static void testLocales(TestLog *log);
private:
Win32NumberTest();
};
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif // #ifdef U_WINDOWS
#endif // __WINNMTST

View File

@ -0,0 +1,81 @@
/*
********************************************************************************
* Copyright (C) 2005-2006, International Business Machines
* Corporation and others. All Rights Reserved.
********************************************************************************
*
* File WINUTIL.CPP
*
********************************************************************************
*/
#include "unicode/utypes.h"
#ifdef U_WINDOWS
#if !UCONFIG_NO_FORMATTING
#include "winutil.h"
#include "locmap.h"
# define WIN32_LEAN_AND_MEAN
# define VC_EXTRALEAN
# define NOUSER
# define NOSERVICE
# define NOIME
# define NOMCX
# include <windows.h>
# include <stdio.h>
static Win32Utilities::LCIDRecord *lcidRecords = NULL;
static int32_t lcidCount = 0;
static int32_t lcidMax = 0;
BOOL CALLBACK EnumLocalesProc(LPSTR lpLocaleString)
{
UErrorCode status = U_ZERO_ERROR;
if (lcidCount >= lcidMax) {
Win32Utilities::LCIDRecord *newRecords = new Win32Utilities::LCIDRecord[lcidMax + 32];
for (int i = 0; i < lcidMax; i += 1) {
newRecords[i] = lcidRecords[i];
}
delete[] lcidRecords;
lcidRecords = newRecords;
lcidMax += 32;
}
sscanf(lpLocaleString, "%8x", &lcidRecords[lcidCount].lcid);
lcidRecords[lcidCount].localeID = uprv_convertToPosix(lcidRecords[lcidCount].lcid, &status);
lcidCount += 1;
return TRUE;
}
Win32Utilities::LCIDRecord *Win32Utilities::getLocales(int32_t &localeCount)
{
LCIDRecord *result;
EnumSystemLocalesA(EnumLocalesProc, LCID_INSTALLED);
localeCount = lcidCount;
result = lcidRecords;
lcidCount = lcidMax = 0;
lcidRecords = NULL;
return result;
}
void Win32Utilities::freeLocales(LCIDRecord *records)
{
delete[] records;
}
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif /* #ifdef U_WINDOWS */

View File

@ -0,0 +1,46 @@
/*
********************************************************************************
* Copyright (C) 2005-2006, International Business Machines
* Corporation and others. All Rights Reserved.
********************************************************************************
*
* File WINUTIL.H
*
********************************************************************************
*/
#ifndef __WINUTIL
#define __WINUTIL
#include "unicode/utypes.h"
#ifdef U_WINDOWS
#if !UCONFIG_NO_FORMATTING
/**
* \file
* \brief C++ API: Format dates using Windows API.
*/
class Win32Utilities
{
public:
struct LCIDRecord
{
int32_t lcid;
const char *localeID;
};
static LCIDRecord *getLocales(int32_t &localeCount);
static void freeLocales(LCIDRecord *records);
private:
Win32Utilities();
};
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif // #ifdef U_WINDOWS
#endif // __WINUTIL