Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion include/protozero/basic_pbf_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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...")

Expand Down
5 changes: 5 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 93 additions & 0 deletions test/assert_tests.cpp
Original file line number Diff line number Diff line change
@@ -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 <catch.hpp>

#include <cstdio>
#include <cstdlib>
#include <string>
#include <utility>

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 <protozero/pbf_writer.hpp>

// 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");
}
10 changes: 9 additions & 1 deletion test/include/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <protozero/pbf_builder.hpp> // IWYU pragma: export
#include <protozero/pbf_message.hpp> // IWYU pragma: export
Expand Down
23 changes: 23 additions & 0 deletions test/t/message/reader_test_cases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Loading