Test detection boundaries
Write a portable test harness that rejects off-by-one detection bugs.
Test the boundary, not just a happy example
A threshold rule often fails at one character: > instead of >=. A useful test suite includes just below the boundary, exactly at it and just above it. A zero count also checks that an empty event set cannot trigger the rule.
The supplied, repository-authored helper exports reaches_threshold count threshold. It returns a shell status: zero for a match and nonzero otherwise, without printing anything. Sourcing this trusted helper is appropriate; sourcing evidence or policy would not be. Keep that trust distinction explicit.
Make failure impossible to overlook
Run each assertion in an if statement and increment a failure counter when the status differs from the expected outcome. Do not rely on set -e to behave like a test framework: its exceptions depend on where a command runs. Report a fixed success summary only when every assertion passed, and exit nonzero when any failed.
The checker substitutes flawed implementations to test your test harness. A harness that merely prints “passed” will be rejected. This is behavioral testing of your assertions, not trust in terminal text. The course’s own grade is computed outside the learner’s Bash environment from exit status and output.
source ./lib/detection.sh
if reaches_threshold 3 3; then printf 'Exact boundary matches\n'; fi
Put it into practice.
Source the trusted lib/detection.sh. Test reaches_threshold for (0,3) false, (2,3) false, (3,3) true and (4,3) true. Run all four cases. On success print exactly 4 tests passed and a newline, with exit 0. If any expectation fails, exit nonzero and include Detection tests failed on stderr; do not print the success message. The checker replaces the helper with boundary bugs, so call the function instead of printing a fixed result.
See an explained solution
source ./lib/detection.sh || exit 1
failures=0
for count in 0 2 3 4; do
if reaches_threshold "$count" 3; then actual=match; else actual=no; fi
case "$count" in 0|2) expected=no;; *) expected=match;; esac
[ "$actual" = "$expected" ] || failures=$((failures+1))
done
if [ "$failures" -ne 0 ]; then printf 'Detection tests failed\n' >&2; exit 1; fi
printf '4 tests passed\n'
The harness exercises both sides of the threshold and the exact boundary, accumulating failures explicitly. Substituting broken helpers proves that its assertions observe behavior rather than unconditionally announcing success.