Result = Add( 3, 4 )
Functions can be complex blocks of statements and contain multiple parameters. Lets look at a simple function:
Add( value1, value2 )
{
Result = value1 + value2
}
If left like this, the following questions apply to debuggers and troubleshooters:
- Is value1 able to use the "+" operation?
- Is value2 able to use the "+" operation?
- Are the "+" operations of value1 and value2 compatible?
- Can the total be assigned to Result?
We know from experience with math that we can answer these question if value1, value2, and Result are numbers, so we may rebuild Add and test it as:
//Documentation notes:
//"NUMBER" is a valid math type with addition as "+" and assignment.
//"IF( x ) THEN y" is defined that when x is TRUE, then y occurs.
//"IF( x ) THEN y ELSE z" as above, also chooses z if x is FALSE.
//"Error( )" is a defined function for reporting errors and halts.
//"Pass( )" is a function indicating success state was expected.
//"Fail( )" is a function indicating failure.
//Description: NumericalAdd is a Basic addition function.
//Expected: NUMBER Result
NUMBER NumericalAdd( NUMBER value1, NUMBER value2 )
{
IF( value1 NOT NUMBER ) THEN Error( )
IF( value2 NOT NUMBER ) THEN Error( )
Result = value1 + value2
}
//Description: TestSuite_NumericalAdd
//If Fail( ) is executed, then NumericalAdd needs to be
//rewritten.
TestSuite_NumericalAdd( )
{
//Result should be 2
IF( NumericalAdd( 1, 1 ) NOT NUMBER ) THEN Fail( ) ELSE Pass( )
//Error should occur for these cases. Pass is returned for the expected Error:
IF( NumericalAdd( 1, 'a') NOT NUMBER ) THEN Pass( ) ELSE Fail( )
IF( NumericalAdd( 'a', 1) NOT NUMBER ) THEN Pass( ) ELSE Fail( )
IF( NumericalAdd( 'a', 'b') NOT NUMBER ) THEN Pass( ) ELSE Fail( )
}
After executing TestSuite_NumericalAdd, we have completed basic debugging the sample function.
No comments:
Post a Comment