Refactor tests

This commit is contained in:
2022-04-07 17:42:45 +02:00
parent cda144aa2a
commit f156da4803
7 changed files with 102 additions and 167 deletions

View File

@ -1,15 +1,17 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class Aufgabe1Test { public class Aufgabe1Test {
public static void main(String[] args) { @Test
//test classes Body and Vector3 public void testEP2() {
// create two bodies // create two bodies
Body sun = new Body(1.989e30,new Vector3(0,0,0),new Vector3(0,0,0)); Body sun = new Body(SolSystem.SUN);
Body earth = new Body(5.972e24,new Vector3(-1.394555e11,5.103346e10,0),new Vector3(-10308.53,-28169.38,0)); Body earth = new Body(SolSystem.EARTH);
testValue(earth.distanceTo(sun), 1.4850000175024106E11); assertEquals(1.4850000175024106E11, earth.distanceTo(sun));
testValue(sun.distanceTo(earth), 1.4850000175024106E11); assertEquals(1.4850000175024106E11, sun.distanceTo(earth));
for (int i = 0; i < 3600 * 24; i++) { for (int i = 0; i < 3600 * 24; i++) {
Vector3 f1 = earth.gravitationalForce(sun); Vector3 f1 = earth.gravitationalForce(sun);
@ -20,36 +22,9 @@ public class Aufgabe1Test {
} }
// a dummy body to check the correct position after 24h of movement // a dummy body to check the correct position after 24h of movement
Body targetPositionEarth = new Body(1, new Vector3(-1.403250141841815E11, Body targetPositionEarth = new Body(1, new Vector3(-1.403250141841815E11, 4.859202658875631E10, 0.0), new Vector3(0, 0, 0));
4.859202658875631E10, 0.0), new Vector3(0,0,0));
// check distance to target position (should be zero) // check distance to target position (should be zero)
testValue(earth.distanceTo(targetPositionEarth), 0); assertEquals(0, earth.distanceTo(targetPositionEarth));
}
public static void testComparison(Object first, Object second, boolean expected) {
boolean real = first == second;
if (real == expected) {
System.out.println("Successful comparison");
} else {
System.out.println("Comparison NOT successful! Expected value: " + expected + " / Given value: " + real);
}
}
public static void testValue(Object given, Object expected) {
if (given == expected) {
System.out.println("Successful test");
} else {
System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given);
}
}
public static void testValue(double given, double expected) {
if (given < expected + (expected+1)/1e12 && given > expected - (expected+1)/1e12) {
System.out.println("Successful test");
} else {
System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given);
}
} }
} }

View File

@ -1,78 +1,51 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class Aufgabe2Test { public class Aufgabe2Test {
public static void main(String[] args) { @Test
//test classes BodyQueue and BodyForceMap public void testEP2() {
// create three bodies // create three bodies
Body sun = new Body(1.989e30,new Vector3(0,0,0),new Vector3(0,0,0)); Body sun = new Body(SolSystem.SUN);
Body earth = new Body(5.972e24,new Vector3(-1.394555e11,5.103346e10,0),new Vector3(-10308.53,-28169.38,0)); Body earth = new Body(SolSystem.EARTH);
Body mercury = new Body(3.301e23,new Vector3(-5.439054e10,9.394878e9,0),new Vector3(-17117.83,-46297.48,-1925.57)); Body mercury = new Body(SolSystem.MERCURY);
// check basic functions of 'BodyQueue' // check basic functions of 'BodyQueue'
System.out.println("Test1:");
BodyQueue bq = new BodyQueue(2); BodyQueue bq = new BodyQueue(2);
bq.add(mercury); bq.add(mercury);
bq.add(sun); bq.add(sun);
bq.add(earth); bq.add(earth);
testValue(bq.size(), 3); assertEquals(3, bq.size());
testValue(bq.poll(), mercury); assertEquals(mercury, bq.poll());
testValue(bq.poll(), sun); assertEquals(sun, bq.poll());
testValue(bq.poll(), earth); assertEquals(earth, bq.poll());
testValue(bq.size(), 0); assertEquals(0, bq.size());
bq.add(mercury); bq.add(mercury);
bq.add(sun); bq.add(sun);
testValue(bq.size(), 2); assertEquals(2, bq.size());
// check constructor of 'BodyQueue' // check constructor of 'BodyQueue'
BodyQueue bqCopy = new BodyQueue(bq); BodyQueue bqCopy = new BodyQueue(bq);
testComparison(bq, bqCopy, false); assertNotEquals(bq, bqCopy);
testComparison(bq.poll(), bqCopy.poll(), true); assertEquals(bqCopy.poll(), bq.poll());
bq.add(earth); bq.add(earth);
testValue(bq.size(), 2); assertEquals(2, bq.size());
testValue(bqCopy.size(), 1); assertEquals(1, bqCopy.size());
// check basic functions of 'BodyForceMap' // check basic functions of 'BodyForceMap'
System.out.println("Test2:");
BodyForceMap bfm = new BodyForceMap(5); BodyForceMap bfm = new BodyForceMap(5);
bfm.put(earth, earth.gravitationalForce(sun)); bfm.put(earth, earth.gravitationalForce(sun));
bfm.put(sun, sun.gravitationalForce(earth)); bfm.put(sun, sun.gravitationalForce(earth));
testValue(bfm.get(earth).distanceTo(earth.gravitationalForce(sun)),0); assertEquals(0, bfm.get(earth).distanceTo(earth.gravitationalForce(sun)));
testValue(bfm.get(sun).distanceTo(sun.gravitationalForce(earth)),0); assertEquals(0, bfm.get(sun).distanceTo(sun.gravitationalForce(earth)));
bfm.put(earth, new Vector3(0, 0, 0)); bfm.put(earth, new Vector3(0, 0, 0));
testValue(bfm.get(earth).distanceTo(new Vector3(0,0,0)), 0); assertEquals(0, bfm.get(earth).distanceTo(new Vector3(0, 0, 0)));
testValue(bfm.get(mercury),null); assertNull(bfm.get(mercury));
}
public static void testComparison(Object first, Object second, boolean expected) {
boolean real = first == second;
if (real == expected) {
System.out.println("Successful comparison");
} else {
System.out.println("Comparison NOT successful! Expected value: " + expected + " / Given value: " + real);
}
}
public static void testValue(Object given, Object expected) {
if (given == expected) {
System.out.println("Successful test");
} else {
System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given);
}
}
public static void testValue(double given, double expected) {
if (given < expected + (expected+1)/1e12 && given > expected - (expected+1)/1e12) {
System.out.println("Successful test");
} else {
System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given);
}
} }
} }

View File

@ -1,72 +1,67 @@
import java.util.Objects; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class Aufgabe3Test { public class Aufgabe3Test {
public static void main(String[] args) { @Test
//test classes BodyLinkedList and BodyForceTreeMap public void testEP2() {
// create five bodies // create five bodies
Body sun = new Body(1.989e30, new Vector3(0, 0, 0), new Vector3(0, 0, 0)); Body sun = new Body(SolSystem.SUN);
Body earth = new Body(5.972e24, new Vector3(-1.394555e11, 5.103346e10, 0), new Vector3(-10308.53, -28169.38, 0)); Body earth = new Body(SolSystem.EARTH);
Body mercury = new Body(3.301e23, new Vector3(-5.439054e10, 9.394878e9, 0), new Vector3(-17117.83, -46297.48, -1925.57)); Body mercury = new Body(SolSystem.MERCURY);
Body venus = new Body(4.86747e24, new Vector3(-1.707667e10, 1.066132e11, 2.450232e9), new Vector3(-34446.02, -5567.47, 2181.10)); Body venus = new Body(SolSystem.VENUS);
Body mars = new Body(6.41712e23, new Vector3(-1.010178e11, -2.043939e11, -1.591727E9), new Vector3(20651.98, -10186.67, -2302.79)); Body mars = new Body(SolSystem.MARS);
// check basic functions of 'BodyLinkedList' // check basic functions of 'BodyLinkedList'
System.out.println("Test1:");
BodyLinkedList bl = new BodyLinkedList(); BodyLinkedList bl = new BodyLinkedList();
bl.addLast(mercury); bl.addLast(mercury);
bl.addLast(sun); bl.addLast(sun);
bl.addLast(earth); bl.addLast(earth);
testValue(bl.size(), 3); assertEquals(3, bl.size());
testValue(bl.getFirst(), mercury); assertEquals(mercury, bl.getFirst());
testValue(bl.getLast(), earth); assertEquals(earth, bl.getLast());
testValue(bl.get(0), mercury); assertEquals(mercury, bl.get(0));
testValue(bl.get(1), sun); assertEquals(sun, bl.get(1));
testValue(bl.get(2), earth); assertEquals(earth, bl.get(2));
System.out.println("Test2:"); assertEquals(2, bl.indexOf(earth));
testValue(bl.indexOf(earth), 2); assertEquals(1, bl.indexOf(sun));
testValue(bl.indexOf(sun), 1); assertEquals(0, bl.indexOf(mercury));
testValue(bl.indexOf(mercury), 0);
System.out.println("Test3:"); assertEquals(mercury, bl.pollFirst());
testValue(bl.pollFirst(), mercury); assertEquals(earth, bl.pollLast());
testValue(bl.pollLast(), earth); assertEquals(sun, bl.pollFirst());
testValue(bl.pollFirst(), sun);
testValue(bl.size(), 0); assertEquals(0, bl.size());
testValue(bl.getFirst(), null); assertNull(bl.getFirst());
System.out.println("Test4:");
bl.addFirst(earth); bl.addFirst(earth);
bl.addFirst(venus); bl.addFirst(venus);
bl.addFirst(sun); bl.addFirst(sun);
bl.add(1, mercury); bl.add(1, mercury);
bl.add(4, mars); bl.add(4, mars);
testValue(bl.size(), 5); assertEquals(5, bl.size());
testValue(bl.get(0), sun); assertEquals(sun, bl.get(0));
testValue(bl.get(1), mercury); assertEquals(mercury, bl.get(1));
testValue(bl.get(2), venus); assertEquals(venus, bl.get(2));
testValue(bl.get(3), earth); assertEquals(earth, bl.get(3));
testValue(bl.get(4), mars); assertEquals(mars, bl.get(4));
// check constructor of 'BodyLinkedList' // check constructor of 'BodyLinkedList'
BodyLinkedList blCopy = new BodyLinkedList(bl); BodyLinkedList blCopy = new BodyLinkedList(bl);
testComparison(bl, blCopy, false); assertNotEquals(bl, blCopy);
testComparison(bl.pollFirst(), blCopy.pollFirst(), true); assertEquals(blCopy.pollFirst(), bl.pollFirst());
bl.addFirst(sun); bl.addFirst(sun);
testValue(bl.size(), 5); assertEquals(5, bl.size());
testValue(blCopy.size(), 4); assertEquals(4, blCopy.size());
// check basic functions of 'BodyForceTreeMap' // check basic functions of 'BodyForceTreeMap'
System.out.println("Test5:");
BodyForceTreeMap bfm = new BodyForceTreeMap(); BodyForceTreeMap bfm = new BodyForceTreeMap();
bfm.put(earth, earth.gravitationalForce(sun)); bfm.put(earth, earth.gravitationalForce(sun));
bfm.put(sun, sun.gravitationalForce(earth).plus(sun.gravitationalForce(venus))); bfm.put(sun, sun.gravitationalForce(earth).plus(sun.gravitationalForce(venus)));
@ -74,37 +69,11 @@ public class Aufgabe3Test {
bfm.put(mars, mars.gravitationalForce(sun)); bfm.put(mars, mars.gravitationalForce(sun));
bfm.put(mercury, mercury.gravitationalForce(sun)); bfm.put(mercury, mercury.gravitationalForce(sun));
testValue(bfm.get(earth).distanceTo(earth.gravitationalForce(sun)), 0); assertEquals(0, bfm.get(earth).distanceTo(earth.gravitationalForce(sun)));
testValue(bfm.get(sun).distanceTo(sun.gravitationalForce(earth).plus(sun.gravitationalForce(venus))), 0); assertEquals(0, bfm.get(sun).distanceTo(sun.gravitationalForce(earth).plus(sun.gravitationalForce(venus))));
testValue(bfm.put(earth, new Vector3(0, 0, 0)).distanceTo(earth.gravitationalForce(sun)), 0); assertEquals(0, bfm.put(earth, new Vector3(0, 0, 0)).distanceTo(earth.gravitationalForce(sun)));
testValue(bfm.get(mercury).distanceTo(mercury.gravitationalForce(sun)), 0); assertEquals(0, bfm.get(mercury).distanceTo(mercury.gravitationalForce(sun)));
testValue(bfm.get(mercury), mercury.gravitationalForce(sun)); assertEquals(mercury.gravitationalForce(sun), bfm.get(mercury));
}
public static void testComparison(Object first, Object second, boolean expected) {
boolean real = first == second;
if (real == expected) {
System.out.println("Successful comparison");
} else {
System.out.println("Comparison NOT successful! Expected value: " + expected + " / Given value: " + real);
}
}
public static void testValue(Object given, Object expected) {
if (Objects.equals(given, expected)) {
System.out.println("Successful test");
} else {
System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given);
}
}
public static void testValue(double given, double expected) {
if (given < expected + (expected + 1) / 1e12 && given > expected - (expected + 1) / 1e12) {
System.out.println("Successful test");
} else {
System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given);
}
} }
} }

View File

@ -4,7 +4,7 @@ import codedraw.CodeDraw;
* This class represents celestial bodies like stars, planets, asteroids, etc... * This class represents celestial bodies like stars, planets, asteroids, etc...
*/ */
public class Body { public class Body {
private double mass; private final double mass;
private Vector3 massCenter; // position of the mass center. private Vector3 massCenter; // position of the mass center.
private Vector3 currentMovement; private Vector3 currentMovement;
@ -14,6 +14,12 @@ public class Body {
this.currentMovement = currentMovement; this.currentMovement = currentMovement;
} }
public Body(Body other) {
this.mass = other.mass;
this.massCenter = new Vector3(other.massCenter);
this.currentMovement = new Vector3(other.currentMovement);
}
/** /**
* Returns the distance between the mass centers of this body and the specified body 'b'. * Returns the distance between the mass centers of this body and the specified body 'b'.
*/ */

7
src/SolSystem.java Normal file
View File

@ -0,0 +1,7 @@
public class SolSystem {
public static final Body SUN = new Body(1.989e30, new Vector3(0, 0, 0), new Vector3(0, 0, 0));
public static final Body EARTH = new Body(5.972e24, new Vector3(-1.394555e11, 5.103346e10, 0), new Vector3(-10308.53, -28169.38, 0));
public static final Body MERCURY = new Body(3.301e23, new Vector3(-5.439054e10, 9.394878e9, 0), new Vector3(-17117.83, -46297.48, -1925.57));
public static final Body VENUS = new Body(4.86747e24, new Vector3(-1.707667e10, 1.066132e11, 2.450232e9), new Vector3(-34446.02, -5567.47, 2181.10));
public static final Body MARS = new Body(6.41712e23, new Vector3(-1.010178e11, -2.043939e11, -1.591727E9), new Vector3(20651.98, -10186.67, -2302.79));
}

View File

@ -8,7 +8,6 @@ public class SpaceDraw {
* where m and r measured in solar units.) * where m and r measured in solar units.)
*/ */
public static double massToRadius(double mass) { public static double massToRadius(double mass) {
return Simulation.SUN_RADIUS * (Math.pow(mass / Simulation.SUN_MASS, 0.5)); return Simulation.SUN_RADIUS * (Math.pow(mass / Simulation.SUN_MASS, 0.5));
} }
@ -34,7 +33,6 @@ public class SpaceDraw {
* Returns the approximate color of temperature 'kelvin'. * Returns the approximate color of temperature 'kelvin'.
*/ */
private static Color kelvinToColor(int kelvin) { private static Color kelvinToColor(int kelvin) {
double k = kelvin / 100D; double k = kelvin / 100D;
double red = k <= 66 ? 255 : 329.698727446 * Math.pow(k - 60, -0.1332047592); double red = k <= 66 ? 255 : 329.698727446 * Math.pow(k - 60, -0.1332047592);
double green = k <= 66 ? 99.4708025861 * Math.log(k) - 161.1195681661 : 288.1221695283 * Math.pow(k - 60, -0.0755148492); double green = k <= 66 ? 99.4708025861 * Math.log(k) - 161.1195681661 : 288.1221695283 * Math.pow(k - 60, -0.0755148492);
@ -51,12 +49,15 @@ public class SpaceDraw {
* A transformation used in the method 'kelvinToColor'. * A transformation used in the method 'kelvinToColor'.
*/ */
private static int limitAndDarken(double color, int kelvin) { private static int limitAndDarken(double color, int kelvin) {
int kelvinNorm = kelvin - 373; int kelvinNorm = kelvin - 373;
if (color < 0 || kelvinNorm < 0) {
if (color < 0 || kelvinNorm < 0) return 0; return 0;
else if (color > 255) return 255; } else if (color > 255) {
else if (kelvinNorm < 500) return (int) ((color / 256D) * (kelvinNorm / 500D) * 256); return 255;
else return (int) color; } else if (kelvinNorm < 500) {
return (int) ((color / 256D) * (kelvinNorm / 500D) * 256);
} else {
return (int) color;
}
} }
} }

View File

@ -22,6 +22,10 @@ public class Vector3 {
this.z = z; this.z = z;
} }
public Vector3(Vector3 other) {
this(other.x, other.y, other.z);
}
/** /**
* Returns the sum of this vector and vector 'v'. * Returns the sum of this vector and vector 'v'.
*/ */