OrderBy.Generated.tt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. <#@ template debug="false" hostspecific="false" language="C#" #>
  5. <#@ assembly name="System.Core" #>
  6. <#@ import namespace="System.Linq" #>
  7. <#@ import namespace="System.Text" #>
  8. <#@ import namespace="System.Collections.Generic" #>
  9. <#@ output extension=".cs" #>
  10. <#
  11. Func<bool, bool, bool, bool, string, string> getAsyncMethod = (then, desc, async, cancel, body) =>
  12. {
  13. var mtd = (then ? "ThenBy" : "OrderBy") + (desc ? "Descending" : "");
  14. var arg = async ? (cancel ? "(x, ct) => " : "x => ") + "new ValueTask<int>(" + body + ")" : "x => " + body;
  15. return "." + mtd + "(" + arg + ")";
  16. };
  17. Func<bool, bool, int, string, string> getAsyncMethodWithVariant = (then, desc, variant, body) =>
  18. {
  19. return getAsyncMethod(then, desc, variant > 0 ? true : false, variant == 2 ? true : false, body);
  20. };
  21. Func<bool, bool, string, string> getSyncMethod = (then, desc, body) =>
  22. {
  23. var mtd = (then ? "ThenBy" : "OrderBy") + (desc ? "Descending" : "");
  24. var arg = "x => " + body;
  25. return "." + mtd + "(" + arg + ")";
  26. };
  27. Func<bool, bool, int, string> getVariantName = (then, desc, variant) =>
  28. {
  29. var v = variant == 0 ? "" : (variant == 1 ? "Async" : "AsyncWithCancellation");
  30. return "_" + (then ? "ThenBy" : "OrderBy") + (desc ? "Descending" : "") + v;
  31. };
  32. Func<bool, string, List<Tuple<string, string, string>>> getAsyncMethodVariants = (then, body) =>
  33. {
  34. var res = new List<Tuple<string, string, string>>();
  35. foreach (var desc in new[] { false, true })
  36. {
  37. foreach (var variant in new[] { 0, 1, 2 })
  38. {
  39. var v = getVariantName(then, desc, variant);
  40. var async = getAsyncMethodWithVariant(then, desc, variant, body);
  41. var sync = getSyncMethod(then, desc, body);
  42. res.Add(new Tuple<string, string, string>(v, async, sync));
  43. }
  44. }
  45. return res;
  46. };
  47. Func<int, List<Tuple<string, string, string>>> getVariants = null;
  48. getVariants = depth =>
  49. {
  50. if (depth == 1)
  51. {
  52. return getAsyncMethodVariants(false, "x % 2");
  53. }
  54. else
  55. {
  56. var res = new List<Tuple<string, string, string>>();
  57. var next = getVariants(depth - 1);
  58. foreach (var item in next)
  59. {
  60. foreach (var variant in getAsyncMethodVariants(true, "x % " + (depth + 1)))
  61. {
  62. res.Add(new Tuple<string, string, string>(item.Item1 + variant.Item1, item.Item2 + variant.Item2, item.Item3 + variant.Item3));
  63. }
  64. }
  65. return res;
  66. }
  67. };
  68. #>
  69. using System;
  70. using System.Collections.Generic;
  71. using System.Linq;
  72. using System.Threading.Tasks;
  73. using Xunit;
  74. namespace Tests
  75. {
  76. partial class OrderBy
  77. {
  78. <#
  79. for (var i = 1; i <= 3; i++)
  80. {
  81. foreach (var v in getVariants(i))
  82. {
  83. var variant = v.Item1;
  84. var asyncCall = v.Item2;
  85. var syncCall = v.Item3;
  86. #>
  87. [Fact]
  88. public async Task OrderBy<#=variant#>()
  89. {
  90. var rand = new Random(42);
  91. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  92. var asyncRes = xs.ToAsyncEnumerable()<#=asyncCall#>;
  93. var syncRes = xs<#=syncCall#>;
  94. await AssertSorted(asyncRes, syncRes);
  95. var asyncSegment = asyncRes.AsAsyncEnumerable();
  96. var syncSegment = syncRes.AsEnumerable();
  97. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  98. {
  99. asyncSegment = asyncSegment.Skip(skipCount);
  100. syncSegment = syncSegment.Skip(skipCount);
  101. }
  102. foreach (var takeCount in new[] { 31, 29, 23 })
  103. {
  104. asyncSegment = asyncSegment.Take(takeCount);
  105. syncSegment = syncSegment.Take(takeCount);
  106. }
  107. await AssertSorted(asyncSegment, syncSegment);
  108. }
  109. <#
  110. }
  111. }
  112. #>
  113. private async Task AssertSorted<T>(IAsyncEnumerable<T> asyncRes, IEnumerable<T> syncRes)
  114. {
  115. Assert.True(await syncRes.ToAsyncEnumerable().SequenceEqualAsync(asyncRes));
  116. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  117. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  118. int syncCount = syncRes.Count();
  119. int asyncCount = await asyncRes.CountAsync();
  120. Assert.Equal(syncCount, asyncCount);
  121. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  122. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  123. for (var i = 0; i < syncCount; i++)
  124. {
  125. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  126. }
  127. }
  128. }
  129. }