CSC151.01 2009F Functional Problem Solving : Labs

Laboratory: Unit Testing


Summary: In the laboratory, you will explore the ways in which small tests can help you develop and update code. You will also familiarize yourself with our testing library.

Exercises

Exercise 1: Testing the Tester

As you may recall from the reading, MediaScript provides four primary testing procedures:

  • (begin-tests!), which prepares the environment for testing.
  • (test! expression value), which determines (a) whether expression can be successfully evaluated and, if so, (b) whether the value of expression is value.
  • (test-error! expression), which determines whether or not expression reports an error (and treats it as a problem if expression does not).
  • (end-tests!), which reports on the tests completed so far.

a. In the Interactions pane, try each of the operations a few times to make sure you understand its operation. (Yes, this instruction is intentionally vague.)

b. What happens if you call end-tests! multiple times, perhaps with some intervening calls to test! or test-error! but with no intervening calls to begin-tests!?

Exercise 2: Testing Triangulation

Consider the following procedure documentation:

;;; Procedure:
;;;   classify-triangle
;;; Parameters:
;;;   side1, a real number [unverified]
;;;   side2, a real number [unverified]
;;;   side3, a real number [unverified]
;;; Purpose:
;;;   Determine the kind of triangle the three sides describe.
;;; Produces:
;;;   classification, a string 
;;; Preconditions:
;;;   side1, side2, and side3 together describe a triangle [verified]
;;; Postconditions:
;;;   If all three sides are equal, classification is "equilateral".
;;;   If exactly two sides are equal, classification is "isosceles".
;;;   If no two sides are equal, classification is "scalene".

a. Write a series of tests for this procedure. Your tests should look something like the following:

(load "/home/davisjan/csc/151/examples/triangles/tri.0000.sct")
(begin-tests!)
(test! (classify-triangle 1 1 1) "equilateral")
...
(end-tests!)

b. There are approximately 40 different versions of the procedure of varying degrees of correctness. (Professor Rebelsky wrote a program to create them!) They are named as follows (and all appear in the same directory as tri.0000.sct): tri.0000.sct, tri.0001.sct, tri.0010.sct, tri.0011.sct, tri.0100.sct, tri.0101.sct, tri.0110.sct, tri.0111.sct, tri.0200.sct, tri.0201.sct, tri.0210.sct, tri.0211.sct, tri.1000.sct, tri.1001.sct, tri.1010.sct, tri.1011.sct, tri.1100.sct, tri.1101.sct, tri.1110.sct, tri.1111.sct, tri.1200.sct, tri.1201.sct, tri.1210.sct, tri.1211.sct, tri.2000.sct, tri.2001.sct, tri.2010.sct, tri.2011.sct, tri.2100.sct, tri.2101.sct, tri.2110.sct, tri.2111.sct, tri.2200.sct, tri.2201.sct, tri.2210.sct, and tri.2211.sct

Pick eight or so of those files and run your tests on them. Which ones pass your tests? (Try to collaborate with your classmates so that all of them get tested.)

Exercise 3: Writing a Triangle Classifier

a. To the best of your ability, write the classify-triangle procedure described in the previous problem.

b. Rerun your tests using this version of the procedure. How many does it pass, how many does it fail? Do not rewrite the procedure to make it pass all these tests.

Exercise 4: A Simple Test Suite

Here is a test that someone might write for the procedure described above.

(load "/home/davisjan/csc/151/examples/triangles/tri.0000.sct")
(begin-tests!)
(test! (classify-triangle 1 1 1) "equilateral")
(test! (classify-triangle 2 2 3) "isosceles")
(test! (classify-triangle 3 4 5) "scalene")
(end-tests!)

a. Does your classify-triangle procedure pass these tests? If not, repair it to ensure that it passes all the tests.

b. Run this test suite on five of the variants mentioned in problem 2 that you have either not tested or that have passed all tests so far. How many pass the new test suite?

c. Can you write an obvious solution to classify-triangle that passes all of these tests, but fails to meet the specifications?

Exercise 5: Testing Error Checking

Someone thinking carefully about the definition of classify-triangle might worry that the tests, as written, do not check for non-triangles. For example, something with side lengths 1, 1, and 3 is not a triangle.

a. Add a test to the testing code above to ensure that classify-triangle rejects that non-triangle.

b. Does your version of classify-triangle pass the revised test suite? If not, correct it so that it does.

c. Run this test suite on five of the variants mentioned in Exercise 2 that you have either not tested or that have passed all tests so far. How many pass the new test suite?

Exercise 6: Testing Error Checking, Revisited

Another set of inputs for which classify-triangle is supposed to fail is one in which any of the side lengths are negative.

a. Add tests to the test suite for classify-triangle to ensure that classify-triangle rejects any set of three numbers that include a zero or negative number.

b. Does your version of classify-triangle pass the revised test suite? If not, correct it so that it does.

c. Run this test suite on five of the variants mentioned in Exercise 2 that you have either not tested or that have passed all tests so far. How many pass the new test suite?

Exercise 7: Symmetric Tests

We've tested for each of the three kinds of triangles, but we've only one test for each. What if the programmer mistakenly forgets to deal with the different orderings of parameters? We should make sure that the implementation works for each.

a. Add tests to the test suite for classify-triangle to ensure that classify-triangle correctly identifies the three kinds of triangles, not matter what the ordering. In particular, make sure that you test for the three variants of isosceles triangles.

(test! (classify-triangle 2 2 3) "isosceles")
(test! (classify-triangle 2 3 2) "isosceles")
(test! (classify-triangle 3 2 2) "isosceles")

b. Are there other tests in which the ordering might matter? (Hint: Think about some of the tests for errors.)

c. Does your version of classify-triangle pass the revised test suite? If not, correct it so that it does.

d. Run this test suite on five of the variants mentioned in problem 2 that you have either not tested or that have passed all tests so far. How many pass the new test suite?

Exercise 8: Testing Large Sides

We now might consider the range of side values for which classify-triangle should be tested.

a. Add tests that ensure that classify-triangle works for very large numbers (say, numbers greater than 10 billion).

b. Does your version of classify-triangle pass the revised test suite? If not, correct it so that it does.

c. Run this test suite on five of the variants mentioned in problem 2 that you have either not tested or that have passed all tests so far. How many pass the new test suite?

Exercise 9: Other Tests

a. Add any other tests you conceive of.

b. Be prepared to discuss those tests in class.

c. Does your version of classify-triangle pass the revised test suite? If not, correct it so that it does.

d. Run these tests on any variants mentioned in problem 2 that you have not yet tested or that have passed all the tests up through exercise 9. How many pass the tests?

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-9 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)

This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.