#include "unicode/utypes.h"
#if !UCONFIG_NO_COLLATION
#include "thcoll.h"
#include "unicode/utypes.h"
#include "unicode/coll.h"
#include "unicode/sortkey.h"
#include "cstring.h"
#include "filestrm.h"
#define TEST_FILE "th18057.txt"
#define TEST_FILE_ENCODING "UTF8"
#define MAX_FAILURES_TO_SHOW 8
#define CASE(id,test) \
case id: \
name = #test; \
if (exec) { \
logln(#test "---"); \
logln((UnicodeString)""); \
test(); \
} \
break;
CollationThaiTest::CollationThaiTest() {
UErrorCode status = U_ZERO_ERROR;
coll = Collator::createInstance(Locale("th", "TH", ""), status);
if (coll && U_SUCCESS(status)) {
coll->setStrength(Collator::TERTIARY);
} else {
delete coll;
coll = 0;
}
}
CollationThaiTest::~CollationThaiTest() {
delete coll;
}
void CollationThaiTest::runIndexedTest(int32_t index, UBool exec, const char* &name,
char* ) {
if((!coll) && exec) {
errln(__FILE__ " cannot test - failed to create collator.");
name = "";
return;
}
switch (index) {
CASE(0,TestDictionary)
CASE(1,TestCornerCases)
CASE(2,TestNamesList)
default: name = ""; break;
}
}
static UBool readLine(FileStream *in, UnicodeString& line, const char* encoding) {
if (T_FileStream_eof(in)) {
return FALSE;
}
char buffer[128];
char* p = buffer;
char* limit = p + sizeof(buffer) - 1; while (p<limit) {
int c = T_FileStream_getc(in);
if (c < 0 || c == 0xD || c == 0xA) {
break;
}
*p++ = c;
}
*p = 0;
line = UnicodeString(buffer, encoding);
return TRUE;
}
void CollationThaiTest::TestNamesList(void) {
if (coll == 0) {
errln("Error: could not construct Thai collator");
return;
}
UErrorCode status = U_ZERO_ERROR;
char buffer[1024];
uprv_strcpy(buffer,IntlTest::loadTestData(status) );
char* index = 0;
if (U_FAILURE(status)) {
errln("ERROR: could not open test data %s", u_errorName(status));
return;
}
index=strrchr(buffer,(char)U_FILE_SEP_CHAR);
if((unsigned int)(index-buffer) != (strlen(buffer)-1)){
*(index+1)=0;
}
uprv_strcat(buffer,".."U_FILE_SEP_STRING);
uprv_strcat(buffer, "TestNames_Thai.txt");
FileStream *in = T_FileStream_open(buffer, "rb");
if (in == 0) {
logln((UnicodeString)"Could not find file: " + buffer +" will not do this test");
return;
}
UnicodeString lastWord, word;
int32_t line = 0;
int32_t wordCount = 0;
while (readLine(in, word, "UTF16LE")) {
line++;
if (word.charAt(0) == 0x23 || word.length() == 0) {
continue;
}
++wordCount;
if (wordCount <= 8) {
UnicodeString str;
logln((UnicodeString)"Word " + wordCount + ": " + IntlTest::prettify(word, str));
}
if (lastWord.length() > 0) {
Collator::EComparisonResult result = coll->compare(lastWord, word);
doTest(coll, lastWord, word, result);
}
lastWord = word;
}
logln((UnicodeString)"Words checked: " + wordCount);
T_FileStream_close(in);
}
void CollationThaiTest::TestDictionary(void) {
if (coll == 0) {
errln("Error: could not construct Thai collator");
return;
}
UErrorCode status = U_ZERO_ERROR;
char buffer[1024];
uprv_strcpy(buffer,IntlTest::loadTestData(status) );
char* index = 0;
if (U_FAILURE(status)) {
errln("ERROR: could not open test data %s", u_errorName(status));
return;
}
index=strrchr(buffer,(char)U_FILE_SEP_CHAR);
if((unsigned int)(index-buffer) != (strlen(buffer)-1)){
*(index+1)=0;
}
uprv_strcat(buffer,".."U_FILE_SEP_STRING);
uprv_strcat(buffer, TEST_FILE);
FileStream *in = T_FileStream_open(buffer, "rb");
if (in == 0) {
errln((UnicodeString)"Error: could not open test file " + buffer);
return;
}
UnicodeString lastWord, word;
int32_t line = 0;
int32_t failed = 0;
int32_t wordCount = 0;
while (readLine(in, word, "UTF8")) {
line++;
if (word.charAt(0) == 0x23 || word.length() == 0) {
continue;
}
++wordCount;
if (wordCount <= 8) {
UnicodeString str;
logln((UnicodeString)"Word " + wordCount + ": " + IntlTest::prettify(word, str));
}
if (lastWord.length() > 0) {
doTest(coll, lastWord, word, Collator::LESS);
int32_t result = coll->compare(lastWord, word);
if (result >= 0) {
failed++;
if (MAX_FAILURES_TO_SHOW < 0 || failed <= MAX_FAILURES_TO_SHOW) {
UnicodeString str;
UnicodeString msg =
UnicodeString("--------------------------------------------\n")
+ line
+ " compare(" + IntlTest::prettify(lastWord, str);
msg += UnicodeString(", ")
+ IntlTest::prettify(word, str) + ") returned " + result
+ ", expected -1\n";
UErrorCode status = U_ZERO_ERROR;
CollationKey k1, k2;
coll->getCollationKey(lastWord, k1, status);
coll->getCollationKey(word, k2, status);
if (U_FAILURE(status)) {
errln((UnicodeString)"Fail: getCollationKey returned " + u_errorName(status));
return;
}
msg.append("key1: ").append(prettify(k1, str)).append("\n");
msg.append("key2: ").append(prettify(k2, str));
errln(msg);
}
}
}
lastWord = word;
}
if (failed != 0) {
if (failed > MAX_FAILURES_TO_SHOW) {
errln((UnicodeString)"Too many failures; only the first " +
MAX_FAILURES_TO_SHOW + " failures were shown");
}
errln((UnicodeString)"Summary: " + failed + " of " + (line - 1) +
" comparisons failed");
}
logln((UnicodeString)"Words checked: " + wordCount);
T_FileStream_close(in);
}
void CollationThaiTest::TestCornerCases(void) {
const char* TESTS[] = {
"\\u0e01", "<", "\\u0e01\\u0e01",
"\\u0e01\\u0e32", "<", "\\u0e01\\u0e49\\u0e32",
"\\u0e01\\u0e32", "<", "\\u0e01\\u0e32\\u0e4c",
"\\u0e01\\u0e32\\u0e01\\u0e49\\u0e32", "<", "\\u0e01\\u0e48\\u0e32\\u0e01\\u0e49\\u0e32",
"\\u0e01\\u0e32", "<", "\\u0e01\\u0e32-",
"\\u0e01\\u0e32-", "<", "\\u0e01\\u0e32\\u0e01\\u0e32",
"\\u0e01\\u0e32", "<", "\\u0e01\\u0e32\\u0e46",
"\\u0e01\\u0e32\\u0e46", "<", "\\u0e01\\u0e32\\u0e01\\u0e32",
"\\u0e24\\u0e29\\u0e35", "<", "\\u0e24\\u0e45\\u0e29\\u0e35",
"\\u0e26\\u0e29\\u0e35", "<", "\\u0e26\\u0e45\\u0e29\\u0e35",
"\\u0e40\\u0e01\\u0e2d", "<", "\\u0e40\\u0e01\\u0e34",
"\\u0e01\\u0e32\\u0e01\\u0e48\\u0e32", "<", "\\u0e01\\u0e49\\u0e32\\u0e01\\u0e32",
"\\u0e01.\\u0e01.", "<", "\\u0e01\\u0e32",
};
const int32_t TESTS_length = (int32_t)(sizeof(TESTS)/sizeof(TESTS[0]));
if (coll == 0) {
errln("Error: could not construct Thai collator");
return;
}
compareArray(*coll, TESTS, TESTS_length);
}
void CollationThaiTest::compareArray(const Collator& c, const char* tests[],
int32_t testsLength) {
UErrorCode status = U_ZERO_ERROR;
for (int32_t i = 0; i < testsLength; i += 3) {
int32_t expect = 0;
if (tests[i+1][0] == '<') {
expect = -1;
} else if (tests[i+1][0] == '>') {
expect = 1;
} else if (tests[i+1][0] == '=') {
expect = 0;
} else {
errln((UnicodeString)"Error: unknown operator " + tests[i+1]);
return;
}
UnicodeString s1, s2;
parseChars(s1, tests[i]);
parseChars(s2, tests[i+2]);
int32_t result = c.compare(s1, s2);
if (sign(result) != sign(expect))
{
UnicodeString t1, t2;
errln(UnicodeString("") +
i/3 + ": compare(" + IntlTest::prettify(s1, t1)
+ " , " + IntlTest::prettify(s2, t2)
+ ") got " + result + "; expected " + expect);
CollationKey k1, k2;
c.getCollationKey(s1, k1, status);
c.getCollationKey(s2, k2, status);
if (U_FAILURE(status)) {
errln((UnicodeString)"Fail: getCollationKey returned " + u_errorName(status));
return;
}
errln((UnicodeString)" key1: " + prettify(k1, t1) );
errln((UnicodeString)" key2: " + prettify(k2, t2) );
}
else
{
CollationKey k1, k2;
c.getCollationKey(s1, k1, status);
c.getCollationKey(s2, k2, status);
if (U_FAILURE(status)) {
errln((UnicodeString)"Fail: getCollationKey returned " + u_errorName(status));
return;
}
result = k1.compareTo(k2);
if (sign(result) != sign(expect)) {
UnicodeString t1, t2;
errln(UnicodeString("") +
i/3 + ": key(" + IntlTest::prettify(s1, t1)
+ ").compareTo(key(" + IntlTest::prettify(s2, t2)
+ ")) got " + result + "; expected " + expect);
errln((UnicodeString)" " + prettify(k1, t1) + " vs. " + prettify(k2, t2));
}
}
}
}
int8_t CollationThaiTest::sign(int32_t i) {
if (i < 0) return -1;
if (i > 0) return 1;
return 0;
}
UnicodeString& CollationThaiTest::parseChars(UnicodeString& result,
const char* chars) {
return result = CharsToUnicodeString(chars);
}
#endif