How to test a canMoveTo(int x, int y) method in a chess game

We have the following method used in a chess game: boolean canMoveTo(int x, int y) x and y are the coordinates of the chess board and it returns whether or not the piece can move to that position. Explain how you would test this method.

My initial thoughts:

  1. Error conditions: x, y are negative numbers; x, y are beyond the coordinates of the chess board. x, y are zero(s).
  2. If a piece can move to (x, y), test whether the method returns true. Similarly, if a piece cannot move to (x, y), test if the method returns false. That is, test if the method follows the rule of chess.

There are two primary types of testing we should do:

  1. Validation of input/output:
    We should validate both the input and output to make sure that each are valid. This might

    1. Checking whether input is within the board limit.
      • Attempt to pass in negative numbers
      • Attempt to pass in x which is larger than the width
      • Attempt to pass in y which is larger than the width

      Depending on the implementation, these should either return false or throw an exception.

    2. Checking if output is within the valid set of return values. (Not an issue in this case, since there are no “invalid” boolean values.)
  2. Functional testing:
    Ideally, we would like to test every possible board, but this is far too big. We can do a reasonable coverage of boards however. There are 6 pieces in chess, so we need to do something like this:

    foreach piece a:
    	for each other type of piece b (6 types + empty space)
    		foreach direction d
    			Create a board with piece a.
    			Place piece b in direction d.
    			Try to move – check return value.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: