Please help with the Junit tests for this code~ THANKS! package plams.cssse; imp
ID: 3796525 • Letter: P
Question
Please help with the Junit tests for this code~ THANKS!
package plams.cssse;
import javax.swing.*;
public class EscapeTimeAlg extends JFrame {
/* */
//Variables used in Escape Time Algorithm Calculation
private double xCalc, yCalc, Dist;
private int Passes;
//Array that will store all x and y values in the Range
private double[][] Frac;
//Array that will store the result of EscapeTime Algorithm
private int[][] Result;
//Constructor class that receives the range of x and y values
public EscapeTimeAlg(double xMin, double xMax, double yMin, double yMax ){
xCalc = 0;
yCalc = 0;
Dist = 0;
Passes = 0;
double constx = (Math.abs(xMax) + Math.abs(xMin))/ 512;
double consty = (Math.abs(yMax) + Math.abs(yMin))/ 512;
Frac = new double[2][512];
for(int i = 0; i< 512; i++){
Frac[0][i] = xMin +(constx * i);
Frac[1][i] = yMin +(consty * i);
Result = new int[512][512];
}
}
//Implementation of Mandelbrot set
public void Mandelbrot(){
for(int x = 0;x <512;x++){
xCalc= Frac[0][x];
for(int y = 0;y <512;y++){
yCalc= Frac[1][y];
Dist = Math.sqrt(Math.pow(xCalc, 2) + Math.pow(yCalc, 2));
while(Dist <= 2 && Passes <255){
double oldx = xCalc;
xCalc += Math.pow(xCalc, 2) - Math.pow(yCalc, 2);
yCalc += 2 * oldx * yCalc;
Passes++;
Dist = Math.sqrt(Math.pow(xCalc, 2) + Math.pow(yCalc, 2));
}
Result[x][y] = Passes;
}}}
public void Julia(){
for (int x = 0 ;x<512;x++){
xCalc = Frac[0][x];
for (int y=0;x<512;y++){
yCalc= Frac[1][y];
Dist = Math.sqrt(Math.pow(xCalc, 2)+Math.pow(yCalc, 2));
while(Dist <=2 && Passes<255)
{
double oldx = xCalc;
xCalc= Math.pow(xCalc,2) - Math.pow(yCalc,2) + (-0.72689);
yCalc= 2 * oldx * yCalc + 0.188887;
Passes++;
}
Result[x][y]=Passes;
}}
}
public void BurningShip(){
for(int x=0; x< 512; x++){
xCalc = Frac[0][x];
for(int y =0; y<512;y++){
yCalc = Frac[1][y];
Dist = Math.sqrt(Math.pow(xCalc, 2)+ Math.pow(yCalc, 2));
while(Dist <= 2 && Passes < 255 )
{
double _oldx = xCalc;
xCalc += Math.pow(xCalc,2) - Math.pow(yCalc,2);
yCalc += Math.abs(2* _oldx* yCalc);
Passes++;
Dist = Math.sqrt(Math.pow(xCalc, 2) + Math.pow(yCalc, 2));
}
Result[x][y] = Passes;
}
}
}
public void MultiBrot(){
for(int x = 0;x <512;x++) {
xCalc= Frac[0][x];
for(int y = 0;y <512;y++) {
yCalc= Frac[1][y];
Dist = Math.sqrt(Math.pow(xCalc, 2) + Math.pow(yCalc, 2));
while(Dist <= 2 && Passes <255)
{
double oldx = xCalc;
xCalc += Math.pow(xCalc, 3) - (3* xCalc * Math.pow(yCalc, 2));
yCalc += (3* Math.pow(oldx, 2)* yCalc) - Math.pow(yCalc, 3);
Passes++;
Dist = Math.sqrt(Math.pow(xCalc, 2) + Math.pow(yCalc, 2));
}
Result[x][y] = Passes;
}
}
}
}
Explanation / Answer
Hi,
Expected output that is to be checked using JUnit test is not provided. Alos please provide the program summary as what is expected so as to provide appropriate answers.
Although below are base test classes that you can use for your purpose.
We need to create 2 classes for this.
1. TestJunit.java 2.TestRunner.java
TestJUnit will have the test unit that is to be tested for the class shared in question. and TestRunner will call the JUnitCore to run the TestJUnit calss
codes are shared below:
1. TestJunit.java
import org.junit.Test;
import static org.junit.Assert.assertEquals;
//JUnit test class
public class TestJunit {
String response = "EscapeTimeAlg[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]"; //Enter the value that is expected from the program
EscapeTimeAlg eta = new EscapeTimeAlg(-2.5, 2.0, -1.5, 1.5); //Enter the values that is to be checked. I have entered random values.
@Test
public void testPrintMessage() {
assertEquals(response,eta); //To compare if the expected result and the program result are equal or not
}
}
2.TestRunner.java
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
//Running the JUnit test class
Result result = JUnitCore.runClasses(TestJunit.class);
//Checking for failures of the assert method - output will be false
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
//print if success - output will be true
System.out.println(result.wasSuccessful());
}
}
please reach out in case of any issue.
Thanks