This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
TakeWhileTests.cs
184 lines (154 loc) · 5.8 KB
/
TakeWhileTests.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections;
using System.Collections.Generic;
using Xunit;
namespace System.Linq.Tests
{
public class TakeWhileTests : EnumerableTests
{
[Fact]
public void SameResultsRepeatCallsIntQuery()
{
var q = from x in new[] { 9999, 0, 888, -1, 66, -777, 1, 2, -12345 }
where x > int.MinValue
select x;
Assert.Equal(q.TakeWhile(x => true), q.TakeWhile(x => true));
}
[Fact]
public void SameResultsRepeatCallsStringQuery()
{
var q = from x in new[] { "!@#$%^", "C", "AAA", "", "Calling Twice", "SoS", string.Empty }
where !string.IsNullOrEmpty(x)
select x;
Assert.Equal(q.TakeWhile(x => true), q.TakeWhile(x => true));
}
[Fact]
public void SourceEmpty()
{
Assert.Empty(Enumerable.Empty<int>().TakeWhile(e => true));
}
[Fact]
public void SourceEmptyIndexed()
{
Assert.Empty(Enumerable.Empty<int>().TakeWhile((e, i) => true));
}
[Fact]
public void SourceNonEmptyPredicateFalseForAll()
{
int[] source = { 9, 7, 15, 3, 11 };
Assert.Empty(source.TakeWhile(x => x % 2 == 0));
}
[Fact]
public void SourceNonEmptyPredicateFalseForAllWithIndex()
{
int[] source = { 9, 7, 15, 3, 11 };
Assert.Empty(source.TakeWhile((x, i) => x % 2 == 0));
}
[Fact]
public void SourceNonEmptyPredicateTrueSomeFalseSecond()
{
int[] source = { 8, 3, 12, 4, 6, 10 };
int[] expected = { 8 };
Assert.Equal(expected, source.TakeWhile(x => x % 2 == 0));
}
[Fact]
public void SourceNonEmptyPredicateTrueSomeFalseSecondWithIndex()
{
int[] source = { 8, 3, 12, 4, 6, 10 };
int[] expected = { 8 };
Assert.Equal(expected, source.TakeWhile((x, i) => x % 2 == 0));
}
[Fact]
public void SourceNonEmptyPredicateTrueSomeFalseFirst()
{
int[] source = { 3, 2, 4, 12, 6 };
Assert.Empty(source.TakeWhile(x => x % 2 == 0));
}
[Fact]
public void SourceNonEmptyPredicateTrueSomeFalseFirstWithIndex()
{
int[] source = { 3, 2, 4, 12, 6 };
Assert.Empty(source.TakeWhile((x, i) => x % 2 == 0));
}
[Fact]
public void FirstTakenByIndex()
{
int[] source = { 6, 2, 5, 3, 8 };
int[] expected = { 6 };
Assert.Equal(expected, source.TakeWhile((element, index) => index == 0));
}
[Fact]
public void AllButLastTakenByIndex()
{
int[] source = { 6, 2, 5, 3, 8 };
int[] expected = { 6, 2, 5, 3 };
Assert.Equal(expected, source.TakeWhile((element, index) => index < source.Length - 1));
}
[Fact]
public void RunOnce()
{
int[] source = {8, 3, 12, 4, 6, 10};
int[] expected = {8};
Assert.Equal(expected, source.RunOnce().TakeWhile(x => x % 2 == 0));
source = new[] {6, 2, 5, 3, 8};
expected = new[] {6, 2, 5, 3};
Assert.Equal(expected, source.RunOnce().TakeWhile((element, index) => index < source.Length - 1));
}
[Fact(Skip = "Valid test but too intensive to enable even in OuterLoop")]
public void IndexTakeWhileOverflowBeyondIntMaxValueElements()
{
var taken = new FastInfiniteEnumerator<int>().TakeWhile((e, i) => true);
using(var en = taken.GetEnumerator())
Assert.Throws<OverflowException>(() =>
{
while(en.MoveNext())
{
}
});
}
[Fact]
public void ThrowsOnNullSource()
{
int[] source = null;
AssertExtensions.Throws<ArgumentNullException>("source", () => source.TakeWhile(x => true));
}
[Fact]
public void ThrowsOnNullPredicate()
{
int[] source = { 1, 2, 3 };
Func<int, bool> nullPredicate = null;
AssertExtensions.Throws<ArgumentNullException>("predicate", () => source.TakeWhile(nullPredicate));
}
[Fact]
public void ThrowsOnNullSourceIndexed()
{
int[] source = null;
AssertExtensions.Throws<ArgumentNullException>("source", () => source.TakeWhile((x, i) => true));
}
[Fact]
public void ThrowsOnNullPredicateIndexed()
{
int[] source = { 1, 2, 3 };
Func<int, int, bool> nullPredicate = null;
AssertExtensions.Throws<ArgumentNullException>("predicate", () => source.TakeWhile(nullPredicate));
}
[Fact]
public void ForcedToEnumeratorDoesntEnumerate()
{
var iterator = NumberRangeGuaranteedNotCollectionType(0, 3).TakeWhile(e => true);
// Don't insist on this behaviour, but check it's correct if it happens
var en = iterator as IEnumerator<int>;
Assert.False(en != null && en.MoveNext());
}
[Fact]
public void ForcedToEnumeratorDoesntEnumerateIndexed()
{
var iterator = NumberRangeGuaranteedNotCollectionType(0, 3).TakeWhile((e, i) => true);
// Don't insist on this behaviour, but check it's correct if it happens
var en = iterator as IEnumerator<int>;
Assert.False(en != null && en.MoveNext());
}
}
}