-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24_LinqBasics.cs
292 lines (215 loc) · 9.5 KB
/
24_LinqBasics.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using basic.Common;
using Xunit;
namespace basic
{
public class Linq2ObjectBasics
{
[Fact]
public void should_filtering_elements_using_where()
{
string[] sequence =
{
"a", "quick", "brown", "fox", "jumps", "over", "a", "lazy", "dog"
};
IEnumerable<string> filteredResult = sequence.Where(item => item.Length == 5);
// please update variable value to fix the test.
IEnumerable<string> expectedResult =
new[] {"quick", "brown", "jumps"};
Assert.Equal(expectedResult, filteredResult);
}
[Fact]
public void should_project_elements_using_select()
{
var sequence = new[]
{
new Name("Edogawa", "Conan"),
new Name("Ogiso", "Setsuna"),
new Name("Kirigaya", "Katsuto")
};
IEnumerable<string> projection = sequence.Select(item => item.ToString());
// please update variable value to fix the test.
IEnumerable<string> expectedResult =
new[] {"Edogawa Conan", "Ogiso Setsuna", "Kirigaya Katsuto"};
Assert.Equal(expectedResult, projection);
}
[Fact]
public void should_take_first_n_elements_using_take()
{
var sequence = new[] {1, 2, 3, 4, 5};
IEnumerable<int> filteredElements = sequence.Take(3);
// please update variable value to fix the test.
IEnumerable<int> expectedResult = new[] {1, 2, 3};
Assert.Equal(expectedResult, filteredElements);
}
[Fact]
public void should_skip_first_n_elements_using_skip()
{
var sequence = new[] {1, 2, 3, 4, 5};
IEnumerable<int> filteredElements = sequence.Skip(3);
// please update variable value to fix the test.
IEnumerable<int> expectedResult = new[] {4, 5};
Assert.Equal(expectedResult, filteredElements);
}
[Fact]
public void should_get_first_element_using_first()
{
var sequence = new[] { 1, 2, 3, 4, 5 };
int firstElement = sequence.First();
int firstEvenNumber = sequence.First(item => item % 2 == 0);
// please update variable values for the following 2 lines to fix the test.
const int expectedFirstElement = 1;
const int expectedFirstEvenNumber = 2;
Assert.Equal(expectedFirstElement, firstElement);
Assert.Equal(expectedFirstEvenNumber, firstEvenNumber);
}
[Fact]
public void should_get_last_element_using_last()
{
var sequence = new[] { 1, 2, 3, 4, 5 };
int lastElement = sequence.Last();
int lastEvenNumber = sequence.Last(item => item % 2 == 0);
// please update variable values for the following 2 lines to fix the test.
const int expectedLastElement = 5;
const int expectedLastEvenNumber = 4;
Assert.Equal(expectedLastElement, lastElement);
Assert.Equal(expectedLastEvenNumber, lastEvenNumber);
}
[Fact]
public void should_get_nth_element_using_element_at()
{
var sequence = new[] { 1, 2, 3, 4, 5 };
int thirdElement = sequence.ElementAt(2);
// please update variable value to fix the test.
const int expectedThirdElement = 3;
Assert.Equal(expectedThirdElement, thirdElement);
}
[Fact]
[SuppressMessage("ReSharper", "UseCollectionCountProperty")]
public void should_get_element_element_using_count()
{
var sequence = new[] { 1, 2, 3, 4, 5 };
int totalNumber = sequence.Count();
int numberOfEvenNumbers = sequence.Count(item => item % 2 == 0);
// please update variable value to fix the test.
const int expectedTotalNumber = 5;
const int expectedNumberOfEvenNumbers = 2;
Assert.Equal(expectedTotalNumber, totalNumber);
Assert.Equal(expectedNumberOfEvenNumbers, numberOfEvenNumbers);
}
[Fact]
public void should_get_minimum_element_using_min()
{
var sequence = new[] { 1, 2, 3, 4, 5 };
int minNumber = sequence.Min();
// please update variable value to fix the test.
const int expectedMinNumber = 1;
Assert.Equal(expectedMinNumber, minNumber);
}
[Fact]
public void should_get_maximum_element_using_max()
{
var sequence = new[] { 1, 2, 3, 4, 5 };
int maxNumber = sequence.Max();
// please update variable value to fix the test.
const int expectedMaxNumber = 5;
Assert.Equal(expectedMaxNumber, maxNumber);
}
[Fact]
public void should_check_if_sequence_contains_element()
{
var sequence = new[] {1, 2, 3, 4, 5};
bool containsTwo = sequence.Contains(2);
bool containsTen = sequence.Contains(10);
// please update variable values of the following 2 lines to fix the test.
const bool expectedContainsTwo = true;
const bool expectedContainsTen = false;
Assert.Equal(expectedContainsTwo, containsTwo);
Assert.Equal(expectedContainsTen, containsTen);
}
[Fact]
public void should_check_if_sequence_has_more_than_zero_elements()
{
var sequence = new[] { 1, 2, 3, 4, 5 };
bool sequenceNotEmpty = sequence.Any();
bool modThreeNotEmpty = sequence.Any(item => item % 3 == 0);
// please update variable values of the following 2 lines to fix the test.
const bool expectedSequenceNotEmpty = true;
const bool expectedModThreeNotEmpty = true;
Assert.Equal(expectedSequenceNotEmpty, sequenceNotEmpty);
Assert.Equal(expectedModThreeNotEmpty, modThreeNotEmpty);
}
[Fact]
public void should_check_if_each_and_every_item_in_sequence_match_prediction()
{
var sequence = new[] { 2, 4, 6, 8, 10 };
bool allEvenNumbers = sequence.All(item => item % 2 == 0);
// please update variable value to fix the test.
const bool expectedAllEvenNumbers = true;
Assert.Equal(expectedAllEvenNumbers, allEvenNumbers);
}
[Fact]
public void should_concat_sequences()
{
var left = new[] {1, 2, 3};
var right = new[] {4, 5};
IEnumerable<int> concat = left.Concat(right);
// please update variable value to fix the test.
IEnumerable<int> expectedConcatResult = new[] {1, 2, 3, 4, 5};
Assert.Equal(expectedConcatResult, concat);
}
[Fact]
public void should_union_sequences()
{
var left = new[] {1, 2, 3};
var right = new[] {4, 3, 5};
IEnumerable<int> unionResult = left.Union(right);
// please update variable value to fix the test.
IEnumerable<int> expectedUnionResult = new[] { 1, 2, 3, 4, 5 };
Assert.Equal(expectedUnionResult, unionResult);
}
[Fact]
public void should_get_sequences_intersection()
{
var firstSequence = new[] { 1, 2, 3 };
var secondSequence = new[] { 4, 3, 5 };
IEnumerable<int> intersection = firstSequence.Intersect(secondSequence);
// please update variable value to fix the test.
IEnumerable<int> expectedIntersection = new[] { 3 };
Assert.Equal(expectedIntersection, intersection);
}
[Fact]
public void should_get_sequence_excpetions()
{
var firstSequence = new[] { 1, 2, 3 };
var secondSequence = new[] { 4, 3, 5 };
IEnumerable<int> except = firstSequence.Except(secondSequence);
// please update variable value to fix the test.
IEnumerable<int> expectedIntersection = new[] { 1, 2 };
Assert.Equal(expectedIntersection, except);
}
[Fact]
public void should_get_ordered_sequence()
{
var sequence = new[] {4, 2, 1, 3, 5};
IOrderedEnumerable<int> orderedResult = sequence.OrderBy(item => item);
// please update variable value to fix the test.
IEnumerable<int> expectedOrderedResult = new[] {1, 2, 3, 4, 5};
Assert.Equal(expectedOrderedResult, orderedResult);
}
[Fact]
public void should_execute_in_a_deferred_way_for_iteration_operation()
{
var sequence = new List<int> {1};
IEnumerable<string> projection =
sequence.Select(item => item.ToString(CultureInfo.InvariantCulture));
sequence.Add(2);
// please update variable value to fix the test.
IEnumerable<string> expectedProjection = new[] { "1", "2" };
Assert.Equal(expectedProjection, projection);
}
}
}