Skip to content

Instantly share code, notes, and snippets.

@azakharov3
Created October 7, 2019 22:28
Show Gist options
  • Save azakharov3/8fed64e6e988480500d17fb2448bf299 to your computer and use it in GitHub Desktop.
Save azakharov3/8fed64e6e988480500d17fb2448bf299 to your computer and use it in GitHub Desktop.
/* *****************************************************************************
* Name:
* Date:
* Description:
**************************************************************************** */
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class FastCollinearPointsTest {
@Test
public void ctor_whenPointsArrayIsNull_throws() {
// Arrange
IllegalArgumentException illegalArgumentException = null;
// Act
try {
new FastCollinearPoints(null);
} catch (IllegalArgumentException e) {
illegalArgumentException = e;
}
// Assert
assertNotNull(illegalArgumentException);
}
@Test
public void ctor_whenPointsArrayContainsNull_throw() {
// Arrange
Point[] points = new Point[] { new Point(1, 1), null, new Point(3, 3)};
IllegalArgumentException illegalArgumentException = null;
// Act
try {
new FastCollinearPoints(points);
} catch (IllegalArgumentException e) {
illegalArgumentException = e;
}
// Assert
assertNotNull(illegalArgumentException);
}
@Test
public void ctor_whenPointsArrayContainsDuplicates_throws() {
// Arrange
Point[] points = new Point[] { new Point(3, 3), new Point(1, 1), new Point(3, 3)};
IllegalArgumentException illegalArgumentException = null;
// Act
try {
new FastCollinearPoints(points);
} catch (IllegalArgumentException e) {
illegalArgumentException = e;
}
// Assert
assertNotNull(illegalArgumentException);
}
/**
* This test is very precise to make sure double equality issues are taken care
*/
@Test
public void segments_whenThereAreNoColinearPoints_returnsNoSegments() {
// Arrange
Point[] points = new Point[] { new Point(10000, 10000), new Point(20000, 20000), new Point(30000, 30000), new Point(40000, 40001)};
FastCollinearPoints fastCollinearPoints = new FastCollinearPoints(points);
// Act
LineSegment[] segments = fastCollinearPoints.segments();
// Assert
assertEquals(0, segments.length);
}
/**
* 6
* 5 E
* 4 D
* 3 C
* 2 B
* 1 A
* 0 1 2 3 4 5 6
*/
@Test
public void segments_whenOneColenearLineWithMoreThanFourPoints_returnsOneSegment() {
// Arrange
Point[] points = new Point[] { new Point(1, 1), new Point(2, 2), new Point(3, 3), new Point(4, 4), new Point(5, 5)};
FastCollinearPoints fastCollinearPoints = new FastCollinearPoints(points);
// Act
LineSegment[] segments = fastCollinearPoints.segments();
// Assert
assertEquals(1, segments.length);
}
/**
* 6
* 5 E
* 4 F D
* 3 C
* 2 B G
* 1 A H
* 0 1 2 3 4 5 6
*/
@Test
public void segments_whenTwoColenearLineWithFivePointsCrossing_returnsTwoSegments() {
// Arrange
Point pointA = new Point(1, 1);
Point pointB = new Point(2, 2);
Point pointC = new Point(3, 3);
Point pointD = new Point(4, 4);
Point pointE = new Point(5, 5);
Point pointF = new Point(2, 4);
Point pointG = new Point(4, 2);
Point pointH = new Point(5, 1);
Point[] points = new Point[] { pointA, pointB, pointC, pointD, pointE, pointF, pointG, pointH };
FastCollinearPoints fastCollinearPoints = new FastCollinearPoints(points);
// Act
LineSegment[] segments = fastCollinearPoints.segments();
// Assert
assertEquals(2, segments.length);
}
/**
* 7
* 6
* 5 I K J L
* 4 F G E H
* 3
* 2
* 1 A B D C
* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
*/
@Test
public void segments_whenThereAreMultipleHorizontalLines_returnsAllHorizontalLines() {
// Arrange
Point pointA = new Point(1, 1);
Point pointB = new Point(2, 1);
Point pointC = new Point(4, 1);
Point pointD = new Point(3, 1);
Point pointF = new Point(2, 4);
Point pointG = new Point(3, 4);
Point pointE = new Point(4, 4);
Point pointH = new Point(5, 4);
Point pointI = new Point(7, 5);
Point pointK = new Point(8, 5);
Point pointJ = new Point(9, 5);
Point pointL = new Point(10, 5);
Point[] points = new Point[] { pointA, pointB, pointC, pointD,
pointE, pointF, pointG, pointH,
pointI, pointJ, pointK, pointL};
FastCollinearPoints fastCollinearPoints = new FastCollinearPoints(points);
// Act
LineSegment[] segments = fastCollinearPoints.segments();
// Assert
assertEquals(3, segments.length);
}
/**
* 6
* 5
* 4
* 3
* 2 A B C D E F G H
* 1
* 0 1 2 3 4 5 6 7 8 9 10
*/
@Test
public void segments_whenTwoHorizontalLinesSeparated_returnsOneSegment() {
// Arrange
Point pointA = new Point(1, 2);
Point pointB = new Point(2, 2);
Point pointC = new Point(3, 2);
Point pointD = new Point(4, 2);
Point pointE = new Point(7, 3);
Point pointF = new Point(8, 4);
Point pointG = new Point(9, 5);
Point pointH = new Point(10, 5);
Point[] points = new Point[] {
pointA, pointB, pointC, pointD,
pointE, pointF, pointG, pointH
};
FastCollinearPoints fastCollinearPoints = new FastCollinearPoints(points);
// Act
LineSegment[] segments = fastCollinearPoints.segments();
// Assert
assertEquals(1, segments.length);
}
/**
* 6
* 5 J I H G
* 4 K F
* 3 L E
* 2 A B C D
* 1
* 0 1 2 3 4 5 6
*/
@Test
public void segments_whenFourColinearLinesMakeSquare_returnsFourSegments() {
// Arrange
Point pointA = new Point(1, 2);
Point pointB = new Point(2, 2);
Point pointC = new Point(3, 2);
Point pointD = new Point(4, 2);
Point pointE = new Point(4, 3);
Point pointF = new Point(4, 4);
Point pointG = new Point(4, 5);
Point pointH = new Point(3, 5);
Point pointI = new Point(2, 5);
Point pointJ = new Point(1, 5);
Point pointK = new Point(1, 4);
Point pointL = new Point(1, 3);
Point[] points = new Point[] {
pointA, pointB, pointC, pointD, pointE, pointF,
pointG, pointH, pointI, pointJ, pointK, pointL
};
FastCollinearPoints fastCollinearPoints = new FastCollinearPoints(points);
// Act
LineSegment[] segments = fastCollinearPoints.segments();
// Assert
assertEquals(4, segments.length);
}
/**
* 7
* 6 D
* 5 C
* 4 B E
* 3 A F
* 2 G
* 1 H
* 0 1 2 3 4 5 6 7
*/
@Test
public void segments_whenTwoParallelLines_returnsTwoSegments() {
// Arrange
Point pointA = new Point(1, 3);
Point pointB = new Point(2, 4);
Point pointC = new Point(3, 5);
Point pointD = new Point(4, 6);
Point pointE = new Point(4, 4);
Point pointF = new Point(3, 3);
Point pointG = new Point(2, 2);
Point pointH = new Point(1, 1);
Point[] points = new Point[] {
pointA, pointB, pointC, pointD,
pointE, pointF, pointG, pointH,
};
FastCollinearPoints fastCollinearPoints = new FastCollinearPoints(points);
// Act
LineSegment[] segments = fastCollinearPoints.segments();
// Assert
assertEquals(2, segments.length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment