ICU-2403 trace tests added, trace API doc comments tweaked.

X-SVN-Rev: 13679
This commit is contained in:
Andy Heninger 2003-11-12 07:00:38 +00:00
parent 61a8c14fc2
commit d1a68204b3
7 changed files with 246 additions and 12 deletions

View File

@ -1076,6 +1076,10 @@ InputPath=.\unicode\utrace.h
# End Source File
# Begin Source File
SOURCE=.\utracimp.h
# End Source File
# Begin Source File
SOURCE=.\unicode\utypes.h
!IF "$(CFG)" == "common - Win32 Release"
@ -1851,10 +1855,6 @@ InputPath=.\unicode\uidna.h
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\utracimp.h
# End Source File
# End Group
# Begin Group "locales & resources"

View File

@ -32,11 +32,17 @@ U_CDECL_BEGIN
*/
enum UTraceLevel {
/** Disable all tracing */
UTRACE_OFF=-1,
/** Trace error conditions only */
UTRACE_ERROR=0,
/** Trace errors and warnings */
UTRACE_WARNING=3,
/** Trace opens and closes of ICU services */
UTRACE_OPEN_CLOSE=5,
/** Trace an intermediate number of ICU operations */
UTRACE_INFO=7,
/** Trace the maximum number of ICU operations */
UTRACE_VERBOSE=9
};
typedef enum UTraceLevel UTraceLevel;
@ -73,11 +79,17 @@ UTraceEntry(const void *context, int32_t fnNumber);
* Type signature for the trace function to be called when exiting from a function.
* @param context value supplied at the time the trace functions are set.
* @param fnNumber Enum value indicating the ICU function being exited.
* @param retType the UTraceExitVal indicating the number and types of
* variable arguments that follow.
* @param argType values returned by the function being traced, may include
* one or both of the function's return value and a
* UErrorCode parameter value.
* @see UTraceExitVal
* @draft ICU 2.8
*/
typedef void U_CALLCONV
UTraceExit(const void *context, int32_t fnNumber,
int32_t retType, va_list args);
int32_t argType, va_list args);
/**
* Type signature for the trace function to be called from within an ICU function
@ -94,7 +106,10 @@ UTraceData(const void *context, int32_t fnNumber, int32_t level,
* Set ICU Tracing functions. Installs application-provided tracing
* functions into ICU. After doing this, subsequent ICU operations
* will call back to the installed functions, providing a trace
* of the use of ICU.
* of the use of ICU. Passing a NULL pointer for a tracing function
* is allowed, and inhibits tracing action at points where that function
* would be called.
*
* @param context an uninterpretted pointer. Whatever is passed in
* here will in turn be passed to each of the tracing
* functions UTraceEntry, UTraceExit and UTraceData.
@ -259,11 +274,17 @@ utrace_format(char *outBuf, int32_t capacity,
* @draft ICU 2.8
*/
enum UTraceExitVal {
/** The traced function returns no value */
UTRACE_EXITV_NONE = 0,
/** The traced function returns an int32_t, or compatible, type. */
UTRACE_EXITV_I32 = 1,
/** The traced function returns a pointer */
UTRACE_EXITV_PTR = 2,
/** The traced function returns a UBool */
UTRACE_EXITV_BOOL = 3,
/** Mask to extract the return type values from a UTraceExitVal */
UTRACE_EXITV_MASK = 0xf,
/** Bit indicating that the traced function includes a UErrorCode parameter */
UTRACE_EXITV_STATUS = 0x10
};
typedef enum UTraceExitVal UTraceExitVal;

View File

@ -134,7 +134,7 @@ static void outputUString(const UChar *s, int32_t len,
for (i=0; i<len || len==-1; i++) {
c = s[i];
outputHexBytes(c, 2, outBuf, outIx, capacity);
outputHexBytes(c, 4, outBuf, outIx, capacity);
outputChar(' ', outBuf, outIx, capacity, indent);
if (len == -1 && c==0) {
break;

View File

@ -15,9 +15,6 @@
*
* Internal header for ICU tracing/logging.
*
* This is currently a design document.
* Latest Version: http://oss.software.ibm.com/cvs/icu/~checkout~/icuhtml/design/utrace.h
* CVS: http://oss.software.ibm.com/cvs/icu/icuhtml/design/utrace.h
*
* Various notes:
* - using a trace level variable to only call trace functions
@ -38,8 +35,6 @@
* va_list should be portable because printf() would fail otherwise!
* - Should enum values like UTraceLevel be passed into int32_t-type arguments,
* or should enum types be used?
* We use enum types in all public APIs, so they should be safe and more
* descriptive/type-safe. (Search case-insensitive for "level".)
*/
#ifndef __UTRACIMP_H__

View File

@ -35,6 +35,8 @@ void addUSetTest(TestNode** root);
void addUStringPrepTest(TestNode** root);
void addIDNATest(TestNode** root);
void addHeapMutexTest(TestNode **root);
void addUTraceTest(TestNode** root);
void addAllTests(TestNode** root)
{
@ -67,6 +69,7 @@ void addAllTests(TestNode** root)
addIDNATest(root);
#endif
addHeapMutexTest(root);
addUTraceTest(root);
}

View File

@ -540,6 +540,10 @@ SOURCE=.\mstrmtst.c
SOURCE=.\putiltst.c
# End Source File
# Begin Source File
SOURCE=.\tracetst.c
# End Source File
# End Group
# Begin Group "normalization"

View File

@ -0,0 +1,211 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 2003, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/*
* File tracetst.c
*
*/
#include "unicode/utypes.h"
#include "unicode/utrace.h"
#include "utracimp.h"
#include "unicode/uclean.h"
#include "unicode/uchar.h"
#include "unicode/ures.h"
#include "cintltst.h"
#include <stdlib.h>
#include <string.h>
static void TestTraceAPI(void);
void
addUTraceTest(TestNode** root)
{
addTest(root, &TestTraceAPI, "tsutil/TraceTest/TestTraceAPI" );
}
/*
* Macro for assert style tests.
*/
#define TEST_ASSERT(expr) \
if (!(expr)) { \
log_err("FAILED Assertion \"" #expr "\" at %s:%d.\n", __FILE__, __LINE__); \
}
/*
* test_format. Helper function for checking the results of a formatting
* operation. Compares the actual results in buf parameter
* with the expected results.
*
* params: format: the format to be applied.
* bufCap buffer size to pass to formatter.
* indent: indent value to give to formatter
* result expected result. Do not truncate for short bufCap -
* this function will do it.
* line __LINE__, so we can report where failure happened.
* ... variable args to pass to formatter
*
*/
static void test_format(const char *format, int32_t bufCap, int32_t indent,
const char *result, int32_t line, ...) {
int32_t len;
va_list args;
char buf[300];
char expectedResult[300];
/* check that local buffers are big enough for the test case */
if (sizeof(buf) <= bufCap) {
log_err("At file:line %s:%d, requested bufCap too large.\n");
return;
}
if (strlen(result) >= sizeof(expectedResult)) {
log_err("At file:line %s:%d, expected result too large.\n");
return;
}
/* Guarantee a nul term if buffer is smaller than output */
strcpy(expectedResult, result);
expectedResult[bufCap] = 0;
/* run the formatter */
va_start(args, line);
memset(buf, 0, sizeof(buf));
len = utrace_format(buf, bufCap, indent, format, args);
/* Check results. */
if (strcmp(expectedResult, buf) != 0) {
log_err("At file:line %s:%d Expected \"%s\", got \"%s\" \n",
__FILE__, line, expectedResult, buf);
}
va_end(args);
}
/*
* define trace functions for use in this test.
*/
void testTraceEntry(const void *context, int32_t fnNumber) {
}
void testTraceExit(const void *context, int32_t fnNumber,
int32_t argType, va_list args) {
}
void testTraceData(const void *context, int32_t fnNumber, int32_t level,
const char *fmt, va_list args) {
}
/*
* TestTraceAPI
*/
static void TestTraceAPI() {
UTraceEntry *originalTEntryFunc;
UTraceExit *originalTExitFunc;
UTraceData *originalTDataFunc;
const void *originalTContext;
/*
* Save the original tracing state so that we can restore it after the test.
*/
utrace_getFunctions(&originalTContext, &originalTEntryFunc, &originalTExitFunc,
&originalTDataFunc);
/* verify that set/get of tracing functions returns what was set. */
{
UTraceEntry *e;
UTraceExit *x;
UTraceData *d;
const void *context;
const void *newContext = (const char *)originalTContext + 1;
TEST_ASSERT(originalTEntryFunc != testTraceEntry);
TEST_ASSERT(originalTExitFunc != testTraceExit);
TEST_ASSERT(originalTDataFunc != testTraceData);
utrace_setFunctions(newContext, testTraceEntry, testTraceExit, testTraceData);
utrace_getFunctions(&context, &e, &x, &d);
TEST_ASSERT(e == testTraceEntry);
TEST_ASSERT(x == testTraceExit);
TEST_ASSERT(d == testTraceData);
TEST_ASSERT(context == newContext);
}
/* verify that set/get level work as a pair, and that the level
* identifiers all exist.
*/
{
int32_t level;
utrace_setLevel(UTRACE_OFF);
level = utrace_getLevel();
TEST_ASSERT(level==UTRACE_OFF);
utrace_setLevel(UTRACE_VERBOSE);
level = utrace_getLevel();
TEST_ASSERT(level==UTRACE_VERBOSE);
utrace_setLevel(UTRACE_ERROR);
utrace_setLevel(UTRACE_WARNING);
utrace_setLevel(UTRACE_OPEN_CLOSE);
utrace_setLevel(UTRACE_INFO);
}
/*
* trace data formatter operation.
*/
{
UChar s1[] = {0x41fe, 0x42, 0x43, 00};
const char *a1[] = {"s1", "s2", "s3"};
void *ptr;
test_format("hello, world", 50, 0, "hello, world", __LINE__);
test_format("hello, world", 50, 4, " hello, world", __LINE__);
test_format("hello, world", 3, 0, "hello, world", __LINE__);
test_format("a character %c", 50, 0, "a character x", __LINE__, 'x');
test_format("a string %s ", 50, 0, "a string hello ", __LINE__, "hello");
test_format("uchars %S ", 50, 0, "uchars 41fe 0042 0043 0000 ", __LINE__, s1, -1);
test_format("uchars %S ", 50, 0, "uchars 41fe 0042 ", __LINE__, s1, 2);
test_format("a byte %b--", 50, 0, "a byte dd--", __LINE__, 0xdd);
test_format("a 16 bit val %h", 50, 0, "a 16 bit val 1234", __LINE__, 0x1234);
test_format("a 32 bit val %d...", 50, 0, "a 32 bit val 6789abcd...", __LINE__, 0x6789abcd);
test_format("a 64 bit val %l", 50, 0, "a 64 bit val 123456780abcdef0"
, __LINE__, 0x123456780abcdef0);
if (sizeof(ptr) == 4) {
ptr = (void *)0xdeadbeef;
test_format("a 32 bit ptr %p", 50, 0, "a 32 bit ptr deadbeef", __LINE__, ptr);
} else if (sizeof(void *) == 8) {
ptr = (void *)0x1000200030004000;
test_format("a 64 bit ptr %p", 50, 0, "a 64 bit ptr 1000200030004000", __LINE__, ptr);
} else {
TEST_ASSERT(FALSE);
/* TODO: others? */
}
test_format("%vc", 100, 0, "abc[ffffffff]", __LINE__, "abc", -1);
test_format("%vs", 100, 0, "s1\ns2\n[00000002]", __LINE__, a1, 2);
test_format("%vs", 100, 4, " s1\n s2\n [00000002]", __LINE__, a1, 2);
test_format("%vb", 100, 0, "41 42 43 [00000003]", __LINE__, "ABC", 3);
}
}