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

Code before:
    bv.fixup_after_load();
    auto bv_snap = bv.make_snapshot(bv.writer().size());
    EXPECT_TRUE(*bv_snap == *_file_bv);
    g.assign_generation(1);
    g.reclaim(2);
}

class BitVectorCompatibilityTest : public BitVectorTest,
                                   public testing::WithParamInterface<uint32_t> {
protected:
    BitVectorCompatibilityTest();
    ~BitVectorCompatibilityTest() override;
};

BitVectorCompatibilityTest::BitVectorCompatibilityTest()
    : BitVectorTest(),
      testing::WithParamInterface<uint32_t>()
{
}

BitVectorCompatibilityTest::~BitVectorCompatibilityTest() = default;

INSTANTIATE_TEST_SUITE_P(BitVectorCompatibilityTest, BitVectorCompatibilityTest, testing::Values(1, 2),
                         param_as_string);

TEST_P(BitVectorCompatibilityTest, read_from_file_bitvector_dictionary_file)
{
    uint32_t old_guard_bits = GetParam();
    std::string bvpath(testdata + "/bv");
    Aligner aligner(file_alignment);
    size_t entry_size = aligner.align(BitVector::num_bytes_plain(_file_bv->size() + old_guard_bits));
    EXPECT_EQ(old_guard_bits == 1 ? 256 : 512, entry_size);
    if (old_guard_bits == BitVector::num_guard_bits) {
        EXPECT_EQ(entry_size, _file_bv->getFileBytes());
    }
    FastOS_File file;
    file.OpenReadOnly(bvpath.c_str());
    ASSERT_TRUE(file.IsOpened());
    ReadStats read_stats;
    auto bv = BitVector::create(_file_bv->size(), file, 0, entry_size, _file_bv->countTrueBits(), read_stats);
    ASSERT_TRUE(bv);
    EXPECT_TRUE(*bv == *_file_bv);
}

TEST_P(BitVectorCompatibilityTest, read_from_nbostream)
{
    uint32_t old_guard_bits = GetParam();
    auto nbos = write_test_bv_to_nbostream(*_file_bv, old_guard_bits, file_alignment);
    if (old_guard_bits == BitVector::num_guard_bits) {
        EXPECT_EQ(_file_bv->getFileBytes() + 3 * sizeof(uint64_t), nbos->size());
    } else {

Review comment (suggestion):
Consider renaming test case to mention interaction with guard bits (alternatively, rename the test class instead (`BitVectorGuardBitCompatibilityTest`...?))

Code after:
    bv.extend(numDocs);
    auto entry_size = BitVector::legacy_num_bytes_with_single_guard_bit(numDocs);
    ASSERT_LE(entry_size, bv.writer().numBytes(bv.writer().size()));
    file.ReadBuf(bv.writer().getStart(), entry_size);
    bv.fixup_after_load();
    auto bv_snap = bv.make_snapshot(bv.writer().size());
    EXPECT_TRUE(*bv_snap == *_file_bv);
    g.assign_generation(1);
    g.reclaim(2);
}

class BitVectorGuardBitsCompatibilityTest : public BitVectorTest,
                                            public testing::WithParamInterface<uint32_t> {
protected:
    BitVectorGuardBitsCompatibilityTest();
    ~BitVectorGuardBitsCompatibilityTest() override;
};

BitVectorGuardBitsCompatibilityTest::BitVectorGuardBitsCompatibilityTest()
    : BitVectorTest(),
      testing::WithParamInterface<uint32_t>()
{
}

BitVectorGuardBitsCompatibilityTest::~BitVectorGuardBitsCompatibilityTest() = default;

INSTANTIATE_TEST_SUITE_P(BitVectorGuardBitsCompatibilityTest, BitVectorGuardBitsCompatibilityTest, testing::Values(1, 2),
                         param_as_string);

TEST_P(BitVectorGuardBitsCompatibilityTest, read_from_file_bitvector_dictionary_file)
{
    uint32_t old_guard_bits = GetParam();
    std::string bvpath(testdata + "/bv");
    Aligner aligner(BitVector::getAlignment());
    size_t entry_size = aligner.align(BitVector::num_bytes_plain(_file_bv->size() + old_guard_bits));
    // Old bitvector used 2047 + old_guard_bits bits, aligned to a multiple of 256 bytes.
    EXPECT_EQ(old_guard_bits == 1 ? 256 : 512, entry_size);
    if (old_guard_bits == BitVector::num_guard_bits) {
        EXPECT_EQ(entry_size, _file_bv->getFileBytes());
    }
    FastOS_File file;
    file.OpenReadOnly(bvpath.c_str());
    ASSERT_TRUE(file.IsOpened());
    ReadStats read_stats;
    auto bv = BitVector::create(_file_bv->size(), file, 0, entry_size, _file_bv->countTrueBits(), read_stats);
    ASSERT_TRUE(bv);
    EXPECT_TRUE(*bv == *_file_bv);
}

TEST_P(BitVectorGuardBitsCompatibilityTest, read_from_nbostream)
{