Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unit Tests for CheckRectangle
package test;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import org.junit.Test;
public class CheckRectangleTest{
List<CheckRectangle.Point> points = new ArrayList<>();
@Test
public void testSinglePoint(){
points.add(new CheckRectangle.Point(10,10));
assertTrue(CheckRectangle.isRectangle(points));
}
@Test
public void testSinglePoint_double(){
points.add(new CheckRectangle.Point(10,10));
points.add(new CheckRectangle.Point(10,10));
assertTrue(CheckRectangle.isRectangle(points));
}
@Test
public void testVerticalLine(){
points.add(new CheckRectangle.Point(10,10));
points.add(new CheckRectangle.Point(10,11));
points.add(new CheckRectangle.Point(10,12));
points.add(new CheckRectangle.Point(10,13));
points.add(new CheckRectangle.Point(10,14));
Collections.shuffle(points, new Random(0));
assertTrue(CheckRectangle.isRectangle(points));
}
@Test
public void testVerticalLine_missingBit(){
points.add(new CheckRectangle.Point(10,10));
points.add(new CheckRectangle.Point(10,11));
points.add(new CheckRectangle.Point(10,12));
//points.add(new CheckRectangle.Point(10,13));
points.add(new CheckRectangle.Point(10,14));
points.add(new CheckRectangle.Point(10,14)); // double
Collections.shuffle(points, new Random(0));
assertFalse(CheckRectangle.isRectangle(points));
}
@Test
public void testHorizontalLine(){
points.add(new CheckRectangle.Point(10,10));
points.add(new CheckRectangle.Point(11,10));
points.add(new CheckRectangle.Point(12,10));
points.add(new CheckRectangle.Point(13,10));
points.add(new CheckRectangle.Point(14,10));
points.add(new CheckRectangle.Point(15,10));
Collections.shuffle(points, new Random(0));
assertTrue(CheckRectangle.isRectangle(points));
}
@Test
public void testHorizontalLine_missingBit(){
points.add(new CheckRectangle.Point(10,10));
points.add(new CheckRectangle.Point(11,10));
//points.add(new CheckRectangle.Point(12,10));
points.add(new CheckRectangle.Point(13,10));
points.add(new CheckRectangle.Point(14,10));
points.add(new CheckRectangle.Point(15,10));
Collections.shuffle(points, new Random(0));
assertFalse(CheckRectangle.isRectangle(points));
}
@Test
public void testSquare(){
points.add(new CheckRectangle.Point(10,10));
points.add(new CheckRectangle.Point(10,11));
points.add(new CheckRectangle.Point(10,12));
points.add(new CheckRectangle.Point(10,13));
points.add(new CheckRectangle.Point(11,13));
points.add(new CheckRectangle.Point(12,13));
points.add(new CheckRectangle.Point(13,13));
points.add(new CheckRectangle.Point(13,12));
points.add(new CheckRectangle.Point(13,11));
points.add(new CheckRectangle.Point(13,10));
points.add(new CheckRectangle.Point(12,10));
points.add(new CheckRectangle.Point(11,10));
Collections.shuffle(points, new Random(0));
assertTrue(CheckRectangle.isRectangle(points));
}
@Test
public void testSquare_doublepoints(){
points.add(new CheckRectangle.Point(10,10));
points.add(new CheckRectangle.Point(10,11));
points.add(new CheckRectangle.Point(10,12));
points.add(new CheckRectangle.Point(10,12)); // double
points.add(new CheckRectangle.Point(10,13));
points.add(new CheckRectangle.Point(11,13));
points.add(new CheckRectangle.Point(12,13));
points.add(new CheckRectangle.Point(13,13));
points.add(new CheckRectangle.Point(13,12));
points.add(new CheckRectangle.Point(13,11));
points.add(new CheckRectangle.Point(13,10));
points.add(new CheckRectangle.Point(12,10));
points.add(new CheckRectangle.Point(11,10));
Collections.shuffle(points, new Random(0));
assertTrue(CheckRectangle.isRectangle(points));
}
@Test
public void testSquare_missingBits(){
points.add(new CheckRectangle.Point(10,10));
points.add(new CheckRectangle.Point(10,11));
//points.add(new CheckRectangle.Point(10,12));
points.add(new CheckRectangle.Point(10,13));
points.add(new CheckRectangle.Point(11,13));
points.add(new CheckRectangle.Point(12,13));
points.add(new CheckRectangle.Point(13,13));
points.add(new CheckRectangle.Point(13,12));
points.add(new CheckRectangle.Point(13,11));
points.add(new CheckRectangle.Point(13,10));
points.add(new CheckRectangle.Point(12,10));
points.add(new CheckRectangle.Point(11,10));
Collections.shuffle(points, new Random(0));
assertFalse(CheckRectangle.isRectangle(points));
}
@Test
public void testSquare_extraBits(){
points.add(new CheckRectangle.Point(10,10));
points.add(new CheckRectangle.Point(10,11));
points.add(new CheckRectangle.Point(10,12));
points.add(new CheckRectangle.Point(10,13));
points.add(new CheckRectangle.Point(11,11)); // extra bit
points.add(new CheckRectangle.Point(11,13));
points.add(new CheckRectangle.Point(12,13));
points.add(new CheckRectangle.Point(13,13));
points.add(new CheckRectangle.Point(13,12));
points.add(new CheckRectangle.Point(13,11));
points.add(new CheckRectangle.Point(13,10));
points.add(new CheckRectangle.Point(12,10));
points.add(new CheckRectangle.Point(11,10));
Collections.shuffle(points, new Random(0));
assertFalse(CheckRectangle.isRectangle(points));
}
@Test
public void testCircle(){
points.add(new CheckRectangle.Point(10,10));
points.add(new CheckRectangle.Point(11,11));
points.add(new CheckRectangle.Point(10,12));
points.add(new CheckRectangle.Point(9,11));
Collections.shuffle(points, new Random(0));
assertFalse(CheckRectangle.isRectangle(points));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.