ICU-3156 fix test program problems introduced by adding u_cleanup to heap/mutex setter tests

X-SVN-Rev: 12851
This commit is contained in:
Andy Heninger 2003-08-16 02:17:56 +00:00
parent 4ce8f701f9
commit 2357f130ab
3 changed files with 47 additions and 25 deletions

View File

@ -88,27 +88,19 @@ int main(int argc, const char* const argv[])
ctst_init();
#endif
/* Initialize ICU */
u_init(&errorCode);
if (U_FAILURE(errorCode)) {
fprintf(stderr,
"#### WARNING! u_init() failed with status = \"%s\".\n"
"Trying again with additional data locations...\n", u_errorName(errorCode));
errorCode = U_ZERO_ERROR;
ctest_setICU_DATA();
ctest_setICU_DATA(); /* u_setDataDirectory() must happen Before u_init() */
u_init(&errorCode);
if (U_FAILURE(errorCode)) {
fprintf(stderr,
"*** %s! ICU Can not be initialized.\n"
"#### ERROR! %s: u_init() failed with status = \"%s\".\n"
"*** Check the ICU_DATA environment variable and \n"
"*** check that the data files are present.\n", warnOrErr, u_errorName(errorCode));
"*** check that the data files are present.\n", argv[0], u_errorName(errorCode));
if(warnOnMissingData == 0) {
fprintf(stderr, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
u_cleanup();
return 1;
}
}
}

View File

@ -15,6 +15,7 @@
#include "cintltst.h"
#include "umutex.h"
#include <stdlib.h>
#include <string.h>
static void TestHeapFunctions(void);
@ -44,6 +45,23 @@ if (!(expr)) { \
log_err("FAILED Assertion \"" #expr "\" at %s:%d.\n", __FILE__, __LINE__); \
}
/* These tests do cleanup and reinitialize ICU in the course of their operation.
* The ICU data directory must be preserved across these operations.
* Here is a helper function to assist with that.
*/
static char *safeGetICUDataDirectory() {
const char *dataDir = u_getDataDirectory(); /* Returned string vanashes with u_cleanup */
char *retStr = NULL;
if (dataDir != NULL) {
retStr = (char *)malloc(strlen(dataDir)+1);
strcpy(retStr, dataDir);
}
return retStr;
}
/*
* Test Heap Functions.
* Implemented on top of the standard malloc heap.
@ -92,8 +110,11 @@ void *myMemRealloc(const void *context, void *mem, size_t size) {
static void TestHeapFunctions() {
UErrorCode status = U_ZERO_ERROR;
UResourceBundle *rb = NULL;
char *icuDataDir;
icuDataDir = safeGetICUDataDirectory(); /* save icu data dir, so we can put it back
* after doing u_cleanup(). */
/* Can not set memory functions if ICU is already initialized */
u_setMemoryFunctions(&gContext, myMemAlloc, myMemRealloc, myMemFree, &status);
TEST_STATUS(status, U_INVALID_STATE_ERROR);
@ -122,6 +143,7 @@ static void TestHeapFunctions() {
/* After reinitializing ICU, we should not be able to set the memory funcs again. */
status = U_ZERO_ERROR;
u_setDataDirectory(icuDataDir);
u_init(&status);
TEST_STATUS(status, U_ZERO_ERROR);
u_setMemoryFunctions(NULL, myMemAlloc, myMemRealloc, myMemFree, &status);
@ -139,6 +161,7 @@ static void TestHeapFunctions() {
/* Cleanup should put the heap back to its default implementation. */
u_cleanup();
u_setDataDirectory(icuDataDir);
status = U_ZERO_ERROR;
u_init(&status);
TEST_STATUS(status, U_ZERO_ERROR);
@ -152,6 +175,7 @@ static void TestHeapFunctions() {
log_err("Heap functions did not reset after u_cleanup.\n");
}
ures_close(rb);
free(icuDataDir);
}
@ -216,8 +240,9 @@ void myMutexUnlock(const void *context, UMTX *mutex) {
static void TestMutexFunctions() {
UErrorCode status = U_ZERO_ERROR;
UResourceBundle *rb = NULL;
char *icuDataDir;
icuDataDir = safeGetICUDataDirectory();
/* Can not set mutex functions if ICU is already initialized */
u_setMutexFunctions(&gContext, myMutexInit, myMutexDestroy, myMutexLock, myMutexUnlock, &status);
TEST_STATUS(status, U_INVALID_STATE_ERROR);
@ -249,6 +274,7 @@ static void TestMutexFunctions() {
/* After reinitializing ICU, we should not be able to set the mutex funcs again. */
status = U_ZERO_ERROR;
u_setDataDirectory(icuDataDir);
u_init(&status);
TEST_STATUS(status, U_ZERO_ERROR);
u_setMutexFunctions(&gContext, myMutexInit, myMutexDestroy, myMutexLock, myMutexUnlock, &status);
@ -266,6 +292,7 @@ static void TestMutexFunctions() {
/* Cleanup should destroy all of the mutexes. */
u_cleanup();
u_setDataDirectory(icuDataDir);
status = U_ZERO_ERROR;
TEST_ASSERT(gTotalMutexesInitialized > 0);
TEST_ASSERT(gTotalMutexesActive == 0);
@ -284,6 +311,7 @@ static void TestMutexFunctions() {
TEST_ASSERT(gTotalMutexesActive == 0);
ures_close(rb);
free(icuDataDir);
}
@ -320,6 +348,7 @@ static int32_t myDecFunc(const void *context, int32_t *p) {
static void TestIncDecFunctions() {
UErrorCode status = U_ZERO_ERROR;
int32_t t;
char *dataDir;
/* Can not set mutex functions if ICU is already initialized */
@ -327,6 +356,7 @@ static void TestIncDecFunctions() {
TEST_STATUS(status, U_INVALID_STATE_ERROR);
/* Un-initialize ICU */
dataDir = safeGetICUDataDirectory();
u_cleanup();
/* Can not set functions with NULL values */
@ -347,6 +377,7 @@ static void TestIncDecFunctions() {
/* After reinitializing ICU, we should not be able to set the inc/dec funcs again. */
status = U_ZERO_ERROR;
u_setDataDirectory(dataDir);
u_init(&status);
TEST_STATUS(status, U_ZERO_ERROR);
u_setAtomicIncDecFunctions(&gIncDecContext, myIncFunc, myDecFunc, &status);
@ -367,6 +398,7 @@ static void TestIncDecFunctions() {
gIncCount = 0;
gDecCount = 0;
status = U_ZERO_ERROR;
u_setDataDirectory(dataDir);
u_init(&status);
TEST_ASSERT(gIncCount == 0);
TEST_ASSERT(gDecCount == 0);

View File

@ -975,19 +975,18 @@ main(int argc, char* argv[])
#endif
/* Initialize ICU */
IntlTest::setICU_DATA(); // Must set data directory before u_init() is called.
u_init(&errorCode);
if (U_FAILURE(errorCode)) {
fprintf(stderr,
"#### WARNING! u_init() failed."
"Trying again with a synthesized ICU_DATA setting.\n");
IntlTest::setICU_DATA();
errorCode = U_ZERO_ERROR;
"#### %s: u_init() failed, error is \"%s\".\n"
"#### Most commonly indicates that the ICU data is not accesible.\n"
"#### Check setting of ICU_DATA, or check that ICU data library is available\n"
"#### Aborting %s\n", argv[0], u_errorName(errorCode));
u_cleanup();
return 1;
}
// If user didn't set ICU_DATA, attempt to generate one.
// Do this whether or not the u_init(), above, succeeded because we need the
// data path to find some of the test data.
IntlTest::setICU_DATA();
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
@ -1359,4 +1358,3 @@ float IntlTest::random() {
* End:
*
*/