01 / 21

Edge Case Testing
in C++

Learn to systematically identify and test boundary conditions, extreme inputs, and special values that break programs: where bugs hide.

C++ Software Testing

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.

Boundary Values
Inputs at the very edge of a valid range: 0, -1, \(2^{31}-1\), empty strings
Corner Cases
Combinations of multiple boundary values interacting simultaneously
Degenerate Inputs
Inputs that are technically valid but trivial: empty containers, single elements, zero-length

Why 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.

Where Bugs Hide: Input Distribution Input Values Bug Density Boundary Typical Boundary

The Integer Boundary Map

C++ integer types have specific ranges. Every boundary is a potential edge case. Memorize these critical values:

INT_MIN -2,147,483,648 -1 0 1 INT_MAX 2,147,483,647 Overflow danger zone Overflow danger zone

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.

Valid region Invalid region \(B\) \(B - 1\) \(B + 1\) Should fail Boundary Should pass

Categories of Edge Cases

Edge Cases Numeric Container String Pointer/Memory State/Logic
Numeric
\(0\), negatives, overflow, \(\text{NaN}\), \(\infty\)
Container
Empty, single element, duplicates, sorted/reversed
String
Empty "", whitespace, special chars, very long
Pointer
nullptr, dangling, double-free, out-of-bounds
State/Logic
Race conditions, unexpected state transitions, first/last item in a loop

Edge 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:

Z
Zero: pass \(0\), empty string, empty container, nullptr
E
Extremes: test INT_MAX, INT_MIN, DBL_MAX, maximum container size
B
Boundaries: test at, just below, and just above every condition threshold
R
Repeated/Reversed: all duplicates, already sorted, reverse sorted, single element repeated
A
Adversarial: negative numbers, special characters, very large inputs, type mismatches

Example 1: Absolute Value

Consider writing a function that returns the absolute value of an integer. Seems trivial, but watch the edge case:

int myAbs(int x) { if (x < 0) return -x; return x; }

Example 2: Binary Search

int binarySearch(vector<int>& arr, int target) { int lo = 0, hi = arr.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; // Bug here! if (arr[mid] == target) return mid; if (arr[mid] < target) lo = mid + 1; else hi = mid - 1; } return -1; }
Overflow Bug
(lo + hi) overflows when both are large. Fix: lo + (hi - lo) / 2
Empty Array
arr.size() - 1 underflows to \(2^{64}-1\) when size is \(0\) (unsigned)
Single Element
Test target found and not found with exactly one element

Example 3: String Reversal

string reverseStr(string s) { for (int i = 0; i < s.size() / 2; i++) { swap(s[i], s[s.size() - 1 - i]); } return s; }

What edge cases should we test for this function?

1
Empty string "": size is \(0\), loop doesn't execute: should return ""
2
Single character "a": size / 2 = 0, no swaps: correct
3
Even length "abcd": 2 swaps needed, all characters pair
4
Odd length "abc": middle character stays in place
5
Palindrome "racecar": result should equal input

Example 4: Safe Division

Building a safe division function requires anticipating multiple edge cases at once:

double safeDivide(int a, int b) { if (b == 0) throw runtime_error("div by zero"); if (a == INT_MIN && b == -1) throw overflow_error("overflow"); return static_cast<double>(a) / b; }
\(b = 0\) Throws error \(\text{INT\_MIN} / -1\)overflow Throws error \(0 / b\) Returns 0.0 \(a/b\) Returns result Complete edge case coverage: (5, 0) (INT_MIN, -1) (0, 5) (10, 3) (0, 0) (-1, -1) (0, -1) (-6, 2)

The Edge Case Checklist

Integers
\(0\), \(-1\), \(1\), INT_MIN, INT_MAX, negative inputs
Containers
Empty, size 1, size 2, all same elements, already sorted, reverse sorted
Strings
"", " ", single char, very long, special characters, Unicode
Pointers
nullptr, self-referential, circular references, dangling pointers
Floating Point
NaN, INFINITY, \(-0.0\), very small (\(\approx 10^{-308}\)), precision loss
Recursion
Base case input, max depth, stack overflow, mutual recursion cycles

Writing Test Cases Systematically

Use the assert macro or a testing framework to encode your edge case tests directly in C++:

#include <cassert> void testMyAbs() { // Zero assert(myAbs(0) == 0); // Typical positive and negative assert(myAbs(5) == 5); assert(myAbs(-5) == 5); // Boundary: adjacent to zero assert(myAbs(1) == 1); assert(myAbs(-1) == 1); // Extreme: INT_MAX assert(myAbs(INT_MAX) == INT_MAX); // Edge: INT_MIN (the dangerous one!) // assert(myAbs(INT_MIN) == ???); // This would cause undefined behavior! }

Common Mistakes

✕ Unsigned Underflow
Using vector.size() - 1 when the vector is empty. size() returns size_t (unsigned), so \(0 - 1\) wraps to a massive number.
✕ Only Happy Path
Testing only typical inputs like n = 5, 10, 100 and never testing n = 0, n = 1, or negative values.
✕ Off-by-One
Using < when you need ≤ (or vice versa). Never testing the exact boundary value where the condition flips.
✕ Ignoring Overflow
Computing 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?

Question 2 of 5

What is the value of v.size() - 1 when v is an empty std::vector<int>?

Question 3 of 5

In boundary value analysis, for the condition if (x ≥ 10), which set of test values provides the best boundary coverage?

Question 4 of 5

Why does int mid = (lo + hi) / 2; have a potential bug in binary search?

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?