IntListMethodsTest: Java Code Solution, Testing, and Analysis

Verified

Added on  2019/09/30

|2
|715
|415
Homework Assignment
AI Summary
This Java code implements and tests methods related to an integer list data structure. The `IntListMethodsTest` class includes a `test` method that performs several operations on an integer list, such as allocating, removing elements based on divisibility, sorting, and releasing the list. It uses a random number generator to populate the list with data and includes checks for correctness after each operation. The code also incorporates checksums to verify data integrity during sorting and removal. The `main` method takes command-line arguments to configure the test, including the seed for the random number generator, the size of the list, and the number of rounds to run. Error checks are included to detect memory leaks and incorrect results. The code provides detailed output, including error messages and timing information, and also includes methods to get a Ryerson ID and author name.
Document Page
import java.util.*;
public class IntListMethodsTest {
// Compute a simple XOR checksum of the indices of the nodes hanging from
n.
private static long checksum(int n) {
int check = 0;
while(n != 0) {
check = check ^ n;
n = IntList.getNext(n);
}
return check;
}
// Run the test and return the measured total running time.
public static int test(int seed, int rounds, int size, boolean verbose) {
int[] data = new int[size];
Random rng = new Random(seed);
IntList.initialize(size + 5);
int totalTime = 0;
long startTime, endTime;
for(int i = 0; i < rounds; i++) {
for(int j = 0; j < size; j++) {
data[j] = rng.nextInt(2 * size + 1) - size;
}
int lock = rng.nextInt();
int k = rng.nextInt(6) + 2;
int n = IntList.allocate(data);
if(verbose) { System.out.println("Original: " +
IntList.toString(n)); }
startTime = System.currentTimeMillis();
n = IntListMethods.removeIfDivisible(n, k);
endTime = System.currentTimeMillis();
totalTime += (int)(endTime - startTime);
if(verbose) { System.out.println("After removeIf(" + k + "): " +
IntList.toString(n)); }
int m = n, idx = 0;
while(m != 0) {
while(data[idx] % k == 0) { idx++; }
if(IntList.getKey(m) != data[idx]) {
System.out.println("ERROR: removeIf " + IntList.getKey(m)
+ " " + data[idx] );
return 9999999;
}
idx++;
m = IntList.getNext(m);
}
Arrays.sort(data);
long check1 = checksum(n);
IntList.lockKeys(lock);
startTime = System.currentTimeMillis();
n = IntListMethods.sort(n);
endTime = System.currentTimeMillis();
totalTime += (int)(endTime - startTime);
IntList.unlockKeys(lock);
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
long check2 = checksum(n);
if(check1 != check2) {
System.out.println("Your sort method does not rearrange the
nodes in place.");
return 9999998;
}
if(verbose) { System.out.println("After sort: " +
IntList.toString(n)); }
m = n; idx = 0;
while(m != 0) {
while(data[idx] % k == 0) { idx++; }
if(IntList.getKey(m) != data[idx]) {
System.out.println("ERROR: sort");
return 9999997;
}
idx++;
m = IntList.getNext(m);
}
IntList.release(n);
}
if(IntList.getAllocatedNodeCount() > 0) {
System.out.println("ERROR: Memory leak of " +
IntList.getAllocatedNodeCount() + " nodes.");
return 9999996;
}
if(verbose) { IntList.printStatistics(); }
return totalTime;
}
public static void main(String[] args) {
int seed = Integer.parseInt(args[0]);
int size = Integer.parseInt(args[1]);
int rounds = Integer.parseInt(args[2]);
int total;
try {
total = test(seed, size, rounds, args.length > 3 &&
args[3].toLowerCase().startsWith("t"));
}
catch(IllegalStateException e) {
System.out.println("Exception caught: " + e);
total = 9999995;
}
System.out.println(total + " " + IntListMethods.getRyersonID() + " "
+ IntListMethods.getAuthorName());
}
}
chevron_up_icon
1 out of 2
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]