From c4176b0d90f0c68af38f870d8a549b3da3cd7576 Mon Sep 17 00:00:00 2001 From: H0w Xd Date: Tue, 5 May 2020 17:50:13 +0800 Subject: [PATCH] Fix issue DistinctInPlaceF crashing --- LinqFaster/Distinct.cs | 5 +++++ Tests/DistinctTests.cs | 23 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/LinqFaster/Distinct.cs b/LinqFaster/Distinct.cs index d20f426..3f063e0 100644 --- a/LinqFaster/Distinct.cs +++ b/LinqFaster/Distinct.cs @@ -24,6 +24,11 @@ public static void DistinctInPlaceF(this List source, IEqualit throw Error.ArgumentNull("source"); } + if (source.Count == 0) + { + return; + } + if (comparer == null) { comparer = Comparer.Default; diff --git a/Tests/DistinctTests.cs b/Tests/DistinctTests.cs index 727e6dd..71571e6 100644 --- a/Tests/DistinctTests.cs +++ b/Tests/DistinctTests.cs @@ -1,11 +1,32 @@ using NUnit.Framework; +using JM.LinqFaster; +using System.Linq; +using static Tests.Test; +using System.Collections.Generic; namespace Tests { [TestFixture] class DistinctTests { - + [Test] + public void DistinctInPlaceEmptyList() + { + var emptyList = new List(); + emptyList.DistinctInPlaceF(); + } + + [Test] + public void DistinctInPlaceIntegerList() + { + var list = new List() { 1, 2, 2, 3, 3, 3 }; + var a = new List(list); + var b = list.Distinct().ToList(); + + a.DistinctInPlaceF(); + + Assert.That(a, Is.EqualTo(b)); + } } }