001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: package com.ibm.icu.dev.test.normalizer;
009:
010: import java.util.Random;
011:
012: import com.ibm.icu.dev.test.TestFmwk;
013: import com.ibm.icu.lang.UCharacter;
014: import com.ibm.icu.lang.UCharacterCategory;
015: import com.ibm.icu.text.Normalizer;
016: import com.ibm.icu.text.UTF16;
017:
018: public class NormalizationMonkeyTest extends TestFmwk {
019: int loopCount = 100;
020: int maxCharCount = 20;
021: int maxCodePoint = 0x10ffff;
022: Random random = null; // initialized in getTestSource
023: UnicodeNormalizer unicode_NFD;
024: UnicodeNormalizer unicode_NFC;
025: UnicodeNormalizer unicode_NFKD;
026: UnicodeNormalizer unicode_NFKC;
027:
028: public NormalizationMonkeyTest() {
029: }
030:
031: public static void main(String[] args) throws Exception {
032: new NormalizationMonkeyTest().run(args);
033: }
034:
035: public void TestNormalize() {
036: if (unicode_NFD == null) {
037: try {
038: unicode_NFD = new UnicodeNormalizer(
039: UnicodeNormalizer.D, true);
040: unicode_NFC = new UnicodeNormalizer(
041: UnicodeNormalizer.C, true);
042: unicode_NFKD = new UnicodeNormalizer(
043: UnicodeNormalizer.KD, true);
044: unicode_NFKC = new UnicodeNormalizer(
045: UnicodeNormalizer.KC, true);
046: } catch (Exception e) {
047: String msg = "Normalization tests could not be run: "
048: + e.getMessage();
049: if (isModularBuild()) {
050: warnln(msg);
051: } else {
052: errln(msg);
053: }
054: }
055: }
056: int i = 0;
057: while (i < loopCount) {
058: String source = getTestSource();
059: logln("Test source:" + source);
060: //NFD
061: String uncodeNorm = unicode_NFD.normalize(source);
062: String icuNorm = Normalizer.normalize(source,
063: Normalizer.NFD);
064: logln("\tNFD(Unicode): " + uncodeNorm);
065: logln("\tNFD(icu4j) : " + icuNorm);
066: if (!uncodeNorm.equals(icuNorm)) {
067: errln("NFD: Unicode sample output => " + uncodeNorm
068: + "; icu4j output=> " + icuNorm);
069: }
070: //NFC
071: uncodeNorm = unicode_NFC.normalize(source);
072: icuNorm = Normalizer.normalize(source, Normalizer.NFC);
073: logln("\tNFC(Unicode): " + uncodeNorm);
074: logln("\tNFC(icu4j) : " + icuNorm);
075: if (!uncodeNorm.equals(icuNorm)) {
076: errln("NFC: Unicode sample output => " + uncodeNorm
077: + "; icu4j output=> " + icuNorm);
078: }
079: //NFKD
080: uncodeNorm = unicode_NFKD.normalize(source);
081: icuNorm = Normalizer.normalize(source, Normalizer.NFKD);
082: logln("\tNFKD(Unicode): " + uncodeNorm);
083: logln("\tNFKD(icu4j) : " + icuNorm);
084: if (!uncodeNorm.equals(icuNorm)) {
085: errln("NFKD: Unicode sample output => " + uncodeNorm
086: + "; icu4j output=> " + icuNorm);
087: }
088: //NFKC
089: uncodeNorm = unicode_NFKC.normalize(source);
090: icuNorm = Normalizer.normalize(source, Normalizer.NFKC);
091: logln("\tNFKC(Unicode): " + uncodeNorm);
092: logln("\tNFKC(icu4j) : " + icuNorm);
093: if (!uncodeNorm.equals(icuNorm)) {
094: errln("NFKC: Unicode sample output => " + uncodeNorm
095: + "; icu4j output=> " + icuNorm);
096: }
097:
098: i++;
099: }
100: }
101:
102: String getTestSource() {
103: if (random == null) {
104: random = createRandom(); // use test framework's random seed
105: }
106: String source = "";
107: int i = 0;
108: while (i < (random.nextInt(maxCharCount) + 1)) {
109: int codepoint = random.nextInt(maxCodePoint);
110: //Elimate unassigned characters
111: while (UCharacter.getType(codepoint) == UCharacterCategory.UNASSIGNED) {
112: codepoint = random.nextInt(maxCodePoint);
113: }
114: source = source + UTF16.valueOf(codepoint);
115: i++;
116: }
117: return source;
118: }
119: }
|