Repository: vespa-engine/vespa
File: searchlib/src/tests/common/bitvector/bitvector_test.cpp (C++)

Code before:
        EXPECT_NE(file_bytes, bv.getFileBytes());
    }
    if (file_bytes > bv.getFileBytes()) {
        EXPECT_TRUE(guard_bits > BitVector::num_guard_bits);
        std::vector<char> zerofill(file_bytes - bv.getFileBytes());
        out->write(zerofill.data(), zerofill.size());
    }
    return out;
}

void
write_test_bv_to_file(const BitVector& bv, const std::string& file_name)
{
    std::filesystem::path bvpath(file_name);
    std::ofstream bvfile(bvpath, std::ios::binary);
    auto legacy_entry_size = BitVector::legacy_num_bytes_with_single_guard_bit(bv.size());
    bvfile.write(static_cast<const char *>(bv.getStart()), legacy_entry_size);
    bvfile.close();
    ASSERT_TRUE(bvfile.good());
    std::filesystem::resize_file(bvpath, 1024u);
}

class BitVectorTest : public ::testing::Test {
protected:
    static std::unique_ptr<const BitVector> _file_bv;
    static constexpr uint32_t file_alignment = 0x100;

    BitVectorTest();
    ~BitVectorTest() override;
    static void SetUpTestSuite();
    static void TearDownTestSuite();
};

std::unique_ptr<const BitVector> BitVectorTest::_file_bv;

BitVectorTest::BitVectorTest()
    : ::testing::Test()
{
}

BitVectorTest::~BitVectorTest() = default;

void
BitVectorTest::SetUpTestSuite()
{
    std::filesystem::remove_all(testdata);
    std::filesystem::create_directory(testdata);
    auto bv = make_test_bv();
    write_test_bv_to_file(*bv,testdata + "/bv");
    _file_bv = std::move(bv);
}

Review comment (suggestion):
I think this might benefit from being in decimal rather than hex, since test code further down checks values derived from `file_alignment` using decimal numbers (easier to mentally connect the two)

Code after:

void
write_test_bv_to_file(const BitVector& bv, const std::string& file_name)
{
    std::filesystem::path bvpath(file_name);
    std::ofstream bvfile(bvpath, std::ios::binary);
    auto legacy_entry_size = BitVector::legacy_num_bytes_with_single_guard_bit(bv.size());
    bvfile.write(static_cast<const char *>(bv.getStart()), legacy_entry_size);
    bvfile.close();
    ASSERT_TRUE(bvfile.good());
    // Resize file to beyond what is needed ( 1 guard bit => 256 bytes, 2 guard bits => 512 bytes).
    std::filesystem::resize_file(bvpath, 1024u);
}

class BitVectorTest : public ::testing::Test {
protected:
    static std::unique_ptr<const BitVector> _file_bv;
    static constexpr uint32_t bitvector_alignment_bits = BitVector::getAlignment() * 8;

    BitVectorTest();
    ~BitVectorTest() override;
    static void SetUpTestSuite();
    static void TearDownTestSuite();
    static std::unique_ptr<const BitVector> make_test_bv();
};

std::unique_ptr<const BitVector> BitVectorTest::_file_bv;

BitVectorTest::BitVectorTest()
    : ::testing::Test()
{
}

BitVectorTest::~BitVectorTest() = default;

void
BitVectorTest::SetUpTestSuite()
{
    std::filesystem::remove_all(testdata);
    std::filesystem::create_directory(testdata);
    auto bv = make_test_bv();
    write_test_bv_to_file(*bv, testdata + "/bv");
    _file_bv = std::move(bv);
}

void
BitVectorTest::TearDownTestSuite()
{
    _file_bv.reset();
    std::filesystem::remove_all(testdata);
}