2003-08-05 01:25:54 +00:00
|
|
|
/********************************************************************
|
|
|
|
* COPYRIGHT:
|
2004-05-19 21:19:13 +00:00
|
|
|
* Copyright (c) 200-20043, International Business Machines Corporation and
|
2003-08-05 01:25:54 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
********************************************************************/
|
|
|
|
/*
|
|
|
|
* File hpmufn.c
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/uclean.h"
|
|
|
|
#include "unicode/uchar.h"
|
|
|
|
#include "unicode/ures.h"
|
|
|
|
#include "cintltst.h"
|
2003-08-08 23:56:02 +00:00
|
|
|
#include "umutex.h"
|
2003-11-26 03:09:51 +00:00
|
|
|
#include "unicode/utrace.h"
|
2003-08-05 01:25:54 +00:00
|
|
|
#include <stdlib.h>
|
2003-08-16 02:17:56 +00:00
|
|
|
#include <string.h>
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2003-12-03 21:16:34 +00:00
|
|
|
/**
|
|
|
|
* This should align the memory properly on any machine.
|
|
|
|
*/
|
|
|
|
typedef union {
|
|
|
|
long t1;
|
|
|
|
double t2;
|
|
|
|
void *t3;
|
|
|
|
} ctest_AlignedMemory;
|
2003-08-05 01:25:54 +00:00
|
|
|
|
|
|
|
static void TestHeapFunctions(void);
|
2003-08-08 16:23:38 +00:00
|
|
|
static void TestMutexFunctions(void);
|
2003-08-08 23:56:02 +00:00
|
|
|
static void TestIncDecFunctions(void);
|
2003-08-05 01:25:54 +00:00
|
|
|
|
|
|
|
void addHeapMutexTest(TestNode **root);
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
addHeapMutexTest(TestNode** root)
|
|
|
|
{
|
2003-10-06 22:32:36 +00:00
|
|
|
addTest(root, &TestHeapFunctions, "tsutil/hpmufn/TestHeapFunctions" );
|
|
|
|
addTest(root, &TestMutexFunctions, "tsutil/hpmufn/TestMutexFunctions" );
|
|
|
|
addTest(root, &TestIncDecFunctions, "tsutil/hpmufn/TestIncDecFunctions");
|
2003-08-05 01:25:54 +00:00
|
|
|
}
|
|
|
|
|
2004-05-11 20:23:54 +00:00
|
|
|
static int32_t gMutexFailures = 0;
|
2003-08-05 01:25:54 +00:00
|
|
|
|
|
|
|
#define TEST_STATUS(status, expected) \
|
|
|
|
if (status != expected) { \
|
|
|
|
log_err("FAIL at %s:%d. Actual status = \"%s\"; Expected status = \"%s\"\n", \
|
2004-05-11 20:23:54 +00:00
|
|
|
__FILE__, __LINE__, u_errorName(status), u_errorName(expected)); gMutexFailures++; }
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2003-08-08 16:23:38 +00:00
|
|
|
|
|
|
|
#define TEST_ASSERT(expr) \
|
|
|
|
if (!(expr)) { \
|
|
|
|
log_err("FAILED Assertion \"" #expr "\" at %s:%d.\n", __FILE__, __LINE__); \
|
2004-05-11 20:23:54 +00:00
|
|
|
gMutexFailures++; \
|
2003-08-08 16:23:38 +00:00
|
|
|
}
|
2003-08-16 02:17:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-08 16:23:38 +00:00
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
/*
|
|
|
|
* Test Heap Functions.
|
|
|
|
* Implemented on top of the standard malloc heap.
|
2003-12-03 21:16:34 +00:00
|
|
|
* All blocks increased in size by 8 to 16 bytes, and the poiner returned to ICU is
|
|
|
|
* offset up by 8 to 16, which should cause a good heap corruption if one of our "blocks"
|
2003-08-05 01:25:54 +00:00
|
|
|
* ends up being freed directly, without coming through us.
|
|
|
|
* Allocations are counted, to check that ICU actually does call back to us.
|
|
|
|
*/
|
|
|
|
int gBlockCount = 0;
|
2003-08-08 16:23:38 +00:00
|
|
|
const void *gContext;
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2004-07-16 06:52:44 +00:00
|
|
|
static void * U_CALLCONV myMemAlloc(const void *context, size_t size) {
|
2003-12-03 21:16:34 +00:00
|
|
|
char *retPtr = (char *)malloc(size+sizeof(ctest_AlignedMemory));
|
2003-08-05 01:25:54 +00:00
|
|
|
if (retPtr != NULL) {
|
2003-12-03 21:16:34 +00:00
|
|
|
retPtr += sizeof(ctest_AlignedMemory);
|
2003-08-05 01:25:54 +00:00
|
|
|
}
|
|
|
|
gBlockCount ++;
|
|
|
|
return retPtr;
|
|
|
|
}
|
|
|
|
|
2004-07-16 06:52:44 +00:00
|
|
|
static void U_CALLCONV myMemFree(const void *context, void *mem) {
|
2003-08-05 01:25:54 +00:00
|
|
|
char *freePtr = (char *)mem;
|
|
|
|
if (freePtr != NULL) {
|
2003-12-03 21:16:34 +00:00
|
|
|
freePtr -= sizeof(ctest_AlignedMemory);
|
2003-08-05 01:25:54 +00:00
|
|
|
}
|
|
|
|
free(freePtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-24 23:56:17 +00:00
|
|
|
|
2004-07-16 06:52:44 +00:00
|
|
|
static void * U_CALLCONV myMemRealloc(const void *context, void *mem, size_t size) {
|
2003-08-05 01:25:54 +00:00
|
|
|
char *p = (char *)mem;
|
|
|
|
char *retPtr;
|
|
|
|
|
|
|
|
if (p!=NULL) {
|
2003-12-03 21:16:34 +00:00
|
|
|
p -= sizeof(ctest_AlignedMemory);
|
2003-08-05 01:25:54 +00:00
|
|
|
}
|
2003-12-03 21:16:34 +00:00
|
|
|
retPtr = realloc(p, size+sizeof(ctest_AlignedMemory));
|
2003-08-05 01:25:54 +00:00
|
|
|
if (retPtr != NULL) {
|
2003-12-03 21:16:34 +00:00
|
|
|
p += sizeof(ctest_AlignedMemory);
|
2003-08-05 01:25:54 +00:00
|
|
|
}
|
|
|
|
return retPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void TestHeapFunctions() {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UResourceBundle *rb = NULL;
|
2003-08-16 02:17:56 +00:00
|
|
|
char *icuDataDir;
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2003-11-26 03:09:51 +00:00
|
|
|
UTraceEntry *traceEntryFunc; /* Tracing function ptrs. We need to save */
|
|
|
|
UTraceExit *traceExitFunc; /* and restore them across calls to */
|
|
|
|
UTraceData *traceDataFunc; /* u_cleanup() that we make in this test. */
|
|
|
|
const void *traceContext;
|
|
|
|
int32_t traceLevel;
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2003-08-16 02:17:56 +00:00
|
|
|
icuDataDir = safeGetICUDataDirectory(); /* save icu data dir, so we can put it back
|
|
|
|
* after doing u_cleanup(). */
|
2003-11-26 03:09:51 +00:00
|
|
|
|
|
|
|
utrace_getFunctions(&traceContext, &traceEntryFunc, &traceExitFunc, &traceDataFunc);
|
|
|
|
traceLevel = utrace_getLevel();
|
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
/* Can not set memory functions if ICU is already initialized */
|
|
|
|
u_setMemoryFunctions(&gContext, myMemAlloc, myMemRealloc, myMemFree, &status);
|
|
|
|
TEST_STATUS(status, U_INVALID_STATE_ERROR);
|
|
|
|
|
|
|
|
/* Un-initialize ICU */
|
|
|
|
u_cleanup();
|
2003-11-26 03:09:51 +00:00
|
|
|
utrace_setFunctions(traceContext, traceEntryFunc, traceExitFunc, traceDataFunc);
|
|
|
|
utrace_setLevel(traceLevel);
|
2003-08-05 01:25:54 +00:00
|
|
|
|
|
|
|
/* Can not set memory functions with NULL values */
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
u_setMemoryFunctions(&gContext, NULL, myMemRealloc, myMemFree, &status);
|
|
|
|
TEST_STATUS(status, U_ILLEGAL_ARGUMENT_ERROR);
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
u_setMemoryFunctions(&gContext, myMemAlloc, NULL, myMemFree, &status);
|
|
|
|
TEST_STATUS(status, U_ILLEGAL_ARGUMENT_ERROR);
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
u_setMemoryFunctions(&gContext, myMemAlloc, myMemRealloc, NULL, &status);
|
|
|
|
TEST_STATUS(status, U_ILLEGAL_ARGUMENT_ERROR);
|
|
|
|
|
|
|
|
/* u_setMemoryFunctions() should work with null or non-null context pointer */
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
u_setMemoryFunctions(NULL, myMemAlloc, myMemRealloc, myMemFree, &status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
u_setMemoryFunctions(&gContext, myMemAlloc, myMemRealloc, myMemFree, &status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
|
|
|
|
|
|
|
|
/* After reinitializing ICU, we should not be able to set the memory funcs again. */
|
|
|
|
status = U_ZERO_ERROR;
|
2003-08-16 02:17:56 +00:00
|
|
|
u_setDataDirectory(icuDataDir);
|
2003-08-05 01:25:54 +00:00
|
|
|
u_init(&status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
u_setMemoryFunctions(NULL, myMemAlloc, myMemRealloc, myMemFree, &status);
|
|
|
|
TEST_STATUS(status, U_INVALID_STATE_ERROR);
|
|
|
|
|
|
|
|
/* Doing ICU operations should cause allocations to come through our test heap */
|
|
|
|
gBlockCount = 0;
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
rb = ures_open(NULL, "es", &status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
if (gBlockCount == 0) {
|
|
|
|
log_err("Heap functions are not being called from ICU.\n");
|
|
|
|
}
|
|
|
|
ures_close(rb);
|
|
|
|
|
|
|
|
/* Cleanup should put the heap back to its default implementation. */
|
|
|
|
u_cleanup();
|
2003-11-26 03:09:51 +00:00
|
|
|
utrace_setFunctions(traceContext, traceEntryFunc, traceExitFunc, traceDataFunc);
|
|
|
|
utrace_setLevel(traceLevel);
|
2003-08-16 02:17:56 +00:00
|
|
|
u_setDataDirectory(icuDataDir);
|
2003-08-05 01:25:54 +00:00
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
u_init(&status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
|
|
|
|
/* ICU operations should no longer cause allocations to come through our test heap */
|
|
|
|
gBlockCount = 0;
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
rb = ures_open(NULL, "fr", &status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
if (gBlockCount != 0) {
|
|
|
|
log_err("Heap functions did not reset after u_cleanup.\n");
|
|
|
|
}
|
|
|
|
ures_close(rb);
|
2003-08-16 02:17:56 +00:00
|
|
|
free(icuDataDir);
|
2003-08-08 16:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test u_setMutexFunctions()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int gTotalMutexesInitialized = 0; /* Total number of mutexes created */
|
|
|
|
int gTotalMutexesActive = 0; /* Total mutexes created, but not destroyed */
|
|
|
|
int gAccumulatedLocks = 0;
|
|
|
|
const void *gMutexContext;
|
|
|
|
|
|
|
|
typedef struct DummyMutex {
|
|
|
|
int fLockCount;
|
|
|
|
int fMagic;
|
|
|
|
} DummyMutex;
|
|
|
|
|
|
|
|
|
2004-07-16 06:52:44 +00:00
|
|
|
static void U_CALLCONV myMutexInit(const void *context, UMTX *mutex, UErrorCode *status) {
|
2003-08-08 16:23:38 +00:00
|
|
|
DummyMutex *theMutex;
|
|
|
|
|
|
|
|
TEST_STATUS(*status, U_ZERO_ERROR);
|
|
|
|
theMutex = (DummyMutex *)malloc(sizeof(DummyMutex));
|
|
|
|
theMutex->fLockCount = 0;
|
|
|
|
theMutex->fMagic = 123456;
|
|
|
|
gTotalMutexesInitialized++;
|
|
|
|
gTotalMutexesActive++;
|
|
|
|
gMutexContext = context;
|
|
|
|
*mutex = theMutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-16 06:52:44 +00:00
|
|
|
static void U_CALLCONV myMutexDestroy(const void *context, UMTX *mutex) {
|
2003-08-08 16:23:38 +00:00
|
|
|
DummyMutex *This = *(DummyMutex **)mutex;
|
|
|
|
|
|
|
|
gTotalMutexesActive--;
|
|
|
|
TEST_ASSERT(This->fLockCount == 0);
|
|
|
|
TEST_ASSERT(This->fMagic == 123456);
|
|
|
|
This->fMagic = 0;
|
|
|
|
This->fLockCount = 0;
|
|
|
|
free(This);
|
|
|
|
}
|
|
|
|
|
2004-07-16 06:52:44 +00:00
|
|
|
static void U_CALLCONV myMutexLock(const void *context, UMTX *mutex) {
|
2003-08-08 16:23:38 +00:00
|
|
|
DummyMutex *This = *(DummyMutex **)mutex;
|
|
|
|
|
|
|
|
TEST_ASSERT(This->fMagic == 123456);
|
|
|
|
This->fLockCount++;
|
|
|
|
gAccumulatedLocks++;
|
|
|
|
}
|
|
|
|
|
2004-07-16 06:52:44 +00:00
|
|
|
static void U_CALLCONV myMutexUnlock(const void *context, UMTX *mutex) {
|
2003-08-08 16:23:38 +00:00
|
|
|
DummyMutex *This = *(DummyMutex **)mutex;
|
|
|
|
|
|
|
|
TEST_ASSERT(This->fMagic == 123456);
|
|
|
|
This->fLockCount--;
|
|
|
|
TEST_ASSERT(This->fLockCount >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void TestMutexFunctions() {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UResourceBundle *rb = NULL;
|
2003-08-16 02:17:56 +00:00
|
|
|
char *icuDataDir;
|
2003-08-08 16:23:38 +00:00
|
|
|
|
2003-11-26 03:09:51 +00:00
|
|
|
UTraceEntry *traceEntryFunc; /* Tracing function ptrs. We need to save */
|
|
|
|
UTraceExit *traceExitFunc; /* and restore them across calls to */
|
|
|
|
UTraceData *traceDataFunc; /* u_cleanup() that we make in this test. */
|
|
|
|
const void *traceContext;
|
|
|
|
int32_t traceLevel;
|
|
|
|
|
2004-05-11 20:23:54 +00:00
|
|
|
gMutexFailures = 0;
|
|
|
|
|
2003-11-26 03:09:51 +00:00
|
|
|
/* Save initial ICU state so that it can be restored later.
|
|
|
|
* u_cleanup(), which is called in this test, resets ICU's state.
|
|
|
|
*/
|
2003-08-16 02:17:56 +00:00
|
|
|
icuDataDir = safeGetICUDataDirectory();
|
2003-11-26 03:09:51 +00:00
|
|
|
utrace_getFunctions(&traceContext, &traceEntryFunc, &traceExitFunc, &traceDataFunc);
|
|
|
|
traceLevel = utrace_getLevel();
|
|
|
|
|
2003-08-08 16:23:38 +00:00
|
|
|
/* 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);
|
|
|
|
|
|
|
|
/* Un-initialize ICU */
|
|
|
|
u_cleanup();
|
2003-11-26 03:09:51 +00:00
|
|
|
utrace_setFunctions(traceContext, traceEntryFunc, traceExitFunc, traceDataFunc);
|
|
|
|
utrace_setLevel(traceLevel);
|
2003-08-08 16:23:38 +00:00
|
|
|
|
|
|
|
/* Can not set Mutex functions with NULL values */
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
u_setMutexFunctions(&gContext, NULL, myMutexDestroy, myMutexLock, myMutexUnlock, &status);
|
|
|
|
TEST_STATUS(status, U_ILLEGAL_ARGUMENT_ERROR);
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
u_setMutexFunctions(&gContext, myMutexInit, NULL, myMutexLock, myMutexUnlock, &status);
|
|
|
|
TEST_STATUS(status, U_ILLEGAL_ARGUMENT_ERROR);
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
u_setMutexFunctions(&gContext, myMutexInit, myMutexDestroy, NULL, myMutexUnlock, &status);
|
|
|
|
TEST_STATUS(status, U_ILLEGAL_ARGUMENT_ERROR);
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
u_setMutexFunctions(&gContext, myMutexInit, myMutexDestroy, myMutexLock, NULL, &status);
|
|
|
|
TEST_STATUS(status, U_ILLEGAL_ARGUMENT_ERROR);
|
|
|
|
|
|
|
|
/* u_setMutexFunctions() should work with null or non-null context pointer */
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
u_setMutexFunctions(&gContext, myMutexInit, myMutexDestroy, myMutexLock, myMutexUnlock, &status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
u_setMutexFunctions(&gContext, myMutexInit, myMutexDestroy, myMutexLock, myMutexUnlock, &status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
|
|
|
|
|
|
|
|
/* After reinitializing ICU, we should not be able to set the mutex funcs again. */
|
|
|
|
status = U_ZERO_ERROR;
|
2003-08-16 02:17:56 +00:00
|
|
|
u_setDataDirectory(icuDataDir);
|
2003-08-08 16:23:38 +00:00
|
|
|
u_init(&status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
u_setMutexFunctions(&gContext, myMutexInit, myMutexDestroy, myMutexLock, myMutexUnlock, &status);
|
|
|
|
TEST_STATUS(status, U_INVALID_STATE_ERROR);
|
|
|
|
|
|
|
|
/* Doing ICU operations should cause allocations to come through our test mutexes */
|
|
|
|
gBlockCount = 0;
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
rb = ures_open(NULL, "es", &status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
TEST_ASSERT(gTotalMutexesInitialized > 0);
|
|
|
|
TEST_ASSERT(gTotalMutexesActive > 0);
|
|
|
|
|
|
|
|
ures_close(rb);
|
|
|
|
|
|
|
|
/* Cleanup should destroy all of the mutexes. */
|
|
|
|
u_cleanup();
|
2003-08-16 02:17:56 +00:00
|
|
|
u_setDataDirectory(icuDataDir);
|
2003-11-26 03:09:51 +00:00
|
|
|
utrace_setFunctions(traceContext, traceEntryFunc, traceExitFunc, traceDataFunc);
|
|
|
|
utrace_setLevel(traceLevel);
|
2003-08-08 16:23:38 +00:00
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
TEST_ASSERT(gTotalMutexesInitialized > 0);
|
|
|
|
TEST_ASSERT(gTotalMutexesActive == 0);
|
2003-08-05 01:25:54 +00:00
|
|
|
|
|
|
|
|
2003-08-08 16:23:38 +00:00
|
|
|
/* Additional ICU operations should no longer use our dummy test mutexes */
|
|
|
|
gTotalMutexesInitialized = 0;
|
|
|
|
gTotalMutexesActive = 0;
|
|
|
|
u_init(&status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
rb = ures_open(NULL, "fr", &status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
TEST_ASSERT(gTotalMutexesInitialized == 0);
|
|
|
|
TEST_ASSERT(gTotalMutexesActive == 0);
|
|
|
|
|
|
|
|
ures_close(rb);
|
2003-08-16 02:17:56 +00:00
|
|
|
free(icuDataDir);
|
2004-05-11 20:23:54 +00:00
|
|
|
|
|
|
|
if(gMutexFailures) {
|
|
|
|
log_info("Note: these failures may be caused by ICU failing to initialize/uninitialize properly.\n");
|
2004-05-18 01:52:57 +00:00
|
|
|
log_verbose("Check for prior tests which may not have closed all open resources. See the internal function ures_flushCache()\n");
|
2004-05-11 20:23:54 +00:00
|
|
|
}
|
2003-08-05 01:25:54 +00:00
|
|
|
}
|
|
|
|
|
2003-08-08 23:56:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test Atomic Increment & Decrement Functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
int gIncCount = 0;
|
|
|
|
int gDecCount = 0;
|
|
|
|
const void *gIncDecContext;
|
|
|
|
|
|
|
|
|
2004-07-16 06:52:44 +00:00
|
|
|
static int32_t U_CALLCONV myIncFunc(const void *context, int32_t *p) {
|
2003-08-08 23:56:02 +00:00
|
|
|
int32_t retVal;
|
|
|
|
TEST_ASSERT(context == gIncDecContext);
|
|
|
|
gIncCount++;
|
|
|
|
retVal = ++(*p);
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2004-07-16 06:52:44 +00:00
|
|
|
static int32_t U_CALLCONV myDecFunc(const void *context, int32_t *p) {
|
2003-08-08 23:56:02 +00:00
|
|
|
int32_t retVal;
|
|
|
|
TEST_ASSERT(context == gIncDecContext);
|
|
|
|
gDecCount++;
|
|
|
|
retVal = --(*p);
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void TestIncDecFunctions() {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2003-10-06 22:32:36 +00:00
|
|
|
int32_t t = 1; /* random value to make sure that Inc/dec works */
|
2003-08-16 02:17:56 +00:00
|
|
|
char *dataDir;
|
2003-08-08 23:56:02 +00:00
|
|
|
|
2003-11-26 03:09:51 +00:00
|
|
|
UTraceEntry *traceEntryFunc; /* Tracing function ptrs. We need to save */
|
|
|
|
UTraceExit *traceExitFunc; /* and restore them across calls to */
|
|
|
|
UTraceData *traceDataFunc; /* u_cleanup() that we make in this test. */
|
|
|
|
const void *traceContext;
|
|
|
|
int32_t traceLevel;
|
2003-08-08 23:56:02 +00:00
|
|
|
|
|
|
|
/* Can not set mutex functions if ICU is already initialized */
|
|
|
|
u_setAtomicIncDecFunctions(&gIncDecContext, myIncFunc, myDecFunc, &status);
|
|
|
|
TEST_STATUS(status, U_INVALID_STATE_ERROR);
|
|
|
|
|
|
|
|
/* Un-initialize ICU */
|
2003-08-16 02:17:56 +00:00
|
|
|
dataDir = safeGetICUDataDirectory();
|
2003-11-26 03:09:51 +00:00
|
|
|
utrace_getFunctions(&traceContext, &traceEntryFunc, &traceExitFunc, &traceDataFunc);
|
|
|
|
traceLevel = utrace_getLevel();
|
2003-08-08 23:56:02 +00:00
|
|
|
u_cleanup();
|
2003-11-26 03:09:51 +00:00
|
|
|
utrace_setFunctions(traceContext, traceEntryFunc, traceExitFunc, traceDataFunc);
|
|
|
|
utrace_setLevel(traceLevel);
|
2003-08-08 23:56:02 +00:00
|
|
|
|
|
|
|
/* Can not set functions with NULL values */
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
u_setAtomicIncDecFunctions(&gIncDecContext, NULL, myDecFunc, &status);
|
|
|
|
TEST_STATUS(status, U_ILLEGAL_ARGUMENT_ERROR);
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
u_setAtomicIncDecFunctions(&gIncDecContext, myIncFunc, NULL, &status);
|
|
|
|
TEST_STATUS(status, U_ILLEGAL_ARGUMENT_ERROR);
|
|
|
|
|
|
|
|
/* u_setIncDecFunctions() should work with null or non-null context pointer */
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
u_setAtomicIncDecFunctions(NULL, myIncFunc, myDecFunc, &status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
u_setAtomicIncDecFunctions(&gIncDecContext, myIncFunc, myDecFunc, &status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
|
|
|
|
|
|
|
|
/* After reinitializing ICU, we should not be able to set the inc/dec funcs again. */
|
|
|
|
status = U_ZERO_ERROR;
|
2003-08-16 02:17:56 +00:00
|
|
|
u_setDataDirectory(dataDir);
|
2003-08-08 23:56:02 +00:00
|
|
|
u_init(&status);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
u_setAtomicIncDecFunctions(&gIncDecContext, myIncFunc, myDecFunc, &status);
|
|
|
|
TEST_STATUS(status, U_INVALID_STATE_ERROR);
|
|
|
|
|
|
|
|
/* Doing ICU operations should cause our functions to be called */
|
|
|
|
gIncCount = 0;
|
|
|
|
gDecCount = 0;
|
|
|
|
umtx_atomic_inc(&t);
|
2003-10-06 22:32:36 +00:00
|
|
|
TEST_ASSERT(t == 2);
|
2003-08-08 23:56:02 +00:00
|
|
|
umtx_atomic_dec(&t);
|
2003-10-06 22:32:36 +00:00
|
|
|
TEST_ASSERT(t == 1);
|
2003-08-08 23:56:02 +00:00
|
|
|
TEST_ASSERT(gIncCount > 0);
|
|
|
|
TEST_ASSERT(gDecCount > 0);
|
|
|
|
|
|
|
|
|
|
|
|
/* Cleanup should cancel use of our inc/dec functions. */
|
|
|
|
/* Additional ICU operations should not use them */
|
|
|
|
u_cleanup();
|
2003-11-26 03:09:51 +00:00
|
|
|
utrace_setFunctions(traceContext, traceEntryFunc, traceExitFunc, traceDataFunc);
|
|
|
|
utrace_setLevel(traceLevel);
|
2003-08-08 23:56:02 +00:00
|
|
|
gIncCount = 0;
|
|
|
|
gDecCount = 0;
|
|
|
|
status = U_ZERO_ERROR;
|
2003-08-16 02:17:56 +00:00
|
|
|
u_setDataDirectory(dataDir);
|
2003-08-08 23:56:02 +00:00
|
|
|
u_init(&status);
|
|
|
|
TEST_ASSERT(gIncCount == 0);
|
|
|
|
TEST_ASSERT(gDecCount == 0);
|
|
|
|
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
umtx_atomic_inc(&t);
|
|
|
|
umtx_atomic_dec(&t);
|
|
|
|
TEST_STATUS(status, U_ZERO_ERROR);
|
|
|
|
TEST_ASSERT(gIncCount == 0);
|
|
|
|
TEST_ASSERT(gDecCount == 0);
|
2003-10-06 22:32:36 +00:00
|
|
|
|
|
|
|
free(dataDir);
|
2003-08-08 23:56:02 +00:00
|
|
|
}
|
|
|
|
|