ICU-4390 tests for api

X-SVN-Rev: 17424
This commit is contained in:
Ram Viswanadha 2005-03-31 00:19:21 +00:00
parent d5c19b276a
commit 3fdcb27040
3 changed files with 223 additions and 1 deletions

View File

@ -105,7 +105,8 @@ static void TestEBCDICSwapLFNL(void);
static void TestConvertEx(void);
static void TestConvertAlgorithmic(void);
void TestDefaultConverterError(void); /* defined in cctest.c */
static void TestToUCountPending(void);
static void TestFromUCountPending(void);
void addTestConvert(TestNode** root);
void addTestConvert(TestNode** root)
@ -125,6 +126,8 @@ void addTestConvert(TestNode** root)
addTest(root, &TestConvertEx, "tsconv/ccapitst/TestConvertEx");
addTest(root, &TestConvertAlgorithmic, "tsconv/ccapitst/TestConvertAlgorithmic");
addTest(root, &TestDefaultConverterError, "tsconv/ccapitst/TestDefaultConverterError");
addTest(root, &TestToUCountPending, "tsconv/ccapitst/TestToUCountPending");
addTest(root, &TestFromUCountPending, "tsconv/ccapitst/TestFromUCountPending");
}
static void ListNames(void) {
@ -2784,3 +2787,204 @@ TestEBCDICSwapLFNL() {
testSwap(tests[i].name, tests[i].swap);
}
}
static const UVersionInfo ICU_34 = {3,4,0,0};
static void TestFromUCountPending(){
UErrorCode status = U_ZERO_ERROR;
/* const UChar expectedUnicode[] = { 0x20ac, 0x0005, 0x0006, 0x000b, 0xdbc4, 0xde34, 0xd84d, 0xdc56, 0xfffd}; */
static const struct {
UChar input[6];
int32_t len;
int32_t exp;
}fromUnicodeTests[] = {
/*m:n conversion*/
{{0xdbc4},1,1},
{{ 0xdbc4, 0xde34, 0xd84d},3,1},
{{ 0xdbc4, 0xde34, 0xd900},3,3},
};
int i;
UConverterFromUCallback *oldFromUAction= NULL;
UConverter* cnv = ucnv_openPackage(loadTestData(&status), "test3", &status);
if(U_FAILURE(status)){
log_err("Could not create converter for test3. Error: %s\n", u_errorName(status));
return;
}
for(i=0; i<LENGTHOF(fromUnicodeTests); ++i) {
char tgt[10];
char* target = tgt;
char* targetLimit = target + 10;
const UChar* source = fromUnicodeTests[i].input;
const UChar* sourceLimit = source + fromUnicodeTests[i].len;
int32_t len = 0;
ucnv_reset(cnv);
ucnv_fromUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, FALSE, &status);
len = ucnv_fromUCountPending(cnv, &status);
if(U_FAILURE(status)){
log_err("ucnv_fromUnicode call did not succeed. Error: %s\n", u_errorName(status));
status = U_ZERO_ERROR;
continue;
}
if(len != fromUnicodeTests[i].exp){
log_err("Did not get the expeced output for ucnv_fromUInputConsumed.\n");
}
}
status = U_ZERO_ERROR;
/* time bomb this test for now */
if(isICUVersionAtLeast(ICU_34)){
/*
\U00101234 -> x (<U101234> \x07 |0)
\U00050005 -> y (<U50005> \x01\x02\x08 |0)
\U00101234\U00050005\u0006006 -> z (<U101234>+<U50005>+<U60006> \x07+\x00+\x01\x02\x0f+\x09 |0)
*/
UChar head[] = {0xDBC4,0xDE34,0x0000};/* \U00101234\U00050005 */
UChar middle[] = {0xD900,0x0000};
UChar tail[] = {0xDC05,0x0006,0x0000};/* \U00080008 */
char tgt[10];
char* target = tgt;
char* targetLimit = target + 2;
const UChar* source = head;
const UChar* sourceLimit = source + u_strlen(head);
int32_t len = 0;
ucnv_reset(cnv);
ucnv_fromUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, FALSE, &status);
len = ucnv_fromUCountPending(cnv, &status);
if(U_FAILURE(status)){
log_err("ucnv_fromUnicode call did not succeed. Error: %s\n", u_errorName(status));
status = U_ZERO_ERROR;
}
if(len!=2){
log_err("ucnv_fromUInputHeld did not return correct length for head\n");
}
source = middle;
sourceLimit = source + u_strlen(middle);
ucnv_fromUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, FALSE, &status);
len = ucnv_fromUCountPending(cnv, &status);
if(U_FAILURE(status)){
log_err("ucnv_fromUnicode call did not succeed. Error: %s\n", u_errorName(status));
status = U_ZERO_ERROR;
}
if(len!=4){
log_err("ucnv_fromUInputHeld did not return correct length for middle\n");
}
source = tail;
sourceLimit = source + u_strlen(tail);
ucnv_fromUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, FALSE, &status);
if(status == U_BUFFER_OVERFLOW_ERROR){
status = U_ZERO_ERROR;
}
if(U_FAILURE(status)){
log_err("ucnv_fromUnicode call did not succeed. Error: %s\n", u_errorName(status));
}
len = ucnv_fromUCountPending(cnv, &status);
if(U_FAILURE(status)){
log_err("ucnv_fromUInputHeld call did not succeed. Error: %s\n", u_errorName(status));
}
if(len!=2){
log_err("ucnv_fromUInputHeld did not return correct length for tail\n");
}
}
}
static void
TestToUCountPending(){
UErrorCode status = U_ZERO_ERROR;
static const struct {
char input[6];
int32_t len;
int32_t exp;
}toUnicodeTests[] = {
/*m:n conversion*/
{{0x05, 0x01, 0x02},3,3},
{{0x01, 0x02},2,2},
{{0x07, 0x00, 0x01, 0x02},4,4},
};
int i;
UConverterToUCallback *oldToUAction= NULL;
UConverter* cnv = ucnv_openPackage(loadTestData(&status), "test3", &status);
if(U_FAILURE(status)){
log_err("Could not create converter for test3. Error: %s\n", u_errorName(status));
return;
}
ucnv_setToUCallBack(cnv, UCNV_TO_U_CALLBACK_STOP, NULL, oldToUAction, NULL, &status);
for(i=0; i<LENGTHOF(toUnicodeTests); ++i) {
UChar tgt[10];
UChar* target = tgt;
UChar* targetLimit = target + 20;
const char* source = toUnicodeTests[i].input;
const char* sourceLimit = source + toUnicodeTests[i].len;
int32_t len = 0;
ucnv_reset(cnv);
ucnv_toUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, FALSE, &status);
len = ucnv_toUCountPending(cnv,&status);
if(U_FAILURE(status)){
log_err("ucnv_toUnicode call did not succeed. Error: %s\n", u_errorName(status));
status = U_ZERO_ERROR;
continue;
}
if(len != toUnicodeTests[i].exp){
log_err("Did not get the expeced output for ucnv_toUInputConsumed.\n");
}
}
status = U_ZERO_ERROR;
ucnv_close(cnv);
{
char head[] = { 0x01, 0x02, 0x03, 0x0a , 0x00};
char mid[] = { 0x01, 0x02, 0x03, 0x0b, 0x00 };
char tail[] = { 0x01, 0x02, 0x03, 0x0d, 0x00 };
/*
0x01, 0x02, 0x03, 0x0a -> x (<U23456> \x01\x02\x03\x0a |0)
0x01, 0x02, 0x03, 0x0b -> y (<U000b> \x01\x02\x03\x0b |0)
0x01, 0x02, 0x03, 0x0a + 0x01, 0x02, 0x03, 0x0b + 0x01, 0x0e -> z <U80008> (\x01\x02\x03\x0a+\x01\x02\x03\x0b+\x01\x0e)
*/
UChar tgt[10];
UChar* target = tgt;
UChar* targetLimit = target + 1;
const char* source = head;
const char* sourceLimit = source + strlen(head);
int32_t len = 0;
cnv = ucnv_openPackage(loadTestData(&status), "test4", &status);
if(U_FAILURE(status)){
log_err("Could not create converter for test3. Error: %s\n", u_errorName(status));
return;
}
ucnv_setToUCallBack(cnv, UCNV_TO_U_CALLBACK_STOP, NULL, oldToUAction, NULL, &status);
ucnv_toUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, FALSE, &status);
len = ucnv_toUCountPending(cnv,&status);
if(U_FAILURE(status)){
log_err("ucnv_toUnicode call did not succeed. Error: %s\n", u_errorName(status));
}
if(len != 4){
log_err("Did not get the expected len for head.\n");
}
source=mid;
sourceLimit = source+strlen(mid);
ucnv_toUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, FALSE, &status);
len = ucnv_toUCountPending(cnv,&status);
if(U_FAILURE(status)){
log_err("ucnv_toUnicode call did not succeed. Error: %s\n", u_errorName(status));
}
if(len != 8){
log_err("Did not get the expected len for mid.\n");
}
source=tail;
sourceLimit = source+strlen(tail);
targetLimit = target;
ucnv_toUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, FALSE, &status);
if(status == U_BUFFER_OVERFLOW_ERROR){
status = U_ZERO_ERROR;
}
len = ucnv_toUCountPending(cnv,&status);
if(U_FAILURE(status)){
log_err("ucnv_toUnicode call did not succeed. Error: %s\n", u_errorName(status));
}
if(len != 4){
log_err("Did not get the expected len for tail.\n");
}
ucnv_close(cnv);
}
}

View File

@ -24,6 +24,7 @@
#include "cintltst.h"
#include "umutex.h"
#include "uassert.h"
#include "cmemory.h"
#include "unicode/uchar.h"
#include "unicode/ustring.h"
#include "unicode/ucnv.h"
@ -690,5 +691,15 @@ U_CFUNC UBool assertEquals(const char* message, const char* expected,
#endif
return TRUE;
}
/*--------------------------------------------------------------------
* Time bomb - allows temporary behavior that expires at a given
* release
*--------------------------------------------------------------------
*/
U_CFUNC UBool isICUVersionAtLeast(const UVersionInfo x) {
UVersionInfo v;
u_getVersion(v);
return (uprv_memcmp(v, x, U_MAX_VERSION_LENGTH) >= 0);
}
#endif

View File

@ -98,4 +98,11 @@ U_CFUNC UBool assertTrue(const char* msg, int condition);
U_CFUNC UBool assertEquals(const char* msg, const char* expectedString,
const char* actualString);
/**
* Time bomb - allows temporary behavior that expires at a given
* release
*
*/
U_CFUNC UBool isICUVersionAtLeast(const UVersionInfo x);
#endif