/** Test Stack and Queue class */
package FlexOr.container;
import java.util.Date;
public class SQtest {
public static void main (String arg[]) {
println("Test started " + new Date());
println("Queue test");
testQueue();
println("Stack test");
testStack();
println("Priority Queue test");
pqTest();
println("Test finished " + new Date());
}
public static void pqTest() {
PriorityQueue list = new PriorityQueue(20); verifyEmpty("1", list);
list.add("a"); verifyL1("2", list, "[ ]");
list.remove(); verifyEmpty("3", list);
list.add("a");
list.add("b"); verifyL2("4", list, "[ ]");
if (list.contains("c")) println("5 shouldn't to contain c");
if (!list.contains("a")) println("6 should contain a");
if (!list.contains("b")) println("7 should contain b");
list.add("c"); verifyL3("8", list, "[ ]");
list.remove(); verifyL2("9", list, "[ ]");
list.remove(); verifyL1("10", list, "[ ]");
list.remove(); verifyEmpty("11", list);
try { list.remove();
} catch (ContainerEmptyException e) { println("12 empty exception"); }
list.add("a"); list.add("b"); list.add("c");
PriorityQueue list2 = new PriorityQueue(20);
list2.add("a"); list2.add("b"); list2.add("c");
if (!list.equals(list2)) println("13 " + list.toString() +
" should equal " + list2.toString());
list2.remove();
if (list.equals(list2)) println("14 " + list.toString() +
" should not be equal " + list2.toString());
PriorityQueue list3 = (PriorityQueue) list.clone();
verifyL3("15 ", list3, "[ ]");
list3.remove(); list3.add("d");
verifyL3("16 ", list3, "[ ]");
println("17 " + list3.toString());
list3.add("e", 1); list3.add("f", 2);
StringBuffer sb = new StringBuffer();
while (! list3.isEmpty()) {
sb = sb.append(list3.remove());
}
String str = "febcd";
if (!sb.toString().equals(str))
println("18 remove order " + sb + " expected to be " + str);
}
public static void testQueue() {
Queue list = new Queue(new CircularArray(20)); verifyEmpty("1", list);
list.add("a"); verifyL1("2", list, "[ a ]");
list.remove(); verifyEmpty("3", list);
list.add("a");
list.add("b"); verifyL2("4", list, "[ a , b ]");
if (list.contains("c")) println("5 shouldn't to contain c");
if (!list.contains("a")) println("6 should contain a");
if (!list.contains("b")) println("7 should contain b");
list.add("c"); verifyL3("8", list, "[ a , b , c ]");
list.remove(); verifyL2("9", list, "[ b , c ]");
list.remove(); verifyL1("10", list, "[ c ]");
list.remove(); verifyEmpty("11", list);
try { list.remove();
} catch (ContainerEmptyException e) { println("12 empty exception"); }
list.add("a"); list.add("b"); list.add("c");
Queue list2 = new Queue(new CircularArray(10));
list2.add("a"); list2.add("b"); list2.add("c");
if (!list.equals(list2)) println("13 " + list.toString() +
" should equal " + list2.toString());
list2.remove();
if (list.equals(list2)) println("14 " + list.toString() +
" should not be equal " + list2.toString());
Queue list3 = (Queue) list.clone();
verifyL3("15 ", list3, "[ a , b , c ]");
println("16 " + list3.toString());
}
public static void testStack() {
Stack list = new Stack(); verifyEmpty("1", list);
list.add("a"); verifyL1("2", list, "[ a ]");
list.remove(); verifyEmpty("3", list);
list.add("a");
list.add("b"); verifyL2("4", list, "[ b , a ]");
if (list.contains("c")) println("5 shouldn't to contain c");
if (!list.contains("a")) println("6 should contain a");
if (!list.contains("b")) println("7 should contain b");
list.add("c"); verifyL3("8", list, "[ c , b , a ]");
list.remove(); verifyL2("9", list, "[ b , a ]");
list.remove(); verifyL1("10", list, "[ a ]");
list.remove(); verifyEmpty("11", list);
try { list.remove();
} catch (ContainerEmptyException e) { println("12 empty exception"); }
list.add("a"); list.add("b"); list.add("c");
Stack list2 = new Stack();
list2.add("a"); list2.add("b"); list2.add("c");
if (!list.equals(list2)) println("13 " + list.toString() +
" should equal " + list2.toString());
list2.remove();
if (list.equals(list2)) println("14 " + list.toString() +
" should not be equal " + list2.toString());
Stack list3 = (Stack) list.clone();
verifyL3("15 ", list3, "[ c , b , a ]");
println("16 " + list3.toString());
}
private static void println(String item) {
System.out.println(item);
}
private static void verifyEmpty(String index, FlexOr.container.Container list) {
String expected = "[]";
if (! list.toString().equals(expected))
println(index + "Invalid contents: " + list.toString() + " " + expected);
if (list.size() != 0) println(index + "size not 0");
if (! list.isEmpty())
println(index + "Empty list is said to be not empty");
}
private static void verifyL1(String index, FlexOr.container.Container list, String expected) {
if (! list.toString().equals(expected))
println(index + "Invalid contents: " + list.toString() +
" " + expected);
if (list.size() != 1) println(index + "size not 1");
}
private static void verifyL2(String index, FlexOr.container.Container list, String expected) {
if (! list.toString().equals(expected))
println(index + "Invalid contents: " + list.toString() +
" " + expected);
if (list.size() != 2) println(index + "size not 2");
}
private static void verifyL3(String index, FlexOr.container.Container list, String expected) {
if (! list.toString().equals(expected))
println(index + "Invalid contents: " + list.toString() +
" " + expected);
if (list.size() != 3) println(index + "size not 3");
}
}