Skip to content

Instantly share code, notes, and snippets.

@azakharov3
Created October 7, 2019 22:28
Show Gist options
  • Save azakharov3/9d4acfe01cb66a4e9c028bacd21ce22c to your computer and use it in GitHub Desktop.
Save azakharov3/9d4acfe01cb66a4e9c028bacd21ce22c to your computer and use it in GitHub Desktop.
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
public class PointTest {
@Test
public void compareTo_whenAandBareSame_shouldReturn0() {
// Arrange
Point pointA = new Point(0, 0);
Point pointB = new Point(0, 0);
// Act
int result = pointA.compareTo(pointB);
// Assert
assertEquals(0, result);
}
@Test
public void compareTo_whenAhigherThanB_shouldReturn1() {
// Arrange
Point pointA = new Point(1, 10);
Point pointB = new Point(5, 5);
// Act
int result = pointA.compareTo(pointB);
// Assert
assertEquals(1, result);
}
@Test
public void compareTo_whenAlowerThanB_shouldReturnMinus1() {
// Arrange
Point pointA = new Point(5, 5);
Point pointB = new Point(1, 10);
// Act
int result = pointA.compareTo(pointB);
// Assert
assertEquals(-1, result);
}
@Test
public void compareTo_whenAisToTheLeftFromB_shouldReturnMinues1() {
// Arrange
Point pointA = new Point(0, 1);
Point pointB = new Point(1, 1);
// Act
int result = pointA.compareTo(pointB);
// Assert
assertEquals(-1, result);
}
@Test
public void compareTo_whenAisToTheRightFromB_shouldReturn1() {
// Arrange
Point pointA = new Point(1, 1);
Point pointB = new Point(0, 1);
// Act
int result = pointA.compareTo(pointB);
// Assert
assertEquals(1, result);
}
@Test
public void slopeTo_whenDegenerateLineSegment_shouldReturnNegativeInfinity() {
// Arrange
Point pointA = new Point(1, 1);
Point pointB = new Point(1, 1);
// Act
double result = pointA.slopeTo(pointB);
// Assert
assertEquals(Double.NEGATIVE_INFINITY, result);
}
/**
* 2
* 2
* 1 A B
* 0 1 2 2
*/
@Test
public void slopeTo_whenHorizontalLeftToRight_shouldReturnPositiveZero() {
Point pointA = new Point(1, 1);
Point pointB = new Point(2, 1);
// Act
double result = pointA.slopeTo(pointB);
// Assert
assertEquals(0.0, result);
}
/**
* 2
* 2
* 1 B A
* 0 1 2 2
*/
@Test
public void slopeTo_whenHorizontalRightToLeft_shouldReturnPositiveZero() {
Point pointA = new Point(2, 1);
Point pointB = new Point(1, 1);
// Act
double result = pointA.slopeTo(pointB);
// Assert
assertEquals(0.0, result);
}
/**
* B
*
* A
*
*/
@Test
public void slope_whenPointcloserToXandY_shouldBePositive() {
Point pointA = new Point(1, 1);
Point pointB = new Point(3, 3);
// Act
double result = pointA.slopeTo(pointB);
// Assert
assertEquals(1.0, result);
}
/**
* 8 A
* 7
* 6
* 5
* 4
* 3
* 2 B
* 1
* 0 1 2 3 4 5 6 7 8 9 10
*/
@Test
public void slope_whenDeclinedToTheRight_shouldBeNegative() {
Point pointA = new Point(2, 8);
Point pointB = new Point(10, 2);
// Act
double result = pointA.slopeTo(pointB);
// Assert
assertEquals(-0.75, result);
}
@Test
public void slopeTo_whenVerticalLine_shouldReturnPositiveZero() {
Point pointA = new Point(1, 1);
Point pointB = new Point(1, 10);
// Act
double result = pointA.slopeTo(pointB);
// Assert
assertEquals(Double.POSITIVE_INFINITY, result);
}
@Test
public void slopeOrder_whenComparingToTheSamePoint_returnsZero() {
// Arrange
Point basePoint = new Point(1, 1);
Point pointA = new Point(10, 10);
Point pointB = new Point(10, 10);
// Act
int result = basePoint.slopeOrder().compare(pointA, pointB);
// Assert
assertEquals(0, result);
}
@Test
public void slopeOrder_whenFirstSlopeSmaller_returnsMinus1() {
// Arrange
Point basePoint = new Point(1, 1);
Point pointA = new Point(2, 2);
Point pointB = new Point(3, 4);
// Act
int result = basePoint.slopeOrder().compare(pointA, pointB);
// Assert
assertEquals(-1, result);
}
@Test
public void slopeOrder_whenFirstSlopeLarger_returnsPlus1() {
// Arrange
Point basePoint = new Point(1, 1);
Point pointA = new Point(2, 4);
Point pointB = new Point(2, 2);
// Act
int result = basePoint.slopeOrder().compare(pointA, pointB);
// Assert
assertEquals(1, result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment