ICU-4035 more tweeks for tests

X-SVN-Rev: 17616
This commit is contained in:
Mark Davis 2005-05-18 01:16:03 +00:00
parent 81732a86c0
commit 62520a08f0
7 changed files with 236 additions and 47 deletions

View File

@ -0,0 +1,43 @@
/*
*******************************************************************************
* Copyright (C) 1996-2004, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
package com.ibm.icu.dev.test.util;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public final class CollectionUtilities {
/**
* Utility like Arrays.asList()
*/
public static Map asMap(Object[][] source, Map target, boolean reverse) {
int from = 0, to = 1;
if (reverse) {
from = 1; to = 0;
}
for (int i = 0; i < source.length; ++i) {
target.put(source[i][from], source[i][to]);
}
return target;
}
public static Map asMap(Object[][] source) {
return asMap(source, new HashMap(), false);
}
/**
* Utility that ought to be on Map
*/
public static Map removeAll(Map m, Collection itemsToRemove) {
for (Iterator it = itemsToRemove.iterator(); it.hasNext();) {
Object item = it.next();
m.remove(item);
}
return m;
}
}

View File

@ -0,0 +1,74 @@
/*
*******************************************************************************
* Copyright (C) 2002-2004, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
package com.ibm.icu.dev.test.util;import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.regex.Pattern;
public class FileUtilities {
public static void appendFile(String filename, String encoding, PrintWriter output) throws IOException {
appendFile(filename, encoding, output, null);
}
public static void appendFile(String filename, String encoding, PrintWriter output, String[] replacementList) throws IOException {
BufferedReader br = BagFormatter.openReader("", filename, encoding);
/*
FileInputStream fis = new FileInputStream(filename);
InputStreamReader isr = (encoding == UTF8_UNIX || encoding == UTF8_WINDOWS) ? new InputStreamReader(fis, "UTF8") : new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr, 32*1024);
*/
while (true) {
String line = br.readLine();
if (line == null) break;
if (replacementList != null) {
for (int i = 0; i < replacementList.length; i += 2) {
line = replace(line, replacementList[i], replacementList[i+1]);
}
}
output.println(line);
}
}
/**
* Replaces all occurances of piece with replacement, and returns new String
*/
public static String replace(String source, String piece, String replacement) {
if (source == null || source.length() < piece.length()) return source;
int pos = 0;
while (true) {
pos = source.indexOf(piece, pos);
if (pos < 0) return source;
source = source.substring(0,pos) + replacement + source.substring(pos + piece.length());
pos += replacement.length();
}
}
public static String replace(String source, String[][] replacements) {
return replace(source, replacements, replacements.length);
}
public static String replace(String source, String[][] replacements, int count) {
for (int i = 0; i < count; ++i) {
source = replace(source, replacements[i][0], replacements[i][1]);
}
return source;
}
public static String replace(String source, String[][] replacements, boolean reverse) {
if (!reverse) return replace(source, replacements);
for (int i = 0; i < replacements.length; ++i) {
source = replace(source, replacements[i][1], replacements[i][0]);
}
return source;
}
public static String anchorize(String source) {
return source.toLowerCase(Locale.ENGLISH).replaceAll("\\P{L}", "_");
}
}

View File

@ -25,7 +25,7 @@ import java.util.TreeSet;
public class ListSet implements Set, List {
List list = new ArrayList();
Set set;
Comparator comparator;
Equator equator;
ListSet(Comparator comparator) {
this.comparator = comparator;

View File

@ -39,7 +39,6 @@ public class TestUtilities extends TestFmwk {
UnicodeMap map1 = new UnicodeMap();
Map map2 = new HashMap();
Map map3 = new TreeMap();
Comparator equator = UnicodeMap.SIMPLE_EQUATOR;
SortedSet log = new TreeSet();
static String[] TEST_VALUES = {null, "A", "B", "C", "D", "E", "F"};
static Random random = new Random(12345);
@ -94,7 +93,31 @@ public class TestUtilities extends TestFmwk {
if (!TestBoilerplate.verifySetsIdentical(this, set1, set2)) {
throw new IllegalArgumentException("Halting");
}
}
}
logln("Getting Scripts");
UnicodeMap scripts = ICUPropertyFactory.make().getProperty("script").getUnicodeMap_internal();
UnicodeMap.Composer composer = new UnicodeMap.Composer() {
public Object compose(Object a, Object b) {
return a.toString() + "_" + b.toString();
}
};
logln("Trying Compose");
UnicodeMap composed = ((UnicodeMap)scripts.clone()).composeWith(map1, composer);
Object last = "";
for (int i = 0; i < 0x10FFFF; ++i) {
Object comp = composed.getValue(i);
Object gc = map1.getValue(i);
Object sc = scripts.getValue(i);
if (!comp.equals(composer.compose(gc, sc))) {
errln("Failed compose at: " + i);
}
if (!last.equals(comp)) {
logln(Utility.hex(i) + "\t" + comp);
last = comp;
}
}
// check boilerplate
List argList = new ArrayList();
@ -113,7 +136,7 @@ public class TestUtilities extends TestFmwk {
UnicodeMap.MapIterator mi = new UnicodeMap.MapIterator(map1);
Map map3 = new TreeMap();
while (mi.nextRange()) {
//System.out.println(Utility.hex(mi.codepoint) + ".." + Utility.hex(mi.codepointEnd) + " => " + mi.value);
logln(Utility.hex(mi.codepoint) + ".." + Utility.hex(mi.codepointEnd) + " => " + mi.value);
for (int i = mi.codepoint; i <= mi.codepointEnd; ++i) {
if (i >= limit) continue;
map3.put(new Integer(i), mi.value);
@ -126,7 +149,7 @@ public class TestUtilities extends TestFmwk {
map3 = new TreeMap();
Object lastValue = new Object();
while (mi.next()) {
if (UnicodeMap.SIMPLE_EQUATOR.compare(lastValue, mi.value) != 0) {
if (!UnicodeMap.areEqual(lastValue, mi.value)) {
// System.out.println("Change: " + Utility.hex(mi.codepoint) + " => " + mi.value);
lastValue = mi.value;
}
@ -140,7 +163,7 @@ public class TestUtilities extends TestFmwk {
for (int i = 0; i < LIMIT; ++i) {
Object value1 = map1.getValue(i);
Object value2 = map2.get(new Integer(i));
if (equator.compare(value1, value2) != 0) {
if (!UnicodeMap.areEqual(value1, value2)) {
errln(counter + " Difference at " + Utility.hex(i)
+ "\t UnicodeMap: " + value1
+ "\t HashMap: " + value2);

View File

@ -9,7 +9,9 @@ package com.ibm.icu.dev.test.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
@ -33,26 +35,17 @@ public final class UnicodeMap implements Cloneable {
private Object[] values = new Object[10];
private boolean errorOnReset = false;
private ListSet availableValues;
private LinkedHashSet availableValues = new LinkedHashSet();
boolean staleAvailableValues = false;
private int lastIndex = 0;
public UnicodeMap(Comparator equator) {
this.equator = equator;
availableValues = new ListSet(equator);
}
public UnicodeMap() {
this(SIMPLE_EQUATOR);
}
/* Boilerplate */
public boolean equals(Object other) {
if (other == null) return false;
try {
UnicodeMap that = (UnicodeMap) other;
if (length != that.length || !equator.equals(that.equator)) return false;
if (length != that.length) return false;
for (int i = 0; i < length-1; ++i) {
if (transitions[i] != that.transitions[i]) return false;
if (!areEqual(values[i], that.values[i])) return false;
@ -68,9 +61,10 @@ public final class UnicodeMap implements Cloneable {
//equator.getHashCode
}
public boolean areEqual(Object a, Object b) {
return equator.compare(a, b) == 0;
//equator.getHashCode
public static boolean areEqual(Object a , Object b) {
if (a == b) return true;
if (a == null || b == null) return false;
return a.equals(b);
}
public int hashCode() {
@ -91,8 +85,7 @@ public final class UnicodeMap implements Cloneable {
that.length = length;
that.transitions = (int[]) transitions.clone();
that.values = (Object[]) values.clone();
that.equator = equator;
that.availableValues = new ListSet(equator);
that.availableValues = new LinkedHashSet(availableValues);
return that;
}
@ -125,21 +118,6 @@ public final class UnicodeMap implements Cloneable {
}
}
private static final class SimpleEquator implements Comparator {
public int compare(Object a, Object b) {
if (a == b) return 0;
if (a == null) return -1;
if (b == null) return 1;
return ((Comparable)a).compareTo((Comparable)b);
}
public int getHashCode(Object a) {
if (a == null) return 0;
return a.hashCode();
}
}
public static Comparator SIMPLE_EQUATOR = new SimpleEquator();
private Comparator equator = SIMPLE_EQUATOR;
/**
* Finds an index such that inversionList[i] <= codepoint < inversionList[i+1]
* Assumes that 0 <= codepoint <= 0x10FFFF
@ -338,6 +316,12 @@ public final class UnicodeMap implements Cloneable {
lastIndex = baseIndex; // store for next time
return this;
}
private UnicodeMap _putAll(int startCodePoint, int endCodePoint, Object value) {
for (int i = startCodePoint; i <= endCodePoint; ++i) {
_put(i, value);
}
return this;
}
/**
* Sets the codepoint value.
* @param codepoint
@ -361,8 +345,8 @@ public final class UnicodeMap implements Cloneable {
public UnicodeMap putAll(UnicodeSet codepoints, Object value) {
// TODO optimize
UnicodeSetIterator it = new UnicodeSetIterator(codepoints);
while (it.next()) {
_put(it.codepoint, value);
while (it.nextRange()) {
_putAll(it.codepoint, it.codepointEnd, value);
}
return this;
}
@ -442,7 +426,7 @@ public final class UnicodeMap implements Cloneable {
if (staleAvailableValues) {
// collect all the current values
// retain them in the availableValues
Set temp = new TreeSet(equator);
Set temp = new HashSet();
for (int i = 0; i < length - 1; ++i) {
temp.add(values[i]);
}
@ -473,6 +457,19 @@ public final class UnicodeMap implements Cloneable {
return values[_findIndex(codepoint)];
}
public interface Composer {
Object compose(Object a, Object b);
}
public UnicodeMap composeWith(UnicodeMap other, Composer composer) {
for (int i = 0; i <= 0x10FFFF; ++i) {
Object v1 = getValue(i);
Object v2 = other.getValue(i);
put(i, composer.compose(v1, v2));
}
return this;
}
/**
* Follow the style used by UnicodeSetIterator
*/

View File

@ -232,7 +232,7 @@ public abstract class UnicodeProperty extends UnicodeLabel {
return result;
}
List temp = new ArrayList(1); // to avoid reallocating...
UnicodeMap um = getUnicodeMap();
UnicodeMap um = getUnicodeMap_internal();
Iterator it = um.getAvailableValues(null).iterator();
main:
while (it.hasNext()) {
@ -308,7 +308,14 @@ public abstract class UnicodeProperty extends UnicodeLabel {
/**
* @return the unicode map
*/
protected UnicodeMap getUnicodeMap() {
public UnicodeMap getUnicodeMap() {
return (UnicodeMap) getUnicodeMap_internal().clone();
}
/**
* @return the unicode map
*/
protected UnicodeMap getUnicodeMap_internal() {
if (unicodeMap == null) unicodeMap = _getUnicodeMap();
return unicodeMap;
}
@ -896,17 +903,18 @@ public abstract class UnicodeProperty extends UnicodeLabel {
addAllUnique(propertyAliases, result);
return result;
}
public BaseProperty addValueAliases(String[][] valueAndAlternates) {
public BaseProperty addValueAliases(String[][] valueAndAlternates, boolean errorIfCant) {
if (toValueAliases == null) _fixValueAliases();
for (int i = 0; i < valueAndAlternates.length; ++i) {
for (int j = 1; j < valueAndAlternates[0].length; ++j) {
addValueAlias(valueAndAlternates[i][0], valueAndAlternates[i][j]);
addValueAlias(valueAndAlternates[i][0], valueAndAlternates[i][j], errorIfCant);
}
}
return this;
}
public void addValueAlias(String value, String valueAlias) {
public void addValueAlias(String value, String valueAlias, boolean errorIfCant) {
List result = (List) toValueAliases.get(value);
if (result == null && !errorIfCant) return;
addUnique(value, result);
addUnique(valueAlias, result);
}
@ -982,7 +990,7 @@ public abstract class UnicodeProperty extends UnicodeLabel {
protected void _fillValues() {
List newvalues = (List) getUnicodeMap().getAvailableValues(new ArrayList());
List newvalues = (List) getUnicodeMap_internal().getAvailableValues(new ArrayList());
for (Iterator it = newvalues.iterator(); it.hasNext();) {
_addToValues((String)it.next(), null);
}
@ -993,7 +1001,7 @@ public abstract class UnicodeProperty extends UnicodeLabel {
if (toValueAliases == null) _fixValueAliases();
addUnique(item, values);
_ensureValueInAliases(item);
addValueAlias(item, alias);
addValueAlias(item, alias, true);
}
/* public String _getVersion() {
return version;

View File

@ -0,0 +1,44 @@
/*
*******************************************************************************
* Copyright (C) 2002-2004, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
package com.ibm.icu.dev.test.util;import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class VariableReplacer {
// simple implementation for now
private Comparator c;
private Map m = new TreeMap(Collections.reverseOrder());
// TODO - fix to do streams also, clean up implementation
public VariableReplacer add(String variable, String value) {
m.put(variable, value);
return this;
}
public String replace(String source) {
String oldSource;
do {
oldSource = source;
for (Iterator it = m.keySet().iterator(); it.hasNext();) {
String variable = (String) it.next();
String value = (String) m.get(variable);
source = replaceAll(source, variable, value);
}
} while (!source.equals(oldSource));
return source;
}
public String replaceAll(String source, String key, String value) {
while (true) {
int pos = source.indexOf(key);
if (pos < 0) return source;
source = source.substring(0,pos) + value + source.substring(pos+key.length());
}
}
}