Skip to content

Commit

Permalink
Update a number of Boost.Test assertions
Browse files Browse the repository at this point in the history
Update to use BOOST_TEST where possible instead of BOOST_CHECK_EQUAL or
BOOST_CHECK. BOOST_TEST is more "modern" but doesn't cope with
expressions that aren't be formatted with ostream, so it's not applied
everywhere.

The main motivation for this is that BOOST_CHECK_EQUAL leads to useless
clang error messages when something goes wrong (it never reports the
assertion that is the problem). This has been showing up with
-Wsign-compare, and in some places the RHS of comparisons has been made
explicitly unsigned to address these errors.
  • Loading branch information
bmerry committed Jul 29, 2024
1 parent 198ea87 commit 3860082
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 131 deletions.
4 changes: 2 additions & 2 deletions src/unittest_logging.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2019, 2023 National Research Foundation (SARAO)
/* Copyright 2019, 2023-2024 National Research Foundation (SARAO)
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
Expand Down Expand Up @@ -170,7 +170,7 @@ BOOST_AUTO_TEST_CASE(throw_errno_zero)
BOOST_FIXTURE_TEST_CASE(default_logger, capture_stderr)
{
spead2::log_info("A test message");
BOOST_CHECK_EQUAL(out.str(), "spead2: info: A test message\n");
BOOST_TEST(out.str() == "spead2: info: A test message\n");
}

BOOST_AUTO_TEST_SUITE_END() // logging
Expand Down
6 changes: 3 additions & 3 deletions src/unittest_memory_allocator.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2016, 2019, 2021, 2023 National Research Foundation (SARAO)
/* Copyright 2016, 2019, 2021, 2023-2024 National Research Foundation (SARAO)
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
Expand Down Expand Up @@ -94,15 +94,15 @@ BOOST_AUTO_TEST_CASE(legacy)
BOOST_TEST(allocator->deleted.empty());
std::uint8_t *raw = ptr.get();
ptr.reset();
BOOST_TEST(allocator->deleted.size() == 1);
BOOST_TEST(allocator->deleted.size() == 1U);
BOOST_TEST(allocator->deleted[0].first == raw);
BOOST_TEST(allocator->deleted[0].second == nullptr);

// Now a pointer with a non-null user value (set via hint)
ptr = allocator->allocate(234, &raw);
raw = ptr.get();
ptr.reset();
BOOST_TEST(allocator->deleted.size() == 2);
BOOST_TEST(allocator->deleted.size() == 2U);
BOOST_TEST(allocator->deleted[1].first == raw);
BOOST_TEST(allocator->deleted[1].second == &raw);
}
Expand Down
48 changes: 24 additions & 24 deletions src/unittest_memory_pool.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2016, 2017, 2019, 2021, 2023 National Research Foundation (SARAO)
/* Copyright 2016, 2017, 2019, 2021, 2023-2024 National Research Foundation (SARAO)
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
Expand Down Expand Up @@ -81,7 +81,7 @@ class mock_allocator : public spead2::memory_allocator

void operator()(std::uint8_t *ptr) const
{
BOOST_CHECK_EQUAL(ptr + 1, next_ptr);
BOOST_TEST(ptr + 1 == next_ptr);
allocator->records.push_back(record{false, 0, ptr});
allocator->saved.emplace_back(ptr);
}
Expand Down Expand Up @@ -164,43 +164,43 @@ BOOST_AUTO_TEST_CASE(memory_pool_pass_deleter)
p5.reset();

// Check results
BOOST_REQUIRE_EQUAL(allocator->records.size(), 8);
BOOST_CHECK_EQUAL(allocator->records[0].allocate, true);
BOOST_CHECK_EQUAL(allocator->records[0].size, 2048);
BOOST_CHECK_EQUAL(allocator->records[1].allocate, true);
BOOST_CHECK_EQUAL(allocator->records[1].size, 2048);
BOOST_CHECK_EQUAL(allocator->records[2].allocate, true);
BOOST_CHECK_EQUAL(allocator->records[2].size, 2048);
BOOST_CHECK_EQUAL(allocator->records[3].allocate, false);
BOOST_CHECK_EQUAL(allocator->records[3].ptr, allocator->records[2].ptr);
BOOST_CHECK_EQUAL(allocator->records[4].allocate, true);
BOOST_CHECK_EQUAL(allocator->records[4].size, 2049);
BOOST_CHECK_EQUAL(allocator->records[5].allocate, false);
BOOST_CHECK_EQUAL(allocator->records[5].ptr, allocator->records[0].ptr);
BOOST_CHECK_EQUAL(allocator->records[6].allocate, false);
BOOST_CHECK_EQUAL(allocator->records[6].ptr, allocator->records[1].ptr);
BOOST_CHECK_EQUAL(allocator->records[7].allocate, false);
BOOST_CHECK_EQUAL(allocator->records[7].ptr, allocator->records[4].ptr);
BOOST_TEST_REQUIRE(allocator->records.size() == 8U);
BOOST_TEST(allocator->records[0].allocate);
BOOST_TEST(allocator->records[0].size == 2048U);
BOOST_TEST(allocator->records[1].allocate);
BOOST_TEST(allocator->records[1].size == 2048U);
BOOST_TEST(allocator->records[2].allocate);
BOOST_TEST(allocator->records[2].size == 2048U);
BOOST_TEST(!allocator->records[3].allocate);
BOOST_TEST(allocator->records[3].ptr == allocator->records[2].ptr);
BOOST_TEST(allocator->records[4].allocate);
BOOST_TEST(allocator->records[4].size == 2049U);
BOOST_TEST(!allocator->records[5].allocate);
BOOST_TEST(allocator->records[5].ptr == allocator->records[0].ptr);
BOOST_TEST(!allocator->records[6].allocate);
BOOST_TEST(allocator->records[6].ptr == allocator->records[1].ptr);
BOOST_TEST(!allocator->records[7].allocate);
BOOST_TEST(allocator->records[7].ptr == allocator->records[4].ptr);
}

// Check that a warning is issued when the memory pool becomes empty, if and
// only if the warning is enabled.
BOOST_FIXTURE_TEST_CASE(memory_pool_warn_on_empty, logger_fixture)
{
auto pool = std::make_shared<spead2::memory_pool>(1024, 2048, 1, 1);
BOOST_CHECK_EQUAL(pool->get_warn_on_empty(), true);
BOOST_TEST(pool->get_warn_on_empty() == true);
auto ptr1 = pool->allocate(1024, nullptr);
// Wasn't empty, should be no messages
BOOST_CHECK_EQUAL(messages[spead2::log_level::warning].size(), 0);
BOOST_TEST(messages[spead2::log_level::warning].size() == 0U);
auto ptr2 = pool->allocate(1024, nullptr);
// Now was empty, should be a warning
BOOST_CHECK_EQUAL(messages[spead2::log_level::warning].size(), 1);
BOOST_TEST(messages[spead2::log_level::warning].size() == 1U);

pool->set_warn_on_empty(false);
BOOST_CHECK_EQUAL(pool->get_warn_on_empty(), false);
BOOST_TEST(pool->get_warn_on_empty() == false);
auto ptr3 = pool->allocate(1024, nullptr);
// Empty again, but should be no new warning
BOOST_CHECK_EQUAL(messages[spead2::log_level::warning].size(), 1);
BOOST_TEST(messages[spead2::log_level::warning].size() == 1U);
}

BOOST_AUTO_TEST_SUITE_END() // memory_pool
Expand Down
52 changes: 26 additions & 26 deletions src/unittest_raw_packet.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2019, 2023 National Research Foundation (SARAO)
/* Copyright 2019, 2023-2024 National Research Foundation (SARAO)
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
Expand Down Expand Up @@ -111,18 +111,18 @@ BOOST_AUTO_TEST_CASE(packet_buffer_construct)
std::uint8_t data[2];
packet_buffer a;
packet_buffer b(data, 2);
BOOST_CHECK_EQUAL(a.data(), (std::uint8_t *) nullptr);
BOOST_CHECK_EQUAL(a.size(), 0);
BOOST_CHECK_EQUAL(b.data(), data);
BOOST_CHECK_EQUAL(b.size(), 2);
BOOST_TEST(a.data() == (std::uint8_t *) nullptr);
BOOST_TEST(a.size() == 0U);
BOOST_TEST(b.data() == data);
BOOST_TEST(b.size() == 2U);
}

BOOST_AUTO_TEST_CASE(parse_ethernet_frame)
{
ethernet_frame frame(data.data(), data.size());
BOOST_CHECK(frame.source_mac() == source_mac);
BOOST_CHECK(frame.destination_mac() == destination_mac);
BOOST_CHECK_EQUAL(frame.ethertype(), ipv4_packet::ethertype);
BOOST_TEST(frame.source_mac() == source_mac);
BOOST_TEST(frame.destination_mac() == destination_mac);
BOOST_TEST(frame.ethertype() == ipv4_packet::ethertype);
}

// Just to get full test coverage
Expand All @@ -136,17 +136,17 @@ BOOST_AUTO_TEST_CASE(parse_ipv4)
ethernet_frame frame(data.data(), data.size());
ipv4_packet ipv4 = frame.payload_ipv4();

BOOST_CHECK_EQUAL(ipv4.version_ihl(), 0x45); // version 4, header length 20
BOOST_CHECK_EQUAL(ipv4.version(), 4);
BOOST_CHECK_EQUAL(ipv4.dscp_ecn(), 0);
BOOST_CHECK_EQUAL(ipv4.total_length(), 40);
BOOST_CHECK_EQUAL(ipv4.identification(), ipv4_identification);
BOOST_CHECK_EQUAL(ipv4.flags_frag_off(), ipv4_packet::flag_do_not_fragment);
BOOST_CHECK_EQUAL(ipv4.ttl(), 1);
BOOST_CHECK_EQUAL(ipv4.protocol(), udp_packet::protocol);
BOOST_CHECK_EQUAL(ipv4.checksum(), ipv4_checksum);
BOOST_CHECK_EQUAL(ipv4.source_address(), source_address);
BOOST_CHECK_EQUAL(ipv4.destination_address(), destination_address);
BOOST_TEST(ipv4.version_ihl() == 0x45); // version 4, header length 20
BOOST_TEST(ipv4.version() == 4);
BOOST_TEST(ipv4.dscp_ecn() == 0);
BOOST_TEST(ipv4.total_length() == 40);
BOOST_TEST(ipv4.identification() == ipv4_identification);
BOOST_TEST(ipv4.flags_frag_off() == ipv4_packet::flag_do_not_fragment);
BOOST_TEST(ipv4.ttl() == 1);
BOOST_TEST(ipv4.protocol() == udp_packet::protocol);
BOOST_TEST(ipv4.checksum() == ipv4_checksum);
BOOST_TEST(ipv4.source_address() == source_address);
BOOST_TEST(ipv4.destination_address() == destination_address);
}

BOOST_AUTO_TEST_CASE(parse_udp)
Expand All @@ -155,11 +155,11 @@ BOOST_AUTO_TEST_CASE(parse_udp)
ipv4_packet ipv4 = frame.payload_ipv4();
udp_packet udp = ipv4.payload_udp();

BOOST_CHECK_EQUAL(udp.source_port(), source_port);
BOOST_CHECK_EQUAL(udp.destination_port(), destination_port);
BOOST_CHECK_EQUAL(udp.length(), 20);
BOOST_CHECK_EQUAL(udp.checksum(), udp_checksum);
BOOST_CHECK_EQUAL(buffer_to_string(udp.payload()), sample_payload);
BOOST_TEST(udp.source_port() == source_port);
BOOST_TEST(udp.destination_port() == destination_port);
BOOST_TEST(udp.length() == 20);
BOOST_TEST(udp.checksum() == udp_checksum);
BOOST_TEST(buffer_to_string(udp.payload()) == sample_payload);
}

BOOST_AUTO_TEST_CASE(ethernet_too_small)
Expand Down Expand Up @@ -213,13 +213,13 @@ BOOST_AUTO_TEST_CASE(udp_bad_length)
BOOST_AUTO_TEST_CASE(udp_from_ethernet)
{
boost::asio::mutable_buffer payload = spead2::udp_from_ethernet(data.data(), data.size());
BOOST_CHECK_EQUAL(buffer_to_string(payload), sample_payload);
BOOST_TEST(buffer_to_string(payload) == sample_payload);
}

BOOST_AUTO_TEST_CASE(udp_from_linux_sll)
{
boost::asio::mutable_buffer payload = spead2::udp_from_linux_sll(sll_data.data(), sll_data.size());
BOOST_CHECK_EQUAL(buffer_to_string(payload), sample_payload);
BOOST_TEST(buffer_to_string(payload) == sample_payload);
}

BOOST_AUTO_TEST_CASE(udp_from_ethernet_not_ipv4)
Expand Down
6 changes: 3 additions & 3 deletions src/unittest_recv_custom_memcpy.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2019-2020, 2023 National Research Foundation (SARAO)
/* Copyright 2019-2020, 2023-2024 National Research Foundation (SARAO)
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
Expand Down Expand Up @@ -89,12 +89,12 @@ BOOST_AUTO_TEST_CASE(test_reverse)
{
if (item.id == 0x1000)
{
BOOST_CHECK(!found);
BOOST_TEST(!found);
BOOST_CHECK_EQUAL_COLLECTIONS(data.rbegin(), data.rend(), item.ptr, item.ptr + item.length);
found = true;
}
}
BOOST_CHECK(found);
BOOST_TEST(found);
}

BOOST_AUTO_TEST_SUITE_END() // custom_memcpy
Expand Down
28 changes: 14 additions & 14 deletions src/unittest_recv_live_heap.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2016, 2018, 2023 National Research Foundation (SARAO)
/* Copyright 2016, 2018, 2023-2024 National Research Foundation (SARAO)
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
Expand Down Expand Up @@ -110,55 +110,55 @@ BOOST_AUTO_TEST_CASE(payload_ranges)
using spead2::recv::live_heap;
live_heap heap(dummy_packet(1), 0);

BOOST_CHECK(heap.add_payload_range(100, 200));
BOOST_TEST(heap.add_payload_range(100, 200));
std::pair<const s_item_pointer_t, s_item_pointer_t> expected1[] = {{100, 200}};
BOOST_CHECK_EQUAL_COLLECTIONS(heap.payload_ranges.begin(), heap.payload_ranges.end(),
std::begin(expected1), std::end(expected1));
// Range prior to all previous values
BOOST_CHECK(heap.add_payload_range(30, 40));
BOOST_TEST(heap.add_payload_range(30, 40));
std::pair<const s_item_pointer_t, s_item_pointer_t> expected2[] = {{30, 40}, {100, 200}};
BOOST_CHECK_EQUAL_COLLECTIONS(heap.payload_ranges.begin(), heap.payload_ranges.end(),
std::begin(expected2), std::end(expected2));
// Range after all existing values
BOOST_CHECK(heap.add_payload_range(300, 350));
BOOST_TEST(heap.add_payload_range(300, 350));
std::pair<const s_item_pointer_t, s_item_pointer_t> expected3[] = {{30, 40}, {100, 200}, {300, 350}};
BOOST_CHECK_EQUAL_COLLECTIONS(heap.payload_ranges.begin(), heap.payload_ranges.end(),
std::begin(expected3), std::end(expected3));
// Insert at start, merge right
BOOST_CHECK(heap.add_payload_range(25, 30));
BOOST_TEST(heap.add_payload_range(25, 30));
std::pair<const s_item_pointer_t, s_item_pointer_t> expected4[] = {{25, 40}, {100, 200}, {300, 350}};
BOOST_CHECK_EQUAL_COLLECTIONS(heap.payload_ranges.begin(), heap.payload_ranges.end(),
std::begin(expected4), std::end(expected4));
// Insert at end, merge left
BOOST_CHECK(heap.add_payload_range(350, 360));
BOOST_TEST(heap.add_payload_range(350, 360));
std::pair<const s_item_pointer_t, s_item_pointer_t> expected5[] = {{25, 40}, {100, 200}, {300, 360}};
BOOST_CHECK_EQUAL_COLLECTIONS(heap.payload_ranges.begin(), heap.payload_ranges.end(),
std::begin(expected5), std::end(expected5));
// Insert in middle, merge left
BOOST_CHECK(heap.add_payload_range(40, 50));
BOOST_TEST(heap.add_payload_range(40, 50));
std::pair<const s_item_pointer_t, s_item_pointer_t> expected6[] = {{25, 50}, {100, 200}, {300, 360}};
BOOST_CHECK_EQUAL_COLLECTIONS(heap.payload_ranges.begin(), heap.payload_ranges.end(),
std::begin(expected6), std::end(expected6));
// Insert in middle, merge right
BOOST_CHECK(heap.add_payload_range(80, 100));
BOOST_TEST(heap.add_payload_range(80, 100));
std::pair<const s_item_pointer_t, s_item_pointer_t> expected7[] = {{25, 50}, {80, 200}, {300, 360}};
BOOST_CHECK_EQUAL_COLLECTIONS(heap.payload_ranges.begin(), heap.payload_ranges.end(),
std::begin(expected7), std::end(expected7));
// Insert in middle, no merge
BOOST_CHECK(heap.add_payload_range(60, 70));
BOOST_TEST(heap.add_payload_range(60, 70));
std::pair<const s_item_pointer_t, s_item_pointer_t> expected8[] = {{25, 50}, {60, 70}, {80, 200}, {300, 360}};
BOOST_CHECK_EQUAL_COLLECTIONS(heap.payload_ranges.begin(), heap.payload_ranges.end(),
std::begin(expected8), std::end(expected8));
// Insert in middle, merge both sides
BOOST_CHECK(heap.add_payload_range(50, 60));
BOOST_TEST(heap.add_payload_range(50, 60));
std::pair<const s_item_pointer_t, s_item_pointer_t> expected9[] = {{25, 70}, {80, 200}, {300, 360}};
BOOST_CHECK_EQUAL_COLLECTIONS(heap.payload_ranges.begin(), heap.payload_ranges.end(),
std::begin(expected9), std::end(expected9));
// Duplicates of various sorts
BOOST_CHECK(!heap.add_payload_range(40, 50));
BOOST_CHECK(!heap.add_payload_range(25, 30));
BOOST_CHECK(!heap.add_payload_range(90, 200));
BOOST_CHECK(!heap.add_payload_range(300, 360));
BOOST_TEST(!heap.add_payload_range(40, 50));
BOOST_TEST(!heap.add_payload_range(25, 30));
BOOST_TEST(!heap.add_payload_range(90, 200));
BOOST_TEST(!heap.add_payload_range(300, 360));
}

BOOST_AUTO_TEST_SUITE_END() // live_heap
Expand Down
Loading

0 comments on commit 3860082

Please sign in to comment.