// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT License. // See the LICENSE file in the project root for more information. using System; using System.Collections.Generic; using System.Linq; using Xunit; namespace Tests { public class Distinct : Tests { [Fact] public void Distinct_Arguments() { AssertThrows(() => EnumerableEx.Distinct(null, _ => _)); AssertThrows(() => EnumerableEx.Distinct([1], null)); AssertThrows(() => EnumerableEx.Distinct(null, _ => _, EqualityComparer.Default)); AssertThrows(() => EnumerableEx.Distinct([1], null, EqualityComparer.Default)); AssertThrows(() => EnumerableEx.Distinct([1], _ => _, null)); } [Fact] public void Distinct1() { var res = Enumerable.Range(0, 10).Distinct(x => x % 5).ToList(); Assert.True(Enumerable.SequenceEqual(res, Enumerable.Range(0, 5))); } [Fact] public void Distinct2() { var res = Enumerable.Range(0, 10).Distinct(x => x % 5, new MyEqualityComparer()).ToList(); Assert.True(Enumerable.SequenceEqual(res, [0, 1])); } private sealed class MyEqualityComparer : IEqualityComparer { public bool Equals(int x, int y) { return x % 2 == y % 2; } public int GetHashCode(int obj) { return EqualityComparer.Default.GetHashCode(obj % 2); } } } }