/* ******************************************************************************* * Copyright (C) 1997-2000, International Business Machines * Corporation and others. All Rights Reserved. ******************************************************************************* * Date Name Description * 06/23/00 aliu Creation. ******************************************************************************* */ #include "unicode/utrans.h" #include "cmemory.h" #include "cstring.h" #include "filestrm.h" #include "cintltst.h" #include "unicode/ustring.h" #include #define TEST(x) addTest(root, &x, "utrans/" # x) void TestAPI(); void TestSimpleRules(); void TestFilter(); void TestOpenInverse(); void TestClone(); void TestRegisterUnregister(); static void _expectRules(const char*, const char*, const char*); static void _expect(const UTransliterator* trans, const char* cfrom, const char* cto); void addUTransTest(TestNode** root) { TEST(TestAPI); TEST(TestSimpleRules); TEST(TestFilter); TEST(TestOpenInverse); TEST(TestClone); TEST(TestRegisterUnregister); } void TestAPI() { enum { BUF_CAP = 128 }; char buf[BUF_CAP], buf2[BUF_CAP]; UErrorCode status = U_ZERO_ERROR; UTransliterator* trans = NULL; int32_t i, n; /* Test getAvailableIDs */ n = utrans_countAvailableIDs(); if (n < 1) { log_err("FAIL: utrans_countAvailableIDs() returned %d\n", n); } else { log_verbose("System ID count: %d\n", n); } for (i=0; ib*/ /* Make sure it doesn't exist */ t1=utrans_open("TestA-TestB", UTRANS_FORWARD, &status); if(t1 != NULL || U_SUCCESS(status)) { log_err("FAIL: TestA-TestB already registered\n"); return; } status=U_ZERO_ERROR; /* Check inverse too */ inverse1=utrans_open("TestA-TestB", UTRANS_REVERSE, &status); if(inverse1 != NULL || U_SUCCESS(status)) { log_err("FAIL: TestA-TestB already registered\n"); return; } status=U_ZERO_ERROR; /* Create it */ rules=utrans_openRules("TestA-TestB", rule, 4, UTRANS_FORWARD, NULL, &status); if(U_FAILURE(status)){ log_err("FAIL: utrans_openRules(a<>B) failed with error=%s\n", myErrorName(status)); return; } status=U_ZERO_ERROR; /* Register it */ utrans_register(rules, &status); if(U_FAILURE(status)){ log_err("FAIL: utrans_register failed with error=%s\n", myErrorName(status)); return; } status=U_ZERO_ERROR; /* Now check again -- should exist now*/ t1= utrans_open("TestA-TestB", UTRANS_FORWARD, &status); if(U_FAILURE(status) || t1 == NULL){ log_err("FAIL: TestA-TestB not registered\n"); return; } utrans_close(t1); /*unregister the instance*/ status=U_ZERO_ERROR; utrans_unregister("TestA-TestB"); /* now Make sure it doesn't exist */ t1=utrans_open("TestA-TestB", UTRANS_FORWARD, &status); if(U_SUCCESS(status) || t1 != NULL) { log_err("FAIL: TestA-TestB isn't unregistered\n"); return; } utrans_close(t1); utrans_close(inverse1); } void TestSimpleRules() { /* Test rules */ /* Example: rules 1. ab>x|y * 2. yc>z * * []|eabcd start - no match, copy e to tranlated buffer * [e]|abcd match rule 1 - copy output & adjust cursor * [ex|y]cd match rule 2 - copy output & adjust cursor * [exz]|d no match, copy d to transliterated buffer * [exzd]| done */ _expectRules("ab>x|y;" "yc>z", "eabcd", "exzd"); /* Another set of rules: * 1. ab>x|yzacw * 2. za>q * 3. qc>r * 4. cw>n * * []|ab Rule 1 * [x|yzacw] No match * [xy|zacw] Rule 2 * [xyq|cw] Rule 4 * [xyqn]| Done */ _expectRules("ab>x|yzacw;" "za>q;" "qc>r;" "cw>n", "ab", "xyqn"); /* Test categories */ _expectRules("$dummy=" "\\uE100" ";" /* careful here with E100 */ "$vowel=[aeiouAEIOU];" "$lu=[:Lu:];" "$vowel } $lu > '!';" "$vowel > '&';" "'!' { $lu > '^';" "$lu > '*';" "a > ERROR", "abcdefgABCDEFGU", "&bcd&fg!^**!^*&"); } void TestFilter() { UErrorCode status = U_ZERO_ERROR; UChar filt[128]; UChar buf[128]; UChar exp[128]; char cbuf[128]; int32_t limit; const char* DATA[] = { "[^c]", /* Filter out 'c' */ "abcde", "\\u0061\\u0062c\\u0064\\u0065", "", /* No filter */ "abcde", "\\u0061\\u0062\\u0063\\u0064\\u0065" }; int32_t DATA_length = sizeof(DATA) / sizeof(DATA[0]); int32_t i; UTransliterator* hex = utrans_open("Unicode-Hex", UTRANS_FORWARD, &status); if (hex == 0 || U_FAILURE(status)) { log_err("FAIL: utrans_open(Unicode-Hex) failed, error=%s\n", u_errorName(status)); goto exit; } for (i=0; i %s\n", DATA[i+1], DATA[i], cbuf); } else { log_err("FAIL: %s | %s -> %s, expected %s\n", DATA[i+1], DATA[i], cbuf, DATA[i+2]); } } exit: utrans_close(hex); } void _expectRules(const char* crules, const char* cfrom, const char* cto) { /* u_uastrcpy has no capacity param for the buffer -- so just * make all buffers way too big */ enum { CAP = 256 }; UChar rules[CAP]; UTransliterator *trans; UErrorCode status = U_ZERO_ERROR; UParseError parseErr; u_uastrcpy(rules, crules); trans = utrans_openRules(crules /*use rules as ID*/, rules, -1, UTRANS_FORWARD, &parseErr, &status); if (U_FAILURE(status)) { utrans_close(trans); log_err("FAIL: utrans_openRules(%s) failed, error=%s\n", crules, u_errorName(status)); return; } _expect(trans, cfrom, cto); utrans_close(trans); } void _expect(const UTransliterator* trans, const char* cfrom, const char* cto) { /* u_uastrcpy has no capacity param for the buffer -- so just * make all buffers way too big */ enum { CAP = 256 }; UChar from[CAP]; UChar to[CAP]; UChar buf[CAP]; char id[CAP]; UErrorCode status = U_ZERO_ERROR; int32_t limit; UTransPosition pos; u_uastrcpy(from, cfrom); u_uastrcpy(to, cto); utrans_getID(trans, id, CAP); /* utrans_transUChars() */ u_strcpy(buf, from); limit = u_strlen(buf); utrans_transUChars(trans, buf, NULL, CAP, 0, &limit, &status); if (U_FAILURE(status)) { log_err("FAIL: utrans_transUChars() failed, error=%s\n", u_errorName(status)); return; } if (0 == u_strcmp(buf, to)) { log_verbose("Ok: utrans_transUChars(%s) x %s -> %s\n", id, cfrom, cto); } else { char actual[CAP]; u_austrcpy(actual, buf); log_err("FAIL: utrans_transUChars(%s) x %s -> %s, expected %s\n", id, cfrom, actual, cto); } /* utrans_transIncrementalUChars() */ u_strcpy(buf, from); pos.start = pos.contextStart = 0; pos.limit = pos.contextLimit = u_strlen(buf); utrans_transIncrementalUChars(trans, buf, NULL, CAP, &pos, &status); utrans_transUChars(trans, buf, NULL, CAP, pos.start, &pos.limit, &status); if (U_FAILURE(status)) { log_err("FAIL: utrans_transIncrementalUChars() failed, error=%s\n", u_errorName(status)); return; } if (0 == u_strcmp(buf, to)) { log_verbose("Ok: utrans_transIncrementalUChars(%s) x %s -> %s\n", id, cfrom, cto); } else { char actual[CAP]; u_austrcpy(actual, buf); log_err("FAIL: utrans_transIncrementalUChars(%s) x %s -> %s, expected %s\n", id, cfrom, actual, cto); } }