test script files

edited July 2009 in Science & Tech
how do I write test scripts for the following functions?
------------------------------------------
%This function takes a board matrix input, a coords input, and an input for
%the coordinates of the shark. If the proposed move is legal, the shark
%moves one space closer to coords.

function [canMove newShark]=moveOneSquare(board,coords,shark)


%If the shark is more rows than columns away from coords, it will move one
%row closer to coords. Otherwise it will move one column closer.
if abs(coords(1)-shark(1))>= abs(coords(2)-shark(2))
         rowDiff=coords(1)-shark(1);
         if rowDiff<0
                 if isLegalMove(board,[shark(1)-1,shark(2)])==true
                         newShark=[shark(1)-1,shark(2)];
                         canMove=true;
                 else
                         canMove=false;
                 end
     else 
                  if isLegalMove(board,[shark(1)+1,shark(2)])==true
                         newShark=[shark(1)+1,shark(2)];
                         canMove=true;
                 else
                         canMove=false;
                  end
         end
else 
         colDiff=coords(2)-shark(2);
         if colDiff<0
                 if isLegalMove(board,[shark(1),(shark(2)-1)])==true
             newShark=[shark(1),(shark(2)-1)];
                         canMove=true;
                 else
             canMove=false;
                 end
         else
         if isLegalMove(board,[shark(1),(shark(2)+1)])==true
                         newShark=[shark(1),(shark(2)+1)];
                         canMove=true;
                 else
                         canMove=false;
                  end
         end
end
--------------------------------------------
%This function moves the shark one square at a time toward a given goldfish
%and terminates when the shark is vertically or horizontally adjacent to
%the said goldfish.


function newShark=moveToGoldfish(board,goldfishCoords,shark)

while isAdjacent(goldfishCoords,shark)~=true
      [canMove newShark]=moveOneSquare(board,goldfishCoords,shark);
      if canMove==true
             board(shark(1),shark(2))='-';
             print(board,newShark);
             shark=newShark;
      else 
              disp('Cannot Move');
     end
end


newShark=shark;
----------------------------------------
function []=play(board,shark,oldShark)
print(board,shark);
[isGoldfish coords]=getClosestGoldfish(board,shark);
if isGoldfish==true
    oldShark=shark;
    newShark=moveToGoldfish(board,coords,shark);
        [newBoard newShark]=eatGoldfish(board,coords,newShark);
        play(newBoard,newShark,oldShark);
else 
        disp('Game Ended');
end
--------------------------------------
I need help with these!!!

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited July 2009
    "How do I write a test script" is like asking "how do i write a function".

    You're asking for help on such a general topic that there's no real way we can help.

    The general way you should approach the problem is to:

    1) Decide what features you want to test
    2) Decide how you want to test them (test it with random values, values that you choose, values that are input)
    3) Write a script .m file that calls your functions with the values.
  • drasnordrasnor Starship Operator Hawthorne, CA Icrontian
    edited July 2009
    On a piece of paper, write down a set of input values and the expected output for each input case. Write a script that delivers each input case to your functions and then logs the output. Compare the output produced to the expected output you wrote down. Debug of necessary and repeat.

    -drasnor :fold:
Sign In or Register to comment.