Programming paths / C++ Tutorial / Unit Testing, Assertions, and Testable Design

C++ course contents

Beginner

  1. What Modern C++ Is Good For
  2. Set Up a Compiler and Build a C++ Program
  3. Write Your First C++ Program
  4. Types, Initialization, and auto
  5. const, constexpr, and Compile-Time Values
  6. Console Input and Validation
  7. Operators, Conversions, and Integer Arithmetic
  8. Branching with if, switch, and Scoped Variables
  9. Loops and Range-Based for
  10. Functions, Parameters, and Return Values
  11. Built-In Arrays, std::array, and std::vector
  12. std::string, std::string_view, and Text Boundaries
  13. Structures, enum class, and Aggregate Initialization
  14. References, Pointers, and nullptr

Intermediate

  1. Classes, Encapsulation, and Invariants
  2. Constructors, Destructors, and Object Lifetimes
  3. RAII and Deterministic Resource Management
  4. Copying, Moving, and Value Semantics
  5. Smart Pointers and Ownership Choices
  6. STL Containers and Their Trade-Offs
  7. Iterators and Standard Algorithms
  8. Lambdas, Captures, and Callable Objects
  9. Function Templates and Type Deduction
  10. Exceptions and Exception-Safe Design
  11. std::optional and std::variant for Explicit States
  12. Files, Streams, and the Filesystem Library
  13. Headers, Translation Units, and the One Definition Rule

Advanced

  1. Concepts and Constrained Templates
  2. Ranges and Views in C++20
  3. Value Categories, std::move, and Perfect Forwarding
  4. Operator Overloading and Regular Types
  5. constexpr, consteval, and Compile-Time Programming
  6. Threads, Tasks, and Shared State
  7. Data Races, Atomics, and Memory Ordering
  8. Choosing Between Exceptions, optional, and Expected Results
  9. Undefined Behavior, Object Lifetimes, and Valid Programs
  10. Project Builds with CMake
  11. C++ Modules: Interfaces and Toolchain Support
  12. Unit Testing, Assertions, and Testable Design
  13. Profiling, Complexity, and Performance
  14. Secure C++: Input, Bounds, and Resource Ownership

Expert

  1. Designing C++ APIs and Library Boundaries
  2. Linkers, Libraries, and ABI Compatibility
  3. C++23 Features and std::expected
  4. Capstone: Build a Tested Command-Line Catalog

Modern C++ • C++20 baseline

Unit Testing, Assertions, and Testable Design

Test observable behavior at boundaries and make core logic independent of hard-to-control systems.

Unit tests check small contracts; integration tests verify components working together. Assertions are useful for internal invariants during development but should not validate untrusted user input. Keep time, files, network access, and randomness behind interfaces when deterministic tests are important.

Key points

  • Test normal, boundary, and failure cases.
  • A passing test suite is evidence, not proof, that all behavior is correct.

Example

#include <cassert>

int clamp_score(int value) {
    if (value < 0) return 0;
    if (value > 100) return 100;
    return value;
}

int main() {
    assert(clamp_score(-1) == 0);
    assert(clamp_score(50) == 50);
    assert(clamp_score(101) == 100);
}

What to expect

All three assertions pass for the boundary and ordinary cases.

Practice exercise

Add a test for each branch and decide whether a table-driven test would be clearer.

Key idea: Design code so important behavior can be tested without relying on a particular environment.