OrderBy.Generated.tt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. if (async)
  16. {
  17. mtd += "Await";
  18. if (cancel)
  19. {
  20. mtd += "WithCancellation";
  21. }
  22. }
  23. return "." + mtd + "(" + arg + ")";
  24. };
  25. Func<bool, bool, int, string, string> getAsyncMethodWithVariant = (then, desc, variant, body) =>
  26. {
  27. return getAsyncMethod(then, desc, variant > 0 ? true : false, variant == 2 ? true : false, body);
  28. };
  29. Func<bool, bool, string, string> getSyncMethod = (then, desc, body) =>
  30. {
  31. var mtd = (then ? "ThenBy" : "OrderBy") + (desc ? "Descending" : "");
  32. var arg = "x => " + body;
  33. return "." + mtd + "(" + arg + ")";
  34. };
  35. Func<bool, bool, int, string> getVariantName = (then, desc, variant) =>
  36. {
  37. var v = variant == 0 ? "" : (variant == 1 ? "Async" : "AsyncWithCancellation");
  38. return "_" + (then ? "ThenBy" : "OrderBy") + (desc ? "Descending" : "") + v;
  39. };
  40. Func<bool, string, List<Tuple<string, string, string>>> getAsyncMethodVariants = (then, body) =>
  41. {
  42. var res = new List<Tuple<string, string, string>>();
  43. foreach (var desc in new[] { false, true })
  44. {
  45. foreach (var variant in new[] { 0, 1, 2 })
  46. {
  47. var v = getVariantName(then, desc, variant);
  48. var async = getAsyncMethodWithVariant(then, desc, variant, body);
  49. var sync = getSyncMethod(then, desc, body);
  50. res.Add(new Tuple<string, string, string>(v, async, sync));
  51. }
  52. }
  53. return res;
  54. };
  55. Func<int, List<Tuple<string, string, string>>> getVariants = null;
  56. getVariants = depth =>
  57. {
  58. if (depth == 1)
  59. {
  60. return getAsyncMethodVariants(false, "x % 2");
  61. }
  62. else
  63. {
  64. var res = new List<Tuple<string, string, string>>();
  65. var next = getVariants(depth - 1);
  66. foreach (var item in next)
  67. {
  68. foreach (var variant in getAsyncMethodVariants(true, "x % " + (depth + 1)))
  69. {
  70. res.Add(new Tuple<string, string, string>(item.Item1 + variant.Item1, item.Item2 + variant.Item2, item.Item3 + variant.Item3));
  71. }
  72. }
  73. return res;
  74. }
  75. };
  76. #>
  77. using System;
  78. using System.Collections.Generic;
  79. using System.Linq;
  80. using System.Threading.Tasks;
  81. using Xunit;
  82. namespace Tests
  83. {
  84. partial class OrderBy
  85. {
  86. <#
  87. for (var i = 1; i <= 3; i++)
  88. {
  89. foreach (var v in getVariants(i))
  90. {
  91. var variant = v.Item1;
  92. var asyncCall = v.Item2;
  93. var syncCall = v.Item3;
  94. #>
  95. [Fact]
  96. public async Task OrderBy<#=variant#>()
  97. {
  98. var rand = new Random(42);
  99. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  100. var asyncRes = xs.ToAsyncEnumerable()<#=asyncCall#>;
  101. var syncRes = xs<#=syncCall#>;
  102. await AssertSorted(asyncRes, syncRes);
  103. var asyncSegment = asyncRes.AsAsyncEnumerable();
  104. var syncSegment = syncRes.AsEnumerable();
  105. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  106. {
  107. asyncSegment = asyncSegment.Skip(skipCount);
  108. syncSegment = syncSegment.Skip(skipCount);
  109. }
  110. foreach (var takeCount in new[] { 31, 29, 23 })
  111. {
  112. asyncSegment = asyncSegment.Take(takeCount);
  113. syncSegment = syncSegment.Take(takeCount);
  114. }
  115. await AssertSorted(asyncSegment, syncSegment);
  116. }
  117. <#
  118. }
  119. }
  120. #>
  121. private async Task AssertSorted<T>(IAsyncEnumerable<T> asyncRes, IEnumerable<T> syncRes)
  122. {
  123. Assert.True(await syncRes.ToAsyncEnumerable().SequenceEqualAsync(asyncRes));
  124. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  125. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  126. int syncCount = syncRes.Count();
  127. int asyncCount = await asyncRes.CountAsync();
  128. Assert.Equal(syncCount, asyncCount);
  129. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  130. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  131. for (var i = 0; i < syncCount; i++)
  132. {
  133. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  134. }
  135. }
  136. }
  137. }