+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
- * Copyright (c) 1998-2010, International Business Machines Corporation and
+ * Copyright (c) 1998-2016, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/*
-* File test.c
+* File udatatst.c
*
* Modification History:
*
#include "unicode/utypes.h"
#include "unicode/putil.h"
#include "unicode/udata.h"
+#include "unicode/ucal.h"
#include "unicode/uchar.h"
#include "unicode/ucnv.h"
#include "unicode/ures.h"
#include "udatamem.h"
#include "cintltst.h"
#include "ubrkimpl.h"
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
+#include "toolutil.h" /* for uprv_fileExists() */
#include <stdlib.h>
#include <stdio.h>
-#ifdef U_WINDOWS
-#include <io.h>
-#else
-#include <unistd.h>
-#endif
-
/* includes for TestSwapData() */
#include "udataswp.h"
#include "ucol_swp.h"
#include "ucnv_bld.h"
#include "sprpimpl.h"
-#include "propname.h"
#include "rbbidata.h"
/* swapping implementation in i18n */
/* other definitions and prototypes */
-#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
-
+#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
static void TestUDataOpen(void);
static void TestUDataOpenChoiceDemo1(void);
static void TestUDataOpenChoiceDemo2(void);
static void TestUDataGetInfo(void);
static void TestUDataGetMemory(void);
-static void TestUDataSetAppData(void);
static void TestErrorConditions(void);
static void TestAppData(void);
-static void TestICUDataName(void);
static void TestSwapData(void);
+#endif
+static void TestUDataSetAppData(void);
+static void TestICUDataName(void);
static void PointerTableOfContents(void);
static void SetBadCommonData(void);
static void TestUDataFileAccess(void);
-
+#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
+static void TestTZDataDir(void);
+#endif
void addUDataTest(TestNode** root);
addTest(root, &PointerTableOfContents, "udatatst/PointerTableOfContents" );
addTest(root, &SetBadCommonData, "udatatst/SetBadCommonData" );
addTest(root, &TestUDataFileAccess, "udatatst/TestUDataFileAccess" );
+#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
+ addTest(root, &TestTZDataDir, "udatatst/TestTZDataDir" );
+#endif
}
#if 0
}
#endif
+#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
static void TestUDataOpen(){
UDataMemory *result;
UErrorCode status=U_ZERO_ERROR;
const char* memMap[][2]={
{"root", "res"},
- {"pnames", "icu"},
{"cnvalias", "icu"},
{"unames", "icu"},
{"ibm-37_P100-1995", "cnv"}
char* path=(char*)malloc(sizeof(char) * (strlen(ctest_dataOutDir())
+ strlen(U_ICUDATA_NAME)
- + strlen("/build")+1 ) );
+ + strlen("/build/tmp/..")+1 ) );
char *icuDataFilePath = 0;
- struct stat stat_buf;
const char* testPath=loadTestData(&status);
if(U_FAILURE(status)) {
}
/* lots_of_mallocs(); */
-
- strcat(strcpy(path, ctest_dataOutDir()), U_ICUDATA_NAME);
-
- log_verbose("Testing udata_open()\n");
+ log_verbose("Testing udata_open(%s)\n", testPath);
result=udata_open(testPath, type, name, &status);
if(U_FAILURE(status)){
log_data_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", testPath, name, type, myErrorName(status));
udata_close(result);
}
- /* If the ICU system common data file is present in this confiugration,
- * verify that udata_open can explicitly fetch items from it.
- * If packaging mode == dll, the file may not exist. So, if the file is
- * missing, skip this test without error.
- */
- icuDataFilePath = (char *)malloc(strlen(path) + 10);
- strcpy(icuDataFilePath, path);
- strcat(icuDataFilePath, ".dat");
- /* lots_of_mallocs(); */
- if (stat(icuDataFilePath, &stat_buf) == 0)
{
- int i;
- log_verbose("Testing udata_open() on %s\n", icuDataFilePath);
- for(i=0; i<sizeof(memMap)/sizeof(memMap[0]); i++){
+ strcat(strcpy(path, ctest_dataOutDir()), U_ICUDATA_NAME);
+
+ /* If the ICU system common data file is present in this confiugration,
+ * verify that udata_open can explicitly fetch items from it.
+ * If packaging mode == dll, the file may not exist. So, if the file is
+ * missing, skip this test without error.
+ */
+ icuDataFilePath = (char *)uprv_malloc(strlen(path) + 10);
+ strcpy(icuDataFilePath, path);
+ strcat(icuDataFilePath, ".dat");
+ /* lots_of_mallocs(); */
+ if (uprv_fileExists(icuDataFilePath))
+ {
+ int i;
+ log_verbose("Testing udata_open() on %s\n", icuDataFilePath);
+ for(i=0; i<UPRV_LENGTHOF(memMap); i++){
/* lots_of_mallocs(); */
status=U_ZERO_ERROR;
result=udata_open(path, memMap[i][1], memMap[i][0], &status);
log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", path, memMap[i][0], memMap[i][1]);
udata_close(result);
}
- }
- }
- else
+ }
+ }
+ else
+ {
+ /* lots_of_mallocs(); */
+ log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n",
+ icuDataFilePath);
+ }
+ uprv_free(icuDataFilePath);
+ }
+ /* try again, adding /tmp */
{
- /* lots_of_mallocs(); */
- log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n",
- icuDataFilePath);
+ strcpy(path, ctest_dataOutDir());
+ strcat(path, "tmp");
+ strcat(path, dirSepString);
+ strcat(path, U_ICUDATA_NAME);
+
+ /* If the ICU system common data file is present in this confiugration,
+ * verify that udata_open can explicitly fetch items from it.
+ * If packaging mode == dll, the file may not exist. So, if the file is
+ * missing, skip this test without error.
+ */
+ icuDataFilePath = (char *)malloc(strlen(path) + 10);
+ strcpy(icuDataFilePath, path);
+ strcat(icuDataFilePath, ".dat");
+ /* lots_of_mallocs(); */
+ if (uprv_fileExists(icuDataFilePath))
+ {
+ int i;
+ log_verbose("Testing udata_open() on %s\n", icuDataFilePath);
+ for(i=0; i<UPRV_LENGTHOF(memMap); i++){
+ /* lots_of_mallocs(); */
+ status=U_ZERO_ERROR;
+ result=udata_open(path, memMap[i][1], memMap[i][0], &status);
+ if(U_FAILURE(status)) {
+ log_data_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", path, memMap[i][0], memMap[i][1], myErrorName(status));
+ } else {
+ log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", path, memMap[i][0], memMap[i][1]);
+ udata_close(result);
+ }
+ }
+ }
+ else
+ {
+ /* lots_of_mallocs(); */
+ log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n",
+ icuDataFilePath);
+ }
}
+
free(icuDataFilePath);
icuDataFilePath = NULL;
/* lots_of_mallocs(); */
strcat(icuDataFilePath, "build");
strcat(icuDataFilePath, dirSepString);
strcat(icuDataFilePath, U_ICUDATA_NAME);
- strcat(icuDataFilePath, "_");
+ strcat(icuDataFilePath, dirSepString);
strcat(icuDataFilePath, "cnvalias.icu");
/* lots_of_mallocs(); */
- if (stat(icuDataFilePath, &stat_buf) == 0)
+ if (uprv_fileExists(icuDataFilePath))
{
int i;
log_verbose("%s exists, so..\n", icuDataFilePath);
strcat(icuDataFilePath, dirSepString);
strcat(icuDataFilePath, U_ICUDATA_NAME);
log_verbose("Testing udata_open() on %s\n", icuDataFilePath);
- for(i=0; i<sizeof(memMap)/sizeof(memMap[0]); i++){
+ for(i=0; i<UPRV_LENGTHOF(memMap); i++){
status=U_ZERO_ERROR;
result=udata_open(icuDataFilePath, memMap[i][1], memMap[i][0], &status);
if(U_FAILURE(status)) {
free(path);
}
+#endif
typedef struct {
uint16_t headerSize;
}
+#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
static void TestUDataOpenChoiceDemo1() {
UDataMemory *result;
UErrorCode status=U_ZERO_ERROR;
}
}
-
static void TestUDataGetInfo() {
UDataMemory *result;
ures_close(icu);
ures_close(app);
}
+#endif
static void TestICUDataName()
{
switch(U_CHARSET_FAMILY)
{
case U_ASCII_FAMILY:
- switch(U_IS_BIG_ENDIAN)
+ switch((int)U_IS_BIG_ENDIAN)
{
case 1:
typeChar = 'b';
break;
}
- sprintf(expectDataName, "%s%d%d%c",
+ /* Only major number is needed. */
+ sprintf(expectDataName, "%s%d%c",
"icudt",
(int)icuVersion[0],
- (int)icuVersion[1],
typeChar);
log_verbose("Expected: %s\n", expectDataName);
/* test data swapping ------------------------------------------------------- */
-#ifdef OS400
+#if U_PLATFORM == U_PF_OS400
/* See comments in genccode.c on when this special implementation can be removed. */
static const struct {
double bogus;
}
};
-/* Unfortunately, trie dictionaries are in a C++ header */
-int32_t
-triedict_swap(const UDataSwapper *ds,
- const void *inData, int32_t length, void *outData,
- UErrorCode *pErrorCode);
+/* Unfortunately, dictionaries are in a C++ header */
+U_CAPI int32_t U_EXPORT2
+udict_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outData, UErrorCode *pErrorCode);
/* test cases for maximum data swapping code coverage */
static const struct {
#if !UCONFIG_NO_COLLATION
/* standalone collation data files */
{"ucadata", "icu", ucol_swap},
+#if 0
+ /* Starting with ICU 53, the "inverse UCA" data is integrated into ucadata.icu. */
{"invuca", "icu", ucol_swapInverseUCA},
#endif
+#endif
#if !UCONFIG_NO_LEGACY_CONVERSION
/* conversion table files */
#if !UCONFIG_NO_BREAK_ITERATION
{"char", "brk", ubrk_swap},
- {"thaidict", "ctd", triedict_swap},
+ {"thaidict", "dict",udict_swap},
#endif
- /* the last item should not be #if'ed so that it can reliably omit the last comma */
-
+#if 0
+ /*
+ * Starting with ICU 4.8, the Unicode property (value) aliases data
+ * is hardcoded in the ICU4C common library.
+ * The swapper was moved to the toolutil library for swapping for ICU4J.
+ */
/* Unicode properties */
{"pnames", "icu", upname_swap},
+#endif
+
#if 0
/*
* Starting with ICU4C 3.4, the core Unicode properties files
* to testdata) for code coverage in tests.
* See Jitterbug 4497.
*
- * ICU4C 4.4 adds normalization data files again, e.g., nfc.nrm.
+ * ICU4C 4.4 adds normalization data files again, e.g., nfkc.nrm.
*/
{"uprops", "icu", uprops_swap},
{"ucase", "icu", ucase_swap},
{"ubidi", "icu", ubidi_swap},
#endif
-#if !UCONFIG_NO_NORMALIZATION
- {"nfc", "nrm", unorm2_swap},
- {"confusables", "cfu", uspoof_swap},
+#if !UCONFIG_NO_NORMALIZATION && !UCONFIG_ONLY_COLLATION
+ {"nfkc", "nrm", unorm2_swap},
+#if !UCONFIG_NO_REGULAR_EXPRESSIONS
+ {"confusables", "cfu", uspoof_swap}, /* spoof data missing without regex */
+#endif
+
#endif
{"unames", "icu", uchar_swapNames}
+ /* the last item should not be #if'ed so that it can reliably omit the last comma */
};
/* Large enough for the largest swappable data item. */
vsprintf((char *)context, fmt, args);
}
+#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
static void
TestSwapData() {
char name[100];
}
errorCode=U_ZERO_ERROR;
- /* Test argument checking. ucol_swapBinary is normally tested via ures_swap, and isn't normally called directly. */
+ /* Test argument checking. ucol_swap is normally tested via ures_swap, and isn't normally called directly. */
#if !UCONFIG_NO_COLLATION
- ucol_swapBinary(NULL, NULL, -1, NULL, NULL);
- ucol_swapBinary(NULL, NULL, -1, NULL, &errorCode);
+ ucol_swap(NULL, NULL, -1, NULL, &errorCode);
if (errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
- log_err("ucol_swapBinary did not fail as expected\n", name);
+ log_err("ucol_swap did not fail as expected\n", name);
}
errorCode=U_ZERO_ERROR;
#endif
- for(i=0; i<LENGTHOF(swapCases); ++i) {
+ for(i=0; i<UPRV_LENGTHOF(swapCases); ++i) {
/* build the name for logging */
errorCode=U_ZERO_ERROR;
if(swapCases[i].name[0]=='*') {
nm=swapCases[i].name+1;
uprv_strcpy(name, "testdata");
} else if (uprv_strcmp(swapCases[i].type, "brk")==0
- || uprv_strcmp(swapCases[i].type, "ctd")==0) {
+ || uprv_strcmp(swapCases[i].type, "dict")==0) {
pkg=U_ICUDATA_BRKITR;
nm=swapCases[i].name;
uprv_strcpy(name, U_ICUDATA_BRKITR);
+#if !UCONFIG_NO_COLLATION
} else if (uprv_strcmp(swapCases[i].name, "ucadata")==0
|| uprv_strcmp(swapCases[i].name, "invuca")==0) {
pkg=U_ICUDATA_COLL;
nm=swapCases[i].name;
uprv_strcpy(name, U_ICUDATA_COLL);
+#endif /* !UCONFIG_NO_COLLATION */
} else {
pkg=NULL;
nm=swapCases[i].name;
uprv_strcat(name, swapCases[i].type);
pData=udata_open(pkg, swapCases[i].type, nm, &errorCode);
+
if(U_SUCCESS(errorCode)) {
TestSwapCase(pData, name, swapCases[i].swapFn, buffer, buffer+SWAP_BUFFER_SIZE);
udata_close(pData);
free(buffer);
}
-
+#endif
static void PointerTableOfContents() {
UDataMemory *dataItem;
}
}
+// Check the override loading of time zone .res files from a specified path
+//
+// Hand testing notes:
+// 1. Run this test with the environment variable set. The following should induce faiures:
+// ICU_TIMEZONE_FILES_DIR=../testdata/out/build LD_LIBRARY_PATH=../../lib:../../stubdata:../../tools/ctestfw:$LD_LIBRARY_PATH ./cintltst /udatatst/TestTZDataDir
+// 2. Build ICU with with U_TIMEZONE_FILES_DIR defined. This should also induce failures.
+// CPPFLAGS=-DU_TIMEZONE_FILES_DIR\=`pwd`/test/testdata/out/testdata ./runConfigureICU Linux
+// make check
+
+static void TestTZDataDir(void) {
+#if !UCONFIG_NO_FORMATTING
+ UErrorCode status = U_ZERO_ERROR;
+ const char *tzDataVersion;
+ const char *testDataPath;
+
+ // Verify that default ICU time zone data version is something newer than 2014a.
+ tzDataVersion = ucal_getTZDataVersion(&status);
+ // printf("tz data version is %s\n", tzDataVersion);
+ if (U_FAILURE(status)) {
+ log_data_err("Failed call to ucal_getTZDataVersion - %s\n", u_errorName(status));
+ return;
+ } else if (strcmp("2014a", tzDataVersion) == 0) {
+ log_err("File %s:%d - expected something newer than time zone data 2014a.\n", __FILE__, __LINE__, tzDataVersion);
+ }
+
+ testDataPath = loadTestData(&status);
+ // The path produced by loadTestData() will look something like
+ // whatever/.../testdata/out/testdata
+ // The test data puts an old (2014a) version of the time zone data there.
+
+ // Switch ICU to the testdata version of zoneinfo64.res, which is verison 2014a.
+ ctest_resetICU();
+ u_setTimeZoneFilesDirectory(testDataPath, &status);
+ tzDataVersion = ucal_getTZDataVersion(&status);
+ if (strcmp("2014a", tzDataVersion) != 0) {
+ log_err("File %s:%d - expected \"2014a\"; actual \"%s\"\n", __FILE__, __LINE__, tzDataVersion);
+ }
+
+ ctest_resetICU(); // Return ICU to using its standard tz data.
+ tzDataVersion = ucal_getTZDataVersion(&status);
+ // printf("tz data version is %s\n", tzDataVersion);
+ if (strcmp("2014a", tzDataVersion) == 0) {
+ log_err("File %s:%d - expected something newer than time zone data 2014a.\n", __FILE__, __LINE__, tzDataVersion);
+ }
+#endif
+}