XP Home

A Second Test for the PIA Class


Next we can create a unit test for writing to the PIA register. In this case we are making sure that only output bits are changed.  

package simulator.r3.unittest;

import unittest.framework.*;
import simulator.r3.*;

class TestWriteToPIA extends Test
{public void runTest()
{PIA.register = 0x00FF;
should(PIA.register == 0x303F, "Write never changes inputs");};}
We also need to put this test into our test suite.  

package simulator.r3.unittest;

import unittest.framework.*;

public class SimulatorTests extends TestSuite
{public SimulatorTests()
{tests = new Test[2];
tests[0] = new TestReadFromPIA();
tests[1] = new TestWriteToPIA();};}
If we stub out our write method we can compile and run our new test to be sure it fails. Now let's create the code for the write method.

package simulator.r3;

public class PIA
{public static int register = 0;
public static int inputBits = 0;

public static int read()
{return register & inputBits;}

public static void write(int theNewOuputs)
{register = read() | (theNewOuputs & ~inputBits);}

public static void setInputs(int aBitMask)
{inputBits = aBitMask;};}
Run the test again. This time it passes as expected. Things are going well. But we are not sure we like the way this code looks. Let's do some refactoring now.Spike Solution

ExtremeProgramming.org home | Back to Creating a Spike Solution | Refactor |

Copyright 1999 by Don Wells.