Skip to content

Instantly share code, notes, and snippets.

@m2web
Last active August 29, 2015 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m2web/057d156dfd31ad3e4af1 to your computer and use it in GitHub Desktop.
Save m2web/057d156dfd31ad3e4af1 to your computer and use it in GitHub Desktop.
C# NUnit Test Except And Intersect
[Test]
public void TestExceptAndIntersect()
{
//check if equal with differing sequence
var list1 = new List<int>(new int[] { 1, 2, 3, 4, 5, 6 });
var list2 = new List<int>(new int[] { 6, 5, 4, 3, 2, 1 });
var result = list1.Except(list2).ToList();
Assert.AreEqual(new int[] { }, result);
var oldList = new List<int>(new int[] { 1, 2, 3, 4, 5, 6 });
var newList = new List<int>(new int[] { 3, 5, 6, 7, 8 });
//get the intersect between the 2 lists
var commonItems = newList.Intersect(oldList).ToList();
Assert.AreEqual(new int[] { 3, 5, 6 }, commonItems);
//what is in the first list that is not in the second list
var toBeRemoved = oldList.Except(newList).ToList();
var toBeAdded = newList.Except(oldList).ToList();
Assert.IsFalse(toBeRemoved.Count == 0);
Assert.IsFalse(toBeAdded.Count == 0);
Assert.AreEqual(new int[] { 1, 2, 4 }, toBeRemoved);
Assert.AreEqual(new int[] { 7, 8 }, toBeAdded);
var updatedList = commonItems.Concat(toBeAdded).ToList();
Assert.AreEqual(new int[] { 3, 5, 6, 7, 8 }, updatedList);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment