Skip to content

Add round-trip serialization tests for *Params types#24

Merged
vladiant merged 1 commit into
mainfrom
add_serialization_tests
Apr 29, 2026
Merged

Add round-trip serialization tests for *Params types#24
vladiant merged 1 commit into
mainfrom
add_serialization_tests

Conversation

@vladiant
Copy link
Copy Markdown
Owner

Summary

The cascade trainer persists every stage's configuration to params.xml
via cv::FileStorage, then reads it back when resuming training or
emitting the final cascade. This MR locks down that contract with a new
test file that round-trips each *Params type through an in-memory
FileStorage and verifies the result matches the source — plus a focused
set of negative cases for the documented validation paths.

Library line coverage rises from 41.1% → 42.4%, with notable gains
in features.cpp (85% → 96%), haarfeatures.cpp (67% → 73%) and
boost.cpp (63% → 70%). No production code changes.

Changes

traincascade/test/test_serialization.cpp (new)

19 doctest test cases, all strictly following the Arrange / Act /
Assert
pattern with comments per step. Two small helpers wrap each
instance under a named section since the write/read methods emit
children directly without their own enclosing node:

template <typename Params>
std::string writeToMemory(const Params& src, const std::string& section);

template <typename Params>
bool readFromMemory(Params& dst, const std::string& xml,
                    const std::string& section);

Both use FileStorage::MEMORY — no temp files, no I/O.

Type Tests Coverage
CvCascadeParams 6 HAAR / LBP / HOG round-trips; empty node; zero width; unknown feature type string
CvFeatureParams 2 Round-trip; empty node returns false
CvHaarFeatureParams 5 BASIC / CORE / ALL round-trips; unknown mode; missing mode
CvLBPFeatureParams 1 Default round-trip (256 / 1)
CvCascadeBoostParams 5 GENTLE / DISCRETE / REAL+LOGIT round-trips; out-of-range minHitRate throws; unknown boost type throws

CMakeLists.txt

  • Add test/test_serialization.cpp to the test_traincascade target.

Library bug surfaced (not fixed in this MR)

CvCascadeBoostParams::CvCascadeBoostParams(int _boostType, ...) writes
boost_type = cv::ml::Boost::GENTLE after delegating to the base
constructor, so the _boostType argument is effectively ignored. The
DISCRETE and REAL/LOGIT round-trip tests work around this by
setting boost_type directly after construction; the workaround is
documented in test comments. Recommended follow-up: drop the override or
honor the argument.

Test results

[doctest] test cases:  80 |  80 passed | 0 failed | 0 skipped
[doctest] assertions: 272 | 272 passed | 0 failed |
[doctest] Status: SUCCESS!

ctest: 100% tests passed, 0 tests failed out of 1

Coverage impact (lib)

Metric Before After
Lines 41.1% (1921 / 4676) 42.4% (1984 / 4676)
Functions 76.0% (196 / 258) 77.5% (200 / 258)
Branches 25.6% (1280 / 5006) 27.7% (1388 / 5006)

Per-file gains:

File Before → After
features.cpp 85% → 96%
haarfeatures.cpp 67% → 73%
boost.cpp 63% → 70%

The new tests fully exercise CvFeatureParams::read/write (including
the empty-node guard), all three CvHaarFeatureParams modes plus their
two failure branches, and both CV_Error paths inside
CvCascadeBoostParams::read (unknown boost type and out-of-range
values).

Risks / Notes

  • All tests use in-memory FileStorage — no filesystem state, no test
    fixtures to clean up.
  • Runtime contribution is well under one second; CI timing is
    unaffected.
  • No changes to library code in this MR; the boost_type issue noted
    above is documented in test comments and tracked separately.

Checklist

  • All 80 tests pass locally (ctest --output-on-failure)
  • AAA pattern with comments in every test
  • Happy paths and edge/error cases covered
  • No new resource files committed
  • No changes to library code

@vladiant vladiant merged commit 13dc33e into main Apr 29, 2026
7 checks passed
@vladiant vladiant deleted the add_serialization_tests branch April 29, 2026 20:49
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