You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2021/06/30 07:54:30 UTC
[commons-rng] branch master updated: Sonar fix: rename 'test'
method to 'contains'
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git
The following commit(s) were added to refs/heads/master by this push:
new 04cd8b6 Sonar fix: rename 'test' method to 'contains'
04cd8b6 is described below
commit 04cd8b6f82ad72f03db97e470edc5496b39c23a1
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Jun 30 08:54:25 2021 +0100
Sonar fix: rename 'test' method to 'contains'
Rename the helper classes to better represent their functionality.
---
.../rng/sampling/shape/TetrahedronSamplerTest.java | 79 +++++++++++-----------
.../rng/sampling/shape/TriangleSamplerTest.java | 64 +++++++++---------
2 files changed, 72 insertions(+), 71 deletions(-)
diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TetrahedronSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TetrahedronSamplerTest.java
index 23db5b7..f4c4f10 100644
--- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TetrahedronSamplerTest.java
+++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TetrahedronSamplerTest.java
@@ -183,12 +183,12 @@ public class TetrahedronSamplerTest {
// To determine the sample is inside the correct tetrahedron it is projected to the
// 4 faces of the tetrahedron along the face normals. The distance should be negative
// when the face normals are orientated outwards.
- final InsideTetrahedron[] insides = {new InsideTetrahedron(d, f, b, c),
- new InsideTetrahedron(d, f, c, g),
- new InsideTetrahedron(d, f, g, h),
- new InsideTetrahedron(d, f, h, e),
- new InsideTetrahedron(d, f, e, a),
- new InsideTetrahedron(d, f, a, b)};
+ final Tetrahedron[] tetrahedrons = {new Tetrahedron(d, f, b, c),
+ new Tetrahedron(d, f, c, g),
+ new Tetrahedron(d, f, g, h),
+ new Tetrahedron(d, f, h, e),
+ new Tetrahedron(d, f, e, a),
+ new Tetrahedron(d, f, a, b)};
final int samples = expected.length * samplesPerBin;
for (int n = 0; n < 1; n++) {
@@ -198,7 +198,7 @@ public class TetrahedronSamplerTest {
for (int i = 0; i < samples; i += 6) {
for (int j = 0; j < 6; j++) {
addObservation(samplers[j].sample(), observed, bins, binsXy,
- lx, ly, lz, sx, sy, sz, insides[j]);
+ lx, ly, lz, sx, sy, sz, tetrahedrons[j]);
}
}
final double p = new ChiSquareTest().chiSquareTest(expected, observed);
@@ -224,17 +224,17 @@ public class TetrahedronSamplerTest {
* @param sx the scale to convert the x coordinate to the x bin
* @param sy the scale to convert the y coordinate to the y bin
* @param sz the scale to convert the z coordinate to the z bin
- * @param inside the inside tetrahedron test
+ * @param tetrahedron the tetrahedron the sample should be within
*/
// CHECKSTYLE: stop ParameterNumberCheck
private static void addObservation(double[] v, long[] observed,
int binsX, int binsXy,
double lx, double ly, double lz,
double sx, double sy, double sz,
- InsideTetrahedron inside) {
+ Tetrahedron tetrahedron) {
Assert.assertEquals(3, v.length);
// Test the point is inside the correct tetrahedron
- Assert.assertTrue("Not inside the tetrahedron", inside.test(v));
+ Assert.assertTrue("Not inside the tetrahedron", tetrahedron.contains(v));
final double x = v[0];
final double y = v[1];
final double z = v[2];
@@ -309,48 +309,48 @@ public class TetrahedronSamplerTest {
}
/**
- * Test the inside tetrahedron predicate.
+ * Test the tetrahedron contains predicate.
*/
@Test
- public void testInsideTetrahedron() {
+ public void testTetrahedronContains() {
final double[][] c1 = new double[][] {
{1, 1, 1}, {1, -1, -1}, {-1, -1, 1}, {-1, 1, -1}
};
- final InsideTetrahedron inside = new InsideTetrahedron(c1[0], c1[1], c1[2], c1[3]);
+ final Tetrahedron tetrahedron = new Tetrahedron(c1[0], c1[1], c1[2], c1[3]);
// Testing points on the vertices, edges or faces are subject to floating point error
final double epsilon = 1e-14;
// Vertices
for (int i = 0; i < 4; i++) {
- Assert.assertTrue(inside.test(c1[i], epsilon));
+ Assert.assertTrue(tetrahedron.contains(c1[i], epsilon));
}
// Edge
- Assert.assertTrue(inside.test(new double[] {1, 0, 0}, epsilon));
- Assert.assertTrue(inside.test(new double[] {0.5, 0.5, 1}, epsilon));
+ Assert.assertTrue(tetrahedron.contains(new double[] {1, 0, 0}, epsilon));
+ Assert.assertTrue(tetrahedron.contains(new double[] {0.5, 0.5, 1}, epsilon));
// Just inside the edge
- Assert.assertTrue(inside.test(new double[] {1 - 1e-10, 0, 0}));
- Assert.assertTrue(inside.test(new double[] {0.5, 0.5, 1 - 1e-10}));
+ Assert.assertTrue(tetrahedron.contains(new double[] {1 - 1e-10, 0, 0}));
+ Assert.assertTrue(tetrahedron.contains(new double[] {0.5, 0.5, 1 - 1e-10}));
// Just outside the edge
- Assert.assertFalse(inside.test(new double[] {1, 0, 1e-10}, epsilon));
- Assert.assertFalse(inside.test(new double[] {0.5, 0.5 + 1e-10, 1}, epsilon));
+ Assert.assertFalse(tetrahedron.contains(new double[] {1, 0, 1e-10}, epsilon));
+ Assert.assertFalse(tetrahedron.contains(new double[] {0.5, 0.5 + 1e-10, 1}, epsilon));
// Face
double x = 1.0 / 3;
- Assert.assertTrue(inside.test(new double[] {x, -x, x}, epsilon));
- Assert.assertTrue(inside.test(new double[] {-x, -x, -x}, epsilon));
- Assert.assertTrue(inside.test(new double[] {x, x, -x}, epsilon));
- Assert.assertTrue(inside.test(new double[] {-x, x, x}, epsilon));
+ Assert.assertTrue(tetrahedron.contains(new double[] {x, -x, x}, epsilon));
+ Assert.assertTrue(tetrahedron.contains(new double[] {-x, -x, -x}, epsilon));
+ Assert.assertTrue(tetrahedron.contains(new double[] {x, x, -x}, epsilon));
+ Assert.assertTrue(tetrahedron.contains(new double[] {-x, x, x}, epsilon));
// Just outside the face
x += 1e-10;
- Assert.assertFalse(inside.test(new double[] {x, -x, x}, epsilon));
- Assert.assertFalse(inside.test(new double[] {-x, -x, -x}, epsilon));
- Assert.assertFalse(inside.test(new double[] {x, x, -x}, epsilon));
- Assert.assertFalse(inside.test(new double[] {-x, x, x}, epsilon));
+ Assert.assertFalse(tetrahedron.contains(new double[] {x, -x, x}, epsilon));
+ Assert.assertFalse(tetrahedron.contains(new double[] {-x, -x, -x}, epsilon));
+ Assert.assertFalse(tetrahedron.contains(new double[] {x, x, -x}, epsilon));
+ Assert.assertFalse(tetrahedron.contains(new double[] {-x, x, x}, epsilon));
// Inside
- Assert.assertTrue(inside.test(new double[] {0, 0, 0}));
- Assert.assertTrue(inside.test(new double[] {0.5, 0.25, -0.1}));
+ Assert.assertTrue(tetrahedron.contains(new double[] {0, 0, 0}));
+ Assert.assertTrue(tetrahedron.contains(new double[] {0.5, 0.25, -0.1}));
// Outside
- Assert.assertFalse(inside.test(new double[] {0, 20, 0}));
- Assert.assertFalse(inside.test(new double[] {-20, 0, 0}));
- Assert.assertFalse(inside.test(new double[] {6, 6, 4}));
+ Assert.assertFalse(tetrahedron.contains(new double[] {0, 20, 0}));
+ Assert.assertFalse(tetrahedron.contains(new double[] {-20, 0, 0}));
+ Assert.assertFalse(tetrahedron.contains(new double[] {6, 6, 4}));
}
/**
@@ -376,7 +376,7 @@ public class TetrahedronSamplerTest {
*
* @see <a href="https://mathworld.wolfram.com/Point-PlaneDistance.html">Point-Plane distance</a>
*/
- private static class InsideTetrahedron {
+ private static class Tetrahedron {
/** The face normals. */
private final double[][] n;
/** The distance of each face from the origin. */
@@ -390,7 +390,7 @@ public class TetrahedronSamplerTest {
* @param v3 The third vertex.
* @param v4 The fourth vertex.
*/
- InsideTetrahedron(double[] v1, double[] v2, double[] v3, double[] v4) {
+ Tetrahedron(double[] v1, double[] v2, double[] v3, double[] v4) {
// Compute the centre of each face
final double[][] x = new double[][] {
centre(v1, v2, v3),
@@ -511,12 +511,12 @@ public class TetrahedronSamplerTest {
}
/**
- * Check the point is inside the tetrahedron.
+ * Check whether or not the tetrahedron contains the given point.
*
* @param x the coordinate
* @return true if inside the tetrahedron
*/
- boolean test(double[] x) {
+ boolean contains(double[] x) {
// Must be below all the face planes
for (int i = 0; i < 4; i++) {
// This distance D of a point xyz to the plane is:
@@ -532,13 +532,14 @@ public class TetrahedronSamplerTest {
}
/**
- * Check the point is inside the tetrahedron within the given absolute epsilon.
+ * Check whether or not the tetrahedron contains the given point
+ * within the given absolute epsilon.
*
* @param x the coordinate
* @param epsilon the epsilon
* @return true if inside the tetrahedron
*/
- boolean test(double[] x, double epsilon) {
+ boolean contains(double[] x, double epsilon) {
for (int i = 0; i < 4; i++) {
// As above but with an epsilon above zero
if (dot(n[i], x) > epsilon - d[i]) {
diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TriangleSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TriangleSamplerTest.java
index 3bf9aaa..fe0d664 100644
--- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TriangleSamplerTest.java
+++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TriangleSamplerTest.java
@@ -316,19 +316,19 @@ public class TriangleSamplerTest {
final TriangleSampler sampler1 = TriangleSampler.of(forward.apply(a), forward.apply(d), forward.apply(b), rng);
final TriangleSampler sampler2 = TriangleSampler.of(forward.apply(b), forward.apply(c), forward.apply(e), rng);
final TriangleSampler sampler3 = TriangleSampler.of(forward.apply(c), forward.apply(d), forward.apply(e), rng);
- final InsideTriangle test1 = new InsideTriangle(a, d, b);
- final InsideTriangle test2 = new InsideTriangle(b, c, e);
- final InsideTriangle test3 = new InsideTriangle(c, d, e);
+ final Triangle triangle1 = new Triangle(a, d, b);
+ final Triangle triangle2 = new Triangle(b, c, e);
+ final Triangle triangle3 = new Triangle(c, d, e);
final int samples = expected.length * samplesPerBin;
for (int n = 0; n < 1; n++) {
// Assign each coordinate to a region inside the combined rectangle
final long[] observed = new long[expected.length];
// Sample according to the area of each triangle (ratio 2:1:1)
for (int i = 0; i < samples; i += 4) {
- addObservation(reverse.apply(sampler1.sample()), observed, bins, sx, sy, test1);
- addObservation(reverse.apply(sampler1.sample()), observed, bins, sx, sy, test1);
- addObservation(reverse.apply(sampler2.sample()), observed, bins, sx, sy, test2);
- addObservation(reverse.apply(sampler3.sample()), observed, bins, sx, sy, test3);
+ addObservation(reverse.apply(sampler1.sample()), observed, bins, sx, sy, triangle1);
+ addObservation(reverse.apply(sampler1.sample()), observed, bins, sx, sy, triangle1);
+ addObservation(reverse.apply(sampler2.sample()), observed, bins, sx, sy, triangle2);
+ addObservation(reverse.apply(sampler3.sample()), observed, bins, sx, sy, triangle3);
}
final double p = new ChiSquareTest().chiSquareTest(expected, observed);
Assert.assertFalse("p-value too small: " + p, p < 0.001);
@@ -348,14 +348,14 @@ public class TriangleSamplerTest {
* @param bins the numbers of bins in the x dimension
* @param sx the scale to convert the x coordinate to the x bin
* @param sy the scale to convert the y coordinate to the y bin
- * @param inside the inside triangle test
+ * @param triangle the triangle the sample should be within
*/
private static void addObservation(double[] v, long[] observed,
- int bins, double sx, double sy, InsideTriangle inside) {
+ int bins, double sx, double sy, Triangle triangle) {
final double x = v[0];
final double y = v[1];
- // Test the point is inside the triangle
- Assert.assertTrue(inside.test(x, y));
+ // Test the point is triangle the triangle
+ Assert.assertTrue(triangle.contains(x, y));
// Add to the correct bin after using the offset
final int binx = (int) (x * sx);
final int biny = (int) (y * sy);
@@ -600,33 +600,33 @@ public class TriangleSamplerTest {
}
/**
- * Test the inside triangle predicate.
+ * Test the triangle contains predicate.
*/
@Test
- public void testInsideTriangle() {
- final InsideTriangle inside = new InsideTriangle(1, 2, 3, 1, 0.5, 6);
+ public void testTriangleContains() {
+ final Triangle triangle = new Triangle(1, 2, 3, 1, 0.5, 6);
// Vertices
- Assert.assertTrue(inside.test(1, 2));
- Assert.assertTrue(inside.test(3, 1));
- Assert.assertTrue(inside.test(0.5, 6));
+ Assert.assertTrue(triangle.contains(1, 2));
+ Assert.assertTrue(triangle.contains(3, 1));
+ Assert.assertTrue(triangle.contains(0.5, 6));
// Edge
- Assert.assertTrue(inside.test(0.75, 4));
+ Assert.assertTrue(triangle.contains(0.75, 4));
// Inside
- Assert.assertTrue(inside.test(1.5, 3));
+ Assert.assertTrue(triangle.contains(1.5, 3));
// Outside
- Assert.assertFalse(inside.test(0, 20));
- Assert.assertFalse(inside.test(-20, 0));
- Assert.assertFalse(inside.test(6, 6));
+ Assert.assertFalse(triangle.contains(0, 20));
+ Assert.assertFalse(triangle.contains(-20, 0));
+ Assert.assertFalse(triangle.contains(6, 6));
// Just outside
- Assert.assertFalse(inside.test(0.75, 4 - 1e-10));
+ Assert.assertFalse(triangle.contains(0.75, 4 - 1e-10));
// Note:
- // Touching triangles can both have the point inside.
+ // Touching triangles can both have the point triangle.
// This predicate is not suitable for assigning points uniquely to
// non-overlapping triangles that share an edge.
- final InsideTriangle inside2 = new InsideTriangle(1, 2, 3, 1, 0, -2);
- Assert.assertTrue(inside.test(2, 1.5));
- Assert.assertTrue(inside2.test(2, 1.5));
+ final Triangle triangle2 = new Triangle(1, 2, 3, 1, 0, -2);
+ Assert.assertTrue(triangle.contains(2, 1.5));
+ Assert.assertTrue(triangle2.contains(2, 1.5));
}
/**
@@ -721,7 +721,7 @@ public class TriangleSamplerTest {
* Point in a triangle</a>
* @see <a href="https://stackoverflow.com/a/34093754">Point inside triangle by Cédric Dufour</a>
*/
- private static class InsideTriangle {
+ private static class Triangle {
private final double p2x;
private final double p2y;
private final double dX21;
@@ -737,7 +737,7 @@ public class TriangleSamplerTest {
* @param p1 triangle vertex 1
* @param p2 triangle vertex 2
*/
- InsideTriangle(double[] p0, double[] p1, double[] p2) {
+ Triangle(double[] p0, double[] p1, double[] p2) {
this(p0[0], p0[1], p1[0], p1[1], p2[0], p2[1]);
}
/**
@@ -750,7 +750,7 @@ public class TriangleSamplerTest {
* @param p2x triangle vertex 2 x coordinate
* @param p2y triangle vertex 2 y coordinate
*/
- InsideTriangle(double p0x, double p0y, double p1x, double p1y, double p2x, double p2y) {
+ Triangle(double p0x, double p0y, double p1x, double p1y, double p2x, double p2y) {
this.p2x = p2x;
this.p2y = p2y;
// Precompute factors
@@ -763,13 +763,13 @@ public class TriangleSamplerTest {
}
/**
- * Check the point is inside the triangle.
+ * Check whether or not the triangle contains the given point.
*
* @param px the point x coordinate
* @param py the point y coordinate
* @return true if inside the triangle
*/
- boolean test(double px, double py) {
+ boolean contains(double px, double py) {
// Barycentric coordinates:
// p = p0 + (p1 - p0) * s + (p2 - p0) * t
// The point p is inside the triangle if 0 <= s <= 1 and 0 <= t <= 1 and s + t <= 1