Test randoms actualizados paa tener en cuenta que puede haber varios null, ningun null, y que el orden de los nulls puede variar
Test fijos modificados para probar casos con todo null y casos sin ningun null
import java.lang.Math; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { private static void Test(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2,double[] expected) { double[] actual = Kata.newton(f, r, m1, m2); int n=0; for(int i=0;i<4;i++) { if(actual[i]==expected[i]) n++; } assertEquals(4, n); } double[] newtonSol(Integer[] force, Integer[] radius, Integer[] mass1, Integer[] mass2){ double[] result = new double[4]; double g = 6.674*Math.pow(10,-11); for(int i = 0; i<4; i++){ if(force[i] == null){ if(radius[i]==null || mass1[i]==null|| mass2[i]==null){ result[i]=-1; }else{ result[i] = (g*mass1[i]*mass2[i])/Math.pow(radius[i],2); } }else if(radius[i] == null){ if(force[i]==null || mass1[i]==null|| mass2[i]==null){ result[i]=-1; }else{ result[i] = Math.sqrt((g * mass1[i] * mass2[i]) / force[i]); } }else if(mass1[i] == null){ if(force[i]==null || radius[i]==null|| mass2[i]==null){ result[i]=-1; }else{ result[i] = (force[i]*Math.pow(radius[i],2))/(g*mass2[i]); } }else if(mass2[i] == null){ if(force[i]==null || radius[i]==null|| mass1[i]==null){ result[i]=-1; }else{ result[i] = (force[i]*Math.pow(radius[i],2))/(g*mass1[i]); } }else{ result[i] = 0; } } return result; } @Test void testRandom() { for(int i=0;i<40;i++){ Integer[] f=new Integer[4]; Integer[] r=new Integer[4]; Integer[] m1=new Integer[4]; Integer[] m2=new Integer[4]; for(int d=0;d<4;d++){ f[d]=(int)(Math.random()*250)+10; r[d]=(int)(Math.random()*250)+10; m1[d]=(int)(Math.random()*250)+10; m2[d]=(int)(Math.random()*250)+10; } for(int d=0;d<4;d++){ int num = (int) (Math.random() * 4) + 1; if(num==1) f[d]=null; } for(int d=0;d<4;d++){ int num = (int) (Math.random() * 4) + 1; if(num==1) r[d]=null; } for(int d=0;d<4;d++){ int num = (int) (Math.random() * 4) + 1; if(num==1) m1[d]=null; } for(int d=0;d<4;d++){ int num = (int) (Math.random() * 4) + 1; if(num==1) m2[d]=null; } double[] expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); } } @Test void testFixed(){ Integer[] f={null, null, null, null}; Integer[] r={100, null, 150, 120}; Integer[] m1={150, 100, 100, 120}; Integer[] m2={120, 100, 150, null}; double[] expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); f= new Integer[] {100, 106, 150, 140}; r=new Integer[] {100, null, 65, null}; m1=new Integer[] {150, 112, null, 120}; m2=new Integer[] {120, 448, 150, null}; expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); f=new Integer[] {null, 106, 150, 240}; r=new Integer[] {null, null, null, null}; m1=new Integer[] {103, 112, null, 120}; m2=new Integer[] {120, 241, 111, null}; expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); } @Test void testFixed2(){ double [] expected = newtonSol(new Integer[]{null,null,null,null},new Integer[]{null,null,null,null}, new Integer[]{null,null,null,null},new Integer[]{null,null,null,null}); Test(new Integer[]{null,null,null,null},new Integer[]{null,null,null,null}, new Integer[]{null,null,null,null},new Integer[]{null,null,null,null},expected); expected = newtonSol(new Integer[]{20,110,80,90},new Integer[]{90,20,117,14}, new Integer[]{17,17,20,17},new Integer[]{15,15,15,20}); Test(new Integer[]{20,110,80,90},new Integer[]{90,20,117,14}, new Integer[]{17,17,20,17},new Integer[]{15,15,15,20},expected); expected = newtonSol(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2}, new Integer[]{3,1,null,2},new Integer[]{3,2,1,null}); Test(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2}, new Integer[]{3,1,null,2},new Integer[]{3,2,1,null},expected); } }
- import java.lang.Math;
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- private static void Test(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2,double[] expected) {
- double[] actual = Kata.newton(f, r, m1, m2);
- int n=0;
- for(int i=0;i<4;i++) {
- if(actual[i]==expected[i]) n++;
- }
- assertEquals(4, n);
- }
double[] newtonSol(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2){- double[] newtonSol(Integer[] force, Integer[] radius, Integer[] mass1, Integer[] mass2){
- double[] result = new double[4];
- double g = 6.674*Math.pow(10,-11);
result[0] = (g*m1[0]*m2[0])/Math.pow(r[0],2);result[1] = Math.sqrt((g * m1[1] * m2[1]) / f[1]);result[2] = (f[2]*Math.pow(r[2],2))/(g*m2[2]);result[3] = (f[3]*Math.pow(r[3],2))/(g*m1[3]);return result;- for(int i = 0; i<4; i++){
- if(force[i] == null){
- if(radius[i]==null || mass1[i]==null|| mass2[i]==null){
- result[i]=-1;
- }else{
- result[i] = (g*mass1[i]*mass2[i])/Math.pow(radius[i],2);
- }
- }else if(radius[i] == null){
- if(force[i]==null || mass1[i]==null|| mass2[i]==null){
- result[i]=-1;
- }else{
- result[i] = Math.sqrt((g * mass1[i] * mass2[i]) / force[i]);
- }
- }else if(mass1[i] == null){
- if(force[i]==null || radius[i]==null|| mass2[i]==null){
- result[i]=-1;
- }else{
- result[i] = (force[i]*Math.pow(radius[i],2))/(g*mass2[i]);
- }
- }else if(mass2[i] == null){
- if(force[i]==null || radius[i]==null|| mass1[i]==null){
- result[i]=-1;
- }else{
- result[i] = (force[i]*Math.pow(radius[i],2))/(g*mass1[i]);
- }
- }else{
- result[i] = 0;
- }
- }
- return result;
- }
- @Test
- void testRandom() {
- for(int i=0;i<40;i++){
- Integer[] f=new Integer[4];
- Integer[] r=new Integer[4];
- Integer[] m1=new Integer[4];
- Integer[] m2=new Integer[4];
- for(int d=0;d<4;d++){
- f[d]=(int)(Math.random()*250)+10;
- r[d]=(int)(Math.random()*250)+10;
- m1[d]=(int)(Math.random()*250)+10;
- m2[d]=(int)(Math.random()*250)+10;
- }
f[0]=null; r[1]=null; m1[2]=null; m2[3]=null;- for(int d=0;d<4;d++){
- int num = (int) (Math.random() * 4) + 1;
- if(num==1) f[d]=null;
- }
- for(int d=0;d<4;d++){
- int num = (int) (Math.random() * 4) + 1;
- if(num==1) r[d]=null;
- }
- for(int d=0;d<4;d++){
- int num = (int) (Math.random() * 4) + 1;
- if(num==1) m1[d]=null;
- }
- for(int d=0;d<4;d++){
- int num = (int) (Math.random() * 4) + 1;
- if(num==1) m2[d]=null;
- }
- double[] expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- }
- }
- @Test
- void testFixed(){
Integer[] f={null, 100, 150, 120};- Integer[] f={null, null, null, null};
- Integer[] r={100, null, 150, 120};
Integer[] m1={150, 100, null, 120};- Integer[] m1={150, 100, 100, 120};
- Integer[] m2={120, 100, 150, null};
- double[] expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
f= new Integer[] {null, 106, 150, 140};r=new Integer[] {100, null, 65, 120};- f= new Integer[] {100, 106, 150, 140};
- r=new Integer[] {100, null, 65, null};
- m1=new Integer[] {150, 112, null, 120};
- m2=new Integer[] {120, 448, 150, null};
- expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- f=new Integer[] {null, 106, 150, 240};
r=new Integer[] {100, null, 65, 120};- r=new Integer[] {null, null, null, null};
- m1=new Integer[] {103, 112, null, 120};
- m2=new Integer[] {120, 241, 111, null};
- expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- }
- @Test
- void testFixed2(){
double [] expected = newtonSol(new Integer[]{null,20,40,60},new Integer[]{20,null,60,40},new Integer[]{20,60,null,40},new Integer[]{20,40,60,null});Test(new Integer[]{null,20,40,60},new Integer[]{20,null,60,40},new Integer[]{20,60,null,40},new Integer[]{20,40,60,null},expected);- double [] expected = newtonSol(new Integer[]{null,null,null,null},new Integer[]{null,null,null,null},
- new Integer[]{null,null,null,null},new Integer[]{null,null,null,null});
- Test(new Integer[]{null,null,null,null},new Integer[]{null,null,null,null},
- new Integer[]{null,null,null,null},new Integer[]{null,null,null,null},expected);
expected = newtonSol(new Integer[]{null,110,80,90},new Integer[]{90,null,117,14},new Integer[]{17,17,null,17},new Integer[]{15,15,15,null});Test(new Integer[]{null,110,80,90},new Integer[]{90,null,117,14},new Integer[]{17,17,null,17},new Integer[]{15,15,15,null},expected);- expected = newtonSol(new Integer[]{20,110,80,90},new Integer[]{90,20,117,14},
- new Integer[]{17,17,20,17},new Integer[]{15,15,15,20});
- Test(new Integer[]{20,110,80,90},new Integer[]{90,20,117,14},
- new Integer[]{17,17,20,17},new Integer[]{15,15,15,20},expected);
- expected = newtonSol(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2},
- new Integer[]{3,1,null,2},new Integer[]{3,2,1,null});
- Test(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2},
- new Integer[]{3,1,null,2},new Integer[]{3,2,1,null},expected);
- }
- }
He añadido nuevos test :/
import java.lang.Math; import java.util.*; public class Kata{ public static double[] newton(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2){ double[] result = new double[4]; double g = 6.674*Math.pow(10,-11); result[0] = (g*m1[0]*m2[0])/Math.pow(r[0],2); result[1] = Math.sqrt((g * m1[1] * m2[1]) / f[1]); result[2] = (f[2]*Math.pow(r[2],2))/(g*m2[2]); result[3] = (f[3]*Math.pow(r[3],2))/(g*m1[3]); return result; } }
- import java.lang.Math;
- import java.util.*;
- public class Kata{
- public static double[] newton(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2){
- double[] result = new double[4];
- double g = 6.674*Math.pow(10,-11);
- result[0] = (g*m1[0]*m2[0])/Math.pow(r[0],2);
- result[1] = Math.sqrt((g * m1[1] * m2[1]) / f[1]);
- result[2] = (f[2]*Math.pow(r[2],2))/(g*m2[2]);
- result[3] = (f[3]*Math.pow(r[3],2))/(g*m1[3]);
return result;- return result;
- }
- }
import java.lang.Math; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { private static void Test(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2,double[] expected) { double[] actual = Kata.newton(f, r, m1, m2); int n=0; for(int i=0;i<4;i++) { if(actual[i]==expected[i]) n++; } assertEquals(4, n); } double[] newtonSol(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2){ double[] result = new double[4]; double g = 6.674*Math.pow(10,-11); result[0] = (g*m1[0]*m2[0])/Math.pow(r[0],2); result[1] = Math.sqrt((g * m1[1] * m2[1]) / f[1]); result[2] = (f[2]*Math.pow(r[2],2))/(g*m2[2]); result[3] = (f[3]*Math.pow(r[3],2))/(g*m1[3]); return result; } @Test void testRandom() { for(int i=0;i<40;i++){ Integer[] f=new Integer[4]; Integer[] r=new Integer[4]; Integer[] m1=new Integer[4]; Integer[] m2=new Integer[4]; for(int d=0;d<4;d++){ f[d]=(int)(Math.random()*250)+10; r[d]=(int)(Math.random()*250)+10; m1[d]=(int)(Math.random()*250)+10; m2[d]=(int)(Math.random()*250)+10; } f[0]=null; r[1]=null; m1[2]=null; m2[3]=null; double[] expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); } } @Test void testFixed(){ Integer[] f={null, 100, 150, 120}; Integer[] r={100, null, 150, 120}; Integer[] m1={150, 100, null, 120}; Integer[] m2={120, 100, 150, null}; double[] expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); f= new Integer[] {null, 106, 150, 140}; r=new Integer[] {100, null, 65, 120}; m1=new Integer[] {150, 112, null, 120}; m2=new Integer[] {120, 448, 150, null}; expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); f=new Integer[] {null, 106, 150, 240}; r=new Integer[] {100, null, 65, 120}; m1=new Integer[] {103, 112, null, 120}; m2=new Integer[] {120, 241, 111, null}; expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); } @Test void testFixed2(){ double [] expected = newtonSol(new Integer[]{null,20,40,60},new Integer[]{20,null,60,40},new Integer[]{20,60,null,40},new Integer[]{20,40,60,null}); Test(new Integer[]{null,20,40,60},new Integer[]{20,null,60,40},new Integer[]{20,60,null,40},new Integer[]{20,40,60,null},expected); expected = newtonSol(new Integer[]{null,110,80,90},new Integer[]{90,null,117,14},new Integer[]{17,17,null,17},new Integer[]{15,15,15,null}); Test(new Integer[]{null,110,80,90},new Integer[]{90,null,117,14},new Integer[]{17,17,null,17},new Integer[]{15,15,15,null},expected); expected = newtonSol(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2},new Integer[]{3,1,null,2},new Integer[]{3,2,1,null}); Test(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2},new Integer[]{3,1,null,2},new Integer[]{3,2,1,null},expected); } }
- import java.lang.Math;
import java.util.*;- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests- class SolutionTest {
- private static void Test(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2,double[] expected) {
- double[] actual = Kata.newton(f, r, m1, m2);
- int n=0;
- for(int i=0;i<4;i++) {
- if(actual[i]==expected[i]) n++;
- }
- assertEquals(4, n);
- }
- double[] newtonSol(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2){
- double[] result = new double[4];
- double g = 6.674*Math.pow(10,-11);
- result[0] = (g*m1[0]*m2[0])/Math.pow(r[0],2);
- result[1] = Math.sqrt((g * m1[1] * m2[1]) / f[1]);
- result[2] = (f[2]*Math.pow(r[2],2))/(g*m2[2]);
- result[3] = (f[3]*Math.pow(r[3],2))/(g*m1[3]);
- return result;
- }
- @Test
- void testRandom() {
- for(int i=0;i<40;i++){
- Integer[] f=new Integer[4];
- Integer[] r=new Integer[4];
- Integer[] m1=new Integer[4];
- Integer[] m2=new Integer[4];
- for(int d=0;d<4;d++){
- f[d]=(int)(Math.random()*250)+10;
- r[d]=(int)(Math.random()*250)+10;
- m1[d]=(int)(Math.random()*250)+10;
- m2[d]=(int)(Math.random()*250)+10;
- }
- f[0]=null;
- r[1]=null;
- m1[2]=null;
- m2[3]=null;
- double[] expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- }
- }
- @Test
- void testFixed(){
- Integer[] f={null, 100, 150, 120};
- Integer[] r={100, null, 150, 120};
- Integer[] m1={150, 100, null, 120};
- Integer[] m2={120, 100, 150, null};
- double[] expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- f= new Integer[] {null, 106, 150, 140};
- r=new Integer[] {100, null, 65, 120};
- m1=new Integer[] {150, 112, null, 120};
- m2=new Integer[] {120, 448, 150, null};
- expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- f=new Integer[] {null, 106, 150, 240};
- r=new Integer[] {100, null, 65, 120};
- m1=new Integer[] {103, 112, null, 120};
- m2=new Integer[] {120, 241, 111, null};
- expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- }
- @Test
- void testFixed2(){
- double [] expected = newtonSol(new Integer[]{null,20,40,60},new Integer[]{20,null,60,40},new Integer[]{20,60,null,40},new Integer[]{20,40,60,null});
- Test(new Integer[]{null,20,40,60},new Integer[]{20,null,60,40},new Integer[]{20,60,null,40},new Integer[]{20,40,60,null},expected);
- expected = newtonSol(new Integer[]{null,110,80,90},new Integer[]{90,null,117,14},new Integer[]{17,17,null,17},new Integer[]{15,15,15,null});
- Test(new Integer[]{null,110,80,90},new Integer[]{90,null,117,14},new Integer[]{17,17,null,17},new Integer[]{15,15,15,null},expected);
- expected = newtonSol(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2},new Integer[]{3,1,null,2},new Integer[]{3,2,1,null});
- Test(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2},new Integer[]{3,1,null,2},new Integer[]{3,2,1,null},expected);
- }
- }