Tutorial 2, CPSC 331, Fall 2008

home page -  news -  syllabus -  schedule -  assignments -  tutorials -  tests -  java -  references -  Mike Jacobson


Tutorial 1 -  Tutorial 2 -  Tutorial 3 -  Tutorial 4 -  Tutorial 5 -  Tutorial 6 -  Tutorial 7 -  Tutorial 8 -  Tutorial 9 -  Tutorial 10 -  Tutorial 11 -  Tutorial 12 -  Tutorial 13 -  Tutorial 14 -  Tutorial 15 -  Tutorial 16

 Testing of Programs

About This Exercise

This exercise is based on material presented during the lectures on Wednesday, September 10 and in Section 2.5 of the textbook. Goals of this exercise are as follows.

Students should work through and try to solve the problems on this exercise before attending the tutorials during September 15—18. Problems on this exercise will be discussed during these tutorial — and you will find the tutorial to be significantly more useful if you have spent on the exercise before then, than you would if you have not.

Note: If possible, you should work on this exercise with another student in the class.

Problems To Be Solved

  1. Modify Your Java Environment. This is a continuation of part of the previous tutorial exercise.

    Confirm that you have access to (and see files in) the directory

    /usr/lib/junit4.4/

    when using your Department of Computer Science account. Then make any changes to your computing environment at school that are needed to ensure that the javac utility automatically searches for source files in this directory, as well as in your own Java source directory. You will need to include the path of the jar file

    /usr/lib/junit4.4/junit-4.4.jar

    in your CLASSPATH in order to use JUnit at school.

    Note that you can also use JUnit from home, after doing some additional work. Files that you will need to download and install, along with some information about how to do this, are available online at http://www.junit.org.

  2. Check Again That You Have Access to JUnit. Download the Java program RectangleTester.java, and place it in the same directory as the shape code from Tutorial 1. This program contains a partial implementation of a JUnit test suite to be used for testing the Rectangle.java class.

    Read this JUnit tutorial, for information about how test programs such as this are created and used. Then experiment, as needed until you are reasonably confident that you have access to JUnit. Notice that the Rectangle class has an error that you should detect using the JUnit tests. Identify and correct the error.

  3. Complete a Requirements Specification. Recall that the greatest common divisor gcd(a, b) of a pair of positive integers a and b is the largest integer that divides both a and b. For example, gcd(12, 16) = 4 and gcd(8, 16) = 8.

    It is possible to extend the definition of a “greatest common divisor” to define gcd(a, b) for all integers a and b. We will do so in the following way.

    • gcd(a, 0) = gcd(0, a) = a, for any nonnegative integer a.

      This defines gcd(a, b) whenever a and b are both nonnegative.

    • If either (or both) of a and b is negative, set gcd(a, b) to the greatest common divisor of the absolute value of a and the absolute value of b.

      This completes the definition.

    Write a specification of a method (of some object) with name “gcd” that receives two integers as inputs and returns their greatest common divisor as an output.

    What exceptions should be thrown by such a method? (Note that you will need to describe these when solving this problem.)

    Note: Requirements specifications for operations are discussed along with specifications for ADTs in Section 1.3 of the textbook.

    Note: When possible (at least, in the workplace, the person(s) who test code should not be the person(s) who wrote the code.

    If you know someone else in the class and would like to have a slightly more “realistic” exercise then it would be appropriate, at this point, to confer with this other person to be sure that you are each working with the same requirements specification for this problem.

  4. Develop Black Box Tests: If necessary, review the information about black box testing presented in lectures and the textbook. Then design a set of test cases for a function that computes the greatest divisor of two integers using the requirements specification that you developed to answer the previous question.

  5. Write Some Code: Write a Java program for a gcd function using the following pseudocode. The value returned by this function should be the greatest common divisor of its inputs. Of course you should include appropriate error checking and ensure that exceptions are thrown when this is appropriate.

    function gcd (a, b: integer) if (a==0) then return b elsif (b==0) then return a else p = abs(a); q = abs(b); while (q <> 0) do   // assert that gcd(p, q) = gcd(a, b) r = p mod q; p = q; q = r; end do; return p end if end function;

    Try to write your code in a way that makes testing easier (as described in the notes). Additional information about this can be found in Sun’s online documentation about programming with assertions.

    If you are working on this exercise with another student in the class then you should exchange your programs at this so that you are testing your colleague’s program instead of your own.

  6. Develop White Box Tests: Now that both a requirements specification and a Java program are available, white box tests can be developed. Review the information about white box testing found in lectures and the textbook, as needed, and then develop additional test cases for the program that you will test.

  7. Carry Out Static Testing: Look for errors in your colleague’s (or, if necessary, your) program by inspecting the code carefully.

    Try to debug the program in order to correct any errors that you found.

  8. Carry Out Dynamic Testing: Write a test class that uses JUnit to carry out the tests that you developed when answering the above questions, applying these tests to the code that is to be tested.

    If time permits then you should also consider writing a Java program that applies these tests to this code without using JUnit. (The objective of this part of the exercise is, of course, to convince you that it is worth having JUnit and learning how to use this utility.)

  9. Debug: Try to correct any errors that you have found. Make sure to avoid the common “common error in debugging” that was discussed in class when you do so!

    Remember, also, that you will need to repeat tests that have been made before, whenever code is changed!

  10. Questions To Think About:

    1. In what way (if any) could the code have been written differently, to make testing and debugging easier? Politely suggest changes along these lines to the author of the code you tested, if you notice any changes that should be made.

    2. There are (at least) two different situations that might arise, that influence the way exceptional cases should be checked for and reported:

      1. Input is supplied directly by, and results are reported to, a human being.

      2. Input is supplied by, and results are reported to, other modules in a software system that are also being rigorously tested.

      How might error checking and reporting be different in each case? Why?

    3. If you carried out testing extensively, or used Java’s assertion mechanism (or both), then you likely needed a reliable method to compute greatest common divisors of integers. Of course you should not have used the method you are testing! (Why not?)

      What did you do or use instead?

  11. Consider each of the following GCD programs.

    1. GCD1.java

    2. GCD2.java

    3. GCD3.java

    4. GCD4.java

    5. GCD5.java

    6. GCD6.java

    7. GCD7.java

    In each case, solve Problems 6–9 of the previous exercise, using the given code instead of the Java program that you wrote yourself.

    Note: To the best of our knowledge one of the above programs does not contain errors. Each of the others does — and, ideally, your testing should be complete enough to discover this.


This page last modified:
http://www.cpsc.ucalgary.ca/~jacobs/cpsc331/F08/tutorials/tutorial2.html