Ask a question from expert

Ask now

IntListMethodsTest Class in Java

2 Pages715 Words415 Views
   

Added on  2019-09-30

About This Document

IntListMethodsTest is a Java class that tests the functionality of IntListMethods. It allocates data, removes divisible nodes, sorts nodes, and computes a checksum of the indices of the nodes hanging from n.

IntListMethodsTest Class in Java

   Added on 2019-09-30

BookmarkShareRelated Documents
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); long check2 = checksum(n);
IntListMethodsTest Class in Java_1

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
PriorityThreadQueue and ListManager
|4
|670
|24