Testing and Debugging Strategies for Computer Science II - Spring 2006 Lab 4 - Prof. Barba, Lab Reports of Data Structures and Algorithms

The strategies for testing and debugging in computer science ii - spring 2006 lab 4. The lab focuses on determining what 2d points are in what 2d rectangles. Students are required to write test code, read the code carefully, use the debugger, and complete several checkpoints. The document also provides instructions on how to use the debugger in visual studio.

Typology: Lab Reports

Pre 2010

Uploaded on 08/09/2009

koofers-user-ve6
koofers-user-ve6 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI-1200 Computer Science II Spring 2006
Lab 4 Testing and Debugging
Testing and debugging are important steps in programming. Loosely, you can think of testing as verifying
that your program works and debugging as finding and fixing errors once you’ve discovered it does not.
Writing test code is an important (and sometimes tedious) step. Many software libraries have “regression
tests” that run automatically to verify that code is behaving the way it should. Here are four strategies for
testing and debugging:
1. When you write a class, write a separate “driver” main function that calls each member function,
providing input that produces a known, correct result. Output of the actual result or, better yet,
automatic comparison between actual and correct result allows for verifying the correctness of a class
and its member functions.
2. Carefully reading the code. In doing so, you must strive to read what the code actually says and does
rather than what you think and hope it will do. Although developing this skill isn’t necessarily easy,
it is important.
3. Judicious use of cout statements to see what the program is actually doing (a.k.a. printf debug-
ging”). This is especially useful for printing the contents of a large data structure or class. It is often
hard to visualize large objects using the debugger (see next item) alone.
4. Using the debugger to (a) step through your program, (b) check the contents of various variables, and
(c) locate floating point exceptions and segmentation violations that cause your program to crash.
Points and Rectangles
The programming context for this lab is the problem of determining what 2D points are in what 2D rect-
angles. For rectangles, we will assume they are aligned with the coordinate axes (a.k.a. axis-aligned). This
makes it easy to represent and to test if a point is inside. Our code will create points and rectangles and
determine which points are in which rectangles. This type of problem is common in graphics and robotics.
The rectangle below is specified by its upper right corner point, (17,9), and its lower left corner point, (4,3).
The point (9,3.5) is inside the rectangle, whereas the point (8,10) is outside.
y
x
(4,3)
(17,9)
(9,3.5)
(8,10)
Please download the following 3 files needed for this lab and then turn off your internet connection:
http://www.cs.rpi.edu/academics/courses/spring06/cs2/labs/04_debugging/point2D.h
http://www.cs.rpi.edu/academics/courses/spring06/cs2/labs/04_debugging/rectangle.h
http://www.cs.rpi.edu/academics/courses/spring06/cs2/labs/04_debugging/rectangle.cpp
Checkpoint 1
Start by creating a project and adding the files Point2D.h,Rectangle.h, and Rectangle.cpp. Exam-
ine these files briefly. Point2D.h has a simple, self-contained class for representing point coordinates in
pf3

Partial preview of the text

Download Testing and Debugging Strategies for Computer Science II - Spring 2006 Lab 4 - Prof. Barba and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Computer Science II — Spring 2006

Lab 4 — Testing and Debugging

Testing and debugging are important steps in programming. Loosely, you can think of testing as verifying that your program works and debugging as finding and fixing errors once you’ve discovered it does not. Writing test code is an important (and sometimes tedious) step. Many software libraries have “regression tests” that run automatically to verify that code is behaving the way it should. Here are four strategies for testing and debugging:

  1. When you write a class, write a separate “driver” main function that calls each member function, providing input that produces a known, correct result. Output of the actual result or, better yet, automatic comparison between actual and correct result allows for verifying the correctness of a class and its member functions.
  2. Carefully reading the code. In doing so, you must strive to read what the code actually says and does rather than what you think and hope it will do. Although developing this skill isn’t necessarily easy, it is important.
  3. Judicious use of cout statements to see what the program is actually doing (a.k.a. “printf debug- ging”). This is especially useful for printing the contents of a large data structure or class. It is often hard to visualize large objects using the debugger (see next item) alone.
  4. Using the debugger to (a) step through your program, (b) check the contents of various variables, and (c) locate floating point exceptions and segmentation violations that cause your program to crash.

Points and Rectangles

The programming context for this lab is the problem of determining what 2D points are in what 2D rect- angles. For rectangles, we will assume they are aligned with the coordinate axes (a.k.a. axis-aligned). This makes it easy to represent and to test if a point is inside. Our code will create points and rectangles and determine which points are in which rectangles. This type of problem is common in graphics and robotics. The rectangle below is specified by its upper right corner point, (17, 9), and its lower left corner point, (4, 3). The point (9, 3 .5) is inside the rectangle, whereas the point (8, 10) is outside.

y

x

(4,3)

(17,9)

(9,3.5)

(8,10)

Please download the following 3 files needed for this lab and then turn off your internet connection:

http://www.cs.rpi.edu/academics/courses/spring06/cs2/labs/04_debugging/point2D.h http://www.cs.rpi.edu/academics/courses/spring06/cs2/labs/04_debugging/rectangle.h http://www.cs.rpi.edu/academics/courses/spring06/cs2/labs/04_debugging/rectangle.cpp

Checkpoint 1

Start by creating a project and adding the files Point2D.h, Rectangle.h, and Rectangle.cpp. Exam- ine these files briefly. Point2D.h has a simple, self-contained class for representing point coordinates in

two-dimensions. No associated .cpp file is needed because all member functions are defined in the class declaration. Rectangle.h and Rectangle.cpp contain the start to the Rectangle class. They also contain a bug. Please read the code now to see if you can find it. Don’t worry if you can not, but don’t fix it in the code if you do!

To complete this checkpoint: Look through Rectangle.h and Rectangle.cpp to determine what function need to be added and finish the implementation. Compile these files and remove any compilation errors.

Checkpoint 2

Create a new file in Visual Studio within the current project and call it test rectangle.cpp. Create a main function within this file. In the main function, write code to test each of the member functions. For example, write code to create several rectangles, and print their contents right after they are created. Write code that should produce both true and false in the function is point within. When there is non-trivial logic in a function, multiple inputs to the test code should be attempted to test every path through the program. Write code to add points (or not) to a rectangle. Write code to find what points are contained in both rectangles.

To complete this checkpoint: Show a TA your test cases and the error(s) that those test cases reveal in the provided code. Even if you know how to fix the bugs, please do not fix them yet.

Checkpoint 3

Now, we need to practice using the debugger to find and fix errors. The instructions below are specific to Visual Studio, but you may use any debugger that that works with your particular development environment. Make sure you learn how to do each of the tasks described below in your debugger.

  • Run your program that has tests for the basic member functions. Even though the program will compile and run, it will not give the correct output. You will get suspicious about a place in your code that might be wrong.
  • Set a breakpoint: In the source code, go to the file where error might have originated. Select Debug -> New Breakpoint. Four options will be presented. You can set a breakpoint based on the program entering a function, at specific point in a file, at a memory address, or when a specific data condition is met. Click File and you will notice that the current line number is displayed. Click OK. A breakpoint is now set. Your program will halt when execution reaches this location in the code (when run using the debugger). Note, that you can set this simplest Breakpoint (with standard options) not only using this menu, but also right clicking at a particular line (then Insert Breakpiont), or even more quickly clicking on a grey bar along left side of your window.
  • Look at the main menu and click Debug -> Start (F5). This will start your program under control of the debugger. A console display will immediately pop up. Your program will execute until reaching the breakpoint you’ve set.
  • At this point, several windows will appear including the source code window. You should still be able to see the console where you typed the input, but this may be hidden. It is important to look back and forth between this and the .net display.
  • Stepping through the program: You can now step through your program one line at a time. Use F10 to step to the next line of code in the current function. This executes this line before stopping and redisplaying (it happens very quickly, though). This works even if the line involves a function call. If you want to see what happens inside the function call, typing F11 will put you into the called function’s code. Try not to do this at calls to standard library functions. It can get messy. If you do, Shift-F11 will get you out.