Project 11: Testing and Analysis Framework
A complete testing and analysis framework with unit tests, static assertions, runtime assertions, and integration of sanitizers and static analyzers.
Quick Reference
| Attribute | Value |
|---|---|
| Primary Language | C |
| Alternative Languages | Python (for test runners) |
| Difficulty | Level 3 - Advanced |
| Time Estimate | See main guide |
| Knowledge Area | Testing, Static/Dynamic Analysis |
| Tooling | GCC, Clang, Valgrind, cppcheck |
| Prerequisites | See main guide |
What You Will Build
A complete testing and analysis framework with unit tests, static assertions, runtime assertions, and integration of sanitizers and static analyzers.
Why It Matters
This project builds core skills that appear repeatedly in real-world systems and tooling.
Core Challenges
- Writing effective tests → Maps to test coverage and edge cases
- Configuring sanitizers → Maps to memory and undefined behavior detection
- Interpreting analysis output → Maps to fixing real bugs
Key Concepts
- Map the project to core concepts before you code.
Real-World Outcome
# 1. Unit test framework
$ ./run_tests
Running test suite: buffer_tests
[PASS] test_buffer_create
[PASS] test_buffer_write
[FAIL] test_buffer_overflow
Expected: ERROR_OVERFLOW
Got: SUCCESS
Location: tests/buffer_test.c:45
[PASS] test_buffer_free
Results: 3/4 passed (75%)
# 2. Static analysis
$ make static-analysis
Running cppcheck...
src/buffer.c:42: warning: Possible null pointer dereference: ptr [nullPointer]
src/string.c:15: warning: Array 'buf[10]' accessed at index 10 [arrayIndexOutOfBounds]
Running clang-tidy...
src/buffer.c:50:5: warning: Value stored to 'result' is never read [deadcode.DeadStores]
src/string.c:20:3: warning: Call to function 'strcpy' is insecure [cert-str-str31-c]
Static analysis complete: 2 errors, 2 warnings
# 3. Sanitizer integration
$ make test-asan
Compiling with AddressSanitizer...
Running tests...
=================================================================
==12345==ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 1 at 0x602000000011
#0 0x401234 in read_buffer src/buffer.c:42
#1 0x401567 in test_buffer_read tests/buffer_test.c:50
...
# 4. Combined CI check
$ make ci-check
[1/4] Building with warnings as errors... PASS
[2/4] Running static analysis... PASS (0 errors)
[3/4] Running tests with ASan... PASS (all tests)
[4/4] Running tests with UBSan... FAIL
Undefined behavior detected in src/math.c:15
CI check failed. See logs above.
Implementation Guide
- Reproduce the simplest happy-path scenario.
- Build the smallest working version of the core feature.
- Add input validation and error handling.
- Add instrumentation/logging to confirm behavior.
- Refactor into clean modules with tests.
Milestones
- Milestone 1: Minimal working program that runs end-to-end.
- Milestone 2: Correct outputs for typical inputs.
- Milestone 3: Robust handling of edge cases.
- Milestone 4: Clean structure and documented usage.
Validation Checklist
- Output matches the real-world outcome example
- Handles invalid inputs safely
- Provides clear errors and exit codes
- Repeatable results across runs
References
- Main guide:
PROFESSIONAL_C_PROGRAMMING_MASTERY.md - Effective C, 2nd Edition by Robert C. Seacord