Name: _____________________ Class: CET 375
SSN/ID:   _____________________ Section & Group: ____________
Black and White Box Testing

Lab Exercise: Testing a Program
This lab exercise is intended to demonstrate the difference between black-box and white-box testing. In black-box testing, one simply executes the program with test data without any knowledge of the program's contents (it's in a black box). In white-box testing, one analyses the program's structure and traces its execution for various data sets, selecting them in such a way as to exercise the various paths of execution through the program. (For reference, please see Section 1.4 of the text C++: An Introduction to Data Structures for more information about black-box and white-box testing.)

A. Black-box Testing

Download the matrix.cpp file from here: source_lab03_matrix.cpp and compile it without examining the source code (for this part of the lab). Recall, in order to compile a file in *nix, you can issue the following command:


g++ source_lab02_matrix.cpp
Once you've compiled this program, you'll use it to search the following 3x3 matrix for some number. You are to carry out black-box testing of this program, beginning with the matrix:

 _        _
| 45 77 93 |
| 78 79 85 |
| 72 96 77 |
‾‾        ‾‾
  1. Search for the value 77. What output is produced?


  2. Search for the value 99. What output is produced?


  3. Try searching for other values and/or with another matrix. Try enough to show that your testing has turned up an error. Record what input you used and the resulting output.


B. White-box Testing

The function that does the searching in the program in A is as follows:


/* MatrixSearch searches the entries of the n X n matrix
 * mat in rowwise order for an entry equal to item.
 *
 * Receive: Matrix mat, integers n and item
 * Return:  true if item is found in mat, false otherwise
 ****************************************************************/

bool MatrixSearch(Matrix mat, int n, int item)
{

  bool found;                                     // 1
  for (int row = 0; row < n; row++)               // 2
    for (int col = 0; col < n; col++)             // 3
      if (mat[row][col] == item)                  // 4
        found = true;                             // 5
      else                                        // 6
        found = false;                            // 7
  return found;                                   // 8
}
  1. Complete the following trace table using item = 77 and
    
           _        _
          | 45 77 93 |
    mat = | 78 79 85 |
          | 72 96 77 |
          ‾‾        ‾‾
    
    Statement # row col mat[row][col] found
    1 ? ? ? ? (? = undefined)
    2 0 ? ? ?
    3 0 0 45 ?
    4,7 0 0 45 false
    3 0 1 77 false
    4,5 0 1 77 true
    3 0 2    
    4,_____        

  2. Complete the following trace table using item = one of the values you used in A that showed the program was not correct and
    
           _        _
          | 45 77 93 |
    mat = | 78 79 85 |
          | 72 96 77 |
          ‾‾        ‾‾
    
    Statement # row col mat[row][col] found
    1 ? ? ? ? (? = undefined)
    2 0 ? ? ?
    3 0 0 45 ?
    4,_____ 0 0 45  
    3 0 1 77  
    4,_____ 0 1 77  
    3 0 2    
    4,_____        

  3. After tracing the preceding function, it should be clear why the program fails to produce the correct output. What's wrong?




  4. If we replace the function MatrixSearch() with the following, then the program is correct, but it is not a good solution to the problem of searching a matrix. Why isn't it? (Consider the efficiency.)
    
    /* MatrixSearch searches the entries of the n X n matrix
     * mat in rowwise order for an entry equal to item.
     *
     * Receive: Matrix mat, integers n and item
     * Return:  true if item is found in mat, false otherwise
     ****************************************************************/
    
    bool MatrixSearch(Matrix mat, int n, int item)
    {
    
      bool found = false;
        for (int row = 0; row < n; row++)  
          for (int col = 0; col < n; col++)
            if (mat[row][col] == item)     
              found = true;                
      return found;                      
    }
    



  5. Show how the preceding function MatrixSearch() can easily be modified to produce a good solution to the problem of searching a matrix.




Hand In: This lab handout with the answers filled in attached to a listing of your final Makefile
(use the enscript command from your Programming Style Sheet to print it out:
enscript -E -G -2rj -M Letter -PECT2_PS <filename>. You can also use a2ps as in: a2ps <filename>).


Ricky J. Sethi <rickys at sethi.org>
Last modified: Mon Mar 28 18:30:31 PST 2005