Skip to content

Instantly share code, notes, and snippets.

@azakharov3
Created October 7, 2019 22:27
Show Gist options
  • Save azakharov3/c32f8b40d6ec77b08a8210b822e60a32 to your computer and use it in GitHub Desktop.
Save azakharov3/c32f8b40d6ec77b08a8210b822e60a32 to your computer and use it in GitHub Desktop.
/* *****************************************************************************
* Name:
* Date:
* Description:
**************************************************************************** */
import org.junit.Assert;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
public class BruteCollinearPointsTest {
@Test
public void ctor_whenPointsArrayNull_throwsIllegalArgumentException() {
// Arrange
Point[] points = null;
IllegalArgumentException illegalArgumentException = null;
// Act
try {
new BruteCollinearPoints(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)};
BruteCollinearPoints bruteCollinearPoints = new BruteCollinearPoints(points);
// Act
LineSegment[] segments = bruteCollinearPoints.segments();
// Assert
Assert.assertEquals(0, segments.length);
}
@Test
public void ctor_whenPointsArrayContainsNullElement_throwsIllegalArgumentException() {
// Arrange
Point[] points = new Point[] { new Point(1,1), null, new Point(3, 3), new Point(4, 4)};
IllegalArgumentException illegalArgumentException = null;
// Act
try {
new BruteCollinearPoints(points);
} catch (IllegalArgumentException e) {
illegalArgumentException = e;
}
// Assert
assertNotNull(illegalArgumentException);
}
@Test
public void ctor_whenAnyPointsArrayContainsDuplicates_throwsIllegalArgumentException() {
// Arrange
Point[] points = new Point[] { new Point(1,1), new Point(2, 2), new Point(3, 3), new Point(3, 3)};
IllegalArgumentException illegalArgumentException = null;
// Act
try {
new BruteCollinearPoints(points);
} catch (IllegalArgumentException e) {
illegalArgumentException = e;
}
// Assert
assertNotNull(illegalArgumentException);
}
@Test
public void numberOfSegments_whenFourOfFourPointsOnTheSameLine_returnsOneSegment() {
// Arrange
Point[] points = new Point[] { new Point(1, 1), new Point(2, 2), new Point(3, 3), new Point(4, 4)};
BruteCollinearPoints bruteCollinearPoints = new BruteCollinearPoints(points);
// Act
int numberOfSegments = bruteCollinearPoints.numberOfSegments();
// Assert
assertEquals(1, numberOfSegments);
}
@Test
public void numberOfSegments_whenFourOfFivePointsOnTheSameLine_returnsOneSegment() {
// Arrange
Point[] points = new Point[] { new Point(1, 2), new Point(1, 1), new Point(2, 2), new Point(3, 3), new Point(4, 4)};
BruteCollinearPoints bruteCollinearPoints = new BruteCollinearPoints(points);
// Act
int numberOfSegments = bruteCollinearPoints.numberOfSegments();
// Assert
assertEquals(1, numberOfSegments);
}
@Test
public void segments_whenFirstOutOfFivePointsIsNotOnTheLine() {
// Arrange
Point[] points = new Point[] { new Point(2, 1), new Point(2, 2), new Point(3, 3), new Point(4, 4), new Point(5, 5)};
BruteCollinearPoints bruteCollinearPoints = new BruteCollinearPoints(points);
// Act
LineSegment segment = bruteCollinearPoints.segments()[0];
// Assert
assertEquals("(2, 2) -> (5, 5)", segment.toString());
}
@Test
public void segments_whenThereAreTwoColinearLines() {
Point[] points = new Point[] {
new Point(10, 0),
new Point(0, 10),
new Point(3, 7),
new Point(7, 3),
new Point(20, 21),
new Point(3, 4),
new Point(14, 15),
new Point(6, 7) };
BruteCollinearPoints bruteCollinearPoints = new BruteCollinearPoints(points);
// Act
LineSegment[] segments = bruteCollinearPoints.segments();
// Assert
assertEquals(2, segments.length);
assertEquals("(10, 0) -> (0, 10)", segments[0].toString());
assertEquals("(3, 4) -> (20, 21)", segments[1].toString());
}
@Test
public void segments_whenThereIsVerticalSegment() {
Point[] points = new Point[] {
new Point(1, 1),
new Point(1, 2),
new Point(1, 3),
new Point(1, 4)};
BruteCollinearPoints bruteCollinearPoints = new BruteCollinearPoints(points);
// Act
LineSegment[] segments = bruteCollinearPoints.segments();
// Assert
assertEquals(1, segments.length);
assertEquals("(1, 1) -> (1, 4)", segments[0].toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment