Merge branch 'main' into ml

This commit is contained in:
ngb
2022-07-14 23:03:08 +02:00
28 changed files with 2020 additions and 341 deletions

View File

@@ -17,9 +17,9 @@ class ColorTest {
assertEquals(255, c.getAlpha());
c = Color.BLUE;
assertEquals(0, c.getRed());
assertEquals(0, c.getGreen());
assertEquals(255, c.getBlue());
assertEquals(49, c.getRed());
assertEquals(197, c.getGreen());
assertEquals(244, c.getBlue());
assertEquals(255, c.getAlpha());
c = new Color(50, 133, 64, 33);
@@ -28,7 +28,7 @@ class ColorTest {
assertEquals(64, c.getBlue());
assertEquals(33, c.getAlpha());
c = new Color(255, 0, 0);
c = new Color(240, 80, 37);
assertEquals(Color.RED, c);
c = new Color(33, 50);
@@ -97,8 +97,9 @@ class ColorTest {
assertEquals(c1, c2);
Color yellow = new Color(255, 255, 0);
assertEquals(java.awt.Color.YELLOW, yellow);
assertNotEquals(java.awt.Color.YELLOW, Color.YELLOW);
assertNotEquals(java.awt.Color.YELLOW, yellow);
assertEquals(yellow, java.awt.Color.YELLOW);
assertNotEquals(Color.YELLOW, java.awt.Color.YELLOW);
}
@Test
@@ -132,7 +133,7 @@ class ColorTest {
@Test
void getRGBColor() {
Color c1 = Color.getRGBColor(0xFFFF0000);
Color c1 = Color.getRGBColor(0xFFF05025);
assertEquals(Color.RED, c1);
}
@@ -157,7 +158,7 @@ class ColorTest {
Color c;
float[] hsl;
c = Color.RED;
c = new Color(255, 0, 0);
hsl = Color.RGBtoHSL(c.getRGBA(), null);
assertArrayEquals(new float[]{0f,1f,.5f}, hsl, 0.0001f);
@@ -183,28 +184,37 @@ class ColorTest {
@Test
void getRGBA() {
Color yellow = new Color(255, 255, 0);
assertEquals(java.awt.Color.YELLOW.getRGB(), Color.YELLOW.getRGBA());
assertEquals(java.awt.Color.YELLOW.getRGB(), yellow.getRGBA());
}
@Test
void getRed() {
Color clr = new Color(123, 92, 0);
assertEquals(123, clr.getRed());
}
@Test
void getGreen() {
Color clr = new Color(123, 92, 0);
assertEquals(92, clr.getGreen());
}
@Test
void getBlue() {
Color clr = new Color(123, 92, 0);
assertEquals(0, clr.getBlue());
}
@Test
void getAlpha() {
Color clr = new Color(123, 92, 0);
assertEquals(255, clr.getAlpha());
Color clr2 = new Color(123, 92, 0, 45);
assertEquals(45, clr2.getAlpha());
}
@Test
void getJavaColor() {
assertEquals(java.awt.Color.YELLOW, Color.YELLOW.getJavaColor());
assertEquals(new java.awt.Color(255, 31, 124), new Color(255, 31, 124).getJavaColor());
}
@@ -212,16 +222,8 @@ class ColorTest {
void brighter() {
}
@Test
void testBrighter() {
}
@Test
void darker() {
}
@Test
void testDarker() {
}
}

View File

@@ -71,23 +71,23 @@ class ConstantsTest {
}
@Test
void b() {
assertTrue(Constants.getBool(true));
assertFalse(Constants.getBool(false));
assertTrue(Constants.getBool(1));
assertFalse(Constants.getBool(0));
assertTrue(Constants.getBool(4.0));
assertFalse(Constants.getBool(0.0));
assertTrue(Constants.getBool(4.0f));
assertFalse(Constants.getBool(0.0f));
assertTrue(Constants.getBool(4L));
assertFalse(Constants.getBool(0L));
assertTrue(Constants.getBool("true"));
assertTrue(Constants.getBool("True"));
assertFalse(Constants.getBool("1"));
assertFalse(Constants.getBool("false"));
assertFalse(Constants.getBool("yes"));
assertFalse(Constants.getBool("no"));
void asBool() {
assertTrue(Constants.asBool(true));
assertFalse(Constants.asBool(false));
assertTrue(Constants.asBool(1));
assertFalse(Constants.asBool(0));
assertTrue(Constants.asBool(4.0));
assertFalse(Constants.asBool(0.0));
assertTrue(Constants.asBool(4.0f));
assertFalse(Constants.asBool(0.0f));
assertTrue(Constants.asBool(4L));
assertFalse(Constants.asBool(0L));
assertTrue(Constants.asBool("true"));
assertTrue(Constants.asBool("True"));
assertFalse(Constants.asBool("1"));
assertFalse(Constants.asBool("false"));
assertFalse(Constants.asBool("yes"));
assertFalse(Constants.asBool("no"));
}
@Test
@@ -128,4 +128,45 @@ class ConstantsTest {
assertEquals(.8f, Math.abs(t/(t+f)), .01f);
}
@Test
void noise() {
double lastNoise = -1.0;
for( int i = 0; i < 100; i++ ) {
double thisNoise = Constants.noise(i * 0.005);
assertInRange(thisNoise);
assertNotEquals(lastNoise, thisNoise);
assertEquals(thisNoise, Constants.noise(i * 0.005), 0.0001);
lastNoise = thisNoise;
}
lastNoise = -1.0;
for( int i = 0; i < 100; i++ ) {
double thisNoise = Constants.noise(i * 0.005, 0.1);
assertInRange(thisNoise);
assertNotEquals(lastNoise, thisNoise);
assertEquals(thisNoise, Constants.noise(i * 0.005, 0.1), 0.0001);
lastNoise = thisNoise;
}
lastNoise = -1.0;
for( int i = 0; i < 100; i++ ) {
double thisNoise = Constants.noise(i * 0.005, 5.5, 100.0/(i+1));
assertInRange(thisNoise);
assertNotEquals(lastNoise, thisNoise);
assertEquals(thisNoise, Constants.noise(i * 0.005, 5.5, 100.0/(i+1)), 0.0001);
lastNoise = thisNoise;
}
}
private void assertInRange( double d ) {
assertFalse(Double.isNaN(d), "Noise value can't be NaN.");
assertTrue(0.0 <= d && 1.0 >= d, "Noise should be in Range 0 to 1. Was <" + d + ">.");
}
}

View File

@@ -38,7 +38,7 @@ public class TestAttraction extends Zeichenmaschine {
posC = new Vector(200, 100);
velC = new Vector(1, 14);
drawing.translate(width /2, height /2);
drawing.translate(canvasWidth /2, canvasHeight /2);
drawing.shear(0.1, 0.5);
recht = new Rectangle(50, 50, 150, 75);
@@ -84,7 +84,7 @@ public class TestAttraction extends Zeichenmaschine {
shapes.clear();
double x = recht.getX();
x = (x+100*delta)% width;
x = (x+100*delta)% canvasWidth;
recht.setX(x);
}

View File

@@ -7,7 +7,6 @@ import schule.ngb.zm.shapes.Rectangle;
import schule.ngb.zm.shapes.Shape;
import java.awt.geom.Point2D;
import java.util.Random;
public class TestShapes extends Zeichenmaschine {
@@ -65,7 +64,7 @@ public class TestShapes extends Zeichenmaschine {
public void shapePositions() {
int pad = 24;
Rectangle bounds = new Rectangle(pad, pad, width-pad, height-pad);
Rectangle bounds = new Rectangle(pad, pad, canvasWidth -pad, canvasHeight -pad);
Rectangle[] rects = new Rectangle[5];
for( int i = 0; i < rects.length; i++ ) {

View File

@@ -0,0 +1,81 @@
package schule.ngb.zm.util;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class FileLoaderTest {
@Test
void loadLines() {
String[] data;
List<String> lines;
data = new String[]{
"Header1,Header2,Header3",
"1.1,1.2,1.3",
"2.1,2.2,2.3",
"3.1,3.2,3.3"
};
lines = FileLoader.loadLines("data_comma.csv");
assertEquals(data.length, lines.size());
for( int i = 0; i < lines.size(); i++ ) {
assertEquals(data[i], lines.get(i));
}
data = new String[]{
"Nöme;Häder2;Straße",
"1.1;1.2;1.3",
"2.1;2.2;2.3",
"3.1;3.2;3.3"
};
lines = FileLoader.loadLines("data_semicolon_latin.csv", FileLoader.ISO_8859_1);
assertEquals(data.length, lines.size());
for( int i = 0; i < lines.size(); i++ ) {
assertEquals(data[i], lines.get(i));
}
}
@Test
void loadText() {
String data;
String text;
data = "Header1,Header2,Header3\n" +
"1.1,1.2,1.3\n" +
"2.1,2.2,2.3\n" +
"3.1,3.2,3.3\n";
text = FileLoader.loadText("data_comma.csv");
assertEquals(data, text);
data = "Nöme;Häder2;Straße\n" +
"1.1;1.2;1.3\n" +
"2.1;2.2;2.3\n" +
"3.1;3.2;3.3\n";
text = FileLoader.loadText("data_semicolon_latin.csv", FileLoader.ISO_8859_1);
assertEquals(data, text);
}
@Test
void loadCsv() {
double[][] data;
double[][] csv;
data = new double[][]{
{1.1,1.2,1.3},
{2.1,2.2,2.3},
{3.1,3.2,3.3}
};
csv = FileLoader.loadDoubles("data_comma.csv", ',', true);
assertArrayEquals(data, csv);
csv = FileLoader.loadDoubles("data_semicolon_latin.csv", ';', true, FileLoader.ISO_8859_1);
assertArrayEquals(data, csv);
}
}

View File

@@ -0,0 +1,126 @@
package schule.ngb.zm.util;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class NoiseTest {
@Test
void noise() {
int N = 1000;
Noise perlin = new Noise();
Noise perlin1 = new Noise(1001);
Noise perlin2 = new Noise(2002);
Noise perlin3 = new Noise(2002);
int pp1 = 0, p1p2 = 0;
for( int i = 1; i < N; i++ ) {
double x = i * 0.005;
assertInRange(perlin.noise(x));
assertInRange(perlin1.noise(x));
assertInRange(perlin2.noise(x));
if( perlin.noise(x) == perlin1.noise(x) ) {
pp1++;
}
if( perlin1.noise(x) == perlin2.noise(x) ) {
p1p2++;
}
assertEquals(perlin2.noise(x), perlin3.noise(x), "perlin2 and perlin3 should be equal for input " + x);
}
assertTrue(pp1 < N * 0.75, "perlin and perlin1 should not be equal more than 75% (was " + pp1 / 1000.0 + ")");
assertTrue(p1p2 < N * 0.75, "perlin1 and perlin2 should not be equal more than 75% (was " + p1p2 / 1000.0 + ")");
}
private void assertInRange( double d ) {
assertTrue(0.0 <= d && 1.0 >= d);
}
private void assertInRange( double d, double min, double max ) {
assertTrue(min <= d && max >= d);
}
@Test
void noise2d() {
int N = 100;
Noise perlin = new Noise();
Noise perlin1 = new Noise(1001);
Noise perlin2 = new Noise(2002);
Noise perlin3 = new Noise(2002);
int pp1 = 0, p1p2 = 0;
for( int i = 1; i < N; i++ ) {
for( int j = 0; j < N; j++ ) {
double x = i * 0.005;
double y = j * 0.001;
assertInRange(perlin.noise(x, y));
assertInRange(perlin1.noise(x, y));
assertInRange(perlin2.noise(x, y));
if( perlin.noise(x, y) == perlin1.noise(x, y) ) {
pp1++;
}
if( perlin1.noise(x, y) == perlin2.noise(x, y) ) {
p1p2++;
}
assertEquals(perlin2.noise(x, y), perlin3.noise(x, y), "perlin2 and perlin3 should be equal for input " + x + "," + y);
}
}
assertTrue(pp1 < N * N * 0.75, "perlin and perlin1 should not be equal more than 75% (was " + (pp1 / 1.0 * N * N) + ")");
assertTrue(p1p2 < N * N * 0.75, "perlin1 and perlin2 should not be equal more than 75% (was " + (p1p2 / 1.0 * N * N) + ")");
}
@Test
void noise3d() {
int N = 100;
Noise perlin = new Noise();
Noise perlin1 = new Noise(1001);
Noise perlin2 = new Noise(2002);
Noise perlin3 = new Noise(2002);
int pp1 = 0, p1p2 = 0;
for( int i = 1; i < N; i++ ) {
for( int j = 0; j < N; j++ ) {
for( int k = 0; k < N; k++ ) {
double x = i * 0.005;
double y = j * 0.001;
double z = k * 0.004;
assertInRange(perlin.noise(x, y, z));
assertInRange(perlin1.noise(x, y, z));
assertInRange(perlin2.noise(x, y, z));
if( perlin.noise(x, y, z) == perlin1.noise(x, y, z) ) {
pp1++;
}
if( perlin1.noise(x, y, z) == perlin2.noise(x, y, z) ) {
p1p2++;
}
assertEquals(perlin2.noise(x, y, z), perlin3.noise(x, y, z), "perlin2 and perlin3 should be equal for input " + x + "," + y);
}
}
}
assertTrue(pp1 < N * N * N* 0.75, "perlin and perlin1 should not be equal more than 75% (was " + (pp1 / 1.0 * N * N * N) + ")");
assertTrue(p1p2 < N * N * N * 0.75, "perlin1 and perlin2 should not be equal more than 75% (was " + (p1p2 / 1.0 * N * N * N) + ")");
}
@Test
void range() {
Noise perlin = new Noise(1001);
perlin.setRange(100, 255);
for( int i = 0; i < 1000; i++ ) {
assertInRange(perlin.noise(i * 0.005), 100, 255);
}
perlin.setRange(-100, 100);
for( int i = 0; i < 1000; i++ ) {
assertInRange(perlin.noise(i * 0.005), -100, 100);
}
}
}

View File

@@ -0,0 +1,4 @@
Header1,Header2,Header3
1.1,1.2,1.3
2.1,2.2,2.3
3.1,3.2,3.3
1 Header1 Header2 Header3
2 1.1 1.2 1.3
3 2.1 2.2 2.3
4 3.1 3.2 3.3

View File

@@ -0,0 +1,4 @@
Header1;Header2;Header3
1.1;1.2;1.3
2.1;2.2;2.3
3.1;3.2;3.3
1 Header1 Header2 Header3
2 1.1 1.2 1.3
3 2.1 2.2 2.3
4 3.1 3.2 3.3

View File

@@ -0,0 +1,4 @@
Nöme;Häder2;Straße
1.1;1.2;1.3
2.1;2.2;2.3
3.1;3.2;3.3
1 Nöme Häder2 Straße
2 1.1 1.2 1.3
3 2.1 2.2 2.3
4 3.1 3.2 3.3