package quiz; import org.junit.Assert; import org.junit.Test; /** * Test case for the Byte class. * * @author Franck van Breugel */ public class ByteTest { /** * Tests the constant MAX_VALUE. */ @Test public void testMaxValue() { final int MAX_VALUE = 127; Assert.assertEquals(MAX_VALUE, Byte.MAX_VALUE); } /** * Tests the constant MIN_VALUE. */ @Test public void testMinValue() { final int MIN_VALUE = -128; Assert.assertEquals(MIN_VALUE, Byte.MIN_VALUE); } /** * Tests the constructor. */ @Test public void testConstructor() { for (int b = Byte.MIN_VALUE; b <= Byte.MAX_VALUE; b++) { Assert.assertNotNull(String.format("Failed for %d;", b), new Byte((byte) b)); } } /** * Tests equals method. */ @Test public void testEquals() { for (int b = Byte.MIN_VALUE; b <= Byte.MAX_VALUE; b++) { Byte object = new Byte((byte) b); Assert.assertTrue(String.format("Failed for %d;", b), object.equals(object)); Assert.assertFalse(String.format("Failed for %d;", b), object.equals(null)); Assert.assertFalse(String.format("Failed for %d;", b), object.equals(new Object())); for (int c = Byte.MIN_VALUE; c <= Byte.MAX_VALUE; c++) { Byte other = new Byte((byte) c); Assert.assertEquals(String.format("Failed for %d and %d;", b, c), b == c, object.equals(other)); } } } /** * Tests hashCode method. */ @Test public void testHashCode() { for (int b = Byte.MIN_VALUE; b <= Byte.MAX_VALUE; b++) { Byte object = new Byte((byte) b); Assert.assertEquals(String.format("Failed for %d;", b), b, object.hashCode()); } } /** * Tests isEven method. */ @Test public void testIsEven() { for (int b = Byte.MIN_VALUE; b <= Byte.MAX_VALUE; b++) { Byte object = new Byte((byte) b); Assert.assertEquals(String.format("Failed for %d;", b), b % 2 == 0, object.isEven()); } } /** * Tests toString method. */ @Test public void testToString() { for (int b = Byte.MIN_VALUE; b <= Byte.MAX_VALUE; b++) { Byte object = new Byte((byte) b); Assert.assertEquals(String.format("Failed for %d;", b), "" + b, object.toString()); } } }