Skip to main content

Class 13: Testing Your Procedures, Continued

Held: Monday, 13 February 2017

We continue to explore testing.

Preliminaries

Overview

  • Pair programming
  • Key ideas in testing
  • Careful postconditions

Updates

News / Etc.

  • New partners! You know the drill.
  • Tuesday is Valentine’s day. I am likely to be off email starting at about 5:30 p.m. If you have exam questions, get them to me before then.
  • Warning! Friday the 13th falls on Monday this week.
    • That’s probably why I don’t yet have grades from the graders.

Reminders

  • Ask questions via email! I’m always happy to (try to) answer questions via email. There is no need to apologize when sending me questions. If I take too long to answer, send another email (or even text, if it’s a reasonable hour).
  • Visit me in my office! I’m always happy to see students during my office hours. I prefer that you book me at https://rebelsky.youcanbook.me, but you can also just show up during office hours and hope that I’m not busy.
  • Use our tutors! We have tutors available Sunday through Thursday evening from 7-10 p.m. in Science 3813/15.
  • Visit mentor sessions! We have mentor sessions on Wednesday and Thursday evenings from 8:00-9:00 p.m. in the CS Commons. Wednesdays will be more Q&A, Thursdays will include sample quizzes.
  • Visit review sessions! I run review sessions on Thursdays at 9am in this room.
  • We have individual tutors!
    We have individual tutors available for those who take advantage of the above and find that it’s not enough.
  • Visit our store! We have office supplies in the commons. Price is free will donation.
  • Get news! Feel free to ask me to sign you up for the department mailing list.
  • Suggest extra credit! Send me extra credit ideas and opportunities.

Upcoming Work

  • Exam 1 due Tuesday at 10:30 p.m.
  • Epilogue due Wednesday at 10:30 p.m.
  • No lab writeup!
  • Reading: Drawings as Values due tomorrow.

Extra credit (Academic/Artistic)

  • CS Table, Tuesday at noon, 14 Feb 2017. On Technology, Slots, and Whales. Reading packets should be available outside Curtsinger’s office or Orsera’s office.
  • Thursday extras, Thursday, 16 Feb 2017, 4:15 p.m., Science 3821: 4-1 joint BA/MSC program with UIowa.

Extra credit (Peer)

  • Friday-Saturday-Sunday, 17th-19th, Swimming and diving conference championships. (You can only get credit for two of the three days; staying for an hour counts.)
  • Saturday the 18th, Symphony Concert. 2-4 p.m. Sound painting and more! In Sebring-Lewis.

Good things to do

  • Women’s basketball (Senior day) next Saturday at 1pm.
  • Men’s basketball (Senior day) next Saturday at 3pm.

Code formatting

What I’d often receive.

(define irgb-weighted-average
  (lambda (weight color)
    (irgb(/(+(*(irgb-red color)weight)(irgb-green color)(irgb-blue color))(+ weight 2))
    (/(+(*(irgb-green color) weight)(irgb-red color)(irgb-blue color))(+ weight 2))
    (/(+(*(irgb-blue color) weight)(irgb-green color)(irgb-red color))(+ weight 2)))))

What I’d like

(define irgb-weighted-average
  (lambda (weight color)
    (irgb (/ (+ (* (irgb-red color) weight) (irgb-green color) (irgb-blue color)) 
             (+ weight 2))
          (/ (+ (* (irgb-green color) weight) (irgb-red color) (irgb-blue color)) 
             (+ weight 2))
          (/ (+ (* (irgb-blue color) weight) (irgb-green color) (irgb-red color)) 
             (+ weight 2)))))

Citing formulae

Sam goes off on storytelling tangents. But at least they are relevant.

Learning Outcomes

  • There are a host of errors we see in procedures.
  • Tests can help us identify those errors.
  • But that means we have to think carefully about possible cases for the procedure.
  • And we find that as we do more tests, our code gets longer.
  • Sometimes we can see problems better than we can write code that tests for those problems.

Old Notes

The following notes are from a previous version of the course. They are preserved for posterity.

Postconditions for center-at

  • Correct position
  • Correct width and height
  • Correct image (shape and color)

Testing center-at

  • Different starting points: origin, each quadrant, axes, whole and fractional positions, exact and inexact positions.
  • Different ending points: origin, each quadrant, axes, whole and fractional positions, exact and inexact positions.
  • Shapes: Squares, circles, non-square rectangles, non-circular ellipses.
  • Sizes: unit, large, small
  • Colors: At least one in which we’ve changed the color

Making Testing More Efficient

  • If your code is the same, write something that’s common.
  • See the extras on the lab.

Old Notes

The following notes are from a previous offering.

Using testing to assess candidate solutions

  • Standard testing model:
    • Hypothesis: This procedure is correct.
    • Experiment: If the procedure is correct, it should have this behavior on these inputs.
      • Ideally, write in such a way that it’s easy to rerun: Protocol in bench science, test suite in CS.
    • Conclusion(s): “It’s broken” or “It seems to work”
  • Note: The more experiments that succeed, the more confident you are, provided the experiments test different values.
  • An additional model from the lab
    • Problem: We have multiple procedures, some of which are likely incorrect.
    • A good test suite should let us identify which are incorrect.
    • Once we’ve eliminated all of the incorrect ones, we hope to be left with the correct ones.
    • We designed the problem this way to help you think about the kinds of tests you write. If you write a good test suite, you will eliminate most of these procedures.
    • The errors in the various versions are ones we’ve seen in student code.
  • Good testing requires some imaginative thinking: Are there inputs that are different than the ones I’ve already tried?

A problem in testing

  • How can we be confident that a classify-triangle procedure is correct?
  • We consider its formal definition.

      ;;; Procedure:
      ;;;   classify-triangle
      ;;; Parameters:
      ;;;   side1, a rational number [unverified]
      ;;;   side2, a rational number [unverified]
      ;;;   side3, a rational 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".
    
  • We start with some simple tests.

      (require triangles/tri-0000)
      (define triangle-tests
        (test-suite 
         "Testing classify-triangle"
         (test-case "unit side length equilateral triangle"
                    (check-equal? (classify-triangle 1 1 1) "equilateral"))))
      (run-tests triangle-tests)
    
  • That’s one test. What are some others?
  • We should check for isoceles triangles.

         (test-case "simple isosceles triangle"
                    (check-equal? (classify-triangle 2 2 3) "isosceles"))
    
  • Is that the only way to write an isosceles triangle? Does it matter? Let’s try another.

         (test-case "simple isosceles triangle"
                    (check-equal? (classify-triangle 2 2 3) "isosceles"))
         (test-case "simple isosceles triangle"
                    (check-equal? (classify-triangle 3 2 2) "isosceles"))
    

Improving your tests

  • As you write tests, you’ll sometimes find that you want to write very similar tests. For example, I might want to try all three isosceles triangles with side lengths 2, 2, and 3 as well as all isosceles triangles with side lengths 10, 10, and 15.
  • Rather than copying and pasting the same three lines over and over again, I might write a helper procedure.

      (define isosceles-tests
        (lambda (message same other)
          (test-suite
            message
            (check-equal? (classify-triangle same same other) "isosceles")
            (check-equal? (classify-triangle same other same) "isosceles")
            (check-equal? (classify-triangle other same same) "isosceles"))))
    
  • Now in my main suite, I can write

          (isosceles-tests "isosceles: same 2, other 3" 2 3)
          (isosceles-tests "isosceles: same 3, other 2" 3 2)
          (isosceles-tests "isosceles: same 10, other 15" 10 15)
          (isosceles-tests "isosceles: same 1/2, other 1/3" 1/2 1/3)
    
  • You’ll see this approach on the exam.