import java.util.Scanner; import java.util.Random; /** Java application whose purpose is to test the QueueViaLink1B class. ** It carries out randomly generated operations upon an instance of ** both that class and the QueueViaLink1 class (which is assumed to ** be correct) and, after each operation, compares the results of the ** two objects' toString() methods. Any difference is reported. */ public class QVL1B_TestApp { private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { Queue q = new QueueViaLink1(); Queue qB = new QueueViaLink1B(); int initSize; // # items to place onto queue initially double deqProb; // probability of an operation being a dequeue // rather than an enqueue int randSeed; // seed for the pseudo-random number generator if (args.length == 0) { // have user enter inputs initSize = getInt("Enter # of items to place into queue initially ", 0, 1000000); deqProb = getDouble("Enter probability of dequeue: ", 0.5, 1.0); randSeed = getInt("Enter seed for pseudorandom # generation: ", 0, 1000000); } else { // use command line args initSize = Integer.parseInt(args[0]); deqProb = Double.parseDouble(args[1]); randSeed = Integer.parseInt(args[2]); } performTest(q, qB, initSize, deqProb, randSeed); System.out.println("Goodbye."); } /* Given two queues (which are assumed to be empty), it performs the same ** sequence of operations upon each one, printing a message whenever it ** determines that the two queues are not in agreement. The sequence of ** operations is as follows: ** (1) A specified number (initSize) of pseudo-randomly generated integers ** are placed onto each queue. ** (2) A sequence of enqueue/dequeue operations, chosen pseudo-randomly, ** is performed until the second queue becomes empty. The 'deleteProb' ** parameter specifies the probability with which the next chosen ** operation is a dequeue. Assuming that that number is greater than ** one-half, the queues should eventually become empty. ** ** The result of calling q2.toString() is printed after each operation ** is performed. After each operation, if q1.toString() and q2.toString() ** are found to differ, a message to that effect is displayed. */ private static void performTest(Queue q1, Queue q2, int initSize, double deleteProb, int randSeed) { final int maxRand = 20; Random rand = new Random(randSeed); System.out.println("Loading queues ..."); for (int i=0; i != initSize; i++) { int m = rand.nextInt(maxRand); System.out.println(" Enqueueing " + m); q1.enqueue(m); q2.enqueue(m); System.out.println("q2 is " + q2); compare(q1, q2); } System.out.println("Done loading queues."); System.out.println("Initial contents: " + q2); while (!q2.isEmpty()) { if (rand.nextDouble() < deleteProb) { int m1 = q1.dequeue(); int m2 = q2.dequeue(); if (m1 != m2) { System.out.println(" **Error: different values dequeued!**"); } System.out.println("After dequeueing " + m2 + ": " + q2); } else { int m = rand.nextInt(maxRand); q1.enqueue(m); q2.enqueue(m); System.out.println("After enqueueing " + m + ": " + q2); } compare(q1, q2); } q1.enqueue(-5); q2.enqueue(-5); compare(q1,q2); } /* Compares the two given queues and prints a message if any ** difference if found. */ private static boolean compare(Queue q1, Queue q2) { boolean result = true; if (q1.isEmpty() != q2.isEmpty()) { System.out.println("queues disagree on isEmpty()"); result = false; } if (q1.sizeOf() != q2.sizeOf()) { System.out.println("queues disagree on sizeOf()"); result = false; } if (!q1.isEmpty() && !q2.isEmpty() && !q1.frontOf().equals(q2.frontOf())) { System.out.println("queues disagree on frontOf()"); result = false; } if (!q1.toString().equals(q2.toString())) { System.out.println("queues disagree on toString():"); System.out.println("q1.toString(): " + q1); System.out.println("q2.toString(): " + q2); result = false; } return result; } private static int getInt(String prompt, int min, int max) { System.out.print(prompt); int result = keyboard.nextInt(); if (result < min || result > max) { System.out.printf("Response must be in the range [%d..%d]\n", min, max); System.exit(1); } return result; } private static double getDouble(String prompt, double min, double max) { System.out.print("Enter # of items to place into queue initially: "); double result = keyboard.nextInt(); if (result < min || result > max) { System.out.printf("Response must be in the range [%d..%d]", min, max); System.exit(1); } return result; } }