JMU
Lab: Experimenting with Testing (Part I)


Instructions:Omitted

Getting Ready:Omitted

1. Black-Box Testing: This part of the lab will help you get seome experience with black-box testing.
  1. The Calculator class contains the following two methods:
        /**
         * Calculates the inverse of a number
         *
         * @param x   The number
         * @return    The inverse (i.e., 1.0/x)
         */
        public static double inverse(double x)
    
    
        /**
         * Calculates the percentage that a part is of a whole
         * (assuming both are non-negative)
         * 
         * @param part  The part (i.e., numerator)
         * @param whole The whole (i.e., denominator)
         * @return      A percentage (where 100.0 = 100%)
         */
        public static double percent(double part, double whole)
        

    Make sure you understand the purpose of these two methods.

  2. Write a driver that can be used to test the inverse() method.
  3. What code did you write?
        public static void main(String[] args)
        {
           double    x;
           
    
           // Small Values
           x = Double.NEGATIVE_INFINITY;
           for (int i=1; i<10; i++)
           {
              testInverse(x + i);
           }
           
    
           // Large Values
           x = Double.POSITIVE_INFINITY - 10.;
           for (int i=0; i<10; i++)
           {
              testInverse(x + i);
           }
           
    
           // Values Near 0
           for (x=-2.0; x<2.0; x+=0.1)
           {
              testInverse(x);
           }
           
           
           // 0
           testInverse(0.0);
        }
    
    
        private static void testInverse(double x)
        {
           System.out.println("Test Case: "+x+"\t"+Calculator.inverse(x));       
        }
        
  4. Compile and execute your driver.
  5. What values (if any) caused inverse() to fail?

    An argument of 0 yields a result of Double.POSITIVE_INFINITY. This may or may not be thought of as a failure.

  6. Write a driver that can be used to test the percent() method.
  7. What code did you write?
    Essentially the same code as above for whole. Now, however, each of these "loops" was executed for several values of part (including extreme positive values, values near 0, 0, and extreme negative values).
  8. Compile and execute your driver.
  9. What values (if any) caused percent() to fail?

    When both parameters are 0.0.

  10. What symptom made you realize that percent() method failed?

    It returned Double.NAN.

  11. Can you test for this symptom in Java? (Don't guess; add code to your driver and see.)

    Not using the == operator. There is a static method Double.isNaN(double) java.lang.Double#isNaN(double) that you can use, however.

2. Clear-Box Testing: This part of the lab will help you gain experience with clear-box testing and desk checking.
  1. Open the Series class.
  2. Desk check the arithmetic() method.
  3. What errors (if any) did you find?

    None.

  4. What oracle could you use to test the arithmetic() method? (Note: An oracle is a component that can be used to verify the function of another component.)

    We could compare the result returned by arithmetic() to (double)(n*(n+1)) / 2.0.

  5. Write a driver that tests the arithmetic() method using this oracle.
  6. Did you detect any failures?

    It does not work for negative numbers. Should it? (This is a discrete math question that you should be able to find the answer to.)

  7. Desk check and/or test the pi(int) method.
  8. Correct the fault(s) you detected (if any).
  9. What failures did you find and what faults did you correct?

    While desk checking I noticed that denominator was an int and, hence, integer division was being performed. I corrected this by making denominator a double (and using 1.0 in the numerator).

3. Clear-Box Testing of Subclasses: This part of the lab will help you understand some of the issues that arise when testing subclasses.
  1. Open the Document and FormattedDocument classes and refresh your memory of them. (You used them in the lab on "Experimenting with Accessibility/Visibility".)
  2. Which class should you test first? Why?

    The parent class. It is impossible to isolate faults in the subclass when the superclass contains faults.

  3. Within the Document class, which method should you test first, getDescription() or getWordCount()? Why?

    getWordCount() because it is used by getDescription().

  4. Describe six important test cases for the getWordCount() method.

    One with no words, one with 1 word, one that has three words using one delimiter, one that has three words using two delimiters, one that has multiple words using one delimiter, one that has multiple words using multiple delimiters (each used multiple times).

  5. Within the FormattedDocument class, in what order should you test the various methods?

    First the constructor. Second the steWidth() method. Third the getText() method. Fourth the getDescription() method.

  6. Do you need to test the inherited methods?

    Yes. But, if you did a good job of testing the superclass this should be easy.

  7. When is it especially important to test inherited methods?

    When they use methods that are overridden in the subclass.

  8. Describe five additional (over and above those you identified for the getWordCount() method) important test cases for the getText() method.

    One with a break that occurs at a delimiter. One with a break that occurs at the first letter of a word. One with a break that occurs in the middle of a word. One with a break that occurs at the end of a word. One with a break that occurs in the middle of a sequence of delimiters.

Copyright 2011