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

Code before:
    ~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 {
        EXPECT_NE(_file_bv->getFileBytes() + 3 * sizeof(uint64_t), nbos->size());
    }
    AllocatedBitVector bv(1u);
    *nbos >> bv;
    EXPECT_TRUE(bv == *_file_bv);
    EXPECT_EQ(0u, nbos->size());
}

GTEST_MAIN_RUN_ALL_TESTS()


Review comment (suggestion):
Consider adding a comment relating the numeric constants to `file_alignment`, as it's not immediately obvious where these come from

Code after:
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)
{
    constexpr uint32_t entry_header_counters = 3;
    uint32_t old_guard_bits = GetParam();
    auto nbos = write_test_bv_to_nbostream(*_file_bv, old_guard_bits, BitVector::getAlignment());
    if (old_guard_bits == BitVector::num_guard_bits) {
        EXPECT_EQ(_file_bv->getFileBytes() + entry_header_counters * sizeof(uint64_t), nbos->size());
    } else {
        EXPECT_NE(_file_bv->getFileBytes() + entry_header_counters * sizeof(uint64_t), nbos->size());
    }
    AllocatedBitVector bv(1u);
    *nbos >> bv;
    EXPECT_TRUE(bv == *_file_bv);