+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
73bc9bb
master
1import java .util .Arrays ; 2import java .util .Random ; 3 4public class MergeTest { 5 6public static void main (String []args ) { 7final int NUM_THREADS =4 ;// Change this to however many threads you want to test with 8testRandom (NUM_THREADS ); 9System .out .println ("Congrats! Your implementation passes the randomly generated test cases!" ); 10 } 11 12/** 13* Generates and tests PMerge 10 times with random array inputs. 14* @param numThreads number of threads to run PMerge with 15*/ 16public static void testRandom (int numThreads ) { 17Random rand =new Random (); 18for (int i =0 ;i <10 ; ++i ) { 19int aSize =rand .nextInt (200 ); 20int bSize =rand .nextInt (200 ); 21int []a =new int [aSize ],b =new int [bSize ]; 22for (int j =0 ;j <a .length ; ++j ) 23a [j ] =rand .nextInt (); 24for (int j =0 ;j <b .length ; ++j ) 25b [j ] =rand .nextInt (); 26int []c =new int [aSize +bSize ]; 27 28Arrays .sort (a ); 29Arrays .sort (b ); 30PMerge .parallelMerge (a ,b ,c ,numThreads ); 31verify (a ,b ,c ); 32 } 33 } 34 35public static void testSmall (int numThreads ) { 36int []a = {1 ,3 ,4 ,23 ,32 ,35 ,234 ,2345 }; 37int []b = {1 ,9 ,11 ,14 ,16 ,17 ,22 ,24 ,499 ,5011 ,6000 }; 38 39int []c =new int [a .length +b .length ]; 40PMerge .parallelMerge (a ,b ,c ,numThreads ); 41 } 42 43static void verify (int []a ,int []b ,int []c ) { 44int []expected =new int [a .length +b .length ]; 45System .arraycopy (a ,0 ,expected ,0 ,a .length ); 46System .arraycopy (b ,0 ,expected ,a .length ,b .length ); 47Arrays .sort (expected ); 48reverse (expected ); 49if (!Arrays .equals (expected ,c )) { 50throw new AssertionError (String .format ("\nExpected: %s\nReceived: %s\n" ,Arrays .toString (expected ),Arrays .toString (c ))); 51 } 52 } 53 54static void reverse (int []a ) { 55for (int i =0 ;i <a .length /2 ; ++i ) { 56a [i ] ^=a [a .length -1 -i ]; 57a [a .length -1 -i ] ^=a [i ]; 58a [i ] ^=a [a .length -1 -i ]; 59 } 60 } 61 62}