blob: 154dc51bf1665e27d0aafa9971e20e14b332f459 [file] [log] [blame]
/********************************************************************
* COPYRIGHT:
* Copyright (c) 2001, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/********************************************************************************
*
* File cmsccoll.C
*
*********************************************************************************/
/**
* These are the tests specific to ICU 1.8 and above, that I didn't know where to
* fit.
*/
#include <stdlib.h>
#include "unicode/utypes.h"
#include "unicode/ucol.h"
#include "unicode/ucoleitr.h"
#include "unicode/uloc.h"
#include "cintltst.h"
#include "cdetst.h"
#include "ccolltst.h"
#include "callcoll.h"
#include "unicode/ustring.h"
#include "string.h"
static UCollator *myCollation;
const static UChar rules[MAX_TOKEN_LEN] =
/*" & 0 < 1,\u2461<a,A"*/
{ 0x0026, 0x0030, 0x003C, 0x0031, 0x002C, 0x2460, 0x003C, 0x0061, 0x002C, 0x0041, 0x0000 };
const static UChar testCase[][MAX_TOKEN_LEN] =
{
/*0*/ {0x0031 /*'1'*/, 0x0061/*'a'*/, 0x0000},
/*1*/ {0x0031 /*'1'*/, 0x0041/*'A'*/, 0x0000},
/*2*/ {0x2460 /*circ'1'*/, 0x0061/*'a'*/, 0x0000},
/*3*/ {0x2460 /*circ'1'*/, 0x0041/*'A'*/, 0x0000}
};
const static UCollationResult caseTestResults[][9] =
{
{ UCOL_LESS, UCOL_LESS, UCOL_LESS, 0, UCOL_LESS, UCOL_LESS, 0, 0, UCOL_LESS },
{ UCOL_GREATER, UCOL_LESS, UCOL_LESS, 0, UCOL_LESS, UCOL_LESS, 0, 0, UCOL_GREATER },
{ UCOL_LESS, UCOL_LESS, UCOL_LESS, 0, UCOL_GREATER, UCOL_LESS, 0, 0, UCOL_LESS },
{ UCOL_GREATER, UCOL_LESS, UCOL_GREATER, 0, UCOL_LESS, UCOL_LESS, 0, 0, UCOL_GREATER }
};
const static UColAttributeValue caseTestAttributes[][2] =
{
{ UCOL_LOWER_FIRST, UCOL_OFF},
{ UCOL_UPPER_FIRST, UCOL_OFF},
{ UCOL_LOWER_FIRST, UCOL_ON},
{ UCOL_UPPER_FIRST, UCOL_ON}
};
const static char cnt1[][10] = {
"AA",
"AC",
"AZ",
"AQ",
"AB",
"ABZ",
"ABQ",
"Z",
"ABC",
"Q",
"B"
};
const static char cnt2[][10] = {
"DA",
"DAD",
"DAZ",
"MAR",
"Z",
"DAVIS",
"MARK",
"DAV",
"DAVI"
};
static void TestCase( )
{
int32_t i,j,k;
UErrorCode status = U_ZERO_ERROR;
myCollation = ucol_open(NULL, &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
return;
}
log_verbose("Testing different case settings\n");
ucol_setStrength(myCollation, UCOL_TERTIARY);
for(k = 0; k<4; k++) {
ucol_setAttribute(myCollation, UCOL_CASE_FIRST, caseTestAttributes[k][0], &status);
ucol_setAttribute(myCollation, UCOL_CASE_LEVEL, caseTestAttributes[k][1], &status);
for (i = 0; i < 3 ; i++) {
for(j = i+1; j<4; j++) {
doTest(myCollation, testCase[i], testCase[j], caseTestResults[k][3*i+j-1]);
}
}
}
ucol_close(myCollation);
myCollation = ucol_openRules(rules, u_strlen(rules), UNORM_NONE, UCOL_TERTIARY, &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
return;
}
log_verbose("Testing different case settings with custom rules\n");
ucol_setStrength(myCollation, UCOL_TERTIARY);
for(k = 0; k<4; k++) {
ucol_setAttribute(myCollation, UCOL_CASE_FIRST, caseTestAttributes[k][0], &status);
ucol_setAttribute(myCollation, UCOL_CASE_LEVEL, caseTestAttributes[k][1], &status);
for (i = 0; i < 3 ; i++) {
for(j = i+1; j<4; j++) {
doTest(myCollation, testCase[i], testCase[j], caseTestResults[k][3*i+j-1]);
}
}
}
ucol_close(myCollation);
}
/**
* Return an integer array containing all of the collation orders
* returned by calls to next on the specified iterator
*/
static int32_t* getOrders(UCollationElements *iter, int32_t *orderLength)
{
UErrorCode status;
int32_t order;
int32_t maxSize = 100;
int32_t size = 0;
int32_t *temp;
int32_t *orders =(int32_t*)malloc(sizeof(int32_t) * maxSize);
status= U_ZERO_ERROR;
while ((order=ucol_next(iter, &status)) != UCOL_NULLORDER)
{
if (size == maxSize)
{
maxSize *= 2;
temp = (int32_t*)malloc(sizeof(int32_t) * maxSize);
memcpy(temp, orders, size * sizeof(int32_t));
free(orders);
orders = temp;
}
orders[size++] = order;
}
if (maxSize > size)
{
temp = (int32_t*)malloc(sizeof(int32_t) * size);
memcpy(temp, orders, size * sizeof(int32_t));
free(orders);
orders = temp;
}
*orderLength = size;
return orders;
}
static void backAndForth(UCollationElements *iter)
{
/* Run through the iterator forwards and stick it into an array */
int32_t index, o;
UErrorCode status = U_ZERO_ERROR;
int32_t orderLength = 0;
int32_t *orders;
orders= getOrders(iter, &orderLength);
/* Now go through it backwards and make sure we get the same values */
index = orderLength;
ucol_reset(iter);
/* synwee : changed */
while ((o = ucol_previous(iter, &status)) != UCOL_NULLORDER)
{
if (o != orders[-- index])
{
if (o == 0)
index ++;
else
{
while (index > 0 && orders[-- index] == 0)
{
}
if (o != orders[index])
{
log_err("Mismatch at index : %d\n", index);
break;
}
}
}
}
while (index != 0 && orders[index - 1] == 0) {
index --;
}
if (index != 0)
{
log_err("Didn't get back to beginning - index is %d\n", index);
ucol_reset(iter);
log_err("\nnext: ");
while ((o = ucol_next(iter, &status)) != UCOL_NULLORDER)
{
log_err("Error at %d\n", o);
}
log_err("\nprev: ");
while ((o = ucol_previous(iter, &status)) != UCOL_NULLORDER)
{
log_err("Error at %d\n", o);
}
log_verbose("\n");
}
free(orders);
}
static void IncompleteCntTest( )
{
UErrorCode status = U_ZERO_ERROR;
UChar *temp=(UChar*)malloc(sizeof(UChar) * 90);
UChar *t1 =(UChar*)malloc(sizeof(UChar) * 90);
UChar *t2 =(UChar*)malloc(sizeof(UChar) * 90);
UCollator *coll = NULL;
uint32_t i = 0, j = 0;
uint32_t size = 0;
u_uastrcpy(temp, " & Z < ABC < Q < B");
coll = ucol_openRules(temp, u_strlen(temp), UCOL_NO_NORMALIZATION,
UCOL_DEFAULT_STRENGTH, &status);
/* problem in strcollinc for unfinshed contractions */
ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
if(U_SUCCESS(status)) {
size = sizeof(cnt1)/sizeof(cnt1[0]);
for(i = 0; i < size-1; i++) {
for(j = i+1; j < size; j++) {
UCollationElements *iter;
u_uastrcpy(t1, cnt1[i]);
u_uastrcpy(t2, cnt1[j]);
doTest(coll, t1, t2, UCOL_LESS);
/* synwee : added collation element iterator test */
iter = ucol_openElements(coll, t2, u_strlen(t2), &status);
if (U_FAILURE(status)) {
log_err("Creation of iterator failed\n");
break;
}
backAndForth(iter);
free(iter);
}
}
}
ucol_close(coll);
u_uastrcpy(temp, " & Z < DAVIS < MARK <DAV");
coll = ucol_openRules(temp, u_strlen(temp), UCOL_NO_NORMALIZATION,
UCOL_DEFAULT_STRENGTH, &status);
/* problem in strcollinc for unfinshed contractions */
ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
if(U_SUCCESS(status)) {
size = sizeof(cnt2)/sizeof(cnt2[0]);
for(i = 0; i < size-1; i++) {
for(j = i+1; j < size; j++) {
UCollationElements *iter;
u_uastrcpy(t1, cnt2[i]);
u_uastrcpy(t2, cnt2[j]);
doTest(coll, t1, t2, UCOL_LESS);
/* synwee : added collation element iterator test */
iter = ucol_openElements(coll, t2, u_strlen(t2), &status);
if (U_FAILURE(status)) {
log_err("Creation of iterator failed\n");
break;
}
backAndForth(iter);
free(iter);
}
}
}
ucol_close(coll);
}
const static char shifted[][20] = {
"black bird",
"black-bird",
"blackbird",
"black Bird",
"black-Bird",
"blackBird",
"black birds",
"black-birds",
"blackbirds"
};
const static UCollationResult shiftedTert[] = {
0,
UCOL_EQUAL,
UCOL_EQUAL,
UCOL_LESS,
UCOL_EQUAL,
UCOL_EQUAL,
UCOL_LESS,
UCOL_EQUAL,
UCOL_EQUAL
};
const static char nonignorable[][20] = {
"black bird",
"black Bird",
"black birds",
"black-bird",
"black-Bird",
"black-birds",
"blackbird",
"blackBird",
"blackbirds"
};
void BlackBirdTest( ) {
UErrorCode status = U_ZERO_ERROR;
UChar *t1 =(UChar*)malloc(sizeof(UChar) * 90);
UChar *t2 =(UChar*)malloc(sizeof(UChar) * 90);
uint32_t i = 0, j = 0;
uint32_t size = 0;
UCollator *coll = ucol_open(NULL, &status);
ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_OFF, &status);
ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status);
if(U_SUCCESS(status)) {
size = sizeof(nonignorable)/sizeof(nonignorable[0]);
for(i = 0; i < size-1; i++) {
for(j = i+1; j < size; j++) {
u_uastrcpy(t1, nonignorable[i]);
u_uastrcpy(t2, nonignorable[j]);
doTest(coll, t1, t2, UCOL_LESS);
}
}
}
ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_QUATERNARY, &status);
if(U_SUCCESS(status)) {
size = sizeof(shifted)/sizeof(shifted[0]);
for(i = 0; i < size-1; i++) {
for(j = i+1; j < size; j++) {
u_uastrcpy(t1, shifted[i]);
u_uastrcpy(t2, shifted[j]);
doTest(coll, t1, t2, UCOL_LESS);
}
}
}
ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_TERTIARY, &status);
if(U_SUCCESS(status)) {
size = sizeof(shifted)/sizeof(shifted[0]);
for(i = 1; i < size; i++) {
u_uastrcpy(t1, shifted[i-1]);
u_uastrcpy(t2, shifted[i]);
doTest(coll, t1, t2, shiftedTert[i]);
}
}
ucol_close(coll);
}
const static UChar testSourceCases[][MAX_TOKEN_LEN] = {
{0x0041/*'A'*/, 0x0300, 0x0301, 0x0000},
{0x0041/*'A'*/, 0x0300, 0x0316, 0x0000},
{0x0041/*'A'*/, 0x0300, 0x0000},
{0x00C0, 0x0301, 0x0000},
/* this would work with forced normalization */
{0x00C0, 0x0316, 0x0000}
};
const static UChar testTargetCases[][MAX_TOKEN_LEN] = {
{0x0041/*'A'*/, 0x0301, 0x0300, 0x0000},
{0x0041/*'A'*/, 0x0316, 0x0300, 0x0000},
{0x00C0, 0},
{0x0041/*'A'*/, 0x0301, 0x0300, 0x0000},
/* this would work with forced normalization */
{0x0041/*'A'*/, 0x0316, 0x0300, 0x0000}
};
const static UCollationResult results[] = {
UCOL_GREATER,
UCOL_EQUAL,
UCOL_EQUAL,
UCOL_GREATER,
UCOL_EQUAL
};
static void FunkyATest( )
{
int32_t i;
UErrorCode status = U_ZERO_ERROR;
myCollation = ucol_open(NULL, &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
return;
}
log_verbose("Testing some A letters, for some reason\n");
ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
ucol_setStrength(myCollation, UCOL_TERTIARY);
for (i = 0; i < 4 ; i++)
{
doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
}
ucol_close(myCollation);
}
void addMiscCollTest(TestNode** root)
{
addTest(root, &TestCase, "tscoll/cmsccoll/TestCase");
addTest(root, &IncompleteCntTest, "tscoll/cmsccoll/IncompleteCntTest");
addTest(root, &BlackBirdTest, "tscoll/cmsccoll/BlackBirdTest");
addTest(root, &FunkyATest, "tscoll/cmsccoll/FunkyATest");
}