added contents

This commit is contained in:
Mark Minas
2024-09-18 17:04:31 +02:00
parent d28b17eba5
commit 71a4ac8d12
176 changed files with 51198 additions and 0 deletions

View File

@@ -0,0 +1,109 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.util;
import org.junit.Test;
import static java.lang.Math.min;
import static org.junit.Assert.assertEquals;
import static pp.util.FloatMath.DEG_TO_RAD;
import static pp.util.FloatMath.ZERO_TOLERANCE;
import static pp.util.FloatMath.cos;
import static pp.util.FloatMath.sin;
public class AngleTest {
@Test
public void compareAngles() {
for (int i = 0; i < 360; i++) {
final Angle u = Angle.fromDegrees(i);
for (int j = 0; j < 360; j++) {
final Angle v = Angle.fromDegrees(j);
assertEquals("compare " + i + "° and " + j + "°", Integer.compare(i, j), (Object) u.compareTo(v));
}
}
}
@Test
public void addAngles() {
for (int i = 0; i < 360; i++) {
final Angle u = Angle.fromDegrees(i);
for (int j = 0; j < 360; j++) {
final Angle v = Angle.fromDegrees(j);
final Angle sum = u.plus(v);
assertEquals(i + "° + " + j + "°, x coordinate", cos((i + j) * DEG_TO_RAD), sum.x, ZERO_TOLERANCE);
assertEquals(i + "° + " + j + "°, y coordinate", sin((i + j) * DEG_TO_RAD), sum.y, ZERO_TOLERANCE);
}
}
}
@Test
public void subtractAngles() {
for (int i = 0; i < 360; i++) {
final Angle u = Angle.fromDegrees(i);
for (int j = 0; j < 360; j++) {
final Angle v = Angle.fromDegrees(j);
final Angle diff = u.minus(v);
assertEquals(i + "° - " + j + "°, x coordinate", cos((i - j) * DEG_TO_RAD), diff.x, ZERO_TOLERANCE);
assertEquals(i + "° - " + j + "°, y coordinate", sin((i - j) * DEG_TO_RAD), diff.y, ZERO_TOLERANCE);
}
}
}
@Test
public void minAngle() {
for (int i = 0; i < 360; i++) {
final Angle u = Angle.fromDegrees(i);
for (int j = 0; j < 360; j++) {
final Angle v = Angle.fromDegrees(j);
final Angle diff = Angle.min(u, v);
assertEquals(i + "° - " + j + "°, x coordinate", cos(min(i, j) * DEG_TO_RAD), diff.x, ZERO_TOLERANCE);
assertEquals(i + "° - " + j + "°, y coordinate", sin(min(i, j) * DEG_TO_RAD), diff.y, ZERO_TOLERANCE);
}
}
}
@Test
public void bisector() {
for (int right = 0; right < 360; right++) {
final Angle rightAngle = Angle.fromDegrees(right);
for (int add = 0; add < 360; add++) {
final int left = right + add;
final Angle bisector = Angle.fromDegrees(left).bisector(rightAngle);
final float exp = (right + 0.5f * add) * DEG_TO_RAD;
assertEquals("left=" + left + "° / right=" + right + "°, x coordinate", cos(exp), bisector.x, ZERO_TOLERANCE);
assertEquals("left=" + left + "° / right=" + right + "°, y coordinate", sin(exp), bisector.y, ZERO_TOLERANCE);
}
}
}
@Test
public void leftOf() {
for (int right = 0; right < 360; right++) {
final Angle rightAngle = Angle.fromDegrees(right);
for (int add = 1; add < 360; add++)
if (add != 180) {
final int left = right + add;
final Angle leftAngle = Angle.fromDegrees(left);
assertEquals(left + "° left of " + right + "°", add < 180, leftAngle.leftOf(rightAngle));
}
}
}
@Test
public void rightOf() {
for (int right = 0; right < 360; right++) {
final Angle rightAngle = Angle.fromDegrees(right);
for (int add = 1; add < 360; add++)
if (add != 180) {
final int left = right + add;
final Angle leftAngle = Angle.fromDegrees(left);
assertEquals(left + "° right of " + right + "°", add > 180, leftAngle.rightOf(rightAngle));
}
}
}
}

View File

@@ -0,0 +1,39 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.util;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static pp.util.FloatMath.ZERO_TOLERANCE;
public class IntervalTest {
private Interval interval;
@Before
public void setUp() {
interval = new Interval(0f, 1f);
}
@Test
public void contains() {
assertTrue(interval.contains(0.5f));
assertTrue(interval.contains(0f));
assertTrue(interval.contains(1f));
assertFalse(interval.contains(1.5f));
assertFalse(interval.contains(-0.5f));
}
@Test
public void matches() {
assertTrue(interval.matches(new Interval(0f, 1f), ZERO_TOLERANCE));
assertFalse(interval.matches(new Interval(0f, 0.99f), ZERO_TOLERANCE));
}
}

View File

@@ -0,0 +1,36 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.util;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class RandomPositionIteratorTest {
public static final int WIDTH = 15;
public static final int HEIGHT = 25;
@Test
public void permutation() {
for (int i = 0; i < 10; i++) {
final List<Position> permutation = new ArrayList<>();
RandomPositionIterator.floatPoints(WIDTH, HEIGHT).forEachRemaining(permutation::add);
assertEquals(WIDTH * HEIGHT, permutation.size());
assertEquals(permutation.size(), new HashSet<>(permutation).size());
for (Position w : permutation) {
assertTrue(w.toString(), 0 <= w.getX() && w.getX() < WIDTH);
assertTrue(w.toString(), 0 <= w.getY() && w.getY() < HEIGHT);
}
}
}
}

View File

@@ -0,0 +1,201 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.util;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static pp.util.FloatMath.FLT_EPSILON;
import static pp.util.FloatMath.PI;
import static pp.util.FloatMath.ZERO_TOLERANCE;
import static pp.util.FloatMath.cos;
import static pp.util.FloatMath.sqr;
import static pp.util.FloatMath.sqrt;
import static pp.util.FloatMath.tan;
public class SegmentTest {
private static final float SQRT2 = sqrt(2f);
private static final Segment segment1 = new Segment(new FloatPoint(1f, -1f), new FloatPoint(1f, 1f));
private static final Segment segment2 = new Segment(new FloatPoint(SQRT2, 0f), new FloatPoint(0f, SQRT2));
private static final Segment segment3 = new Segment(new FloatPoint(2f, -1f), new FloatPoint(2f, 1f));
private static final Segment segment4 = new Segment(new FloatPoint(SQRT2, -1f), new FloatPoint(SQRT2, 1f));
private static final Segment segment5 = new Segment(new FloatPoint(-SQRT2, 2f * SQRT2), new FloatPoint(2f * SQRT2, -SQRT2));
private static final Segment segment6 = new Segment(new FloatPoint(0f, 0f), new FloatPoint(SQRT2, 1f));
private static final FloatPoint ZERO = new FloatPoint(0f, 0f);
public static final FloatPoint ONE_UP = new FloatPoint(0f, 1f);
public static final FloatPoint ONE_DOWN = new FloatPoint(0f, -1f);
@Test
public void dist1() {
assertEquals(1f, segment1.dist(ZERO, 0f), FLT_EPSILON);
assertEquals(1f, segment1.dist(ONE_UP, 0f), FLT_EPSILON);
assertEquals(1f, segment1.dist(ONE_DOWN, 0f), FLT_EPSILON);
assertEquals(1f / cos(0.125f * PI), segment1.dist(ZERO, 0.125f * PI), FLT_EPSILON);
assertEquals(1f / cos(0.125f * PI), segment1.dist(ONE_UP, 0.125f * PI), FLT_EPSILON);
assertEquals(1f / cos(0.125f * PI), segment1.dist(ONE_DOWN, 0.125f * PI), FLT_EPSILON);
assertEquals(1f / cos(0.125f * PI), segment1.dist(ZERO, -0.125f * PI), FLT_EPSILON);
assertEquals(1f / cos(0.125f * PI), segment1.dist(ONE_UP, -0.125f * PI), FLT_EPSILON);
assertEquals(1f / cos(0.125f * PI), segment1.dist(ONE_DOWN, -0.125f * PI), FLT_EPSILON);
assertEquals(SQRT2, segment1.dist(ZERO, 0.25f * PI), FLT_EPSILON);
assertEquals(SQRT2, segment1.dist(ZERO, -0.25f * PI), FLT_EPSILON);
}
@Test
public void dist2() {
assertEquals(1f, segment2.dist(ZERO, 0.25f * PI), FLT_EPSILON);
assertEquals(SQRT2, segment2.dist(ZERO, 0f), FLT_EPSILON);
assertEquals(SQRT2, segment2.dist(ZERO, 0.5f * PI), FLT_EPSILON);
assertEquals(1f / cos(0.125f * PI), segment2.dist(ZERO, 0.375f * PI), FLT_EPSILON);
assertEquals(SQRT2, segment2.dist(ZERO, PI / 2f), FLT_EPSILON);
}
@Test
public void quotient1() {
assertEquals(0.5f, segment1.quotient(ZERO, 0f), FLT_EPSILON);
assertEquals(1f, segment1.quotient(ONE_UP, 0f), FLT_EPSILON);
assertEquals(0f, segment1.quotient(ONE_DOWN, 0f), FLT_EPSILON);
assertEquals(0.5f * tan(0.125f * PI) + 0.5f, segment1.quotient(ZERO, 0.125f * PI), FLT_EPSILON);
assertEquals(0.5f * tan(0.125f * PI) + 1f, segment1.quotient(ONE_UP, 0.125f * PI), FLT_EPSILON);
assertEquals(0.5f * tan(0.125f * PI), segment1.quotient(ONE_DOWN, 0.125f * PI), FLT_EPSILON);
assertEquals(0.5f - 0.5f * tan(0.125f * PI), segment1.quotient(ZERO, -0.125f * PI), FLT_EPSILON);
assertEquals(1f - 0.5f * tan(0.125f * PI), segment1.quotient(ONE_UP, -0.125f * PI), FLT_EPSILON);
assertEquals(-0.5f * tan(0.125f * PI), segment1.quotient(ONE_DOWN, -0.125f * PI), FLT_EPSILON);
assertEquals(1f, segment1.quotient(ZERO, 0.25f * PI), FLT_EPSILON);
assertEquals(0f, segment1.quotient(ZERO, -0.25f * PI), FLT_EPSILON);
}
@Test
public void quotient2() {
assertEquals(0.5f, segment2.quotient(ZERO, 0.25f * PI), FLT_EPSILON);
assertEquals(0f, segment2.quotient(ZERO, 0f), FLT_EPSILON);
assertEquals(1f, segment2.quotient(ZERO, 0.5f * PI), FLT_EPSILON);
assertEquals(0.5f * SQRT2, segment2.quotient(ZERO, 0.375f * PI), FLT_EPSILON);
assertEquals(1f - 0.5f * SQRT2, segment2.quotient(ZERO, 0.125f * PI), FLT_EPSILON);
}
@Test
public void project() {
assertEquals(0.5f, segment1.project(ZERO), FLT_EPSILON);
assertEquals(0.5f, segment2.project(ZERO), FLT_EPSILON);
}
@Test
public void minDistanceSquared1() {
assertEquals(0f, segment1.minDistanceSquared(segment1), ZERO_TOLERANCE);
assertEquals(0f, segment1.minDistanceSquared(segment2), ZERO_TOLERANCE);
assertEquals(0f, segment2.minDistanceSquared(segment1), ZERO_TOLERANCE);
assertEquals(1f, segment1.minDistanceSquared(segment3), ZERO_TOLERANCE);
assertEquals(1f, segment3.minDistanceSquared(segment1), ZERO_TOLERANCE);
assertEquals(sqr(SQRT2 - 1f), segment1.minDistanceSquared(segment4), ZERO_TOLERANCE);
assertEquals(sqr(SQRT2 - 1f), segment4.minDistanceSquared(segment1), ZERO_TOLERANCE);
assertEquals(0f, segment1.minDistanceSquared(segment5), ZERO_TOLERANCE);
assertEquals(0f, segment5.minDistanceSquared(segment1), ZERO_TOLERANCE);
assertEquals(0f, segment1.minDistanceSquared(segment6), ZERO_TOLERANCE);
assertEquals(0f, segment6.minDistanceSquared(segment1), ZERO_TOLERANCE);
assertEquals(0f, segment2.minDistanceSquared(segment2), ZERO_TOLERANCE);
assertEquals(sqr(2f - SQRT2), segment2.minDistanceSquared(segment3), ZERO_TOLERANCE);
assertEquals(sqr(2f - SQRT2), segment3.minDistanceSquared(segment2), ZERO_TOLERANCE);
assertEquals(0f, segment2.minDistanceSquared(segment4), ZERO_TOLERANCE);
assertEquals(0f, segment4.minDistanceSquared(segment2), ZERO_TOLERANCE);
assertEquals(0f, segment2.minDistanceSquared(segment5), ZERO_TOLERANCE);
assertEquals(0f, segment5.minDistanceSquared(segment2), ZERO_TOLERANCE);
assertEquals(0f, segment2.minDistanceSquared(segment6), ZERO_TOLERANCE);
assertEquals(0f, segment6.minDistanceSquared(segment2), ZERO_TOLERANCE);
assertEquals(0f, segment3.minDistanceSquared(segment3), ZERO_TOLERANCE);
assertEquals(sqr(2f - SQRT2), segment3.minDistanceSquared(segment4), ZERO_TOLERANCE);
assertEquals(sqr(2f - SQRT2), segment4.minDistanceSquared(segment3), ZERO_TOLERANCE);
assertEquals(0f, segment3.minDistanceSquared(segment5), ZERO_TOLERANCE);
assertEquals(0f, segment5.minDistanceSquared(segment3), ZERO_TOLERANCE);
assertEquals(sqr(2f - SQRT2), segment3.minDistanceSquared(segment6), ZERO_TOLERANCE);
assertEquals(sqr(2f - SQRT2), segment6.minDistanceSquared(segment3), ZERO_TOLERANCE);
assertEquals(0f, segment4.minDistanceSquared(segment4), ZERO_TOLERANCE);
assertEquals(0f, segment4.minDistanceSquared(segment5), ZERO_TOLERANCE);
assertEquals(0f, segment5.minDistanceSquared(segment4), ZERO_TOLERANCE);
assertEquals(0f, segment4.minDistanceSquared(segment6), ZERO_TOLERANCE);
assertEquals(0f, segment6.minDistanceSquared(segment4), ZERO_TOLERANCE);
assertEquals(0f, segment5.minDistanceSquared(segment5), ZERO_TOLERANCE);
assertEquals(0f, segment5.minDistanceSquared(segment6), ZERO_TOLERANCE);
assertEquals(0f, segment6.minDistanceSquared(segment5), ZERO_TOLERANCE);
assertEquals(0f, segment6.minDistanceSquared(segment6), ZERO_TOLERANCE);
}
@Test
public void minDistanceSquared2() {
final Segment s1 = new Segment(new FloatPoint(0f, 0f), new FloatPoint(2f, 1f));
for (int i = -20; i <= 40; i++) {
final float x = i * 0.1f;
final Segment s2 = new Segment(new FloatPoint(x, 2f), new FloatPoint(x + 2f, -2f));
final float dist;
if (i <= -10)
dist = 0.8f * sqr(1f + x);
else if (i <= 15)
dist = 0f;
else
dist = 0.8f * sqr(x - 1.5f);
assertEquals("x = " + x, dist, s1.minDistanceSquared(s2), ZERO_TOLERANCE);
assertEquals("x = " + x, dist, s2.minDistanceSquared(s1), ZERO_TOLERANCE);
}
}
@Test
public void minDistanceSquared3() {
final Segment s1 = new Segment(new FloatPoint(0f, 0f), new FloatPoint(2f, 1f));
for (float i = -30; i <= 30; i++) {
final float x = i * 0.1f;
final Segment s2 = new Segment(new FloatPoint(x, 0.5f * x), new FloatPoint(x + 2f, 0.5f * x + 1f));
final float dist;
if (i <= -20)
dist = 1.25f * sqr(2f + x);
else if (i <= 20)
dist = 0f;
else
dist = 1.25f * sqr(x - 2f);
assertEquals("x = " + x, dist, s1.minDistanceSquared(s2), ZERO_TOLERANCE);
assertEquals("x = " + x, dist, s2.minDistanceSquared(s1), ZERO_TOLERANCE);
}
}
@Test
public void minDistanceSquared4() {
final Segment s1 = new Segment(new FloatPoint(0f, 0f), new FloatPoint(3f, 1.5f));
for (float i = -30; i <= 50; i++) {
final float x = i * 0.1f;
final float y = 1f - 0.5f * x;
final Segment s2 = new Segment(new FloatPoint(x, 1f), new FloatPoint(x, 1f));
final float dist;
if (i <= -5)
dist = sqr(x) + 1f;
else if (i <= 32)
dist = 0.2f * sqr(x - 2f);
else
dist = sqr(x - 3f) + 0.25f;
assertEquals("x = " + x, dist, s1.minDistanceSquared(s2), ZERO_TOLERANCE);
assertEquals("x = " + x, dist, s2.minDistanceSquared(s1), ZERO_TOLERANCE);
}
}
}

View File

@@ -0,0 +1,101 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.util.config;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class ConfigTest {
private TestConfig config;
@Before
public void setUp() {
config = new TestConfig();
}
@Test
public void testReadFromProperties() {
Properties properties = new Properties();
properties.setProperty("test.string", "hello"); //NON-NLS
properties.setProperty("test.int", "42");
properties.setProperty("test.boolean", "true"); //NON-NLS
properties.setProperty("test.intArray", "1; 2 ;3;4");
config.readFrom(properties);
assertEquals("hello", config.getTestString());
assertEquals(42, config.getTestInt());
assertTrue(config.isTestBoolean());
assertArrayEquals(new int[]{1, 2, 3, 4}, config.getTestIntArray());
}
@Test
public void testReadFromFile() throws IOException {
Properties properties = new Properties();
properties.setProperty("test.string", "fileTest");
properties.setProperty("test.int", "24");
properties.setProperty("test.boolean", "false"); //NON-NLS
properties.setProperty("test.intArray", "10;20;30");
File tempFile = File.createTempFile("testConfig", ".properties");
try (FileWriter writer = new FileWriter(tempFile)) {
properties.store(writer, null);
}
config.readFrom(tempFile);
assertEquals("fileTest", config.getTestString());
assertEquals(24, config.getTestInt());
assertFalse(config.isTestBoolean());
assertArrayEquals(new int[]{10, 20, 30}, config.getTestIntArray());
// Clean up
tempFile.delete();
}
@Test
public void testConvertToType() {
assertEquals(42, config.convertToType("42", int.class));
assertEquals(true, config.convertToType("true", boolean.class)); //NON-NLS
assertEquals(3.14, config.convertToType("3.14", double.class));
}
@Test(expected = IllegalArgumentException.class)
public void testConvertToTypeWithUnsupportedType() {
config.convertToType("unsupported", Object.class); //NON-NLS
}
@Test
public void testToString() {
Properties properties = new Properties();
properties.setProperty("test.string", "stringValue");
properties.setProperty("test.int", "123");
properties.setProperty("test.boolean", "true"); //NON-NLS
properties.setProperty("test.intArray", "5;6;7");
config.readFrom(properties);
String expected = "[\ntest.boolean -> testBoolean = true,\n" +
"test.int -> testInt = 123,\n" +
"test.intArray -> testIntArray = {5, 6, 7},\n" +
"test.string -> testString = stringValue\n" +
"]";
assertEquals(expected, config.toString());
}
}

View File

@@ -0,0 +1,40 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.util.config;
class TestConfig extends Config {
@Property("test.string")
private String testString;
@Property("test.int")
private int testInt;
@Property("test.boolean")
private boolean testBoolean;
@Property("test.intArray")
@Separator(";")
private int[] testIntArray;
// Getters for testing
public String getTestString() {
return testString;
}
public int getTestInt() {
return testInt;
}
public boolean isTestBoolean() {
return testBoolean;
}
public int[] getTestIntArray() {
return testIntArray;
}
}