Question: I need to write some Methods ( See TODO ) in a junit program that tests another class. import java.util.LinkedList; import java.util.Arrays; import static org.junit.jupiter.api.Assertions.

I need to write some Methods (See TODO) in a junit program that tests another class.
import java.util.LinkedList;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class PermutationTest {
PermutationVariation p1;
PermutationVariation p2;
public int n1;
public int n2;
int cases=0;
void initialize(){
n1=4;
n2=6;
Cases c= new Cases();
p1= c.switchforTesting(cases, n1);
p2= c.switchforTesting(cases, n2);
}
private int factorial(int n){
int result =1;
for (int i =2; i <= n; i++){
result *= i;
}
return result;
}
//TODO test the constructor of the permutation class, the variable original needs to be initialized in the constructor with the given length, and no number is allowed to be used twice. allDerangements needs to be initialized with an empty list
@Test
void testPermutation(){
initialize();
assertNotNull(p1.original);
assertEquals(n1, p1.original.length);
assertTrue(Arrays.stream(p1.original).distinct().count()== n1);
assertNotNull(p1.allDerangements);
assertTrue(p1.allDerangements.isEmpty());
}
//TODO needs to test the derangement method of the permutation class, this method developes all the fixed point free permutations and saves them in allDerangements. This test meeds tp determine whether the number of derangements is correct and if they're actually derangements (if its actually permutations in the next one)
@Test
void testDerangements(){
initialize();
fixConstructor();
p1.derangements();
assertNotNull(p1.allDerangements);
assertTrue(p1.allDerangements.isEmpty());
// Check if all are derangements
for (int[] arr : p1.allDerangements){
for (int i =0; i < arr.length; i++){
assertNotEquals(i, arr[i]);
}
}
// Check the count of derangements
double expectedCount = factorial(n1)/ Math.E;
assertEquals(expectedCount, p1.allDerangements.size());
}
//TODO test whether all of the in the derangement method developed sequences are actually permutations of the original array "orriginal", if there were no permutations calculated, this test should not work
@Test
void testsameElements(){
initialize();
fixConstructor();
p1.derangements();
for (int[] derangement : p1.allDerangements){
int[] sortedDerangement = Arrays.copyOf(derangement, derangement.length);
Arrays.sort(sortedDerangement);
assertArrayEquals(p1.original, sortedDerangement);
}
}
void setCases(int c){
this.cases=c;
}
public void fixConstructor(){
//in case there is something wrong with the constructor
p1.allDerangements=new LinkedList();
for(int i=0;i();
for(int i=0;i();
}
public void derangements(){
backtracking(new LinkedList());
}
public void backtracking(LinkedList candidate){
int[] o=this.original;
if (candidate.size()== o.length){
int[] finalist=new int[o.length];
for(int i=0;i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!