Skip to content

Two identical references should always be equal #2271

@weifenluo

Description

@weifenluo

When class implement IReadOnlyList<T> to return single item of itself, Assert.Equal will crash with stack overflow when comparing of instance of this class. MSTest does not have this problem.

Here is the code:

using System;
using System.Collections;
using System.Collections.Generic;
using Xunit;

namespace TestProject1
{
    public class UnitTest1
    {
        private sealed class Field : IReadOnlyList<Field>
        {
            Field IReadOnlyList<Field>.this[int index]
            {
                get
                {
                    if (index != 0)
                        throw new ArgumentOutOfRangeException(nameof(index));

                    return this;
                }
            }

            int IReadOnlyCollection<Field>.Count => 1;

            IEnumerator<Field> IEnumerable<Field>.GetEnumerator()
            {
                yield return this;
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                yield return this;
            }
        }

        [Fact]
        public void Test1()
        {
            Field x = new Field();

            // This works as expected:
            Assert.Same(x, x);

            // This does not work: The active test run was aborted. Reason: Test host process crashed : Stack overflow.
            Assert.Equal(x, x);
        }
    }
}

Full project repo here: https://github.com/weifenluo/XunitAssertEqualBug

Metadata

Metadata

Assignees

No one assigned

    Labels

    Assertion libraryxunit.v3.assertBugA fault in an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions