Tutorial 3, CPSC 331, Winter 2017

home page -  news -  syllabus -  schedule -  assignments -  tutorials -  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 -  Tutorial 17 -  Tutorial 18 -  Tutorial 19 -  Tutorial 20 -  Tutorial 21 -  Tutorial 22 -  Tutorial 23 -  Tutorial 24

 Testing of Programs

About This Exercise

This exercise can be used by students to make sure that they understand the basic concepts concerning software testing.

Students should read through this exercise, and spend time trying to solve the problems on it, before attending the tutorial.

Expected Background and Preparation

As noted above, the following problems test comprehension and ability to apply material presented during the lectures on software testing. The appendix on testing of the Koffman and Wolfgang supplemental reference listed on the computer science background page are also recommended reading.

Warmup Exercises — To Check That You Understand the Basics

It is to be expected that all of the questions in this first section can be answered by any student who has successfully completed the prerequisites for this course, attended the lectures in in which software testing has been discussed, and carefully read through the material about this that is now available.

Teaching assistants will not be spending very much time in tutorials discussing these questions! Students should look at them anyway to make sure that they know how to answer them.

Students who do have difficulty with these questions should contact the course instructor to get extra help.

Definitions

  1. Explain the meaning of each of the following.

    1. a syntax error
    2. a run-time error
    3. a logic error
    4. a test plan
    5. a unit test
    6. a regression test
    7. integration testing
    8. validation testing
    9. system testing
    10. static testing
    11. dynamic testing
    12. a black box test
    13. a white box test
    14. functional testing
    15. structural testing
    16. defensive programming
    17. debugging
  2. What should a test plan include?

Testing Principles

  1. When (during software development) should testing begin? Why?

  2. Who should test your software? Why?

  3. Explain why tests should be thoroughly documented.

  4. Is it possible to test, thoroughly and effectively, if a specification of the problem to be solved is not available? Why (or why not)?

  5. Why are proofs of correctness and testing of software both useful?

  6. It was claimed in class that the objective of testing is to prove that software is incorrect, and that a test is successful if it finds evidence of an error. Why is this the case?

  7. If your experience as a programmer has been limited to the course you’ve taken here and at schools you’ve attended before, then it might seem like far more time and effort is now being devoted to testing than is necessary.

    Why is testing worthwhile anyway? In particular, when (or for what kind of software development) is this worthwhile?

Problems for Discussion in the Tutorial

The following longer problems will be discussed during the tutorial. They ask you to apply the concepts that you have now heard about in order to design a test plan for a program, test, and then debug it.

Please try to solve the first of these problems before you look at the one that follows it!

  1. Design black box tests (as many as you feel are needed to be sure that testing is reasonably comprehensive) for a method whose signature, precondition and postconditions are all as follows.

    Signature: public static boolean distinctEntries ( int[] A )

    Precondition P: Inputs include

    • n: a postive integer
    • A: an integer array of length n, with entries A[0], A[1], …, A[n−1]

    Postcondition Q:

    • The array A has not been changed
    • Value returned is true if the entries of A are distinct, and is false otherwise
  2. If you solved the previous problem then you have developed a set of black box tests for a method that decides whether the entries of a given integer array are distinct.

    1. Design any additional white box tests that you think are appropriate for the following method.

    public static boolean distinctEntries ( int[] A ) { for (int i=1; i <= A.length; i++ ) { /* * Loop Invariant: * a) i is an integer such that 1 <= i < A.length * b) A[r] is not equal to A[s] for all integers r * and s such that 0 <= r < s < i * Loop Variant: A.length − i */ for (int j=1; j <= i; j++ ) { /* * Loop Invariant: * a) i and j are integers such that * 0 <= j < i < A.length * b) A[r] is not equal to A[s] for all * integers r and s such that * 0 <= r < s < i * c) A[r] is not equal to A[i] for every * integer r such that * 0 <= r < j * Loop Variant: i−j */ if (A[j] = A[i]) { return false; }; }; }; return true; }

    1. Perform “static testing” to try to find as many errors in the above code as you can. Correct any errors that you find.

  3. The loop invariants (especially the one for the inner loop) that are included in the documentation of the above program probably seem to be awfully long and complicated.

    Since this is the first example of a program that has been given in this course that includes a nested loop — and since you will almost certainly need to write and document such programs yourself, later on — it worth spending time to study these loop invariants and to make sure that you understand them.

    1. Sketch a proof of the correctness of the corrected program you obtained from the above, using its documentation and the techniques for this that have now been introduced. Depending on how successful you were in answering the above question, you might be able to do this with few problems — or you might discover an error or two that your testing missed!

    2. Now, suppose you try to simplify the loop invariant for the inner loop by leaving out conditions (a) and (b). What goes wrong?

  4. For extra practice, try implementing the above method in Java and implement your tests using the JUnit framework. You can find information about working with JUnit on the Java testing page of the course website and some relevant practice exercises in Self-Study Exercise 3.


Last updated:
http://www.cpsc.ucalgary.ca/~jacobs/Courses/cpsc331/W17/tutorials/tutorial3.html