Data Structures and Algorithms • C# examples
Testing, Benchmarking, and Adversarial Inputs
Validate algorithm behavior and performance with boundary cases and representative workloads.
Concept
A useful test suite checks empty inputs, single values, duplicates, sorted and reverse-sorted data, extreme numeric values, and invalid constraints. Benchmarks should use realistic input distributions, warm-up, and repeated measurements; asymptotic reasoning remains necessary because one benchmark size can mislead.
How to approach it
Compare optimized implementations against a simple trusted oracle on generated small inputs, then benchmark release builds.
Complexity
Testing cost is workload-dependent; algorithm complexity still predicts scaling.
C# example
foreach (int[] sample in testCases)
{
int[] expected = SlowReference(sample);
int[] actual = Candidate(sample);
if (!expected.SequenceEqual(actual))
throw new InvalidOperationException("Mismatch");
}
Expected result or behavior
A reference implementation catches disagreements on small test cases.
Practice exercise
Generate random arrays and compare your custom sort against Array.Sort.
Key takeaway: Correctness tests and performance measurements answer different questions.