forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionTests.cs
More file actions
209 lines (169 loc) · 6.43 KB
/
UnionTests.cs
File metadata and controls
209 lines (169 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using Xunit;
namespace System.Linq.Tests
{
public class UnionTests : EnumerableTests
{
[Fact]
public void SameResultsRepeatCallsIntQuery()
{
var q1 = from x1 in new int?[] { 2, 3, null, 2, null, 4, 5 }
select x1;
var q2 = from x2 in new int?[] { 1, 9, null, 4 }
select x2;
Assert.Equal(q1.Union(q2), q1.Union(q2));
}
[Fact]
public void SameResultsRepeatCallsStringQuery()
{
var q1 = from x1 in new[] { "AAA", String.Empty, "q", "C", "#", "!@#$%^", "0987654321", "Calling Twice" }
select x1;
var q2 = from x2 in new[] { "!@#$%^", "C", "AAA", "", "Calling Twice", "SoS" }
select x2;
Assert.Equal(q1.Union(q2), q1.Union(q2));
}
[Fact]
public void BothEmpty()
{
int[] first = { };
int[] second = { };
Assert.Empty(first.Union(second));
}
[Fact]
public void CustomComparer()
{
string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" };
string[] second = { "ttaM", "Charlie", "Bbo" };
string[] expected = { "Bob", "Robert", "Tim", "Matt", "Charlie" };
var comparer = new AnagramEqualityComparer();
Assert.Equal(expected, first.Union(second, comparer), comparer);
}
[Fact]
public void FirstNullCustomComparer()
{
string[] first = null;
string[] second = { "ttaM", "Charlie", "Bbo" };
var ane = Assert.Throws<ArgumentNullException>("first", () => first.Union(second, new AnagramEqualityComparer()));
}
[Fact]
public void SecondNullCustomComparer()
{
string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" };
string[] second = null;
var ane = Assert.Throws<ArgumentNullException>("second", () => first.Union(second, new AnagramEqualityComparer()));
}
[Fact]
public void FirstNullNoComparer()
{
string[] first = null;
string[] second = { "ttaM", "Charlie", "Bbo" };
var ane = Assert.Throws<ArgumentNullException>("first", () => first.Union(second));
}
[Fact]
public void SecondNullNoComparer()
{
string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" };
string[] second = null;
var ane = Assert.Throws<ArgumentNullException>("second", () => first.Union(second));
}
[Fact]
public void SingleNullWithEmpty()
{
string[] first = { null };
string[] second = new string[0];
string[] expected = { null };
Assert.Equal(expected, first.Union(second, EqualityComparer<string>.Default));
}
[Fact]
public void NullEmptyStringMix()
{
string[] first = { null, null, string.Empty };
string[] second = { null, null };
string[] expected = { null, string.Empty };
Assert.Equal(expected, first.Union(second, EqualityComparer<string>.Default));
}
[Fact]
public void DoubleNullWithEmpty()
{
string[] first = { null, null };
string[] second = new string[0];
string[] expected = { null };
Assert.Equal(expected, first.Union(second, EqualityComparer<string>.Default));
}
[Fact]
public void EmptyWithNonEmpty()
{
int[] first = { };
int[] second = { 2, 4, 5, 3, 2, 3, 9 };
int[] expected = { 2, 4, 5, 3, 9 };
Assert.Equal(expected, first.Union(second));
}
[Fact]
public void NonEmptyWithEmpty()
{
int[] first = { 2, 4, 5, 3, 2, 3, 9 };
int[] second = { };
int[] expected = { 2, 4, 5, 3, 9 };
Assert.Equal(expected, first.Union(second));
}
[Fact]
public void CommonElementsShared()
{
int[] first = { 1, 2, 3, 4, 5, 6 };
int[] second = { 6, 7, 7, 7, 8, 1 };
int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8 };
Assert.Equal(expected, first.Union(second));
}
[Fact]
public void SameElementRepeated()
{
int[] first = { 1, 1, 1, 1, 1, 1 };
int[] second = { 1, 1, 1, 1, 1, 1 };
int[] expected = { 1 };
Assert.Equal(expected, first.Union(second));
}
[Fact]
public void RepeatedElementsWithSingleElement()
{
int[] first = { 1, 2, 3, 5, 3, 6 };
int[] second = { 7 };
int[] expected = { 1, 2, 3, 5, 6, 7 };
Assert.Equal(expected, first.Union(second));
}
[Fact]
public void SingleWithAllUnique()
{
int?[] first = { 2 };
int?[] second = { 3, null, 4, 5 };
int?[] expected = { 2, 3, null, 4, 5 };
Assert.Equal(expected, first.Union(second));
}
[Fact]
public void EachHasRepeatsBetweenAndAmongstThemselves()
{
int?[] first = { 1, 2, 3, 4, null, 5, 1 };
int?[] second = { 6, 2, 3, 4, 5, 6 };
int?[] expected = { 1, 2, 3, 4, null, 5, 6 };
Assert.Equal(expected, first.Union(second));
}
[Fact]
public void NullEqualityComparer()
{
string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" };
string[] second = { "ttaM", "Charlie", "Bbo" };
string[] expected = { "Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo" };
Assert.Equal(expected, first.Union(second, null));
}
[Fact]
public void ForcedToEnumeratorDoesntEnumerate()
{
var iterator = NumberRangeGuaranteedNotCollectionType(0, 3).Union(Enumerable.Range(0, 3));
// Don't insist on this behaviour, but check its correct if it happens
var en = iterator as IEnumerator<int>;
Assert.False(en != null && en.MoveNext());
}
}
}