Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Ko <jamesqko@gmail.com>2017-02-12 02:44:56 +0300
committerJames Ko <jamesqko@gmail.com>2017-02-12 02:44:56 +0300
commitce1f4b57e7fc02d5779abf2c026247d421a643df (patch)
tree3f4a5c1a179927ada743674cf912b5a15d9aa486
parentd6e48890eba08f7316bd6cf9ef4d458fa8ec7349 (diff)
Fix SelectMany bug
-rw-r--r--src/System.Linq/src/System/Linq/SelectMany.cs2
-rw-r--r--src/System.Linq/tests/SelectManyTests.cs39
2 files changed, 40 insertions, 1 deletions
diff --git a/src/System.Linq/src/System/Linq/SelectMany.cs b/src/System.Linq/src/System/Linq/SelectMany.cs
index 5a4b81abf3..d3dc30f321 100644
--- a/src/System.Linq/src/System/Linq/SelectMany.cs
+++ b/src/System.Linq/src/System/Linq/SelectMany.cs
@@ -242,7 +242,7 @@ namespace System.Linq
continue;
}
- builder.AddRange(_selector(element));
+ builder.AddRange(enumerable);
}
TResult[] array = builder.ToArray();
diff --git a/src/System.Linq/tests/SelectManyTests.cs b/src/System.Linq/tests/SelectManyTests.cs
index ebed4cd158..637b8a5448 100644
--- a/src/System.Linq/tests/SelectManyTests.cs
+++ b/src/System.Linq/tests/SelectManyTests.cs
@@ -475,5 +475,44 @@ namespace System.Linq.Tests
var iterator = counts.SelectMany(c => Enumerable.Range(1, c));
Assert.Throws<OverflowException>(() => iterator.Count());
}
+
+ [Theory]
+ [InlineData(10)]
+ public void EvaluateSelectorOncePerItem(int count)
+ {
+ int[] timesCalledMap = new int[count];
+
+ IEnumerable<int> source = Enumerable.Range(0, 10);
+ IEnumerable<int> iterator = source.SelectMany(index =>
+ {
+ timesCalledMap[index]++;
+ return new[] { index };
+ });
+
+ // Iteration
+ foreach (int index in iterator)
+ {
+ Assert.Equal(Enumerable.Repeat(1, index + 1), timesCalledMap.Take(index + 1));
+ Assert.Equal(Enumerable.Repeat(0, timesCalledMap.Length - index - 1), timesCalledMap.Skip(index + 1));
+ }
+
+ Array.Clear(timesCalledMap, 0, timesCalledMap.Length);
+
+ // ToArray
+ iterator.ToArray();
+ Assert.Equal(Enumerable.Repeat(1, timesCalledMap.Length), timesCalledMap);
+
+ Array.Clear(timesCalledMap, 0, timesCalledMap.Length);
+
+ // ToList
+ iterator.ToList();
+ Assert.Equal(Enumerable.Repeat(1, timesCalledMap.Length), timesCalledMap);
+
+ Array.Clear(timesCalledMap, 0, timesCalledMap.Length);
+
+ // ToHashSet
+ iterator.ToHashSet();
+ Assert.Equal(Enumerable.Repeat(1, timesCalledMap.Length), timesCalledMap);
+ }
}
}