Skip to content

Commit

Permalink
Test cases see issue #5 (#259)
Browse files Browse the repository at this point in the history
Add unit test for:
- Envelope: getCenter() & Merge()
- Envelope2D: clipLine() & sqrDistances()
  • Loading branch information
johansettlin committed Aug 12, 2020
1 parent 6add959 commit d5a9a3f
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/test/java/com/esri/core/geometry/TestEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,107 @@

public class TestEnvelope
{
@Test
/**The function returns the x and y coordinates of the center of the envelope.
* If the envelope is empty the point is set to empty otherwise the point is set to the center of the envelope.
*/
public void testGetCeneter(){
//xmin,ymin,xmax,ymax of envelope
Envelope env1 = new Envelope(1,1, 2, 4);
Envelope env2 = new Envelope();
Point p = new Point();
Point p1 = new Point(1,2);

/**Tests if the point is correctly set to the center of the envelope, */
env1.getCenter(p);
assertTrue(p.getX() == 1.5);

/** Tests if the point is empty because of the envelope is empty */
env2.getCenter(p1);
assertTrue(p1.isEmpty());
}
@Test
/* Merge takes a Point as input and increas the bouandary of the envelope to contain the point.
*If the point is empty the envelope remains the same or if the envelope is empty the coordinates
*of the point is assigned to the envelope */
public void testMerge(){

/* To increase the covarege the branch where the envelope is empty can be tested
* And that the envelope and the point is not empty */
Envelope env1 = new Envelope(1,1, 2, 4);
Envelope env2 = new Envelope(1,1, 2, 4);
Envelope env3 = new Envelope(1,1, 2, 4);
Point p = new Point(100,4);

/*This should be false since env1 should change depending on point p */
env1.merge(p);
assertFalse(env1.equals(env2));

/* This assert should be true since the point is empty and therefore env2 should not change */
Point p1 = new Point();
env2.merge(p1);
assertTrue(env2.equals(env3));
}


@Test
/** TESTEST ENVELOPE2D **
* ClipLine modify a line to be inside a envelope if possible */
public void TestClipLine(){

//checking if segPrama is 0 and the segment is outside of the clipping window
//covers first return
Envelope2D env0 = new Envelope2D(1, 1, 4, 4);
// Reaches the branch where the delta is 0
Point2D p1 = new Point2D(2,2);
Point2D p2 = new Point2D(2,2);

int lineExtension = 0;
double[] segParams = {3,4};
//reaches the branch where boundaryDistances is not 0
double[] boundaryDistances = {2.0, 3.0};

int a = env0.clipLine(p1, p2, lineExtension, segParams, boundaryDistances);
//should be true since the points are inside the envelope
assertTrue(a == 4);

// Changes p3 to fit the envelop, the line is on the edge of the envelope
Envelope2D env1 = new Envelope2D(1, 1, 4, 4);
Point2D p3 = new Point2D(1,10);
Point2D p4 = new Point2D(1,1);

int b = env1.clipLine(p3, p4, lineExtension, segParams, boundaryDistances);
assertTrue(b == 1);
// the second point is outside and therefore changed
Envelope2D env2 = new Envelope2D(1, 1, 4, 4);
Point2D p5 = new Point2D(2,2);
Point2D p6 = new Point2D(1,10);

int c = env2.clipLine(p5, p6, lineExtension, segParams, boundaryDistances);
assertTrue(c == 2);

//Both points is outside the envelope and therefore no line is possible to clip, and this should return 0
Envelope2D env3 = new Envelope2D(1, 1, 4, 4);
Point2D p7 = new Point2D(11,10);
Point2D p8 = new Point2D(5,5);

int d = env3.clipLine(p7, p8, lineExtension, segParams, boundaryDistances);
assertTrue(d == 0);
}

@Test
public void testSqrDistances(){
//the point is on the envelope, which means that the distance is 0
Envelope2D env0 = new Envelope2D(1, 1, 4, 4);
Point2D p0 = new Point2D(4,4);
assertTrue(env0.sqrDistance(p0) == 0.0);

Envelope2D env1 = new Envelope2D(1, 1, 4, 4);
Point2D p1 = new Point2D(1,0);

assertTrue(env0.sqrDistance(p1) == 1.0);

}
@Test
public void testIntersect() {
assertIntersection(new Envelope(0, 0, 5, 5), new Envelope(0, 0, 5, 5), new Envelope(0, 0, 5, 5));
Expand Down

0 comments on commit d5a9a3f

Please sign in to comment.