Tuesday, April 1, 2014

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

Problem
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.
 Solution
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
    entail:
    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.
    

Reference
http://tianrunhe.wordpress.com/2012/04/04/how-to-test-a-canmovetoint-x-int-y-method-in-a-chess-game/

Reactions:

0 comments:

Post a Comment