Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions