Last active
August 29, 2015 14:11
-
-
Save mohamed-ennahdi/df2d414bfa087d5b85a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.exercises.test; | |
import java.util.Random; | |
import com.exercises.main.EvaluationExercise; | |
public class EvaluationExerciseTest { | |
/** | |
* Unit testing of a random number of cases. | |
* When a case is incorrect, the testing is interrupted. It continues, otherwise. | |
* The program displays number of cases to be tested. | |
* The program also displays the original array, its rotated version, and the number of rotation, | |
* as well as the test unit verdict (OK, KO). | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
EvaluationExercise exercise; | |
int cases = Math.abs(new Random().nextInt()) % EvaluationExercise.MAX_SIZE; | |
System.out.println(cases + " arrays to be tested."); | |
System.out.println(); | |
do { | |
exercise = new EvaluationExercise(EvaluationExerciseTest.generateRandomArray()); | |
int n = Math.abs(new Random().nextInt()) % exercise.getArray().length; | |
/* | |
* Making a copy of the original array. | |
*/ | |
int[] arrayCopy = exercise.getArray().clone(); | |
/* | |
* Rotating. | |
*/ | |
exercise.rotate(n); | |
/* | |
* Test Unit of the array, to verify the correctness of the result. | |
*/ | |
assert EvaluationExerciseTest.testUnitArrayRotation(n, exercise.getArray(), arrayCopy); | |
cases --; | |
} while (cases > 0); | |
} | |
/** | |
* Unit testing of the array. | |
* The property to be tested is the new position of an element: Was it "shifted" n times as expected. | |
* | |
* @param rotationNumber | |
* @param array | |
* @param arrayCopy | |
* @return | |
*/ | |
public static boolean testUnitArrayRotation(int rotationNumber, int[] array, int[] arrayCopy) { | |
System.out.println("Original array: ");new EvaluationExercise(arrayCopy).displayArray(); | |
System.out.println(); | |
System.out.println("Rotated array: ");new EvaluationExercise(array).displayArray(); | |
System.out.println(); | |
System.out.println("Number of rotations: " + rotationNumber); | |
for (int i = 0; i < array.length; i++) { | |
if ( array[ ( i + rotationNumber ) % array.length ] != arrayCopy[ i ]) { | |
System.out.println("Test KO"); | |
return false; | |
} | |
} | |
System.out.println("Test OK"); | |
System.out.println(); | |
return true; | |
} | |
/** | |
* This function generates a random array of integers (random size, random positive elements). | |
* @return | |
*/ | |
public static int[] generateRandomArray() { | |
Random r = new Random(); | |
int size = Math.abs(r.nextInt()) % EvaluationExercise.MAX_SIZE + 1; | |
int[] array = new int[size]; | |
for (int i = 0; i < size; i ++) { | |
array[i] = Math.abs(r.nextInt()) % EvaluationExercise.MAX_SIZE; | |
} | |
return array; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment