Skip to content

Commit

Permalink
Add another test
Browse files Browse the repository at this point in the history
  • Loading branch information
Qup42 committed Jul 16, 2024
1 parent 21cfbba commit 928920d
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 80 deletions.
1 change: 0 additions & 1 deletion src/index/LocatedTriples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ IdTable LocatedTriplesPerBlock::mergeTriplesImpl(size_t blockIndex,
AD_CONTRACT_CHECK(map_.contains(blockIndex));

AD_CONTRACT_CHECK(numIndexColumns <= block.numColumns());
AD_CONTRACT_CHECK(block.numColumns() >= 1);

auto numInsertsAndDeletes = numTriples(blockIndex);
IdTable result{block.numColumns(), block.getAllocator()};
Expand Down
191 changes: 112 additions & 79 deletions test/LocatedTriplesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,33 @@ TEST_F(LocatedTriplesTest, mergeTriples) {
ad_utility::Exception);
}

// There must at least `numIndexColumns` columns in the block.
// There cannot be fewer index columns in the block than there are columns in
// total.
{
IdTable block = makeIdTableFromVector({
{10, 10}, // Row 0
{15, 20}, // Row 1
{15, 30}, // Row 2
{20, 10}, // Row 3
{30, 20}, // Row 4
{30, 30} // Row 5
});
auto locatedTriplesPerBlock = makeLocatedTriplesPerBlock({
LT{1, IT(1, 5, 10), true}, // Insert before row 0
LT{1, IT(1, 10, 10), false}, // Delete row 0
LT{1, IT(1, 10, 11), true}, // Insert before row 1
LT{1, IT(2, 11, 10), true}, // Insert before row 1
LT{1, IT(2, 30, 10), true}, // Insert before row 4
LT{1, IT(2, 30, 20), false}, // Delete row 4
LT{1, IT(3, 30, 25), false}, // Delete non-existent row
LT{1, IT(3, 30, 30), false}, // Delete row 5
LT{1, IT(4, 10, 10), true}, // Insert after row 5
});
EXPECT_THROW(locatedTriplesPerBlock.mergeTriples(1, block, 3),
ad_utility::Exception);
}

// There can be at most 3 index columns.
{
IdTable block = makeIdTableFromVector({
{1, 10, 10}, // Row 0
Expand Down Expand Up @@ -521,85 +547,92 @@ TEST_F(LocatedTriplesTest, augmentedMetadata) {
// Create a vector that is automatically converted to a span.
using Span = std::vector<IdTriple<0>>;

auto PT1 = PT(1, 10, 10);
auto PT2 = PT(2, 10, 10);
auto PT3 = PT(2, 15, 20);
auto PT4 = PT(2, 15, 30);
auto PT5 = PT(2, 20, 10);
auto PT6 = PT(2, 30, 20);
auto PT7 = PT(2, 30, 30);
auto PT8 = PT(3, 10, 10);
const std::vector<CompressedBlockMetadata> metadata = {
CBM(PT1, PT1), CBM(PT2, PT3), CBM(PT4, PT5), CBM(PT6, PT7),
CBM(PT8, PT8)};
std::vector<CompressedBlockMetadata> expectedAugmentedMetadata{metadata};

auto T1 = IT(1, 5, 10); // Before block 0
auto T2 = IT(2, 12, 10); // Inside block 1
auto T3 = IT(2, 15, 30); // Equal PT4, beginning of block 2
auto T4 = IT(2, 40, 30); // Before block 4

ad_utility::SharedCancellationHandle handle =
std::make_shared<ad_utility::CancellationHandle<>>();

// At the beginning the augmented metadata is equal to the block metadata.
LocatedTriplesPerBlock locatedTriplesPerBlock;
locatedTriplesPerBlock.setOriginalMetadata(metadata);

EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// Adding no triples does no changed the augmented metadata.
locatedTriplesPerBlock.add(LocatedTriple::locateTriplesInPermutation(
Span{}, metadata, {0, 1, 2}, true, handle));

EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// T1 is before block 0. The beginning of block 0 changes.
locatedTriplesPerBlock.add(LocatedTriple::locateTriplesInPermutation(
Span{T1}, metadata, {0, 1, 2}, false, handle));

expectedAugmentedMetadata[0] = CBM(T1.toPermutedTriple(), PT1);
EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// T2 is inside block 1. Borders don't change.
locatedTriplesPerBlock.add(LocatedTriple::locateTriplesInPermutation(
Span{T2}, metadata, {0, 1, 2}, true, handle));

EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// T3 is equal to PT4, the beginning of block 2. All update (update and
// delete) add to the block borders. Borders don't change.
locatedTriplesPerBlock.add(LocatedTriple::locateTriplesInPermutation(
Span{T3}, metadata, {0, 1, 2}, false, handle));

EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// T4 is before block 4. The beginning of block 4 changes.
auto handles =
locatedTriplesPerBlock.add(LocatedTriple::locateTriplesInPermutation(
Span{T4}, metadata, {0, 1, 2}, true, handle));

expectedAugmentedMetadata[4] = CBM(T4.toPermutedTriple(), PT8);
EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// Erasing the update of T4 restores the beginning of block 4.
locatedTriplesPerBlock.erase(4, handles[0]);

expectedAugmentedMetadata[4] = CBM(PT8, PT8);
EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// Clearing the updates restores the original block borders.
locatedTriplesPerBlock.clear();
{
auto PT1 = PT(1, 10, 10);
auto PT2 = PT(2, 10, 10);
auto PT3 = PT(2, 15, 20);
auto PT4 = PT(2, 15, 30);
auto PT5 = PT(2, 20, 10);
auto PT6 = PT(2, 30, 20);
auto PT7 = PT(2, 30, 30);
auto PT8 = PT(3, 10, 10);
const std::vector<CompressedBlockMetadata> metadata = {
CBM(PT1, PT1), CBM(PT2, PT3), CBM(PT4, PT5), CBM(PT6, PT7),
CBM(PT8, PT8)};
std::vector<CompressedBlockMetadata> expectedAugmentedMetadata{metadata};

auto T1 = IT(1, 5, 10); // Before block 0
auto T2 = IT(2, 12, 10); // Inside block 1
auto T3 = IT(2, 15, 30); // Equal PT4, beginning of block 2
auto T4 = IT(2, 40, 30); // Before block 4

ad_utility::SharedCancellationHandle handle =
std::make_shared<ad_utility::CancellationHandle<>>();

// At the beginning the augmented metadata is equal to the block metadata.
LocatedTriplesPerBlock locatedTriplesPerBlock;
locatedTriplesPerBlock.setOriginalMetadata(metadata);

EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// Adding no triples does no changed the augmented metadata.
locatedTriplesPerBlock.add(LocatedTriple::locateTriplesInPermutation(
Span{}, metadata, {0, 1, 2}, true, handle));

EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// T1 is before block 0. The beginning of block 0 changes.
locatedTriplesPerBlock.add(LocatedTriple::locateTriplesInPermutation(
Span{T1}, metadata, {0, 1, 2}, false, handle));

expectedAugmentedMetadata[0] = CBM(T1.toPermutedTriple(), PT1);
EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// T2 is inside block 1. Borders don't change.
locatedTriplesPerBlock.add(LocatedTriple::locateTriplesInPermutation(
Span{T2}, metadata, {0, 1, 2}, true, handle));

EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// T3 is equal to PT4, the beginning of block 2. All update (update and
// delete) add to the block borders. Borders don't change.
locatedTriplesPerBlock.add(LocatedTriple::locateTriplesInPermutation(
Span{T3}, metadata, {0, 1, 2}, false, handle));

EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// T4 is before block 4. The beginning of block 4 changes.
auto handles =
locatedTriplesPerBlock.add(LocatedTriple::locateTriplesInPermutation(
Span{T4}, metadata, {0, 1, 2}, true, handle));

expectedAugmentedMetadata[4] = CBM(T4.toPermutedTriple(), PT8);
EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// Erasing the update of T4 restores the beginning of block 4.
locatedTriplesPerBlock.erase(4, handles[0]);

expectedAugmentedMetadata[4] = CBM(PT8, PT8);
EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(expectedAugmentedMetadata));

// Clearing the updates restores the original block borders.
locatedTriplesPerBlock.clear();

EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(metadata));
}

EXPECT_THAT(locatedTriplesPerBlock.getAugmentedMetadata(),
testing::ElementsAreArray(metadata));
{
LocatedTriplesPerBlock ltpb;
EXPECT_THROW(ltpb.getAugmentedMetadata(), ad_utility::Exception);
}
}

TEST_F(LocatedTriplesTest, debugPrints) {
Expand Down

0 comments on commit 928920d

Please sign in to comment.