Test data consists of the user providing some input values and predicting the outputs. This can be quite easy for a simple program and the test data can be used twice.
- to check the model to see if it produces the correct results (model checking)
- to check the coded program to see if it produces the correct results (code checking)
Test data is developed by using the algorithm of the program. This algorithm is usually documented during the program design with either flowcharting or pseudocode. Here is the comment method I described in the previous section describing the flow - the IPO - for a program used for painting rectangular buildings.
// Preprocessor directives // Namespace int main() // ask user for the length of the building // accept length into variable bldgLength // ask user for the width of the building // accept width into variable bldgWidth // ask user for the height of the building // accept height iunto variable bldgHeight // ask user for the price per gallon of paint // accept price per gallon of paint into variable pricePerGal // ask user for the sq ft coverage of a gallon of paint // accept sq ft coverage of a gallon of paint into variable coveragePerGal // calculate the total area of the building: // area1 = (bldgLength * bldgHeight) *2 // area2 = (bldgWidth * bldgHeight) *2 // bldgArea = area1 + area2 You could combine all of this into one statement // calculate the number of gallons of paint needed: // totalGallons = bldgArea / coveragePerGal // round totalGallons up to the next whole gallon Can do this using the C++ round() function // paintCost = totalGallons * pricePerGal // output the number of gallons needed // display the total cost of the paint
Creating Test Data and Model Checking
Test data is used to verify that the inputs, processing and outputs are working correctly. As test data is initially developed it can verify that the documented algorithm (comment code in the example we are doing) is correct. It helps us understand and even visualize the inputs, processing and outputs of the program.
Inputs: The building is 100 feet long by 40 feet wide and 10 feet in height and I selected paint costing $28.49 per gallon that will cover 250 square feet per gallon. We should verify that the comment code is prompting the user for this data.
Processing: Using my solar powered hand held calculator, I can calculate (or predict) the total area would be: (100 x 10 x 2 plus 40 x 10 x 2) or 2,800 sq ft. The total gallons of paint would be: (2800 / 250) or 11.2 gallons. But rounded up, I would need twelve (12) gallons of paint. The total cost would be: (28.49 times 12) or $341.88. We should verify that the comment code is performing the correct calculations.
Output: Only the significant information (number of gallons to buy and the total cost) are displayed for the user to see. We should verify that the appropriate information is being displayed.
Testing the Coded Program - Code Checking
The test data can be developed and used to test the algorithm that is documented (in our case our comment code) during the program design phase. Once the program is code with compiler and linker errors resolved, the programmer gets to play user and should test the program using the test data developed. When you run your program, how will you know that it is working properly? Did you properly plan your logic to accomplish your purpose? Even if your plan was correct, did it get converted correctly (coded) into the chosen programming language (in our case C++)? The answer (or solution) to all of these questions is our test data.
By developing test data we are predicting what the results should be, thus we can verify that our program is working properly. When we run the program we would enter the input values used in our test data. Hopefully the program will output the predicted values. If not then our problem could be any of the following:
- The plan (IPO outline or other item) could be wrong
- The conversion of the plan to code might be wrong
- The test data results were calculated wrong
Resolving problems of this nature can be the most difficult problems a programmer encounters. You must review each of the above to determine where the error is lies. Fix the error and re-test your program.