public static void ILookup_MembersBehaveCorrectly(Labeled<ParallelQuery<int>> labeled, int count) { int NonExistentKey = count * 2; ILookup<int, int> lookup = labeled.Item.ToLookup(x => x); // Count Assert.Equal(count, lookup.Count); // Contains Assert.All(lookup, group => lookup.Contains(group.Key)); Assert.False(lookup.Contains(NonExistentKey)); // Indexer Assert.All(lookup, group => Assert.Equal(group, lookup[group.Key])); Assert.Equal(Enumerable.Empty<int>(), lookup[NonExistentKey]); // GetEnumerator IEnumerator e1 = ((IEnumerable)lookup).GetEnumerator(); IEnumerator<IGrouping<int, int>> e2 = lookup.GetEnumerator(); while (e1.MoveNext()) { e2.MoveNext(); Assert.Equal(((IGrouping<int,int>)e1.Current).Key, e2.Current.Key); } Assert.False(e2.MoveNext()); }
public static void AsSequential_LinqBinding(Labeled<ParallelQuery<int>> labeled, int count) { IEnumerable<int> seq = labeled.Item.AsSequential(); // The LINQ Cast<T>() retains origin type for ParallelEnumerable and Partitioner when unordered, // (and for all sources when ordered, due to the extra wrapper) // although aliased as IEnumerable<T>, so further LINQ calls work as expected. // If this test starts failing, update this test, and maybe mention it in release notes. Assert.IsNotType<ParallelQuery<int>>(seq.Cast<int>()); Assert.True(seq.Cast<int>() is ParallelQuery<int>); Assert.False(seq.Concat(Enumerable.Range(0, count)) is ParallelQuery<int>); Assert.False(seq.DefaultIfEmpty() is ParallelQuery<int>); Assert.False(seq.Distinct() is ParallelQuery<int>); Assert.False(seq.Except(Enumerable.Range(0, count)) is ParallelQuery<int>); Assert.False(seq.GroupBy(x => x) is ParallelQuery<int>); Assert.False(seq.GroupJoin(Enumerable.Range(0, count), x => x, y => y, (x, g) => x) is ParallelQuery<int>); Assert.False(seq.Intersect(Enumerable.Range(0, count)) is ParallelQuery<int>); Assert.False(seq.Join(Enumerable.Range(0, count), x => x, y => y, (x, y) => x) is ParallelQuery<int>); Assert.False(seq.OfType<int>() is ParallelQuery<int>); Assert.False(seq.OrderBy(x => x) is ParallelQuery<int>); Assert.False(seq.OrderByDescending(x => x) is ParallelQuery<int>); Assert.False(seq.Reverse() is ParallelQuery<int>); Assert.False(seq.Select(x => x) is ParallelQuery<int>); Assert.False(seq.SelectMany(x => new[] { x }) is ParallelQuery<int>); Assert.False(seq.Skip(count / 2) is ParallelQuery<int>); Assert.False(seq.SkipWhile(x => true) is ParallelQuery<int>); Assert.False(seq.Take(count / 2) is ParallelQuery<int>); Assert.False(seq.TakeWhile(x => true) is ParallelQuery<int>); Assert.False(seq.Union(Enumerable.Range(0, count)) is ParallelQuery<int>); Assert.False(seq.Where(x => true) is ParallelQuery<int>); Assert.False(seq.Zip(Enumerable.Range(0, count), (x, y) => x) is ParallelQuery<int>); }
public static void Any_AllFalse(Labeled<ParallelQuery<int>> labeled, int count) { ParallelQuery<int> query = labeled.Item; IntegerRangeSet seen = new IntegerRangeSet(0, count); Assert.False(query.Any(x => !seen.Add(x))); seen.AssertComplete(); }
public static void ToList(Labeled<ParallelQuery<int>> labeled, int count) { ParallelQuery<int> query = labeled.Item; int seen = 0; Assert.All(query.ToList(), x => Assert.Equal(seen++, x)); Assert.Equal(count, seen); }
public static void Distinct_Unordered_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count) { ParallelQuery<int> query = labeled.Item; IntegerRangeSet seen = new IntegerRangeSet(0, count); Assert.All(query.Distinct(new ModularCongruenceComparer(count)).ToList(), x => seen.Add(x % count)); seen.AssertComplete(); }
public static void Where_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count) { ParallelQuery<int> query = labeled.Item; int seen = -2; Assert.All(query.Where(x => x % 2 == 0).ToList(), x => Assert.Equal(seen += 2, x)); Assert.Equal(count - (count - 1) % 2 - 1, seen); }
public static void ForAll(Labeled<ParallelQuery<int>> labeled, int count) { ParallelQuery<int> query = labeled.Item; IntegerRangeSet seen = new IntegerRangeSet(0, count); query.ForAll<int>(x => seen.Add(x)); seen.AssertComplete(); }
public static void Cast_Unordered_Valid_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count) { ParallelQuery<int> query = labeled.Item; IntegerRangeSet seen = new IntegerRangeSet(0, count); Assert.All(query.Select(x => (object)x).Cast<int>().ToList(), x => seen.Add(x)); seen.AssertComplete(); }
public static void Single_NoMatch(Labeled<ParallelQuery<int>> labeled, int count, int element) { ParallelQuery<int> query = labeled.Item; IntegerRangeSet seen = new IntegerRangeSet(0, count); Assert.Throws<InvalidOperationException>(() => query.Single(x => !seen.Add(x))); seen.AssertComplete(); }
public static void SingleOrDefault_NoMatch(Labeled<ParallelQuery<int>> labeled, int count, int element) { ParallelQuery<int> query = labeled.Item; IntegerRangeSet seen = new IntegerRangeSet(0, count); Assert.Equal(default(int), query.SingleOrDefault(x => !seen.Add(x))); seen.AssertComplete(); }
public static void Sum_Int_Overflow(Labeled<ParallelQuery<int>> labeled, int count) { Functions.AssertThrowsWrapped<OverflowException>(() => labeled.Item.Select(x => x == 0 ? int.MaxValue : x).Sum()); Functions.AssertThrowsWrapped<OverflowException>(() => labeled.Item.Select(x => x == 0 ? int.MaxValue : (int?)x).Sum()); Functions.AssertThrowsWrapped<OverflowException>(() => labeled.Item.Sum(x => x == 0 ? int.MinValue : -x)); Functions.AssertThrowsWrapped<OverflowException>(() => labeled.Item.Sum(x => x == 0 ? int.MinValue : -(int?)x)); }
public static void ToList_OperationCanceledException_PreCanceled(Labeled<ParallelQuery<int>> labeled, int count) { CancellationTokenSource cs = new CancellationTokenSource(); cs.Cancel(); Functions.AssertIsCanceled(cs, () => labeled.Item.WithCancellation(cs.Token).ToList()); }
public static void ToArray_Unordered(Labeled<ParallelQuery<int>> labeled, int count) { ParallelQuery<int> query = labeled.Item; IntegerRangeSet seen = new IntegerRangeSet(0, count); Assert.All(query.ToArray(), x => seen.Add(x)); seen.AssertComplete(); }
public static void AsEnumerable(Labeled<ParallelQuery<int>> labeled, int count) { int seen = 0; IEnumerable<int> enumerable = labeled.Item.AsEnumerable(); Assert.All(enumerable, x => Assert.Equal(seen++, x)); Assert.Equal(count, seen); }
public static void GetEnumerator_Unordered(Labeled<ParallelQuery<int>> labeled, int count) { IntegerRangeSet seen = new IntegerRangeSet(0, count); IEnumerator<int> enumerator = labeled.Item.GetEnumerator(); if (count == 0 && labeled.ToString().Contains("Array")) { Assert.Same(enumerator, labeled.Item.GetEnumerator()); } else { Assert.NotSame(enumerator, labeled.Item.GetEnumerator()); } while (enumerator.MoveNext()) { int current = enumerator.Current; seen.Add(current); Assert.Equal(current, enumerator.Current); } seen.AssertComplete(); if (labeled.ToString().StartsWith("Enumerable.Range") || labeled.ToString().StartsWith("Partitioner")) { Assert.Throws<NotSupportedException>(() => enumerator.Reset()); } else { enumerator.Reset(); seen = new IntegerRangeSet(0, count); while (enumerator.MoveNext()) { Assert.True(seen.Add(enumerator.Current)); } seen.AssertComplete(); } }
public static void Contains_OneMatching(Labeled<ParallelQuery<int>> labeled, int count, int position) { ParallelQuery<int> query = labeled.Item; Assert.True(query.Contains(position)); Assert.True(query.Contains(position, null)); Assert.True(query.Contains(position, new ModularCongruenceComparer(count))); }
public static void GetEnumerator(Labeled<ParallelQuery<int>> labeled, int count) { int seen = 0; IEnumerator<int> enumerator = labeled.Item.GetEnumerator(); if (count == 0 && labeled.ToString().Contains("Array")) { Assert.Same(enumerator, labeled.Item.GetEnumerator()); } else { Assert.NotSame(enumerator, labeled.Item.GetEnumerator()); } while (enumerator.MoveNext()) { int current = enumerator.Current; Assert.Equal(seen++, current); Assert.Equal(current, enumerator.Current); } Assert.Equal(count, seen); if (labeled.ToString().StartsWith("Enumerable.Range") || labeled.ToString().StartsWith("Partitioner")) { Assert.Throws<NotSupportedException>(() => enumerator.Reset()); } else { enumerator.Reset(); seen = 0; while (enumerator.MoveNext()) { Assert.Equal(seen++, enumerator.Current); } Assert.Equal(count, seen); } }
public static void Cast_Valid_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count) { ParallelQuery<int> query = labeled.Item; int seen = 0; Assert.All(query.Select(x => (object)x).Cast<int>().ToList(), x => Assert.Equal(seen++, x)); Assert.Equal(count, seen); }
public static void Reverse_Unordered_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count) { ParallelQuery<int> query = labeled.Item; IntegerRangeSet seen = new IntegerRangeSet(0, count); Assert.All(query.Reverse().ToList(), x => seen.Add(x)); seen.AssertComplete(); }
public static void Where_Unordered_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count) { ParallelQuery<int> query = labeled.Item; IntegerRangeSet seen = new IntegerRangeSet(0, (count + 1) / 2); Assert.All(query.Where(x => x % 2 == 0).ToList(), x => seen.Add(x / 2)); seen.AssertComplete(); }
public static void Reverse_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count) { ParallelQuery<int> query = labeled.Item; int seen = count; Assert.All(query.Reverse().ToList(), x => Assert.Equal(--seen, x)); Assert.Equal(0, seen); }
public static void ElementAtOrDefault(Labeled<ParallelQuery<int>> labeled, int count, int position) { // For unordered collections, which element is chosen isn't actually guaranteed, but an effect of the implementation. // If this test starts failing it should be split, and possibly mentioned in release notes. ParallelQuery<int> query = labeled.Item; Assert.Equal(position, query.ElementAtOrDefault(position)); }
public static void AsSequential_Unordered(Labeled<ParallelQuery<int>> labeled, int count) { IntegerRangeSet seen = new IntegerRangeSet(0, count); IEnumerable<int> seq = labeled.Item.AsSequential(); Assert.All(seq, x => seen.Add(x)); seen.AssertComplete(); }
public static void Average_Long_Overflow(Labeled<ParallelQuery<int>> labeled, int count) { Functions.AssertThrowsWrapped<OverflowException>(() => labeled.Item.Select(x => x == 0 ? 1 : long.MaxValue).Average()); Functions.AssertThrowsWrapped<OverflowException>(() => labeled.Item.Select(x => x == 0 ? (long?)1 : long.MaxValue).Average()); Functions.AssertThrowsWrapped<OverflowException>(() => labeled.Item.Average(x => x == 0 ? -1 : long.MinValue)); Functions.AssertThrowsWrapped<OverflowException>(() => labeled.Item.Average(x => x == 0 ? (long?)-1 : long.MinValue)); }
public static void AsSequential(Labeled<ParallelQuery<int>> labeled, int count) { int seen = 0; IEnumerable<int> seq = labeled.Item.AsSequential(); Assert.All(seq, x => Assert.Equal(seen++, x)); Assert.Equal(count, seen); }
public static void Aggregate_Product_Seed(Labeled<ParallelQuery<int>> labeled, int count, int start) { ParallelQuery<int> query = labeled.Item; // The operation will overflow for long-running sizes, but that's okay: // The helper is overflowing too! Assert.Equal(Functions.ProductRange(start, count), query.Aggregate(1L, (x, y) => x * y)); }
public static void Contains_NoMatching(Labeled<ParallelQuery<int>> labeled, int count) { ParallelQuery<int> query = labeled.Item; Assert.False(query.Contains(count)); Assert.False(query.Contains(count, null)); Assert.False(query.Contains(count, new ModularCongruenceComparer(count + 1))); }
public static void Where_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count) { ParallelQuery<int> query = labeled.Item; int seen = 0; Assert.All(query.Where(x => x % 2 == 0).ToList(), x => { Assert.Equal(seen, x); seen += 2; }); Assert.Equal(count + (count % 2), seen); }
public static void Min_Long(Labeled<ParallelQuery<int>> labeled, int count, long min) { ParallelQuery<int> query = labeled.Item; Assert.Equal(0, query.Select(x => (long)x).Min()); Assert.Equal(0, query.Select(x => (long?)x).Min()); Assert.Equal(min, query.Min(x => -(long)x)); Assert.Equal(min, query.Min(x => -(long?)x)); }
public static void Min_Int(Labeled<ParallelQuery<int>> labeled, int count, int min) { ParallelQuery<int> query = labeled.Item; Assert.Equal(0, query.Min()); Assert.Equal(0, query.Select(x => (int?)x).Min()); Assert.Equal(min, query.Min(x => -x)); Assert.Equal(min, query.Min(x => -(int?)x)); }