1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 2001-2010, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/************************************************************************
* This test program is intended for testing Replaceable class.
*
* Date Name Description
* 11/28/2001 hshih Ported back from Java.
*
************************************************************************/
#include "unicode/utypes.h"
#if !UCONFIG_NO_TRANSLITERATION
#include "ittrans.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "unicode/rep.h"
#include "reptest.h"
//---------------------------------------------
// runIndexedTest
//---------------------------------------------
/**
* This is a test class that simulates styled text.
* It associates a style number (0..65535) with each character,
* and maintains that style in the normal fashion:
* When setting text from raw string or characters,<br>
* Set the styles to the style of the first character replaced.<br>
* If no characters are replaced, use the style of the previous character.<br>
* If at start, use the following character<br>
* Otherwise use NO_STYLE.
*/
class TestReplaceable : public Replaceable {
UnicodeString chars;
UnicodeString styles;
static const char16_t NO_STYLE;
static const char16_t NO_STYLE_MARK;
/**
* The address of this static class variable serves as this class's ID
* for ICU "poor man's RTTI".
*/
static const char fgClassID;
public:
TestReplaceable (const UnicodeString& text,
const UnicodeString& newStyles) {
chars = text;
UnicodeString s;
for (int i = 0; i < text.length(); ++i) {
if (i < newStyles.length()) {
s.append(newStyles.charAt(i));
} else {
if (text.charAt(i) == NO_STYLE_MARK) {
s.append(NO_STYLE);
} else {
s.append(static_cast<char16_t>(i + 0x0031));
}
}
}
this->styles = s;
}
virtual TestReplaceable *clone() const override {
return new TestReplaceable(chars, styles);
}
~TestReplaceable() {}
UnicodeString getStyles() {
return styles;
}
UnicodeString toString() {
UnicodeString s = chars;
s.append("{");
s.append(styles);
s.append("}");
return s;
}
void extractBetween(int32_t start, int32_t limit, UnicodeString& result) const override {
chars.extractBetween(start, limit, result);
}
/**
* ICU "poor man's RTTI", returns a UClassID for this class.
*
* @draft ICU 2.2
*/
static inline UClassID getStaticClassID() { return (UClassID)&fgClassID; }
/**
* ICU "poor man's RTTI", returns a UClassID for the actual class.
*
* @draft ICU 2.2
*/
virtual inline UClassID getDynamicClassID() const override { return getStaticClassID(); }
protected:
virtual int32_t getLength() const override {
return chars.length();
}
virtual char16_t getCharAt(int32_t offset) const override {
return chars.charAt(offset);
}
virtual UChar32 getChar32At(int32_t offset) const override {
return chars.char32At(offset);
}
void fixStyles(int32_t start, int32_t limit, int32_t newLen) {
char16_t newStyle = NO_STYLE;
if (start != limit && styles.charAt(start) != NO_STYLE) {
newStyle = styles.charAt(start);
} else if (start > 0 && getCharAt(start-1) != NO_STYLE_MARK) {
newStyle = styles.charAt(start-1);
} else if (limit < styles.length()) {
newStyle = styles.charAt(limit);
}
// dumb implementation for now.
UnicodeString s;
for (int i = 0; i < newLen; ++i) {
// this doesn't really handle an embedded NO_STYLE_MARK
// in the middle of a long run of characters right -- but
// that case shouldn't happen anyway
if (getCharAt(start+i) == NO_STYLE_MARK) {
s.append(NO_STYLE);
} else {
s.append(newStyle);
}
}
styles.replaceBetween(start, limit, s);
}
virtual void handleReplaceBetween(int32_t start, int32_t limit, const UnicodeString& text) override {
UnicodeString s;
this->extractBetween(start, limit, s);
if (s == text) return; // NO ACTION!
this->chars.replaceBetween(start, limit, text);
fixStyles(start, limit, text.length());
}
virtual void copy(int32_t start, int32_t limit, int32_t dest) override {
chars.copy(start, limit, dest);
styles.copy(start, limit, dest);
}
};
const char TestReplaceable::fgClassID=0;
const char16_t TestReplaceable::NO_STYLE = 0x005F;
const char16_t TestReplaceable::NO_STYLE_MARK = 0xFFFF;
void
ReplaceableTest::runIndexedTest(int32_t index, UBool exec,
const char* &name, char* /*par*/) {
switch (index) {
TESTCASE(0,TestReplaceableClass);
default: name = ""; break;
}
}
/*
* Dummy Replaceable implementation for better API/code coverage.
*/
class NoopReplaceable : public Replaceable {
public:
virtual int32_t getLength() const override {
return 0;
}
virtual char16_t getCharAt(int32_t /*offset*/) const override {
return 0xffff;
}
virtual UChar32 getChar32At(int32_t /*offset*/) const override {
return 0xffff;
}
void extractBetween(int32_t /*start*/, int32_t /*limit*/, UnicodeString& result) const override {
result.remove();
}
virtual void handleReplaceBetween(int32_t /*start*/, int32_t /*limit*/, const UnicodeString &/*text*/) override {
/* do nothing */
}
virtual void copy(int32_t /*start*/, int32_t /*limit*/, int32_t /*dest*/) override {
/* do nothing */
}
static inline UClassID getStaticClassID() { return (UClassID)&fgClassID; }
virtual inline UClassID getDynamicClassID() const override { return getStaticClassID(); }
private:
static const char fgClassID;
};
const char NoopReplaceable::fgClassID=0;
void ReplaceableTest::TestReplaceableClass() {
char16_t rawTestArray[][6] = {
{0x0041, 0x0042, 0x0043, 0x0044, 0x0000, 0x0000}, // ABCD
{0x0061, 0x0062, 0x0063, 0x0064, 0x00DF, 0x0000}, // abcd\u00DF
{0x0061, 0x0042, 0x0043, 0x0044, 0x0000, 0x0000}, // aBCD
{0x0041, 0x0300, 0x0045, 0x0300, 0x0000, 0x0000}, // A\u0300E\u0300
{0x00C0, 0x00C8, 0x0000, 0x0000, 0x0000, 0x0000}, // \u00C0\u00C8
{0x0077, 0x0078, 0x0079, 0x0000, 0x0000, 0x0000}, /* "wxy" */
{0x0077, 0x0078, 0x0079, 0x007A, 0x0000, 0x0000}, /* "wxyz" */
{0x0077, 0x0078, 0x0079, 0x007A, 0x0075, 0x0000}, /* "wxyzu" */
{0x0078, 0x0079, 0x007A, 0x0000, 0x0000, 0x0000}, /* "xyz" */
{0x0077, 0x0078, 0x0079, 0x0000, 0x0000, 0x0000}, /* "wxy" */
{0xFFFF, 0x0078, 0x0079, 0x0000, 0x0000, 0x0000}, /* "*xy" */
{0xFFFF, 0x0078, 0x0079, 0x0000, 0x0000, 0x0000}, /* "*xy" */
};
check("Lower", rawTestArray[0], "1234");
check("Upper", rawTestArray[1], "123455"); // must map 00DF to SS
check("Title", rawTestArray[2], "1234");
check("NFC", rawTestArray[3], "13");
check("NFD", rawTestArray[4], "1122");
check("*(x) > A $1 B", rawTestArray[5], "11223");
check("*(x)(y) > A $2 B $1 C $2 D", rawTestArray[6], "113322334");
check("*(x)(y)(z) > A $3 B $2 C $1 D", rawTestArray[7], "114433225");
// Disabled for 2.4. TODO Revisit in 2.6 or later.
//check("*x > a", rawTestArray[8], "223"); // expect "123"?
//check("*x > a", rawTestArray[9], "113"); // expect "123"?
//check("*x > a", rawTestArray[10], "_33"); // expect "_23"?
//check("*(x) > A $1 B", rawTestArray[11], "__223");
// improve API/code coverage
NoopReplaceable noop;
Replaceable *p;
if((p=noop.clone())!=nullptr) {
errln("Replaceable::clone() does not return nullptr");
delete p;
}
if(!noop.hasMetaData()) {
errln("Replaceable::hasMetaData() does not return true");
}
// try to call the compiler-provided
// UMemory/UObject/Replaceable assignment operators
NoopReplaceable noop2;
noop2=noop;
if((p=noop2.clone())!=nullptr) {
errln("noop2.Replaceable::clone() does not return nullptr");
delete p;
}
// try to call the compiler-provided
// UMemory/UObject/Replaceable copy constructors
NoopReplaceable noop3(noop);
if((p=noop3.clone())!=nullptr) {
errln("noop3.Replaceable::clone() does not return nullptr");
delete p;
}
}
void ReplaceableTest::check(const UnicodeString& transliteratorName,
const UnicodeString& test,
const UnicodeString& shouldProduceStyles)
{
UErrorCode status = U_ZERO_ERROR;
TestReplaceable *tr = new TestReplaceable(test, "");
UnicodeString expectedStyles = shouldProduceStyles;
UnicodeString original = tr->toString();
Transliterator* t;
if (transliteratorName.charAt(0) == 0x2A /*'*'*/) {
UnicodeString rules(transliteratorName);
rules.remove(0,1);
UParseError pe;
t = Transliterator::createFromRules("test", rules, UTRANS_FORWARD,
pe, status);
// test clone()
TestReplaceable *tr2 = tr->clone();
if(tr2 != nullptr) {
delete tr;
tr = tr2;
}
} else {
t = Transliterator::createInstance(transliteratorName, UTRANS_FORWARD, status);
}
if (U_FAILURE(status)) {
dataerrln("FAIL: failed to create the " + transliteratorName + " transliterator");
delete tr;
return;
}
t->transliterate(*tr);
UnicodeString newStyles = tr->getStyles();
if (newStyles != expectedStyles) {
errln("FAIL Styles: " + transliteratorName + "{" + original + "} => "
+ tr->toString() + "; should be {" + expectedStyles + "}!");
} else {
log("OK: ");
log(transliteratorName);
log("(");
log(original);
log(") => ");
logln(tr->toString());
}
delete tr;
delete t;
}
#endif /* #if !UCONFIG_NO_TRANSLITERATION */
|