📄 filterlistrandomizer.java
字号:
case 4:
param1 = new Element[referenceList.size()];
param2 = new Element[referenceList.size()];
break;
case 5:
param1 = new Element[100];
param2 = new Element[100];
break;
case 6:
param1 = new CDATA[0];
param2 = new CDATA[0];
break;
case 7:
param1 = new CDATA[referenceList.size()];
param2 = new CDATA[referenceList.size()];
break;
case 8:
param1 = new CDATA[100];
param2 = new CDATA[100];
break;
default:
throw new RuntimeException("Unknown toArray() test case: " + test);
}
Object result1, result2;
try
{
result1 = referenceList.toArray(param1);
}
catch(Exception ex)
{
result1 = ex;
}
try
{
result2 = testList.toArray(param2);
}
catch(Exception ex)
{
result2 = ex;
}
assertEquals(result1, result2);
}
private void testIsEmpty()
{
if (debug) System.out.println("## testIsEmpty");
assertEquals(referenceList.isEmpty(), testList.isEmpty());
}
private void testIterator()
{
if (debug) System.out.println("## testIterator");
// Hand off to FilterListIteratorRandomizer.
FilterListIteratorRandomizer r = new FilterListIteratorRandomizer(
elementsOnly, random, objects, elements, parent, referenceList, testList);
// This will end up testing the iterator a lot more than the list,
// since the iterator is probably more complex (and more stateful).
if (random.nextDouble() < .01)
r.test(random.nextInt(5000));
else
r.test(random.nextInt(50));
checkLists();
}
private void testContainsAll()
{
if (debug) System.out.println("## testContainsAll");
ArrayList list = new ArrayList();
int count = random.nextInt(4);
for (int i = 0; i < count; i++)
list.add(randomItem());
assertEquals(referenceList.containsAll(list), testList.containsAll(list));
}
private void testAddAll()
{
if (debug) System.out.println("## testAddAll");
ArrayList list = new ArrayList();
int count = random.nextInt(4);
for (int i = 0; i < count; i++)
list.add(randomItem());
Object result1, result2;
try
{
result1 = new Boolean(referenceList.addAll(list));
}
catch(Exception ex)
{
result1 = ex;
}
try
{
result2 = new Boolean(testList.addAll(list));
}
catch(Exception ex)
{
result2 = ex;
fixupObjects();
}
assertEquals(result1, result2);
checkLists();
}
private void testAddAll2()
{
if (debug) System.out.println("## testAddAll2");
int index = random.nextInt(referenceList.size() + 1);
ArrayList list = new ArrayList();
int count = random.nextInt(4);
for (int i = 0; i < count; i++)
list.add(randomItem());
Object result1, result2;
try
{
result1 = new Boolean(referenceList.addAll(index, list));
}
catch(Exception ex)
{
result1 = ex;
}
try
{
result2 = new Boolean(testList.addAll(index, list));
}
catch(Exception ex)
{
result2 = ex;
fixupObjects();
}
assertEquals(result1, result2);
checkLists();
}
private void testRemoveAll()
{
if (debug) System.out.println("## testRemoveAll");
ArrayList list = new ArrayList();
int count = random.nextInt(4);
for (int i = 0; i < count; i++)
list.add(randomItem());
Object result1, result2;
try
{
result1 = new Boolean(referenceList.removeAll(list));
}
catch(Exception ex)
{
result1 = ex;
}
try
{
result2 = new Boolean(testList.removeAll(list));
}
catch(Exception ex)
{
result2 = ex;
fixupObjects();
}
assertEquals(result1, result2);
checkLists();
}
private void testSubList()
{
if (debug) System.out.println("## testSubList");
int index = random.nextInt(referenceList.size() + 1);
int index2 = index + random.nextInt(referenceList.size() - index + 2);
Object result1, result2;
try
{
result1 = referenceList.subList(index, index2);
}
catch(Exception ex)
{
result1 = ex;
}
try
{
result2 = testList.subList(index, index2);
}
catch(Exception ex)
{
result2 = ex;
}
assertEquals(result1, result2);
// This is not really a complete test...
}
private void testRetainAll()
{
if (debug) System.out.println("## testSubList");
ArrayList list = new ArrayList();
int count = random.nextInt(4);
for (int i = 0; i < count; i++)
list.add(randomItem());
Object result1, result2;
try
{
result1 = new Boolean(referenceList.retainAll(list));
}
catch(Exception ex)
{
result1 = ex;
}
try
{
result2 = new Boolean(testList.retainAll(list));
}
catch(Exception ex)
{
result2 = ex;
fixupObjects();
}
assertEquals(result1, result2);
checkLists();
}
///////////////////////////////////////////////////////////
private void assertTrue(boolean value)
{
if (value == false)
throw new RuntimeException("Expected: true Actual: false");
}
private void assertEquals(boolean value1, boolean value2)
{
if (value1 != value2)
throw new RuntimeException("Expected: " + value1 + " Actual: " + value2);
}
private void assertEquals(int value1, int value2)
{
if (value1 != value2)
throw new RuntimeException("Expected: " + value1 + " Actual: " + value2);
}
private void assertEquals(Object value1, Object value2)
{
boolean areEqual;
if (value1 == null)
{
areEqual = (value2 == null);
}
else if (value1 instanceof Object[])
{
areEqual = Arrays.equals((Object[])value1, (Object[])value2);
}
else if (value1 instanceof Exception)
{
areEqual = (value2 != null && value1.getClass().equals(value2.getClass()));
}
else
{
areEqual = (value1.equals(value2));
}
if (!areEqual)
{
if (value2 instanceof Exception)
((Exception)value2).printStackTrace();
throw new RuntimeException("Expected: " + value1 + " Actual: " + value2);
}
}
// Returns a random item from sourceList.
private Object randomItem()
{
return sourceList.get(random.nextInt(sourceList.size()));
}
// Since our addAll() and removeAll() methods aren't perfect, we need
// to do a little fixup if we want to be able to keep going.
private void fixupObjects()
{
for (int i = 0; i < objects.size(); i++)
{
Object obj = objects.get(i);
if (referenceList.contains(obj))
setParent(obj, parent);
else
setParent(obj, null);
}
}
// Sets the parent of the specified object.
private static void setParent(Object obj, Element parent)
{
if (obj instanceof OurElement)
((OurElement)obj).setParent(parent);
else if (obj instanceof OurComment)
((OurComment)obj).setParent(parent);
else if (obj instanceof OurEntityRef)
((OurEntityRef)obj).setParent(parent);
else if (obj instanceof OurProcessingInstruction)
((OurProcessingInstruction)obj).setParent(parent);
else if (obj instanceof OurCDATA)
((OurCDATA)obj).setParent(parent);
else if (obj instanceof OurText)
((OurText)obj).setParent(parent);
}
// After modification, call this method to ensure that the
// two lists are still equivalent.
private void checkLists()
{
if (!referenceList.equals(testList))
{
dumpLists();
throw new RuntimeException("Lists are different");
}
}
// Dumps the contents of both lists to System.out.
private void dumpLists()
{
System.out.println(" >> Reference List (size=" + referenceList.size() + "):");
Iterator iter = referenceList.iterator();
while(iter.hasNext())
System.out.println(" " + iter.next());
System.out.println(" >> Test List (size=" + testList.size() + "):");
Iterator iter2 = testList.iterator();
while(iter2.hasNext())
System.out.println(" " + iter2.next());
if (elementsOnly)
{
List content = parent.getContent();
System.out.println(" >> parent (size=" + content.size() + "):");
for (int i = 0; i < content.size(); i++)
System.out.println(" " + content.get(i));
}
}
private static void setDebug(boolean b)
{
debug = b;
}
private static boolean debug = false;
///////////////////////////////////////////////////////////
//
// This List class acts more like FilterList - it won't let you
// add the same item twice. (FilterList will throw an exception
// if you try to do this, because the item will already have
// a parent. So we want to throw an exception too, to make the
// lists completely equivalent.)
private static class ReferenceList extends ArrayList
{
public Object set(int index, Object o)
{
if (this.get(index) != o)
check(o);
return super.set(index, o);
}
public boolean add(Object o)
{
check(o);
return super.add(o);
}
public void add(int index, Object o)
{
check(o);
super.add(index, o);
}
public boolean addAll(Collection c)
{
check(c);
return super.addAll(c);
}
public boolean addAll(int index, Collection c)
{
check(c);
return super.addAll(index, c);
}
private void check(Object o)
{
// TODO: According to the List interface spec, FilterList should probably
// throw IllegalArgumentException, not IllegalAddException. But for now we just want
// to be compatible with FilterList's behavior.
if (this.contains(o))
throw new IllegalAddException("Item is already in here.");
}
private void check(Collection c)
{
List list = (List)c; // cheat
for (int i = 0; i < list.size(); i++)
{
Object obj = list.get(i);
check(obj);
for (int j = 0; j < list.size(); j++)
{
if (i != j && obj == list.get(j))
throw new IllegalAddException("Can't add the same item twice.");
}
}
}
}
public class OurElement extends Element
{
OurElement(String s)
{
super(s);
}
public Element setParent(Element parent)
{
return (Element) super.setParent(parent);
}
}
public class OurComment extends Comment
{
OurComment(String s)
{
super(s);
}
public Comment setParent(Element parent)
{
return (Comment) super.setParent(parent);
}
}
public class OurEntityRef extends EntityRef
{
OurEntityRef(String s)
{
super(s);
}
public EntityRef setParent(Element parent)
{
return (EntityRef) super.setParent(parent);
}
}
public class OurProcessingInstruction extends ProcessingInstruction
{
OurProcessingInstruction(String s, String s2)
{
super(s, s2);
}
public ProcessingInstruction setParent(Element parent)
{
return (ProcessingInstruction) super.setParent(parent);
}
}
public class OurCDATA extends CDATA
{
OurCDATA(String s)
{
super(s);
}
public Text setParent(Element parent)
{
return (Text) super.setParent(parent);
}
}
public class OurText extends Text
{
OurText(String s)
{
super(s);
}
public Text setParent(Element parent)
{
return (Text) super.setParent(parent);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -