2003-08-05 01:25:54 +00:00
|
|
|
/********************************************************************
|
|
|
|
* COPYRIGHT:
|
2015-02-27 23:01:33 +00:00
|
|
|
* Copyright (c) 2003-2015, International Business Machines Corporation and
|
2003-08-05 01:25:54 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
********************************************************************/
|
|
|
|
/*
|
|
|
|
* File hpmufn.c
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2004-09-23 00:24:36 +00:00
|
|
|
#include "unicode/putil.h"
|
2003-08-05 01:25:54 +00:00
|
|
|
#include "unicode/uclean.h"
|
|
|
|
#include "unicode/uchar.h"
|
|
|
|
#include "unicode/ures.h"
|
|
|
|
#include "cintltst.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);
|
|
|
|
|
|
|
|
void addHeapMutexTest(TestNode **root);
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
addHeapMutexTest(TestNode** root)
|
|
|
|
{
|
2006-10-23 03:59:00 +00:00
|
|
|
addTest(root, &TestHeapFunctions, "hpmufn/TestHeapFunctions" );
|
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) { \
|
2009-06-15 03:06:50 +00:00
|
|
|
log_err_status(status, "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;
|
2004-12-22 07:33:15 +00:00
|
|
|
UVersionInfo unicodeVersion = {0,0,0,0};
|
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
|
|
|
|
|
|
|
|
2005-08-17 17:25:38 +00:00
|
|
|
/* Verify that ICU can be cleaned up and reinitialized successfully.
|
|
|
|
* Failure here usually means that some ICU service didn't clean up successfully,
|
|
|
|
* probably because some earlier test accidently left something open. */
|
2007-12-07 10:49:34 +00:00
|
|
|
ctest_resetICU();
|
2005-08-17 17:25:38 +00:00
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
/* Un-initialize ICU */
|
|
|
|
u_cleanup();
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
|
2015-02-27 23:01:33 +00:00
|
|
|
/* After reinitializing ICU, we can not set the memory funcs again. */
|
2003-08-05 01:25:54 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
/* 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. */
|
2007-12-07 10:49:34 +00:00
|
|
|
ctest_resetICU();
|
2004-12-24 07:53:41 +00:00
|
|
|
u_getUnicodeVersion(unicodeVersion);
|
|
|
|
if (unicodeVersion[0] <= 0) {
|
|
|
|
log_err("Properties doesn't reinitialize without u_init.\n");
|
|
|
|
}
|
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);
|
2007-12-07 10:49:34 +00:00
|
|
|
|
|
|
|
ctest_resetICU();
|
2003-08-08 16:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|