import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * The test class NumberDisplayTest. * * @author Brad Richards * @version 1.0 */ public class NumberDisplayTest { /** * Default constructor for test class NumberDisplayTest */ public NumberDisplayTest() { } /** * Sets up the test fixture. * * Called before every test case method. */ @Before public void setUp() { } /** * Tears down the test fixture. * * Called after every test case method. */ @After public void tearDown() { } /** * Verifies that constructor initially sets the NumberDisplay to zero. * This test also */ @Test public void constructor_InitiallyZero() { NumberDisplay nd = new NumberDisplay(10); assertEquals(0, nd.getValue()); } /** * Verifies that the display value string is "00" for a new NumberDisplay. */ @Test public void constructor_InitialDisplay() { NumberDisplay nd = new NumberDisplay(10); assertEquals("00", nd.getDisplayValue()); } /** * Tests that a single call to increment works. */ @Test public void increment_InitialIncrement() { NumberDisplay nd = new NumberDisplay(10); assertEquals(0, nd.getValue()); nd.increment(); assertEquals(1, nd.getValue()); } /** * Tests that a series of calls to increment works. */ @Test public void increment_MultipleIncrements() { NumberDisplay nd = new NumberDisplay(10); assertEquals(0, nd.getValue()); for(int i=0; i<9; i=i+1) { nd.increment(); }; assertEquals(9, nd.getValue()); } /** * Tests that the value wraps around (and keeps going). */ @Test public void increment_Wraps() { NumberDisplay nd = new NumberDisplay(10); assertEquals(0, nd.getValue()); for(int i=0; i<12; i=i+1) { nd.increment(); }; assertEquals(2, nd.getValue()); } /** * Ensures that setValue() calls can set the value to something in the middle * of the range, and to the legal min and max values. */ @Test public void setValue_LegalValues() { NumberDisplay nd = new NumberDisplay(10); nd.setValue(3); assertEquals(3, nd.getValue()); nd.setValue(0); assertEquals(0, nd.getValue()); nd.setValue(9); assertEquals(9, nd.getValue()); nd.setValue(10); assertEquals(9, nd.getValue()); nd.setValue(-1); assertEquals(9, nd.getValue()); } /** * Verifies that attempts to set to an illegal value are ignored. */ @Test public void setValue_IllegalValues() { NumberDisplay nd = new NumberDisplay(10); nd.setValue(10); assertEquals(0, nd.getValue()); nd.setValue(-1); assertEquals(0, nd.getValue()); } /** * Verifies that getDisplayString() works properly for single-digit * values (that it "pads" the value with a "0" as necessary). */ @Test public void displayString_SingleDigit() { NumberDisplay nd = new NumberDisplay(20); assertEquals("00", nd.getDisplayValue()); nd.setValue(9); assertEquals("09", nd.getDisplayValue()); } /** * Verifies that getDisplayString() works properly for double-digit * values (no padding necessary). */ @Test public void displayString_DoubleDigit() { NumberDisplay nd = new NumberDisplay(20); nd.setValue(10); assertEquals("10", nd.getDisplayValue()); nd.setValue(19); assertEquals("19", nd.getDisplayValue()); } }