Skip to content

Commit

Permalink
Ensure we don't try and build an empty table when modifying entries w…
Browse files Browse the repository at this point in the history
…ith no values (#54639)
  • Loading branch information
chsienki authored and allisonchou committed Jul 8, 2021
1 parent 5b7a57a commit 5c29ded
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ public void Node_Table_Single_Returns_Second_Item_When_First_Is_Removed()
Assert.Equal(2, table.Single());
}

[Fact]
public void Node_Builder_Handles_Modification_When_Both_Tables_Have_Empty_Entries()
{
var builder = NodeStateTable<int>.Empty.ToBuilder();
builder.AddEntries(ImmutableArray.Create(1, 2), EntryState.Added);
builder.AddEntries(ImmutableArray<int>.Empty, EntryState.Added);
builder.AddEntries(ImmutableArray.Create(3, 4), EntryState.Added);
var previousTable = builder.ToImmutableAndFree();

var expected = ImmutableArray.Create((1, EntryState.Added), (2, EntryState.Added), (3, EntryState.Added), (4, EntryState.Added));
AssertTableEntries(previousTable, expected);

builder = previousTable.ToBuilder();
Assert.True(builder.TryModifyEntries(ImmutableArray.Create(3, 2), EqualityComparer<int>.Default)); // ((3, EntryState.Modified), (2, EntryState.Cached))
Assert.True(builder.TryModifyEntries(ImmutableArray<int>.Empty, EqualityComparer<int>.Default)); // nothing
Assert.True(builder.TryModifyEntries(ImmutableArray.Create(3, 5), EqualityComparer<int>.Default)); // ((3, EntryState.Cached), (5, EntryState.Modified))
var newTable = builder.ToImmutableAndFree();

expected = ImmutableArray.Create((3, EntryState.Modified), (2, EntryState.Cached), (3, EntryState.Cached), (5, EntryState.Modified));
AssertTableEntries(newTable, expected);
}

[Fact]
public void Driver_Table_Calls_Into_Node_With_Self()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ public bool TryModifyEntries(ImmutableArray<T> outputs, IEqualityComparer<T> com
// - Added when new item position < previousTable.length

var previousEntry = _previous._states[_states.Count];

// when both entries have no items, we can short circuit
if (previousEntry.Count == 0 && outputs.Length == 0)
{
_states.Add(previousEntry);
return true;
}

var modified = new TableEntry.Builder();
var sharedCount = Math.Min(previousEntry.Count, outputs.Length);

Expand Down

0 comments on commit 5c29ded

Please sign in to comment.