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.Parallel.Tests
9 {
10     public static class AsSequentialTests
11     {
12         [Theory]
13         [MemberData(nameof(UnorderedSources.Ranges), new[] { 0, 1, 2, 16 }, MemberType = typeof(UnorderedSources))]
AsSequential_Unordered(Labeled<ParallelQuery<int>> labeled, int count)14         public static void AsSequential_Unordered(Labeled<ParallelQuery<int>> labeled, int count)
15         {
16             IntegerRangeSet seen = new IntegerRangeSet(0, count);
17             IEnumerable<int> seq = labeled.Item.AsSequential();
18             Assert.All(seq, x => seen.Add(x));
19             seen.AssertComplete();
20         }
21 
22         [Theory]
23         [OuterLoop]
24         [MemberData(nameof(UnorderedSources.OuterLoopRanges), MemberType = typeof(UnorderedSources))]
AsSequential_Unordered_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)25         public static void AsSequential_Unordered_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)
26         {
27             AsSequential_Unordered(labeled, count);
28         }
29 
30         [Theory]
31         [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 16 }, MemberType = typeof(Sources))]
AsSequential(Labeled<ParallelQuery<int>> labeled, int count)32         public static void AsSequential(Labeled<ParallelQuery<int>> labeled, int count)
33         {
34             int seen = 0;
35             IEnumerable<int> seq = labeled.Item.AsSequential();
36             Assert.All(seq, x => Assert.Equal(seen++, x));
37             Assert.Equal(count, seen);
38         }
39 
40         [Theory]
41         [OuterLoop]
42         [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))]
AsSequential_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)43         public static void AsSequential_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)
44         {
45             AsSequential(labeled, count);
46         }
47 
48         [Theory]
49         [MemberData(nameof(Sources.Ranges), new[] { 0, 16 }, MemberType = typeof(Sources))]
AsSequential_LinqBinding(Labeled<ParallelQuery<int>> labeled, int count)50         public static void AsSequential_LinqBinding(Labeled<ParallelQuery<int>> labeled, int count)
51         {
52             IEnumerable<int> seq = labeled.Item.AsSequential();
53 
54             // The LINQ Cast<T>() retains origin type for ParallelEnumerable  and Partitioner when unordered,
55             // (and for all sources when ordered, due to the extra wrapper)
56             // although aliased as IEnumerable<T>, so further LINQ calls work as expected.
57             // If this test starts failing, update this test, and maybe mention it in release notes.
58             Assert.IsNotType<ParallelQuery<int>>(seq.Cast<int>());
59             Assert.True(seq.Cast<int>() is ParallelQuery<int>);
60 
61             Assert.False(seq.Concat(Enumerable.Range(0, count)) is ParallelQuery<int>);
62             Assert.False(seq.DefaultIfEmpty() is ParallelQuery<int>);
63             Assert.False(seq.Distinct() is ParallelQuery<int>);
64             Assert.False(seq.Except(Enumerable.Range(0, count)) is ParallelQuery<int>);
65             Assert.False(seq.GroupBy(x => x) is ParallelQuery<int>);
66             Assert.False(seq.GroupJoin(Enumerable.Range(0, count), x => x, y => y, (x, g) => x) is ParallelQuery<int>);
67             Assert.False(seq.Intersect(Enumerable.Range(0, count)) is ParallelQuery<int>);
68             Assert.False(seq.Join(Enumerable.Range(0, count), x => x, y => y, (x, y) => x) is ParallelQuery<int>);
69             Assert.False(seq.OfType<int>() is ParallelQuery<int>);
70             Assert.False(seq.OrderBy(x => x) is ParallelQuery<int>);
71             Assert.False(seq.OrderByDescending(x => x) is ParallelQuery<int>);
72             Assert.False(seq.Reverse() is ParallelQuery<int>);
73             Assert.False(seq.Select(x => x) is ParallelQuery<int>);
74             Assert.False(seq.SelectMany(x => new[] { x }) is ParallelQuery<int>);
75             Assert.False(seq.Skip(count / 2) is ParallelQuery<int>);
76             Assert.False(seq.SkipWhile(x => true) is ParallelQuery<int>);
77             Assert.False(seq.Take(count / 2) is ParallelQuery<int>);
78             Assert.False(seq.TakeWhile(x => true) is ParallelQuery<int>);
79             Assert.False(seq.Union(Enumerable.Range(0, count)) is ParallelQuery<int>);
80             Assert.False(seq.Where(x => true) is ParallelQuery<int>);
81             Assert.False(seq.Zip(Enumerable.Range(0, count), (x, y) => x) is ParallelQuery<int>);
82         }
83 
84         [Fact]
AsSequential_ArgumentNullException()85         public static void AsSequential_ArgumentNullException()
86         {
87             AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<int>)null).AsSequential());
88         }
89     }
90 }
91