/* ***************************************************************************************** * * * COPYRIGHT: * * (C) Copyright Taligent, Inc., 1996 * * (C) Copyright International Business Machines Corporation, 1999 * * Licensed Material - Program-Property of IBM - All Rights Reserved. * * US Government Users Restricted Rights - Use, duplication, or disclosure * * restricted by GSA ADP Schedule Contract with IBM Corp. * * * ***************************************************************************************** ******************************************************************************** * * File CAPITEST.C * * Modification History: * Name Description * Madhu Katragadda Ported for CAPI ********************************************************************************* *//* C API TEST For COLLATOR */ #include "utypes.h" #include "ucol.h" #include "uloc.h" #include "cintltst.h" #include "capitst.h" #include "ustring.h" #include #include void addCollAPITest(TestNode** root) { addTest(root, &TestProperty, "tscoll/capitst/TestProperty"); addTest(root, &TestRuleBasedColl, "tscoll/capitst/TestRuleBasedColl"); addTest(root, &TestCompare, "tscoll/capitst/TestCompare"); addTest(root, &TestSortKey, "tscoll/capitst/TestSortKey"); addTest(root, &TestHashCode, "tscoll/capitst/TestHashCode"); addTest(root, &TestElemIter, "tscoll/capitst/TestElemIter"); addTest(root, &TestGetAll, "tscoll/capitst/TestGetAll"); } static void doAssert(int condition, const char *message) { if (condition==0) { log_err("ERROR : %c\n", message); } } /* Collator Properties ucol_open, ucol_strcoll, getStrength/setStrength getDecomposition/setDecomposition, getDisplayName*/ void TestProperty() { UCollator *col; UChar *disName; int32_t len; UChar *source, *target; int32_t tempLength; UErrorCode status = ZERO_ERROR; log_verbose("The property tests begin : \n"); log_verbose("Test ucol_strcoll : \n"); col = ucol_open(NULL, &status); if (FAILURE(status)) { log_err("Default Collator creation failed.: %s\n", myErrorName(status)); return; } source=(UChar*)malloc(sizeof(UChar) * 12); target=(UChar*)malloc(sizeof(UChar) * 12); u_uastrcpy(source, "ab"); u_uastrcpy(target, "abc"); doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_LESS), "ab < abc comparison failed"); u_uastrcpy(source, "ab"); u_uastrcpy(target, "AB"); doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_LESS), "ab < AB comparison failed"); u_uastrcpy(source, "black-bird"); u_uastrcpy(target, "blackbird"); doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_GREATER), "black-bird > blackbird comparison failed"); u_uastrcpy(source, "black bird"); u_uastrcpy(target, "black-bird"); doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_LESS), "black bird < black-bird comparison failed"); u_uastrcpy(source, "Hello"); u_uastrcpy(target, "hello"); doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_GREATER), "Hello > hello comparison failed"); free(source); free(target); log_verbose("Test ucol_strcoll ends.\n"); log_verbose("testing ucol_getStrength() method ...\n"); doAssert( (ucol_getStrength(col) == UCOL_TERTIARY), "collation object has the wrong strength"); doAssert( (ucol_getStrength(col) != UCOL_PRIMARY), "collation object's strength is primary difference"); log_verbose("testing ucol_setStrength() method ...\n"); ucol_setStrength(col, UCOL_SECONDARY); doAssert( (ucol_getStrength(col) != UCOL_TERTIARY), "collation object's strength is secondary difference"); doAssert( (ucol_getStrength(col) != UCOL_PRIMARY), "collation object's strength is primary difference"); doAssert( (ucol_getStrength(col) == UCOL_SECONDARY), "collation object has the wrong strength"); log_verbose("testing ucol_setDecomposition() method ...\n"); ucol_setNormalization(col, UCOL_NO_NORMALIZATION); doAssert( (ucol_getNormalization(col) != UCOL_DECOMP_CAN_COMP_COMPAT), "collation object's normalization mode is Canonical decomposition followed by canonical composition"); doAssert( (ucol_getNormalization(col) != UCOL_DECOMP_CAN), "collation object's normalization mode is canonical decomposition"); doAssert( (ucol_getNormalization(col) == UCOL_NO_NORMALIZATION), "collation object has the wrong normalization mode"); log_verbose("Get display name for the default collation in German : \n"); len=ucol_getDisplayName("en_US", "de_DE", NULL, 0, &status); if(status==BUFFER_OVERFLOW_ERROR){ status=ZERO_ERROR; disName=(UChar*)malloc(sizeof(UChar) * (len+1)); ucol_getDisplayName("en_US", "de_DE", disName, len+1, &status); } if(FAILURE(status)){ log_err("ERROR: in getDisplayName: %s\n", myErrorName(status)); return; } log_verbose("the display name for default collation in german: %s\n", austrdup(disName) ); log_verbose("Default collation getDisplayName ended.\n"); free(disName); log_verbose("ucol_getRules() testing ...\n"); ucol_getRules(col, &tempLength); doAssert( tempLength != 0, "getRules() result incorrect" ); log_verbose("getRules tests end.\n"); ucol_close(col); log_verbose("open an collator for french locale"); col = ucol_open("fr_FR", &status); if (FAILURE(status)) { log_err("ERROR: Creating French collation failed.: %s\n", myErrorName(status)); return; } ucol_setStrength(col, UCOL_PRIMARY); log_verbose("testing ucol_getStrength() method again ...\n"); doAssert( (ucol_getStrength(col) != UCOL_TERTIARY), "collation object has the wrong strength"); doAssert( (ucol_getStrength(col) == UCOL_PRIMARY), "collation object's strength is not primary difference"); log_verbose("testing French ucol_setStrength() method ...\n"); ucol_setStrength(col, UCOL_TERTIARY); doAssert( (ucol_getStrength(col) == UCOL_TERTIARY), "collation object's strength is not tertiary difference"); doAssert( (ucol_getStrength(col) != UCOL_PRIMARY), "collation object's strength is primary difference"); doAssert( (ucol_getStrength(col) != UCOL_SECONDARY), "collation object's strength is secondary difference"); ucol_close(col); log_verbose("Get display name for the french collation in english : \n"); len=ucol_getDisplayName("fr_FR", "en_US", NULL, 0, &status); if(status==BUFFER_OVERFLOW_ERROR){ status=ZERO_ERROR; disName=(UChar*)malloc(sizeof(UChar) * (len+1)); ucol_getDisplayName("fr_FR", "en_US", disName, len+1, &status); } if(FAILURE(status)){ log_err("ERROR: in getDisplayName: %s\n", myErrorName(status)); return; } log_verbose("the display name for french collation in german: %s\n", austrdup(disName) ); log_verbose("Default collation getDisplayName ended.\n"); free(disName); } /* Test RuleBasedCollator and getRules*/ void TestRuleBasedColl() { UCollator *col1, *col2, *col3, *col4; UChar ruleset1[60]; UChar ruleset2[50]; const UChar *rule1, *rule2, *rule3, *rule4; int32_t tempLength; UErrorCode status = ZERO_ERROR; u_uastrcpy(ruleset1, "< a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E"); u_uastrcpy(ruleset2, "< a, A < b, B < c, C < d, D, e, E"); col1 = ucol_openRules(ruleset1, u_strlen(ruleset1), UCOL_DEFAULT_NORMALIZATION, UCOL_DEFAULT_STRENGTH, &status); if (FAILURE(status)) { log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status)); return; } else log_verbose("PASS: RuleBased Collator creation passed\n"); status = ZERO_ERROR; col2 = ucol_openRules(ruleset2, u_strlen(ruleset2), UCOL_DEFAULT_NORMALIZATION, UCOL_DEFAULT_STRENGTH, &status); if (FAILURE(status)) { log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status)); return; } else log_verbose("PASS: RuleBased Collator creation passed\n"); status = ZERO_ERROR; col3= ucol_open(NULL, &status); if (FAILURE(status)) { log_err("Default Collator creation failed.: %s\n", myErrorName(status)); return; } else log_verbose("PASS: Default Collator creation passed\n"); rule1 = ucol_getRules(col1, &tempLength); rule2 = ucol_getRules(col2, &tempLength); rule3 = ucol_getRules(col3, &tempLength); doAssert((u_strcmp(rule1, rule2) != 0), "Default collator getRules failed"); doAssert((u_strcmp(rule2, rule3) != 0), "Default collator getRules failed"); doAssert((u_strcmp(rule1, rule3) != 0), "Default collator getRules failed"); col4=ucol_openRules(rule2, u_strlen(rule2), UCOL_DEFAULT_NORMALIZATION, UCOL_DEFAULT_STRENGTH, &status); if (FAILURE(status)) { log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status)); return; } rule4= ucol_getRules(col4, &tempLength); doAssert((u_strcmp(rule2, rule4) == 0), "Default collator getRules failed"); ucol_close(col1); ucol_close(col2); ucol_close(col3); ucol_close(col4); } void TestCompare() { UErrorCode status = ZERO_ERROR; UCollator *col; UChar* test1; UChar* test2; log_verbose("The compare tests begin : \n"); status=ZERO_ERROR; col = ucol_open("en_US", &status); if(FAILURE(status)) { log_err("ucal_open() collation creation failed.: %s\n", myErrorName(status)); return; } test1=(UChar*)malloc(sizeof(UChar) * 6); test2=(UChar*)malloc(sizeof(UChar) * 6); u_uastrcpy(test1, "Abcda"); u_uastrcpy(test2, "abcda"); log_verbose("Use tertiary comparison level testing ....\n"); doAssert( (!ucol_equal(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" != \"abcda\" "); doAssert( (ucol_greater(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" >>> \"abcda\" "); doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" >>> \"abcda\""); ucol_setStrength(col, UCOL_SECONDARY); log_verbose("Use secondary comparison level testing ....\n"); doAssert( (ucol_equal(col, test1, u_strlen(test1), test2, u_strlen(test2) )), "Result should be \"Abcda\" == \"abcda\""); doAssert( (!ucol_greater(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\""); doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2) )), "Result should be \"Abcda\" == \"abcda\""); ucol_setStrength(col, UCOL_PRIMARY); log_verbose("Use primary comparison level testing ....\n"); doAssert( (ucol_equal(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\""); doAssert( (!ucol_greater(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\""); doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\""); log_verbose("The compare tests end.\n"); ucol_close(col); free(test1); free(test2); } /* ---------------------------------------------------------------------------- ctor -- Tests the getSortKey */ void TestSortKey() { uint8_t *sortk1, *sortk2, *sortk3; int32_t sortklen; UCollator *col; UChar *test1, *test2, *test3; UErrorCode status = ZERO_ERROR; log_verbose("testing SortKey begins...\n"); col = ucol_open(NULL, &status); if (FAILURE(status)) { log_err("ERROR: Default collation creation failed.: %s\n", myErrorName(status)); return; } test1=(UChar*)malloc(sizeof(UChar) * 6); test2=(UChar*)malloc(sizeof(UChar) * 6); test3=(UChar*)malloc(sizeof(UChar) * 6); u_uastrcpy(test1, "Abcda"); u_uastrcpy(test2, "abcda"); u_uastrcpy(test3, "abcda"); log_verbose("Use tertiary comparison level testing ....\n"); sortklen=ucol_getSortKey(col, test1, u_strlen(test1), NULL, 0); sortk1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1)); ucol_getSortKey(col, test1, u_strlen(test1), sortk1, sortklen+1); sortklen=ucol_getSortKey(col, test2, u_strlen(test2), NULL, 0); sortk2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1)); ucol_getSortKey(col, test2, u_strlen(test2), sortk2, sortklen+1); sortklen=ucol_getSortKey(col, test2, u_strlen(test3), NULL, 0); sortk3=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1)); ucol_getSortKey(col, test2, u_strlen(test2), sortk3, sortklen+1); doAssert( (memcmp(sortk1, sortk2, sortklen) > 0), "Result should be \"Abcda\" > \"abcda\""); doAssert( (memcmp(sortk2, sortk1, sortklen) < 0), "Result should be \"abcda\" < \"Abcda\""); doAssert( (memcmp(sortk2, sortk3, sortklen) == 0), "Result should be \"abcda\" == \"abcda\""); free(sortk1); free(sortk2); free(sortk3); log_verbose("Use secondary comparision level testing ...\n"); ucol_setStrength(col, UCOL_SECONDARY); sortklen=ucol_getSortKey(col, test1, u_strlen(test1), NULL, 0); sortk1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1)); ucol_getSortKey(col, test1, u_strlen(test1), sortk1, sortklen+1); sortklen=ucol_getSortKey(col, test2, u_strlen(test2), NULL, 0); sortk2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1)); ucol_getSortKey(col, test2, u_strlen(test2), sortk2, sortklen+1); doAssert( !(memcmp(sortk1, sortk2, sortklen) > 0), "Result should be \"Abcda\" == \"abcda\""); doAssert( !(memcmp(sortk2, sortk1, sortklen) < 0), "Result should be \"abcda\" == \"Abcda\""); doAssert( (memcmp(sortk1, sortk2, sortklen) == 0), "Result should be \"abcda\" == \"abcda\""); log_verbose("testing sortkey ends...\n"); ucol_close(col); free(test1); free(test2); free(test3); free(sortk1); free(sortk2); } void TestHashCode() { uint8_t *sortk1, *sortk2, *sortk3; int32_t sortk1len, sortk2len, sortk3len; UCollator *col; UChar *test1, *test2, *test3; UErrorCode status = ZERO_ERROR; log_verbose("testing getHashCode begins...\n"); col = ucol_open("en_US", &status); if (FAILURE(status)) { log_err("ERROR: Default collation creation failed.: %s\n", myErrorName(status)); return; } test1=(UChar*)malloc(sizeof(UChar) * 6); test2=(UChar*)malloc(sizeof(UChar) * 6); test3=(UChar*)malloc(sizeof(UChar) * 6); u_uastrcpy(test1, "Abcda"); u_uastrcpy(test2, "abcda"); u_uastrcpy(test3, "abcda"); log_verbose("Use tertiary comparison level testing ....\n"); sortk1len=ucol_getSortKey(col, test1, u_strlen(test1), NULL, 0); sortk1=(uint8_t*)malloc(sizeof(uint8_t) * (sortk1len+1)); ucol_getSortKey(col, test1, u_strlen(test1), sortk1, sortk1len+1); sortk2len=ucol_getSortKey(col, test2, u_strlen(test2), NULL, 0); sortk2=(uint8_t*)malloc(sizeof(uint8_t) * (sortk2len+1)); ucol_getSortKey(col, test2, u_strlen(test2), sortk2, sortk2len+1); sortk3len=ucol_getSortKey(col, test2, u_strlen(test3), NULL, 0); sortk3=(uint8_t*)malloc(sizeof(uint8_t) * (sortk3len+1)); ucol_getSortKey(col, test2, u_strlen(test2), sortk3, sortk3len+1); log_verbose("ucol_hashCode() testing ...\n"); doAssert( ucol_keyHashCode(sortk1, sortk1len) != ucol_keyHashCode(sortk2, sortk2len), "Hash test1 result incorrect" ); doAssert( !(ucol_keyHashCode(sortk1, sortk1len) == ucol_keyHashCode(sortk2, sortk2len)), "Hash test2 result incorrect" ); doAssert( ucol_keyHashCode(sortk2, sortk2len) == ucol_keyHashCode(sortk3, sortk3len), "Hash result not equal" ); log_verbose("hashCode tests end.\n"); ucol_close(col); free(sortk1); free(sortk2); free(sortk3); } /* *---------------------------------------------------------------------------- * Tests the UCollatorElements API. * */ void TestElemIter() { UTextOffset offset; int32_t order1, order2, order3; UChar *testString1, *testString2; UCollator *col; UCollationElements *iterator1, *iterator2, *iterator3; UErrorCode status = ZERO_ERROR; log_verbose("testing UCollatorElements begins...\n"); col = ucol_open(NULL, &status); ucol_setNormalization(col, UCOL_NO_NORMALIZATION); if (FAILURE(status)) { log_err("ERROR: Default collation creation failed.: %s\n", myErrorName(status)); return; } testString1=(UChar*)malloc(sizeof(UChar) * 150); testString2=(UChar*)malloc(sizeof(UChar) * 150); u_uastrcpy(testString1, "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?"); u_uastrcpy(testString2, "Xf ile What subset of all possible test cases has the lowest probability of detecting the least errors?"); log_verbose("Constructors and comparison testing....\n"); iterator1 = ucol_openElements(col, testString1, u_strlen(testString1), &status); if(FAILURE(status)) { log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status)); ucol_close(col); return; } else{ log_verbose("PASS: Default collationElement iterator1 creation passed\n");} iterator2 = ucol_openElements(col, testString1, u_strlen(testString1), &status); if(FAILURE(status)) { log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status)); ucol_close(col); return; } else{ log_verbose("PASS: Default collationElement iterator2 creation passed\n");} iterator3 = ucol_openElements(col, testString2, u_strlen(testString2), &status); if(FAILURE(status)) { log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status)); ucol_close(col); return; } else{ log_verbose("PASS: Default collationElement iterator3 creation passed\n");} offset=ucol_getOffset(iterator1); ucol_setOffset(iterator1, 6, &status); if (FAILURE(status)) { log_err("Error in setOffset for UCollatorElements iterator.: %s\n", myErrorName(status)); return; } if(ucol_getOffset(iterator1)==6) log_verbose("setOffset and getOffset working fine\n"); else{ log_err("error in set and get Offset got %d instead of 6\n", ucol_getOffset(iterator1)); } ucol_setOffset(iterator1, 0, &status); order1 = ucol_next(iterator1, &status); if (FAILURE(status)) { log_err("Somehow ran out of memory stepping through the iterator1.: %s\n", myErrorName(status)); return; } order2=ucol_getOffset(iterator2); doAssert((order1 != order2), "The first iterator advance failed"); order2 = ucol_next(iterator2, &status); if (FAILURE(status)) { log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status)); return; } order3 = ucol_next(iterator3, &status); if (FAILURE(status)) { log_err("Somehow ran out of memory stepping through the iterator3.: %s\n", myErrorName(status)); return; } doAssert((order1 == order2), "The second iterator advance failed should be the same as first one"); doAssert( ((order1 & UCOL_PRIMARYMASK) == (order3 & UCOL_PRIMARYMASK)), "The primary orders should be identical"); doAssert( ((order1 & UCOL_SECONDARYMASK) == (order3 & UCOL_SECONDARYMASK)), "The secondary orders should be identical"); doAssert( ((order1 & UCOL_TERTIARYMASK) == (order3 & UCOL_TERTIARYMASK)), "The tertiary orders should be identical"); order1=ucol_next(iterator1, &status); order3=ucol_next(iterator3, &status); doAssert( ((order1 & UCOL_PRIMARYMASK) == (order3 & UCOL_PRIMARYMASK)), "The primary orders should be identical"); doAssert( ((order1 & UCOL_TERTIARYMASK) != (order3 & UCOL_TERTIARYMASK)), "The tertiary orders should be different"); order1=ucol_next(iterator1, &status); order3=ucol_next(iterator3, &status); doAssert( ((order1 & UCOL_SECONDARYMASK) != (order3 & UCOL_SECONDARYMASK)), "The secondary orders should be different"); doAssert( (order1 != UCOL_NULLORDER), "Unexpected end of iterator reached"); ucol_reset(iterator1); ucol_reset(iterator2); ucol_reset(iterator3); free(testString1); free(testString2); ucol_closeElements(iterator1); ucol_closeElements(iterator2); ucol_closeElements(iterator3); ucol_close(col); log_verbose("testing CollationElementIterator ends...\n"); } void TestGetAll() { int32_t i, count; count=ucol_countAvailable(); /* use something sensible w/o hardcoding the count */ if(count < 0){ log_err("Error in countAvailable(), it returned %d\n", count); } else{ log_verbose("Pass: countAvailable() successful, it returned %d\n", count); } for(i=0;i