Testing and Debugging

COS1511 - Introduction To Programming I · Introduction to Computers and C++ Programming

Testing and Debugging

Understanding Program Errors

Errors in programming can be classified into three main types: syntax errors, runtime errors, and logical errors.

Syntax Errors

Syntax errors occur when the code does not conform to the rules of the programming language. These errors are usually detected by the compiler. For example:

#include 

int main() {
    std::cout << "Hello, World!";
    return 0;
}

If you forget a semicolon at the end of the line with std::cout, like this:

std::cout << "Hello, World!"

The compiler will generate a syntax error, indicating that it cannot understand the code.

Watch out: Always check for missing semicolons and brackets, as these are common sources of syntax errors.

Runtime Errors

Runtime errors occur while the program is running. These errors can cause the program to crash or behave unexpectedly. For example, dividing by zero will cause a runtime error:

int main() {
    int x = 0;
    int y = 10 / x; // This will cause a runtime error
    return 0;
}

In this case, the program will terminate unexpectedly because division by zero is not defined.

Logical Errors

Logical errors occur when the program runs without crashing, but produces incorrect results. For example:

int main() {
    int a = 5;
    int b = 10;
    int sum = a - b; // This should be a + b
    std::cout << sum;
    return 0;
}

Here, the program will compile and run, but it will output the wrong result because of a mistake in the logic.

Remember: Always verify your logic to ensure that your program produces the expected results.

Testing Your Program

Testing is a crucial part of the development process. It involves executing your program with various inputs to check if it behaves as expected. Here are some common testing strategies:

Unit Testing

Unit testing involves testing individual components or functions of the program in isolation. Each function should be tested with different input values to ensure it behaves correctly. For example:

int add(int a, int b) {
    return a + b;
}

You can create unit tests for the add function:

assert(add(2, 3) == 5);
assert(add(-1, 1) == 0);
assert(add(0, 0) == 0);

Using assertions helps to verify that the function produces the expected output.

Integration Testing

Integration testing checks if different parts of the program work together correctly. This is important because a function might work well in isolation but fail when combined with other functions.

System Testing

System testing evaluates the entire program as a whole. It checks if the software meets the specified requirements. This includes testing the user interface, performance, and overall functionality.

Debugging Techniques

Debugging is the process of identifying and fixing errors in your program. Here are some techniques to help you debug effectively:

Using Print Statements

One of the simplest debugging techniques is to use print statements to track the flow of your program and the values of variables. For example:

int main() {
    int a = 5;
    int b = 10;
    std::cout << "Value of a: " << a << "\n";
    std::cout << "Value of b: " << b << "\n";
    return 0;
}

This will help you understand how variables change throughout the execution of your program.

Using a Debugger

A debugger is a tool that allows you to run your program step by step. You can set breakpoints, which are specific points in your code where the execution will pause. This lets you inspect variable values and control the flow of execution.

Check Common Errors

When debugging, always check for common errors such as:

  • Uninitialized variables
  • Off-by-one errors in loops
  • Incorrect use of operators (e.g., using = instead of ==)

Common Pitfalls

Watch out: Do not assume your program is correct just because it compiles without errors. Always test your program thoroughly.

Summary

  • Program errors can be syntax, runtime, or logical errors.
  • Testing includes unit testing, integration testing, and system testing.
  • Debugging techniques include using print statements and debuggers.
  • Common errors to check for include uninitialized variables and incorrect operator use.

Check your understanding

  1. What are the three main types of program errors?
  2. How can you test a function in isolation?
  3. What is the purpose of using a debugger?
  4. Why should you not assume your program is correct just because it compiles without errors?