ICU-1794 test UnicodeString::hasMoreChar32Than

X-SVN-Rev: 10004
This commit is contained in:
Markus Scherer 2002-10-09 21:36:47 +00:00
parent 634c3d5a9d
commit a617b35b4a
2 changed files with 99 additions and 31 deletions

View File

@ -24,6 +24,8 @@ using namespace std;
#endif
#define LENGTHOF(array) (sizeof(array)/sizeof((array)[0]))
void UnicodeStringTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char *par)
{
if (exec) logln("TestSuite UnicodeStringTest: ");
@ -49,6 +51,7 @@ void UnicodeStringTest::runIndexedTest( int32_t index, UBool exec, const char* &
case 11: name = "TestMiscellaneous"; if (exec) TestMiscellaneous(); break;
case 12: name = "TestStackAllocation"; if (exec) TestStackAllocation(); break;
case 13: name = "TestUnescape"; if (exec) TestUnescape(); break;
case 14: name = "TestCountChar32"; if (exec) TestCountChar32(); break;
default: name = ""; break; //needed to end loop
}
@ -133,37 +136,6 @@ UnicodeStringTest::TestBasicManipulation()
) {
errln("UnicodeString::moveIndex32() failed");
}
// test countChar32()
// note that this also calls and tests u_countChar32(length>=0)
if(
s.countChar32()!=4 ||
s.countChar32(1)!=4 ||
s.countChar32(2)!=3 ||
s.countChar32(2, 3)!=2 ||
s.countChar32(2, 0)!=0
) {
errln("UnicodeString::countChar32() failed");
}
// NUL-terminate the string buffer and test u_countChar32(length=-1)
const UChar *buffer=s.getTerminatedBuffer();
if(
u_countChar32(buffer, -1)!=4 ||
u_countChar32(buffer+1, -1)!=4 ||
u_countChar32(buffer+2, -1)!=3 ||
u_countChar32(buffer+3, -1)!=3 ||
u_countChar32(buffer+4, -1)!=2 ||
u_countChar32(buffer+5, -1)!=1 ||
u_countChar32(buffer+6, -1)!=0
) {
errln("u_countChar32(length=-1) failed");
}
// test u_countChar32() with bad input
if(u_countChar32(NULL, 5)!=0 || u_countChar32(buffer, -2)!=0) {
errln("u_countChar32(bad input) failed (returned non-zero counts)");
}
}
{
@ -1202,3 +1174,96 @@ void UnicodeStringTest::TestUnescape(void) {
errln("FAIL: unescaping of a string with an illegal escape sequence did not return an empty string");
}
}
/* test code point counting functions --------------------------------------- */
/* reference implementation of UnicodeString::hasMoreChar32Than() */
static int32_t
_refUnicodeStringHasMoreChar32Than(const UnicodeString &s, int32_t start, int32_t length, int32_t number) {
int32_t count=s.countChar32(start, length);
return count>number;
}
/* compare the real function against the reference */
void
UnicodeStringTest::_testUnicodeStringHasMoreChar32Than(const UnicodeString &s, int32_t start, int32_t length, int32_t number) {
if(s.hasMoreChar32Than(start, length, number)!=_refUnicodeStringHasMoreChar32Than(s, start, length, number)) {
errln("hasMoreChar32Than(%d, %d, %d)=%hd is wrong\n",
start, length, number, s.hasMoreChar32Than(start, length, number));
}
}
void
UnicodeStringTest::TestCountChar32(void) {
{
UnicodeString s=UNICODE_STRING("\\U0002f999\\U0001d15f\\u00c4\\u1ed0", 32).unescape();
// test countChar32()
// note that this also calls and tests u_countChar32(length>=0)
if(
s.countChar32()!=4 ||
s.countChar32(1)!=4 ||
s.countChar32(2)!=3 ||
s.countChar32(2, 3)!=2 ||
s.countChar32(2, 0)!=0
) {
errln("UnicodeString::countChar32() failed");
}
// NUL-terminate the string buffer and test u_countChar32(length=-1)
const UChar *buffer=s.getTerminatedBuffer();
if(
u_countChar32(buffer, -1)!=4 ||
u_countChar32(buffer+1, -1)!=4 ||
u_countChar32(buffer+2, -1)!=3 ||
u_countChar32(buffer+3, -1)!=3 ||
u_countChar32(buffer+4, -1)!=2 ||
u_countChar32(buffer+5, -1)!=1 ||
u_countChar32(buffer+6, -1)!=0
) {
errln("u_countChar32(length=-1) failed");
}
// test u_countChar32() with bad input
if(u_countChar32(NULL, 5)!=0 || u_countChar32(buffer, -2)!=0) {
errln("u_countChar32(bad input) failed (returned non-zero counts)");
}
}
/* test data and variables for hasMoreChar32Than() */
static const UChar str[]={
0x61, 0x62, 0xd800, 0xdc00,
0xd801, 0xdc01, 0x63, 0xd802,
0x64, 0xdc03, 0x65, 0x66,
0xd804, 0xdc04, 0xd805, 0xdc05,
0x67
};
UnicodeString string(str, LENGTHOF(str));
int32_t start, length, number;
/* test hasMoreChar32Than() */
for(length=string.length(); length>=0; --length) {
for(start=0; start<=length; ++start) {
for(number=-1; number<=((length-start)+2); ++number) {
_testUnicodeStringHasMoreChar32Than(string, start, length-start, number);
}
}
}
/* test hasMoreChar32Than() with pinning */
for(start=-1; start<=string.length()+1; ++start) {
for(number=-1; number<=((string.length()-start)+2); ++number) {
_testUnicodeStringHasMoreChar32Than(string, start, 0x7fffffff, number);
}
}
/* test hasMoreChar32Than() with a bogus string */
string.setToBogus();
for(length=-1; length<=1; ++length) {
for(start=-1; start<=length; ++start) {
for(number=-1; number<=((length-start)+2); ++number) {
_testUnicodeStringHasMoreChar32Than(string, start, length-start, number);
}
}
}
}

View File

@ -71,6 +71,9 @@ public:
* Test the unescape() function.
*/
void TestUnescape(void);
void _testUnicodeStringHasMoreChar32Than(const UnicodeString &s, int32_t start, int32_t length, int32_t number);
void TestCountChar32(void);
};
class StringCaseTest: public IntlTest {