|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +// Collection equivalence means the collections have the exact same values |
| 6 | +// in any order. |
| 7 | + |
| 8 | +public class CollectionExamples |
| 9 | +{ |
| 10 | + [Fact] |
| 11 | + public void CollectionEquality() |
| 12 | + { |
| 13 | + List<int> left = [4, 12, 16, 27]; |
| 14 | + List<int> right = [4, 12, 16, 27]; |
| 15 | + |
| 16 | + Assert.Equal(left, right, new CollectionEquivalenceComparer<int>()); |
| 17 | + } |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public void LeftCollectionSmallerThanRight() |
| 21 | + { |
| 22 | + List<int> left = [4, 12, 16]; |
| 23 | + List<int> right = [4, 12, 16, 27]; |
| 24 | + |
| 25 | + Assert.NotEqual(left, right, new CollectionEquivalenceComparer<int>()); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void LeftCollectionLargerThanRight() |
| 30 | + { |
| 31 | + List<int> left = [4, 12, 16, 27, 42]; |
| 32 | + List<int> right = [4, 12, 16, 27]; |
| 33 | + |
| 34 | + Assert.NotEqual(left, right, new CollectionEquivalenceComparer<int>()); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public void SameValuesOutOfOrder() |
| 39 | + { |
| 40 | + List<int> left = [4, 16, 12, 27]; |
| 41 | + List<int> right = [4, 12, 16, 27]; |
| 42 | + |
| 43 | + Assert.Equal(left, right, new CollectionEquivalenceComparer<int>()); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void DuplicatedItemInOneListOnly() |
| 48 | + { |
| 49 | + List<int> left = [4, 12, 16, 27, 4]; |
| 50 | + List<int> right = [4, 12, 16, 27]; |
| 51 | + |
| 52 | + Assert.NotEqual(left, right, new CollectionEquivalenceComparer<int>()); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public void DuplicatedItemInBothLists() |
| 57 | + { |
| 58 | + List<int> left = [4, 16, 12, 27, 4]; |
| 59 | + List<int> right = [4, 12, 16, 4, 27]; |
| 60 | + |
| 61 | + Assert.Equal(left, right, new CollectionEquivalenceComparer<int>()); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class CollectionEquivalenceComparer<T> : IEqualityComparer<IEnumerable<T>> |
| 66 | + where T : IEquatable<T> |
| 67 | +{ |
| 68 | + public bool Equals(IEnumerable<T>? x, IEnumerable<T>? y) |
| 69 | + { |
| 70 | + if (x is null) |
| 71 | + return y is null; |
| 72 | + if (y is null) |
| 73 | + return false; |
| 74 | + |
| 75 | + var leftList = new List<T>(x); |
| 76 | + var rightList = new List<T>(y); |
| 77 | + leftList.Sort(); |
| 78 | + rightList.Sort(); |
| 79 | + |
| 80 | + using var enumeratorX = leftList.GetEnumerator(); |
| 81 | + using var enumeratorY = rightList.GetEnumerator(); |
| 82 | + |
| 83 | + while (true) |
| 84 | + { |
| 85 | + bool hasNextX = enumeratorX.MoveNext(); |
| 86 | + bool hasNextY = enumeratorY.MoveNext(); |
| 87 | + |
| 88 | + if (!hasNextX || !hasNextY) |
| 89 | + return (hasNextX == hasNextY); |
| 90 | + |
| 91 | + if (!enumeratorX.Current.Equals(enumeratorY.Current)) |
| 92 | + return false; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public int GetHashCode(IEnumerable<T> obj) => |
| 97 | + throw new NotImplementedException(); |
| 98 | +} |
0 commit comments