@@ -710,6 +710,52 @@ TYPED_TEST(BruteForceMultiTest, search_empty_index) {
710710 VecSimIndex_Free (index);
711711}
712712
713+ TYPED_TEST (BruteForceMultiTest, removeVectorWithSwaps) {
714+ size_t dim = 4 ;
715+ size_t n = 6 ;
716+
717+ BFParams params = {.dim = dim, .metric = VecSimMetric_L2};
718+ auto *index = this ->CastToBF_Multi (this ->CreateNewIndex (params));
719+
720+ // Insert 3 vectors under two different labels, so that we will have:
721+ // {first_label->[0,1,3], second_label->[2,4,5]}
722+ labelType first_label = 1 ;
723+ labelType second_label = 2 ;
724+
725+ GenerateAndAddVector<TEST_DATA_T>(index, dim, first_label);
726+ GenerateAndAddVector<TEST_DATA_T>(index, dim, first_label);
727+ GenerateAndAddVector<TEST_DATA_T>(index, dim, second_label);
728+ GenerateAndAddVector<TEST_DATA_T>(index, dim, first_label);
729+ GenerateAndAddVector<TEST_DATA_T>(index, dim, second_label);
730+ GenerateAndAddVector<TEST_DATA_T>(index, dim, second_label);
731+ ASSERT_EQ (VecSimIndex_IndexSize (index), n);
732+
733+ // Artificially reorder the internal ids to test that we make the right changes
734+ // when we have an id that appears twice in the array upon deleting the ids one by one.
735+ ASSERT_EQ (index->labelToIdsLookup .at (second_label).size (), n / 2 );
736+ index->labelToIdsLookup .at (second_label)[0 ] = 4 ;
737+ index->labelToIdsLookup .at (second_label)[1 ] = 2 ;
738+ index->labelToIdsLookup .at (second_label)[2 ] = 5 ;
739+
740+ // Expect that the ids array of the second label will behave as following:
741+ // [|4, 2, 5] -> [4, |2, 4] -> [4, 2, |2] (where | marks the current position).
742+ index->deleteVector (second_label);
743+ ASSERT_EQ (index->indexLabelCount (), 1 );
744+ ASSERT_EQ (VecSimIndex_IndexSize (index), n / 2 );
745+
746+ // Check that the internal ids of the first label are as expected.
747+ auto ids = index->labelToIdsLookup .at (first_label);
748+ ASSERT_EQ (ids.size (), n / 2 );
749+ ASSERT_TRUE (std::find (ids.begin (), ids.end (), 0 ) != ids.end ());
750+ ASSERT_TRUE (std::find (ids.begin (), ids.end (), 1 ) != ids.end ());
751+ ASSERT_TRUE (std::find (ids.begin (), ids.end (), 2 ) != ids.end ());
752+ index->deleteVector (first_label);
753+ ASSERT_EQ (index->indexLabelCount (), 0 );
754+ ASSERT_EQ (VecSimIndex_IndexSize (index), 0 );
755+
756+ VecSimIndex_Free (index);
757+ }
758+
713759TYPED_TEST (BruteForceMultiTest, remove_vector_after_replacing_block) {
714760 size_t dim = 4 ;
715761 size_t bs = 2 ;
0 commit comments