Edge Case Testing
in C++
Learn to systematically identify and test boundary conditions, extreme inputs, and special values that break programs: where bugs hide.
What Are Edge Cases?
An edge case is an input or situation that occurs at the extreme boundary of expected operating parameters. These are the values your code encounters at the very limits of valid (or invalid) input ranges.
0, -1, \(2^{31}-1\), empty stringsWhy Edge Cases Matter
Most bugs live at the boundaries. A function that works for n = 100 may crash for n = 0 or overflow for n = \(2^{31} - 1\). Edge case testing catches these failures before deployment.
The Integer Boundary Map
C++ integer types have specific ranges. Every boundary is a potential edge case. Memorize these critical values:
The Boundary Testing Principle
For any condition that checks a boundary value \(B\), you must test three points: just below, exactly at, and just above the boundary.
Categories of Edge Cases
"", whitespace, special chars, very longnullptr, dangling, double-free, out-of-boundsEdge vs. Corner vs. Boundary
| Edge Case | Corner Case | Boundary Value | |
|---|---|---|---|
| Definition | Extreme of one parameter | Extremes of multiple parameters at once | Exact limit of a valid range |
| Example | Array with \(0\) elements | Empty array + null callback | x = \(2^{31} - 1\) |
| Frequency | Common | Rare | Common |
| Severity | High | Very High | Medium-High |
The ZEBRA Method
A systematic checklist for identifying edge cases in any C++ function:
nullptrINT_MAX, INT_MIN, DBL_MAX, maximum container sizeExample 1: Absolute Value
Consider writing a function that returns the absolute value of an integer. Seems trivial, but watch the edge case:
Example 2: Binary Search
(lo + hi) overflows when both are large. Fix: lo + (hi - lo) / 2arr.size() - 1 underflows to \(2^{64}-1\) when size is \(0\) (unsigned)Example 3: String Reversal
What edge cases should we test for this function?
"": size is \(0\), loop doesn't execute: should return """a": size / 2 = 0, no swaps: correct"abcd": 2 swaps needed, all characters pair"abc": middle character stays in place"racecar": result should equal inputExample 4: Safe Division
Building a safe division function requires anticipating multiple edge cases at once:
The Edge Case Checklist
INT_MIN, INT_MAX, negative inputs"", " ", single char, very long, special characters, Unicodenullptr, self-referential, circular references, dangling pointersNaN, INFINITY, \(-0.0\), very small (\(\approx 10^{-308}\)), precision lossWriting Test Cases Systematically
Use the assert macro or a testing framework to encode your edge case tests directly in C++:
Common Mistakes
vector.size() - 1 when the vector is empty. size() returns size_t (unsigned), so \(0 - 1\) wraps to a massive number.n = 5, 10, 100 and never testing n = 0, n = 1, or negative values.< when you need ≤ (or vice versa). Never testing the exact boundary value where the condition flips.a + b without checking if the sum exceeds INT_MAX. Classic in midpoint: (lo + hi) / 2.Edge Case Summary
| Input Type | Critical Edge Cases | Test Strategy |
|---|---|---|
| int | \(0\), \(\pm 1\), INT_MIN, INT_MAX |
BVA: test at and around each limit |
| size_t / unsigned | \(0\), \(1\), subtraction results | Watch for underflow wrapping |
| double | 0.0, -0.0, NaN, INFINITY |
Test arithmetic with special values |
| string | "", " ", 1-char, max length |
Empty and single-char first |
| vector / array | Empty, 1-element, all same, sorted | ZEBRA: Zero, Extremes, Boundaries... |
| pointer | nullptr, dangling |
Always check before dereference |
Question 1 of 5
Given the function int f(int n) { return 100 / n; }, which input is the most critical edge case to test?
100 / 0 crashes the program. This is the most critical edge case because it causes an immediate runtime error, not just a wrong answer.Question 2 of 5
What is the value of v.size() - 1 when v is an empty std::vector<int>?
vector::size() returns size_t, which is unsigned. Subtracting 1 from unsigned \(0\) wraps around to the maximum value of the type (\(2^{64} - 1\) on 64-bit). This is a classic unsigned underflow edge case.Question 3 of 5
In boundary value analysis, for the condition if (x ≥ 10), which set of test values provides the best boundary coverage?
> instead of ≥.Question 4 of 5
Why does int mid = (lo + hi) / 2; have a potential bug in binary search?
lo and hi are both large (e.g., both near \(10^9\)), their sum exceeds INT_MAX (\(2^{31}-1\)), causing signed integer overflow (undefined behavior). The safe fix is: int mid = lo + (hi - lo) / 2;Question 5 of 5
A function processes a std::string by accessing s[0] and s[s.size()-1]. Using the ZEBRA method, which is the first edge case category you should test?
s[0] undefined behavior (accessing past the end), and s.size()-1 wraps to a huge number (unsigned underflow). This is the most likely crash scenario.