Skip to content

Add unit tests for traincascade library#22

Merged
vladiant merged 6 commits into
mainfrom
add_unit_tests
Apr 29, 2026
Merged

Add unit tests for traincascade library#22
vladiant merged 6 commits into
mainfrom
add_unit_tests

Conversation

@vladiant
Copy link
Copy Markdown
Owner

Summary

Introduces a doctest-based unit test suite for the traincascade library, wires it into CI across Ubuntu / macOS / Windows, and registers the test target with the top-level CTest runner.

Motivation

The traincascade library previously had a doctest harness scaffolded (main.cpp) but no actual test cases. This MR fills that gap with a comprehensive unit test suite focused on the library's pure / configuration surface, and ensures the tests run automatically on every push.

Changes

New test files

File Coverage area
test_o_utils.cpp icvCmpIntegers, cvAlign, CV_DTREE_CAT_DIR, LessThanIdx, LessThanPtr, cvPreprocessIndexArray, BlockedRange / parallel_for / parallel_reduce, CvDTreeNode::get_num_valid / set_num_valid
test_params.cpp CvDTreeParams, CvBoostParams, CvCascadeBoostParams, CvCascadeParams (default + parameterized ctors, scanAttr)
test_features.cpp CvFeatureParams::create, CvFeatureEvaluator::create, CvHaarFeatureParams / CvLBPFeatureParams / CvHOGFeatureParams, evaluator init / generateFeatures / setImage
test_imagestorage.cpp CvCascadeImageReader::create failure paths

All tests follow the Arrange–Act–Assert pattern with explicit comments and cover both the happy path and edge cases (out-of-range indices, duplicates, multi-dimensional inputs, unsupported types, empty ranges, null vs. allocated num_valid, too-small HOG window, unknown attribute names, missing files, …).

Build system

  • CMakeLists.txt
    • Registered the four new test sources on the test_traincascade target.
    • Removed -Wold-style-cast, -Wsign-conversion, -Wconversion from the test target only — these are triggered by inline definitions in the library's public headers (haarfeatures.h, lbpfeatures.h, HOGfeatures.h, traincascade_features.h) which the library itself does not enable.
  • CMakeLists.txt
    • Added include(CTest) + enable_testing() at the project root so ctest discovers the test from the top-level build directory.

CI

Added a Test step after the build step in:

  • ubuntu.yml
  • macos.yml
  • windows.yml

Each runs ctest --output-on-failure -C $BUILD_TYPE from the top-level build directory.

Test results

[doctest] test cases:  56 |  56 passed | 0 failed | 0 skipped
[doctest] assertions: 194 | 194 passed | 0 failed |
[doctest] Status: SUCCESS!

CTest from the top-level build directory:

1/1 Test #1: test_traincascade ................   Passed    0.73 sec
100% tests passed, 0 tests failed out of 1

Coverage

Measured with clang -fprofile-instr-generate -fcoverage-mapping:

Module Line coverage
o_cvboostparams.cpp, o_cvdtreeparams.cpp 100.00%
o_utils.cpp 93.75%
lbpfeatures.cpp 77.78%
haarfeatures.cpp 70.31%
features.cpp 59.02%
HOGfeatures.cpp 32.12%
imagestorage.cpp 20.13%
boost.cpp 11.16%
cascadeclassifier.cpp 8.09%
o_cvdtree*, o_cvboost*, o_cvcascadeboost*, o_cvstatmodel.cpp 0.00%

The suite achieves near-complete coverage of the public configuration surface (params + factories). The uncovered code is the heavyweight ML training internals reachable only through CvCascadeClassifier::train, which would require integration-style fixtures (real .vec + bg.txt) or further refactoring to be unit-testable.

Verification

  • ✅ Builds cleanly under clang 18 with the project's -Werror flags.
  • ✅ All 56 test cases / 194 assertions pass locally.
  • ctest discovers and runs the suite from the top-level build dir.
  • ✅ No changes to library source or behavior — tests are purely additive.

Out of scope / follow-ups

  • Integration tests for CvCascadeClassifier::train using the existing samples in res.
  • Round-trip serialization tests (read/write via cv::FileStorage) for *Params types.
  • setImage / operator() numerical tests on synthetic images for each feature evaluator.

@vladiant vladiant merged commit 70bb8b9 into main Apr 29, 2026
7 checks passed
@vladiant vladiant deleted the add_unit_tests branch April 29, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant