C++ Test Cases
What is a Test Case?
In the Problem Solving chapter, you tested a program by running it with different temperatures, and checking the output. Each of these checks is called a test case.
A test case has two parts: an input, and the expected output (the output that a correct program prints).
Here are the test cases for the practice problem Even or Odd. Read a whole number, and print Even if it can be divided by 2, or Odd if it cannot:
| Input | Expected output |
|---|---|
| 7 | Odd |
| 4 | Even |
| 0 | Even |
| 13 | Odd |
Check Even or Odd
How do you know if a number is even? Use the modulus operator % (see Arithmetic Operators). It gives the remainder of a division: what is left over.
7 % 2is1: 7 divided by 2 is 3, and 1 is left over.4 % 2is0: 4 divided by 2 is 2, and nothing is left over.
So if n % 2 is 0, the number is even. If not, it is odd:
Example
#include <iostream>
using namespace std;
int main() {
int n = 7;
if (n % 2 == 0) {
cout << "Even\n";
} else {
cout << "Odd\n";
}
return 0;
}
Result:
Odd
Try it Yourself »
Click "Try it Yourself", change n to 4, and run the code again. Now it prints Even.
The Output Must Match Exactly
When you submit a practice problem, your program runs once for each test case. Each time, it gets the input, and its output is compared with the expected output.
The output must be exactly the same, letter by letter. If the input is 7, only the first output passes:
| Your output | Result |
|---|---|
| Odd | Passed |
| odd | Failed: a lowercase o |
| Odd! | Failed: an extra exclamation mark |
| The number is Odd | Failed: extra words |
So print exactly what the problem asks for, and nothing more.
Edge Cases
When you test a program, you should not only test normal values, like 5 or 42. You should also test unusual values, because many bugs hide there.
These unusual values are called edge cases.
Here are edge cases to try for common kinds of input:
| Input | Edge cases to try |
|---|---|
| A number | 0, 1, a negative number, a very large number |
| An array | No values at all, only one value, all values the same |
| A string | An empty string (""), one letter, uppercase letters, spaces |
| A range, like 1 to 100 | The first and last value (1 and 100), and the values just outside (0 and 101) |
In Even or Odd, the input is a number. So test 0, and a negative number like -7:
0: is 0 even? Yes. 0 divided by 2 is 0, and nothing is left over, so0 % 2is0.-7: it is odd, but in C++,-7 % 2is-1, not1. So if your code checksn % 2 == 1, it says that -7 is even! You will see this in the tests below.
Run the Tests
Here are three solutions to Even or Odd. Choose a solution, and click "Run tests" to check it with five test cases, just like the practice editor does:
Code
Test cases
Explanation
Test Your Own Code
You can also test your code yourself, before you submit it. Put the solution in a function, and write a small test() function. It compares the output with the expected output, and prints PASS or FAIL:
Example
#include <iostream>
#include <string>
using namespace std;
string evenOrOdd(int n) {
if (n % 2 == 0) {
return "Even";
} else {
return "Odd";
}
}
void test(int input, string expected) {
string output = evenOrOdd(input);
if (output == expected) {
cout << "PASS: " << input << "\n";
} else {
cout << "FAIL: " << input << " (expected " << expected << ", got " << output << ")\n";
}
}
int main() {
test(7, "Odd");
test(4, "Even");
test(0, "Even");
test(13, "Odd");
test(-7, "Odd");
return 0;
}
Result:
PASS: 7
PASS: 4
PASS: 0
PASS: 13
PASS: -7
Try it Yourself »
In C++, you can compare two strings with == (see Strings). Try to change "Odd" to "odd" in evenOrOdd(), and run the tests again. The FAIL lines tell you exactly which test cases broke.
Your Turn
Now solve Even or Odd in our practice editor. The starter code already reads the number from input. Remember the edge cases, and print exactly Even or Odd!
Summary
- A test case is an input, together with the expected output.
- Practice problems run your program once for each test case. Your output must be exactly the same as the expected output.
- Edge cases, like
0and negative numbers, are where bugs often hide. - A
test()function lets you check your own code with many test cases at once.
You now know how to understand a problem, build the logic, and test your code. Use your new skills on our C++ Practice Problems!