Lecture 7: Debugging and Testing
Background Material
- C, C++, Java, Perl
- Think about your programming experience. What types of bugs
have you encountered? How did you find them? Which bugs were
particularly difficult to find. How do you test your code? Do
you test while you are developing your code or do you wait until
you are done? How do you determine your test cases?
Reading
Topics
- Debugging
- Debuggers (gdb)
- Effective use of print statements
- Stack trace
- Use of assertions.
- Look for familiar patterns.
- Examine the most recent changes
- Don't make the same mistake twice
- Debug it now, not later
- Think before debugging (what do you expect to happen?
each test should aim at a definitive outcome that confirms
or denies a specific hypothesis).
- Write trivial programs to test hypotheses or confirm your
understanding of how something works.
- Divide and Conquer (to look for problem in code and to generate
small inputs that cause problems).
- Display output to localize your search.
- Write self-checking code.
- Write a log file.
- Draw a picture.
- Use tools (diff, makes, memory leaks, version control).
- Keep records.
- Testing
- Test code at its boundaries.
- Test pre- and post-conditions (use assertions).
- Program defensively.
- Check error returns.
- Test Incrementally.
- Test simple parts first.
- Know what output to expect.
- Compare independent implementations.
- Measure test coverage.
- Automate testing (regression testing, stress tests).
- Try to force errors to happen (cause memory allocator to fail)
Lecture Notes
Programs
- bug1.c - common bug in C.
- bug2.c - common bug in C.
- A program that causes a segmentation violation
sorti.c.
To generate the error run sorti with the input
in"
This program uses qsort to sort an array of integers. The error
comes from passing qsort a function to compare strings rather than
integers. Why can't the compiler catch this? You can uncover the problem
by producing looking at a stack trace from when the program crashed (see
the lab on how to do this). What happens if you are using qsort to sort
strings and accidently pass the integer comparison function?
- avg.c - example use of assert.
- quicksortbug.c - simple
bug in the quicksort program from the text - how would you find it? What
assertions could you put into the code to check that the algorithm was
implemented properly?
Resources
- Materials on different languages: perl, C, C++ (STL), Java
- Introduction to Perl perl.ppt and
perl.pdf
Lab
Assignments
Created: Jan. 27, 2005 () by jjohnson@cs.drexel.edu