ICU-4157 text access, work in progress.
X-SVN-Rev: 17645
This commit is contained in:
parent
4b5c53be74
commit
36f008d0bb
200
icu4c/source/test/intltest/utxttest.cpp
Normal file
200
icu4c/source/test/intltest/utxttest.cpp
Normal file
@ -0,0 +1,200 @@
|
||||
/********************************************************************
|
||||
* 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 <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unicode/utext.h>
|
||||
#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;\
|
||||
}}
|
||||
|
||||
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;
|
||||
|
||||
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; i<sa.length(); i=sa.moveIndex32(i, 1)) {
|
||||
c = sa.char32At(i);
|
||||
cpMap[j].nativeIdx = i;
|
||||
cpMap[j].cp = c;
|
||||
j++;
|
||||
cpCount++;
|
||||
}
|
||||
|
||||
|
||||
// UChar * test, null term
|
||||
|
||||
// UChar * test, with length
|
||||
|
||||
// const UChar * test, null term
|
||||
|
||||
|
||||
// const UChar * test, length
|
||||
|
||||
// UnicodeString test
|
||||
UText ut;
|
||||
utext_setUnicodeString(&ut, &sa);
|
||||
TestAccess(&ut, cpCount, cpMap);
|
||||
|
||||
// UTF-8 test
|
||||
|
||||
// UTF-32 test
|
||||
|
||||
// Code Page test
|
||||
|
||||
// Replaceable test
|
||||
|
||||
}
|
||||
|
||||
|
||||
void UTextTest::TestAccess(UText *ut, int cpCount, m *cpMap) {
|
||||
//
|
||||
// Iterate forwards, verify that we get the correct code points
|
||||
// at the correct native offsets.
|
||||
//
|
||||
UTextIterator it(ut);
|
||||
int i = 0;
|
||||
int index;
|
||||
int expectedIndex;
|
||||
int foundIndex;
|
||||
UChar32 expectedC;
|
||||
UChar32 foundC;
|
||||
int32_t len;
|
||||
|
||||
UTextIterator uti(ut);
|
||||
for (i=0; i<cpCount; i++) {
|
||||
expectedIndex = cpMap[i].nativeIdx;
|
||||
foundIndex = uti.getIndex();
|
||||
TEST_ASSERT(expectedIndex == foundIndex);
|
||||
expectedC = cpMap[i].cp;
|
||||
foundC = uti.next32();
|
||||
TEST_ASSERT(expectedC == foundC);
|
||||
if (gFailed) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
foundC = uti.next32();
|
||||
TEST_ASSERT(foundC == U_SENTINEL);
|
||||
|
||||
//
|
||||
// Forward iteration (above) should have left index at the
|
||||
// end of the input, which should == length().
|
||||
//
|
||||
len = uti.length();
|
||||
foundIndex = uti.getIndex();
|
||||
TEST_ASSERT(len == foundIndex);
|
||||
|
||||
//
|
||||
// Iterate backwards over entire test string
|
||||
//
|
||||
len = uti.getIndex();
|
||||
uti.setIndex(len);
|
||||
for (i=cpCount-1; i>=0; i--) {
|
||||
foundC = uti.previous32();
|
||||
expectedC = cpMap[i].cp;
|
||||
foundIndex = uti.getIndex();
|
||||
expectedIndex = cpMap[i].nativeIdx;
|
||||
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 = uti.getIndex();
|
||||
TEST_ASSERT(foundIndex == 0);
|
||||
|
||||
foundC = uti.previous32();
|
||||
TEST_ASSERT(foundC == U_SENTINEL);
|
||||
foundIndex = uti.getIndex();
|
||||
TEST_ASSERT(foundIndex == 0);
|
||||
if (gFailed) {
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Iterate in a somewhat random order.
|
||||
//
|
||||
int cpIndex = 0;
|
||||
for (i=0; i<cpCount; i++) {
|
||||
cpIndex = (cpIndex + 9973) % cpCount;
|
||||
index = cpMap[cpIndex].nativeIdx;
|
||||
expectedC = cpMap[cpIndex].cp;
|
||||
foundC = uti.next32From(index);
|
||||
TEST_ASSERT(expectedC == foundC);
|
||||
TEST_ASSERT(expectedIndex == foundIndex);
|
||||
if (gFailed) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
cpIndex = 0;
|
||||
for (i=0; i<cpCount; i++) {
|
||||
cpIndex = (cpIndex + 9973) % cpCount;
|
||||
index = cpMap[cpIndex].nativeIdx;
|
||||
expectedC = cpMap[cpIndex].cp;
|
||||
foundC = uti.previous32From(index+1);
|
||||
TEST_ASSERT(expectedC == foundC);
|
||||
TEST_ASSERT(expectedIndex == foundIndex);
|
||||
if (gFailed) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
44
icu4c/source/test/intltest/utxttest.h
Normal file
44
icu4c/source/test/intltest/utxttest.h
Normal file
@ -0,0 +1,44 @@
|
||||
/********************************************************************
|
||||
* COPYRIGHT:
|
||||
* Copyright (c) 2005, International Business Machines Corporation and
|
||||
* others. All Rights Reserved.
|
||||
********************************************************************/
|
||||
/************************************************************************
|
||||
* Tests for the UText and UTextIterator text abstraction classses
|
||||
*
|
||||
************************************************************************/
|
||||
|
||||
|
||||
#ifndef UTXTTEST_H
|
||||
#define UTXTTEST_H
|
||||
|
||||
#include "unicode/utypes.h"
|
||||
#include "unicode/unistr.h"
|
||||
#include "unicode/utext.h"
|
||||
|
||||
#include "intltest.h"
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @summary Testing the Replaceable class
|
||||
*/
|
||||
class UTextTest : public IntlTest {
|
||||
public:
|
||||
UTextTest();
|
||||
virtual ~UTextTest();
|
||||
|
||||
void runIndexedTest(int32_t index, UBool exec, const char* &name, char* par=NULL);
|
||||
void TextTest();
|
||||
|
||||
private:
|
||||
struct m { // Map between native indices & code points.
|
||||
int nativeIdx;
|
||||
UChar32 cp;
|
||||
};
|
||||
|
||||
void TestString(const UnicodeString &s);
|
||||
void TestAccess(UText *ut, int cpCount, m *cpMap);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user