/******************************************************************** * COPYRIGHT: * Copyright (c) 2005, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /************************************************************************ * Tests for the UText and UTextIterator text abstraction classses * ************************************************************************/ #include "unicode/utypes.h" #include #include #include #include #include #include #include "utxttest.h" UBool gFailed = FALSE; #define TEST_ASSERT(x) \ {if ((x)==FALSE) {errln("Test failure in file %s at line %d\n", __FILE__, __LINE__);\ gFailed = TRUE;\ }} #define TEST_SUCCESS(status) \ {if (U_FAILURE(status)) {errln("Test failure in file %s at line %d. Error = \"%s\"\n", \ __FILE__, __LINE__, u_errorName(status)); \ gFailed = TRUE;\ }} UTextTest::UTextTest() { } UTextTest::~UTextTest() { } void UTextTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) { switch (index) { case 0: name = "TextTest"; if(exec) TextTest(); break; default: name = ""; break; } } void UTextTest::TextTest() { TestString("abcd\\U00010001xyz"); } // // mapping between native indexes and code points. // native indexes could be utf-8, utf-16, utf32, or some code page. // The general purpose UText test funciton takes an array of these as // expected contents of the text being accessed. // void UTextTest::TestString(const UnicodeString &s) { int i; int j; UChar32 c; int cpCount = 0; UErrorCode status = U_ZERO_ERROR; UnicodeString sa = s.unescape(); // // Build up the mapping between code points and UTF-16 code unit indexes. // m *cpMap = new m[sa.length() + 1]; j = 0; for (i=0; ilength(ut); TEST_ASSERT(expectedLen == utlen); // // Iterate forwards, verify that we get the correct code points // at the correct native offsets. // int i = 0; int index; int expectedIndex = 0; int foundIndex = 0; UChar32 expectedC; UChar32 foundC; int32_t len; for (i=0; i=0; i--) { expectedC = cpMap[i].cp; expectedIndex = cpMap[i].nativeIdx; foundC = utext_previous32(ut); foundIndex = utext_getIndex(ut); TEST_ASSERT(expectedIndex == foundIndex); TEST_ASSERT(expectedC == foundC); if (gFailed) { return; } } // // Backwards iteration, above, should have left our iterator // position at zero, and continued backwards iterationshould fail. // foundIndex = utext_getIndex(ut); TEST_ASSERT(foundIndex == 0); foundC = utext_previous32(ut); TEST_ASSERT(foundC == U_SENTINEL); foundIndex = utext_getIndex(ut); TEST_ASSERT(foundIndex == 0); if (gFailed) { return; } // // next32From(), prevous32From(), Iterate in a somewhat random order. // int cpIndex = 0; for (i=0; i=0; i--) { expectedIndex = cpMap[i].nativeIdx; index = utext_getIndex(ut); TEST_ASSERT(expectedIndex == index); utext_moveIndex(ut, -1); } // walk through backwards, decrementing by three i = cpMap[cpCount].nativeIdx; utext_setIndex(ut, i); for (i=cpCount; i>=0; i-=3) { expectedIndex = cpMap[i].nativeIdx; index = utext_getIndex(ut); TEST_ASSERT(expectedIndex == index); utext_moveIndex(ut, -3); } // // Extract // int bufSize = us.length() + 10; UChar *buf = new UChar[bufSize]; status = U_ZERO_ERROR; expectedLen = us.length(); len = utext_extract(ut, 0, utlen, buf, bufSize, &status); TEST_SUCCESS(status); TEST_ASSERT(len == expectedLen); int compareResult = us.compare(buf, -1); TEST_ASSERT(compareResult == 0); status = U_ZERO_ERROR; len = utext_extract(ut, 0, utlen, NULL, 0, &status); TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR) TEST_ASSERT(len == expectedLen); status = U_ZERO_ERROR; u_memset(buf, 0x5555, bufSize); len = utext_extract(ut, 0, utlen, buf, 1, &status); if (us.length() == 0) { TEST_SUCCESS(status); TEST_ASSERT(buf[0] == 0); } else { TEST_ASSERT(buf[0] == us.charAt(0)); TEST_ASSERT(buf[1] == 0x5555); if (us.length() == 1) { TEST_ASSERT(status == U_STRING_NOT_TERMINATED_WARNING); } else { TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR); } } delete buf; }