Skip to content

Count() return wrong value when using ZLinq with ICollection<T> based collection. #242

Description

@filzrev

I'm trying to merge following System.Linq tests to ZLinq compatibility tests.
dotnet/runtime@087c690

But following edge case tests are failed on ZLinq.
(Currently it also failed with SystemLinq. Because these fix is not released yet)

  • AppendOverflowCountWithICollection()
  • AppendPrependNOverflowCountWithICollection()
  • PrependOverflowCountWithICollection()

It seems following method needed to be modified. (with checked?)

TestCode

public class UnitTests
{
    [Fact]
    public void EdgeCaseTest()
    {
        // Arrange
        var source = new MockCollection<int>(int.MaxValue); // Use custom ICollection based collection.

        // Act
        var results = source.AsValueEnumerable().Append(1).Prepend(1);

        // Assert
        var count = results.Count(); // It should be failed by OverflowException.

        Assert.Equal(2, count); // 2 returned instead. it seems source.Count is not used.
    }
}

public sealed class MockCollection<T> : ICollection<T>
{
    private readonly int _count;

    public MockCollection(int count) => _count = count;

    public int Count => _count;
    public bool IsReadOnly => true;

    public void Add(T item) => throw new NotSupportedException();
    public void Clear() => throw new NotSupportedException();
    public bool Contains(T item) => false;
    public void CopyTo(T[] array, int arrayIndex) { }
    public bool Remove(T item) => throw new NotSupportedException();
    public IEnumerator<T> GetEnumerator() => Enumerable.Empty<T>().GetEnumerator();
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions