📄 filterlistrandomizer.java
字号:
package org.jdom.test.filterlist;
import org.jdom.*;
import java.util.*;
public final class FilterListRandomizer
{
private boolean elementsOnly; // True if we're checking Element.getChildren(), false if Element.getContent()
private ArrayList objects = new ArrayList(); // All the children of the parent Element.
private ArrayList elements = new ArrayList(); // Only the Element children of the parent Element.
private ArrayList sourceList; // Where to get test objects from - points to "objects" or "elements".
private Random random; // Source of randomness.
private Element parent; // Parent Element.
private List referenceList; // Reference list we're currently using.
private List testList; // Test list we're currently using.
private static int finished; // Total number of iterations finished.
// Command-line entry point.
public static void main(String[] args)
{
Random r = new Random(1234);
System.out.println("Phase 1");
for (int i = 0; i < 100; i++)
{
FilterListRandomizer tester = new FilterListRandomizer(true, r);
tester.test(100000);
FilterListRandomizer tester2 = new FilterListRandomizer(false, r);
tester2.test(100000);
}
System.out.println("Phase 2");
for (int i = 0; i < 100000; i++)
{
FilterListRandomizer tester = new FilterListRandomizer(true, r);
tester.test(100);
FilterListRandomizer tester2 = new FilterListRandomizer(false, r);
tester2.test(100);
}
}
// Constructor.
public FilterListRandomizer(boolean elementsOnly, Random random)
{
this.elementsOnly = elementsOnly;
this.random = random;
init();
}
// Initialize our lists.
private void init()
{
// Put some Elements into "elements" and "objects".
for (int i = 1; i <= 9; i++)
{
Element el = new OurElement("Element" + i);
objects.add(el);
elements.add(el);
}
// Put some other objects into "objects" only.
for (int i = 1; i <= 3; i++)
{
objects.add(new OurComment("Comment" + i));
objects.add(new OurEntityRef("EntityRef" + i));
objects.add(new OurProcessingInstruction("PI" + i, "data"));
objects.add(new OurCDATA("CDATA" + i));
objects.add(new OurText("Text" + i));
}
// Points to the list of items available for testing.
// If we're testing getChildren(), then sourceList will point to the Elements.
// If we're testing getContent(), then sourceList will
// point to *all* the children.
sourceList = (elementsOnly ? elements : objects);
}
// Populate the reference list and the parent Element with the same items.
// (Actually not necessarily the same - the parent Element will be loaded
// up with all types of children, but if elementsOnly is true, then only the
// children that are Elements will go into the reference list.)
private void populate()
{
// Clone the list of all objects, so we can take out one by one,
// so we never add a duplicate.
ArrayList clonedList = (ArrayList)objects.clone();
// Add a random number of items.
int count = random.nextInt(clonedList.size());
for (int i = 0; i < count; i++)
{
// Pick a random item to add.
Object obj = clonedList.remove(random.nextInt(clonedList.size()));
// Add it to the parent Element.
addContent(parent, obj);
// If we're doing elementsOnly, and this isn't an Element, then don't
// add it to the reference list. Otherwise, do.
if (!elementsOnly || obj instanceof Element)
{
// Special case: Element.addContent() will concatenate two adjacent Text nodes.
// So we need to duplicate that behavior here.
int last = referenceList.size() - 1;
if (obj instanceof Text
&& last >= 0
&& referenceList.get(last) instanceof Text)
{
// We actually don't want to modify the Text node here. Since we're running
// the test list and the reference list in parallel, and since we're using
// the exact same objects in each one, then the addition of the Text node in
// the test list will cause the the same Text node that's at the end of
// *both* lists to be modified. i.e. they're both pointing to the same
// Text node, so no need to modify it here, as it was already modified in
// addContent() above.
}
else
{
referenceList.add(obj);
}
}
}
}
// Adds the specified object to the parent Element.
private static void addContent(Element parent, Object obj)
{
if (obj instanceof Element)
parent.addContent((Element)obj);
else if (obj instanceof Comment)
parent.addContent((Comment)obj);
else if (obj instanceof EntityRef)
parent.addContent((EntityRef)obj);
else if (obj instanceof ProcessingInstruction)
parent.addContent((ProcessingInstruction)obj);
else if (obj instanceof CDATA)
parent.addContent((CDATA)obj);
else if (obj instanceof Text)
parent.addContent((Text)obj);
else
throw new RuntimeException("Unknown object type: " + obj);
}
// Do the tests.
public void test(int iterations)
{
try
{
internalTest(iterations);
}
catch(RuntimeException ex)
{
System.out.println("finished = " + finished);
throw ex;
}
}
private void internalTest(int iterations)
{
// Setup the reference list and the parent Element.
referenceList = new ReferenceList();
parent = new Element("Parent");
populate();
// Get the list to be tested from the parent Element.
testList = (elementsOnly ? parent.getChildren() : parent.getContent());
// The two lists should *always* be equivalent from now on.
checkLists();
// Run the tests.
for (int i = 0; i < iterations; i++)
{
// if (finished == 402) setDebug(true);
if (debug) dumpLists();
int test = random.nextInt(23);
switch(test)
{
case 0:
testHashCode();
break;
case 1:
testEquals();
break;
case 2:
testIndexOf();
break;
case 3:
testLastIndexOf();
break;
case 4:
testGet();
break;
case 5:
testSet();
break;
case 6:
testAdd();
break;
case 7:
testAdd2();
break;
case 8:
testSize();
break;
case 9:
testRemove();
break;
case 10:
testRemove2();
break;
case 11:
testContains();
break;
case 12:
testClear();
break;
case 13:
testToArray();
break;
case 14:
testToArray2();
break;
case 15:
testIsEmpty();
break;
case 16:
testIterator();
break;
case 17:
testContainsAll();
break;
case 18:
testAddAll();
break;
case 19:
testAddAll2();
break;
case 20:
testRemoveAll();
break;
case 21:
testRetainAll();
break;
case 22:
testSubList();
break;
default:
throw new RuntimeException("Unknown test number " + test);
}
finished++;
if (finished % 10000 == 0)
System.out.println("FL: Finished " + finished + " iterations; list size is " + referenceList.size());
}
}
///////////////////////////////////////////////////////////
private void testHashCode()
{
if (debug) System.out.println("## testHashCode");
assertEquals(referenceList.hashCode(), testList.hashCode());
}
private void testEquals()
{
if (debug) System.out.println("## testEquals");
assertTrue(referenceList.equals(testList));
}
private void testIndexOf()
{
Object obj = randomItem();
if (debug) System.out.println("## testIndexOf");
assertEquals(referenceList.indexOf(obj), testList.indexOf(obj));
}
private void testLastIndexOf()
{
Object obj = randomItem();
if (debug) System.out.println("## testLastIndexOf");
assertEquals(referenceList.lastIndexOf(obj), testList.lastIndexOf(obj));
}
private void testGet()
{
int index = random.nextInt(referenceList.size() + 1);
if (debug) System.out.println("## testGet "+index);
Object result1, result2;
try
{
result1 = referenceList.get(index);
}
catch(Exception ex)
{
result1 = ex;
}
try
{
result2 = testList.get(index);
}
catch(Exception ex)
{
result2 = ex;
}
assertEquals(result1, result2);
}
private void testSet()
{
int index = random.nextInt(referenceList.size() + 1);
Object obj = randomItem();
if (debug) System.out.println("## testSet "+index+" "+obj);
Object result1, result2;
try
{
result1 = referenceList.set(index, obj);
}
catch(Exception ex)
{
result1 = ex;
}
try
{
result2 = testList.set(index, obj);
}
catch(Exception ex)
{
result2 = ex;
}
assertEquals(result1, result2);
checkLists();
}
private void testAdd()
{
Object obj = randomItem();
if (debug) System.out.println("## testAdd "+obj);
Object result1, result2;
try
{
result1 = new Boolean(referenceList.add(obj));
}
catch(Exception ex)
{
result1 = ex;
}
try
{
result2 = new Boolean(testList.add(obj));
}
catch(Exception ex)
{
result2 = ex;
}
assertEquals(result1, result2);
checkLists();
}
private void testAdd2()
{
int index = random.nextInt(referenceList.size() + 1);
Object obj = randomItem();
if (debug) System.out.println("## testAdd2 "+index+" "+obj);
Object result1 = null, result2 = null;
try
{
referenceList.add(index, obj);
}
catch(Exception ex)
{
result1 = ex;
}
try
{
testList.add(index, obj);
}
catch(Exception ex)
{
result2 = ex;
}
assertEquals(result1, result2);
checkLists();
}
private void testSize()
{
if (debug) System.out.println("## testSize");
assertEquals(referenceList.size(), testList.size());
}
private void testRemove()
{
int index = random.nextInt(referenceList.size() + 1);
if (debug) System.out.println("## testRemove "+index);
Object result1, result2;
try
{
result1 = referenceList.remove(index);
}
catch(Exception ex)
{
result1 = ex;
}
try
{
result2 = testList.remove(index);
}
catch(Exception ex)
{
result2 = ex;
}
assertEquals(result1, result2);
checkLists();
}
private void testRemove2()
{
Object obj = randomItem();
if (debug) System.out.println("## testRemove2 "+obj);
Object result1, result2;
try
{
result1 = new Boolean(referenceList.remove(obj));
}
catch(Exception ex)
{
result1 = ex;
}
try
{
result2 = new Boolean(testList.remove(obj));
}
catch(Exception ex)
{
result2 = ex;
}
assertEquals(result1, result2);
checkLists();
}
private void testContains()
{
Object obj = randomItem();
if (debug) System.out.println("## testContains "+obj);
assertEquals(referenceList.contains(obj), testList.contains(obj));
}
private void testClear()
{
// Want to do this one less often - otherwise the list will
// always be pretty short.
if (random.nextDouble() < .9)
return;
if (debug) System.out.println("## testClear");
referenceList.clear();
testList.clear();
checkLists();
}
private void testToArray()
{
if (debug) System.out.println("## testToArray");
assertEquals(referenceList.toArray(), testList.toArray());
}
private void testToArray2()
{
if (debug) System.out.println("## testToArray2");
// We test short, long, and just-right arrays.
// And we test Object[], Element[], and (arbitrarily) CDATA[].
Object[] param1, param2;
int test = random.nextInt(9);
switch(test)
{
case 0:
param1 = new Object[0];
param2 = new Object[0];
break;
case 1:
param1 = new Object[referenceList.size()];
param2 = new Object[referenceList.size()];
break;
case 2:
param1 = new Object[100];
param2 = new Object[100];
break;
case 3:
param1 = new Element[0];
param2 = new Element[0];
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -