From 7f318e1dd0496384e7bc6d8754c5a2a618a14b2a Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Fri, 14 Nov 2025 01:42:15 +0200 Subject: [PATCH] test: Add better coverage for Autofile size() The new test explicitly checks that the function does not change the current position. --- src/test/streams_tests.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp index 6b9d2b35db9..9633a79c95d 100644 --- a/src/test/streams_tests.cpp +++ b/src/test/streams_tests.cpp @@ -807,4 +807,41 @@ BOOST_AUTO_TEST_CASE(streams_hashed) BOOST_CHECK_EQUAL(hash_writer.GetHash(), hash_verifier.GetHash()); } +BOOST_AUTO_TEST_CASE(size_preserves_position) +{ + const fs::path path = m_args.GetDataDirBase() / "size_pos_test.bin"; + AutoFile f{fsbridge::fopen(path, "w+b")}; + for (uint8_t j = 0; j < 10; ++j) { + f << j; + } + + // Test that usage of size() does not change the current position + // + // Case: Pos at beginning of the file + f.seek(0, SEEK_SET); + (void)f.size(); + uint8_t first{}; + f >> first; + BOOST_CHECK_EQUAL(first, 0); + + // Case: Pos at middle of the file + f.seek(0, SEEK_SET); + // Move pos to middle + f.ignore(4); + (void)f.size(); + uint8_t middle{}; + f >> middle; + // Pos still at 4 + BOOST_CHECK_EQUAL(middle, 4); + + // Case: Pos at EOF + f.seek(0, SEEK_END); + (void)f.size(); + uint8_t end{}; + BOOST_CHECK_EXCEPTION(f >> end, std::ios_base::failure, HasReason{"AutoFile::read: end of file"}); + + BOOST_REQUIRE_EQUAL(f.fclose(), 0); + fs::remove(path); +} + BOOST_AUTO_TEST_SUITE_END()