import lab.Color; import java.util.Random; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** * Test case for the lab.Color class. * * @author Franck van Breugel */ public class ColorTest { /** * Tests the constructor and accessors for each RGB combination. */ @Test public void testConstructorAndAccessors() { for (int r = Byte.MIN_VALUE; r <= Byte.MAX_VALUE; r++) { for (int g = Byte.MIN_VALUE; g <= Byte.MAX_VALUE; g++) { for (int b = Byte.MIN_VALUE; b <= Byte.MAX_VALUE; b++) { Color color = new Color((byte) r, (byte) g, (byte) b); Assertions.assertNotNull("Constructed object is null", color); Assertions.assertEquals("Accessor of red returns the incorrect value", r, color.getRed()); Assertions.assertEquals("Accessor of green returns the incorrect value", g, color.getGreen()); Assertions.assertEquals("Accessor of blue returns the incorrect value", b, color.getBlue()); } } } } /** * Tests that the constant BLACK is not null. */ @Test public void testBLACK() { Assertions.assertNotNull("The constant BLACK is null", Color.BLACK); } /** * Tests the equals method. */ @Test public void testEquals() { for (int r = Byte.MIN_VALUE; r <= Byte.MAX_VALUE; r++) { for (int g = Byte.MIN_VALUE; g <= Byte.MAX_VALUE; g++) { for (int b = Byte.MIN_VALUE; b <= Byte.MAX_VALUE; b++) { Color color = new Color((byte) r, (byte) g, (byte) b); Object object = new Object(); Color copy = new Color((byte) r, (byte) g, (byte) b); Assertions.assertFalse("null is equal to a Color", color.equals(null)); Assertions.assertFalse("An Object is equal to a Color", color.equals(object)); Assertions.assertTrue("A Color is not equal to itself", color.equals(color)); Assertions.assertTrue("A Color is not equal to another color with the same RGB values", color.equals(copy)); } } } } /** * Tests the equals method for different Color objects. */ @Test public void testEqualsRandomly() { final int CASES = 100000000; Random random = new Random(); for (int c = 0; c < CASES; c++) { byte b = (byte) (Byte.MIN_VALUE + random.nextInt(Byte.MAX_VALUE - Byte.MIN_VALUE)); byte r = (byte) (Byte.MIN_VALUE + random.nextInt(Byte.MAX_VALUE - Byte.MIN_VALUE)); byte g = (byte) (Byte.MIN_VALUE + random.nextInt(Byte.MAX_VALUE - Byte.MIN_VALUE)); Color one = new Color(r, g, b); b = (byte) (Byte.MIN_VALUE + random.nextInt(Byte.MAX_VALUE - Byte.MIN_VALUE)); r = (byte) (Byte.MIN_VALUE + random.nextInt(Byte.MAX_VALUE - Byte.MIN_VALUE)); g = (byte) (Byte.MIN_VALUE + random.nextInt(Byte.MAX_VALUE - Byte.MIN_VALUE)); Color other = new Color(r, g, b); Assertions.assertEquals("Comparison of two Colors failed", one.equals(other), one.getRed() == other.getRed() && one.getGreen() == other.getGreen() && one.getBlue() == other.getBlue()); } } }