1 Random Testing Teachpack
2 Testing beside
3 Testing plot-dataset

Lab 5

Language: Intermediate Student with lambda

This lab explores the idea of generating random inputs to a function and then testing that the function works properly for those random inputs. This consists of three major pieces.

This lab works thru two examples of how to do this.

Certainly random testing has its limitations, but you may be surprised by how quickly it finds ordinary errors!

1 Random Testing Teachpack

To help with the actual testing, we have provided the teachpack rand-test.ss. Skim this section and skip ahead to the next, referring back here as appropriate to complete the exercises in the next two sections.

The teachpack provides three constructs. The first, check-property, is an extension of check-expect that repeatedly tests some predicate of a function.

(check-property number predicate argument-list)

check-property runs the argument-list expression number times. Each time it is expected to produce a (random) list of arguments suitable for predicate. check-property then passes the elements of that list to predicate.

If predicate ever returns false, then check-property prints out the value of the argument-list. If it always returns true, check-property informs you that it could not find a failing test within number tries.

(If you cannot find failing tests within thousands of tries, then probably random testing cannot find a failing test.)

(make-fake-random N ...+)
  (-> natural-number/c natural-number/c)
  N : natural-number/c

The make-fake-random procedure accepts one or more numbers and returns a procedure that looks like random, except that the function is not actually random. Instead, the function returns the arguments passed to make-fake-random, one at a time.

For example,

  (define not-random (make-fake-random 1 2 3 4))
  (list (not-random 7) (not-random 7) (not-random 7) (not-random 7))

returns the list '(1 2 3 4).

(find-image/00 i1 i2)  posn?
  i1 : image?
  i2 : image?

Just like find-image, except this function always returns the location of i2 relative to the upper-left corner of i1, rather than relative to the pinholes.

2 Testing beside

You may have noticed that it is difficult to test random-solid-rectangle. And, in general, it is difficult to test functions that return random results. For this lab, therefore, we will not have functions that directly call random any longer. Instead, all of our random generation functions will take an extra argument named random that they use to generate random numbers and we will test the random generation functions using make-fake-random (above).

Rewrite random-solid-rectangle so that it now has the following contract and purpose, and thus can be tested properly. Test it.

(random-solid-rectangle c n random)  image?
  c : color
  n : natural-number/c
  random : (-> natural-number/c natural-number/c)

Returns a solid rectangle of color c with a random width and height (bounded by n) and a pinhole in a random place (using put-pinhole).

Uses random to choose the sizes.

3 Testing plot-dataset