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

Code before:
        bv.shrink(255);
        EXPECT_EQ(255 + slack, bv.reader().getFirstTrueBit(0));
        bv.extend(bv_size);
        EXPECT_EQ(bv_size + num_end_padding_bits, bv.writer().capacity());
        bv.writer().setInterval(0, bv_size);
        if (BitVector::num_guard_bits > 1) {
            EXPECT_EQ(bv_size + ((num_end_padding_bits == 0) ? slack : 0), bv.reader().getFirstFalseBit(0));
        }
        bv.writer().clearBit(300);
        bv.shrink(257);
        /*
         * Only odd guard bits are set to '0' when using multiple guard bits.
         * No '0' guard bit is set and the whole cleared interval is overwritten by new '1'
         * guard bit when using single guard bit and shrinking size by 1.
         */
        EXPECT_EQ(single_guard_bit ? 258 : 257, bv.reader().getFirstFalseBit(0));
        bv.shrink(256);
        EXPECT_EQ(single_guard_bit ? 258 : 257, bv.reader().getFirstFalseBit(0));
        bv.shrink(255);
        EXPECT_EQ(single_guard_bit ? 258 : 255, bv.reader().getFirstFalseBit(0));
    }
    g.assign_generation(1);
    g.reclaim(2);
}

TEST_F(BitVectorTest, read_from_attriute_vector_file)
{
    vespalib::GenerationHolder g;
    std::string bvpath(testdata + "/bv");
    FastOS_File file;
    file.OpenReadOnly(bvpath.c_str());
    ASSERT_TRUE(file.IsOpened());
    GrowableBitVector bv(1, 1, g);
    bv.writer().clear();
    uint32_t numDocs = _file_bv->size();
    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 BitVectorCompatibilityTest : public BitVectorTest,
                                   public testing::WithParamInterface<uint32_t> {
protected:
    BitVectorCompatibilityTest();
    ~BitVectorCompatibilityTest() override;

Review comment (suggestion):
Nit: typo in `attriute`.

Also, if this tests that we can load attribute vector files with single guard bits (as opposed to testing attribute vector file loading "in general"), consider renaming the test case to indicate this

Code after:
        bv.shrink(257);
        EXPECT_EQ(257 + slack, bv.reader().getFirstTrueBit(0));
        bv.shrink(256);
        EXPECT_EQ(256, bv.reader().getFirstTrueBit(0));
        bv.shrink(255);
        EXPECT_EQ(255 + slack, bv.reader().getFirstTrueBit(0));
        bv.extend(bv_size);
        EXPECT_EQ(bv_size + num_end_padding_bits, bv.writer().capacity());
        bv.writer().setInterval(0, bv_size);
        if (BitVector::num_guard_bits > 1) {
            EXPECT_EQ(bv_size + ((num_end_padding_bits == 0) ? slack : 0), bv.reader().getFirstFalseBit(0));
        }
        bv.writer().clearBit(300);
        bv.shrink(257);
        /*
         * Only odd guard bits are set to '0' when using multiple guard bits.
         * No '0' guard bit is set and the whole cleared interval is overwritten by new '1'
         * guard bit when using single guard bit and shrinking size by 1.
         */
        EXPECT_EQ(single_guard_bit ? 258 : 257, bv.reader().getFirstFalseBit(0));
        bv.shrink(256);
        EXPECT_EQ(single_guard_bit ? 258 : 257, bv.reader().getFirstFalseBit(0));
        bv.shrink(255);
        EXPECT_EQ(single_guard_bit ? 258 : 255, bv.reader().getFirstFalseBit(0));
    }
    g.assign_generation(1);
    g.reclaim(2);
}

TEST_F(BitVectorTest, read_from_attribute_vector_file_with_1_guard_bit_in_stored_bitvector)
{
    vespalib::GenerationHolder g;
    std::string bvpath(testdata + "/bv");
    FastOS_File file;
    file.OpenReadOnly(bvpath.c_str());
    ASSERT_TRUE(file.IsOpened());
    GrowableBitVector bv(1, 1, g);
    bv.writer().clear();
    uint32_t numDocs = _file_bv->size();
    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,