1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Collections.Generic; 6 using Xunit; 7 8 namespace System.Linq.Tests 9 { 10 public class IntersectTests : EnumerableBasedTests 11 { 12 [Fact] BothEmpty()13 public void BothEmpty() 14 { 15 int[] first = { }; 16 int[] second = { }; 17 Assert.Empty(first.AsQueryable().Intersect(second.AsQueryable())); 18 } 19 20 [Fact] FirstNullCustomComparer()21 public void FirstNullCustomComparer() 22 { 23 IQueryable<string> first = null; 24 string[] second = { "ekiM", "bBo" }; 25 26 var ane = AssertExtensions.Throws<ArgumentNullException>("source1", () => first.Intersect(second.AsQueryable(), new AnagramEqualityComparer())); 27 } 28 29 [Fact] SecondNullCustomComparer()30 public void SecondNullCustomComparer() 31 { 32 string[] first = { "Tim", "Bob", "Mike", "Robert" }; 33 IQueryable<string> second = null; 34 35 var ane = AssertExtensions.Throws<ArgumentNullException>("source2", () => first.AsQueryable().Intersect(second, new AnagramEqualityComparer())); 36 } 37 38 [Fact] FirstNullNoComparer()39 public void FirstNullNoComparer() 40 { 41 IQueryable<string> first = null; 42 string[] second = { "ekiM", "bBo" }; 43 44 var ane = AssertExtensions.Throws<ArgumentNullException>("source1", () => first.Intersect(second.AsQueryable())); 45 } 46 47 [Fact] SecondNullNoComparer()48 public void SecondNullNoComparer() 49 { 50 string[] first = { "Tim", "Bob", "Mike", "Robert" }; 51 IQueryable<string> second = null; 52 53 var ane = AssertExtensions.Throws<ArgumentNullException>("source2", () => first.AsQueryable().Intersect(second)); 54 } 55 56 [Fact] SingleNullWithEmpty()57 public void SingleNullWithEmpty() 58 { 59 string[] first = { null }; 60 string[] second = new string[0]; 61 Assert.Empty(first.AsQueryable().Intersect(second.AsQueryable(), EqualityComparer<string>.Default)); 62 } 63 64 [Fact] CustomComparer()65 public void CustomComparer() 66 { 67 string[] first = { "Tim", "Bob", "Mike", "Robert" }; 68 string[] second = { "ekiM", "bBo" }; 69 string[] expected = { "Bob", "Mike" }; 70 71 Assert.Equal(expected, first.AsQueryable().Intersect(second.AsQueryable(), new AnagramEqualityComparer())); 72 } 73 74 [Fact] Intersect1()75 public void Intersect1() 76 { 77 var count = (new int[] { 0, 1, 2 }).AsQueryable().Intersect((new int[] { 1, 2, 3 }).AsQueryable()).Count(); 78 Assert.Equal(2, count); 79 } 80 81 [Fact] Intersect2()82 public void Intersect2() 83 { 84 var count = (new int[] { 0, 1, 2 }).AsQueryable().Intersect((new int[] { 1, 2, 3 }).AsQueryable(), EqualityComparer<int>.Default).Count(); 85 Assert.Equal(2, count); 86 } 87 } 88 } 89