diff --git a/include/protozero/basic_pbf_writer.hpp b/include/protozero/basic_pbf_writer.hpp index 836c4bb9..e92ac0e8 100644 --- a/include/protozero/basic_pbf_writer.hpp +++ b/include/protozero/basic_pbf_writer.hpp @@ -269,12 +269,15 @@ class basic_pbf_writer { /** * A basic_pbf_writer object can be moved. After this the other * basic_pbf_writer will be invalid. + * + * @pre The other basic_pbf_writer must not have an open submessage. */ basic_pbf_writer(basic_pbf_writer&& other) noexcept : m_data{other.m_data}, m_parent_writer{other.m_parent_writer}, m_rollback_pos{other.m_rollback_pos}, m_pos{other.m_pos} { + protozero_assert(other.m_pos == 0 && "you can't move a basic_pbf_writer with an open submessage"); other.m_data = nullptr; other.m_parent_writer = nullptr; other.m_rollback_pos = 0; @@ -286,10 +289,13 @@ class basic_pbf_writer { * basic_pbf_writer will be invalid. If this basic_pbf_writer is for an * open submessage, that submessage is closed first, the same way the * destructor does. + * + * @pre Neither basic_pbf_writer must have an open submessage. */ basic_pbf_writer& operator=(basic_pbf_writer&& other) noexcept { // The temporary takes over the old state of *this and closes its - // submessage (if any) when it is destructed. + // submessage (if any) when it is destructed. The move constructor and + // swap() check that neither writer has an open submessage. basic_pbf_writer tmp{std::move(other)}; swap(tmp); return *this; @@ -321,8 +327,11 @@ class basic_pbf_writer { * Swap the contents of this object with the other. * * @param other Other object to swap data with. + * @pre Neither basic_pbf_writer must have an open submessage. */ void swap(basic_pbf_writer& other) noexcept { + protozero_assert(m_pos == 0 && "you can't move a basic_pbf_writer with an open submessage"); + protozero_assert(other.m_pos == 0 && "you can't move a basic_pbf_writer with an open submessage"); using std::swap; swap(m_data, other.m_data); swap(m_parent_writer, other.m_parent_writer); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b9a8c2da..42b5734b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -61,6 +61,17 @@ add_test(NAME reader_tests COMMAND reader_tests) set_tests_properties(reader_tests PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") +# See assert_tests.cpp for why every test case needs its own process. +add_executable(assert_tests assert_tests.cpp unit/main.cpp) + +foreach(_test_case IN ITEMS "move construct parent with open child" + "move assign from parent with open child" + "move assign onto parent with open child" + "swap parent with open child") + string(REPLACE " " "_" _test_name "assert_tests_${_test_case}") + add_test(NAME ${_test_name} COMMAND assert_tests -w NoTests "${_test_case}") +endforeach() + if(PROTOBUF_FOUND) message(STATUS "Found protobuf libraries: Adding writer tests...") diff --git a/test/README.md b/test/README.md index 01936ba9..4b1e2c71 100644 --- a/test/README.md +++ b/test/README.md @@ -9,6 +9,11 @@ Unit tests test low-level functions of the library. They are in the `unit` directory. +## Assert tests + +Most tests check for failed asserts with `REQUIRE_THROWS_AS(..., assert_error)`, because `test.hpp` defines `protozero_assert()` to throw. That doesn't work for asserts in `noexcept` functions, where the exception would call `std::terminate()`. Those are tested in `assert_tests.cpp`, where a failed assert ends the process, so CMake runs every test case in its own process. When adding a test case there, also add its name to the list in `CMakeLists.txt`. Tests checking that something does *not* assert go with the other tests. See the comment at the top of `assert_tests.cpp` for details. + + ## Organization of the reader/writer test cases The hart of the tests are the reader/writer tests checking all aspects of diff --git a/test/assert_tests.cpp b/test/assert_tests.cpp new file mode 100644 index 00000000..946f0cdc --- /dev/null +++ b/test/assert_tests.cpp @@ -0,0 +1,93 @@ + +// Tests for protozero_assert() checks in noexcept functions. +// +// The other tests define protozero_assert() to throw (see test.hpp), so that +// they can check for failed asserts with REQUIRE_THROWS_AS. That doesn't work +// in noexcept functions, because the exception calls std::terminate(). Here +// protozero_assert() ends the process instead, so every test case needs a +// process of its own. test/CMakeLists.txt adds a ctest test for each of them; +// when adding a test case here, add its name there, too. Tests that check +// that something does *not* assert belong with the other tests. +// +// A test case sets expected_assert to the assert message (with quotes) it +// expects. The process exits successfully only if an assert fails and its +// expression ends with that message. If no assert fails, the FAIL() at the +// end of the test case makes the process exit with an error. Matching the +// whole message matters: FAIL() throws, and destructors running during stack +// unwinding can fail other asserts, which must not count as success. + +#include + +#include +#include +#include +#include + +namespace { + +// The message (including quotes) the asserts checked by the current test case +// end with: protozero_assert(condition && "message"). +std::string expected_assert; + +bool ends_with(const std::string& str, const std::string& suffix) { + return str.size() >= suffix.size() && + str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; +} + +[[noreturn]] void assert_failed(const char* expr) { + std::printf("assert failed: %s\n", expr); + const bool ok = !expected_assert.empty() && ends_with(expr, expected_assert); + // Don't use std::exit(), it would run the destructors of Catch's static + // objects in the middle of a test case. + std::fflush(stdout); + std::_Exit(ok ? EXIT_SUCCESS : EXIT_FAILURE); +} + +const char* const open_submessage = R"("you can't move a basic_pbf_writer with an open submessage")"; + +} // anonymous namespace + +#define protozero_assert(x) if (!(x)) { assert_failed(#x); } // NOLINT(readability-simplify-boolean-expr) + +#include + +// Moving or swapping a writer with an open submessage leaves the sub-writer +// pointing to the moved-from writer, so these must assert. + +TEST_CASE("move construct parent with open child") { + expected_assert = open_submessage; + std::string buffer; + protozero::pbf_writer pw{buffer}; + const protozero::pbf_writer sub{pw, 1}; + const protozero::pbf_writer pw2{std::move(pw)}; + FAIL("expected assert did not happen"); +} + +TEST_CASE("move assign from parent with open child") { + expected_assert = open_submessage; + std::string buffer; + protozero::pbf_writer pw{buffer}; + const protozero::pbf_writer sub{pw, 1}; + protozero::pbf_writer pw2; + pw2 = std::move(pw); + FAIL("expected assert did not happen"); +} + +TEST_CASE("move assign onto parent with open child") { + expected_assert = open_submessage; + std::string buffer; + protozero::pbf_writer pw{buffer}; + const protozero::pbf_writer sub{pw, 1}; + pw = protozero::pbf_writer{}; + FAIL("expected assert did not happen"); +} + +TEST_CASE("swap parent with open child") { + expected_assert = open_submessage; + std::string buffer; + protozero::pbf_writer pw{buffer}; + const protozero::pbf_writer sub{pw, 1}; + protozero::pbf_writer pw2; + pw2.swap(pw); + FAIL("expected assert did not happen"); +} diff --git a/test/include/test.hpp b/test/include/test.hpp index 5449c906..3e76d837 100644 --- a/test/include/test.hpp +++ b/test/include/test.hpp @@ -15,7 +15,15 @@ struct assert_error : public std::runtime_error { explicit assert_error(const char* what_arg) : std::runtime_error(what_arg) { } }; -#define protozero_assert(x) if (!(x)) { throw assert_error{#x}; } // NOLINT(readability-simplify-boolean-expr) + +// Throw from a separate function, so that the compiler doesn't complain about +// asserts in noexcept functions. Those asserts can't be checked with this +// (it would call std::terminate()), use assert_tests.cpp for them instead. +[[noreturn]] inline void throw_assert_error(const char* what_arg) { + throw assert_error{what_arg}; +} + +#define protozero_assert(x) if (!(x)) { throw_assert_error(#x); } // NOLINT(readability-simplify-boolean-expr) #include // IWYU pragma: export #include // IWYU pragma: export diff --git a/test/t/message/reader_test_cases.cpp b/test/t/message/reader_test_cases.cpp index 97426efd..e4ffeac6 100644 --- a/test/t/message/reader_test_cases.cpp +++ b/test/t/message/reader_test_cases.cpp @@ -194,3 +194,26 @@ TEST_CASE("self-move-assigning an open subwriter keeps it open") { pbf_submessage.commit(); REQUIRE(buffer == load_data("message/data-message")); } + +TEST_CASE("moving and swapping writers without open submessage doesn't assert") { + std::string buffer; + protozero::pbf_writer pbf_test{buffer}; + { + protozero::pbf_writer pbf_submessage{pbf_test, 1}; + pbf_submessage.add_string(1, "foobar"); + protozero::pbf_writer sub2{std::move(pbf_submessage)}; + protozero::pbf_writer sub3; + sub3 = std::move(sub2); + sub3.swap(sub2); + REQUIRE(sub2.valid()); + REQUIRE_FALSE(sub3.valid()); + } + protozero::pbf_writer pbf_test2{std::move(pbf_test)}; + pbf_test = std::move(pbf_test2); + pbf_test.swap(pbf_test2); + REQUIRE_FALSE(pbf_test.valid()); + REQUIRE(pbf_test2.valid()); + pbf_test2.add_string(2, "x"); + + REQUIRE(buffer == load_data("message/data-message") + "\x12\x01x"); +}