EnumerableAsQueryableTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. //
  2. // EnumerableAsQueryableTest.cs
  3. //
  4. // Authors:
  5. // Roei Erez ([email protected])
  6. //
  7. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Linq;
  31. using System.Text;
  32. using NUnit.Framework;
  33. using System.Linq.Expressions;
  34. using System.Runtime.CompilerServices;
  35. using System.Reflection;
  36. namespace MonoTests.System.Linq {
  37. [TestFixture]
  38. public class EnumerableAsQueryableTest {
  39. int [] _array;
  40. IQueryable<int> _src;
  41. [SetUp]
  42. public void MyTestCleanup ()
  43. {
  44. _array = new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  45. _src = _array.AsQueryable<int> ();
  46. }
  47. [Test]
  48. public void Aggregate ()
  49. {
  50. Assert.AreEqual (_src.Aggregate<int> ((n, m) => n + m), _array.Aggregate<int> ((n, m) => n + m));
  51. }
  52. [Test]
  53. public void All ()
  54. {
  55. Assert.AreEqual (_src.All<int> ((n) => n < 11), _array.All<int> ((n) => n < 11));
  56. Assert.AreEqual (_src.All<int> ((n) => n < 10), _array.All<int> ((n) => n < 10));
  57. }
  58. [Test]
  59. public void Any ()
  60. {
  61. Assert.AreEqual (_src.Any<int> (i => i > 5), _array.Any<int> (i => i > 5));
  62. }
  63. [Test]
  64. public void Average ()
  65. {
  66. Assert.AreEqual (_src.Average<int> ((n) => 11), _array.Average<int> ((n) => 11));
  67. }
  68. [Test]
  69. public void Concat ()
  70. {
  71. Assert.AreEqual (_src.Concat<int> (_src).Count (), _array.Concat<int> (_src).Count ());
  72. }
  73. [Test]
  74. public void Contains ()
  75. {
  76. for (int i = 1; i < 20; ++i)
  77. Assert.AreEqual (_src.Contains<int> (i), _array.Contains<int> (i));
  78. }
  79. [Test]
  80. public void Count ()
  81. {
  82. Assert.AreEqual (_src.Count<int> (), _array.Count<int> ());
  83. }
  84. [Test]
  85. public void Distinct ()
  86. {
  87. Assert.AreEqual (_src.Distinct<int> ().Count (), _array.Distinct<int> ().Count ());
  88. Assert.AreEqual (_src.Distinct<int> (new CustomEqualityComparer ()).Count (), _array.Distinct<int> (new CustomEqualityComparer ()).Count ());
  89. }
  90. [Test]
  91. public void ElementAt ()
  92. {
  93. for (int i = 0; i < 10; ++i)
  94. Assert.AreEqual (_src.ElementAt<int> (i), _array.ElementAt<int> (i));
  95. }
  96. [Test]
  97. public void ElementAtOrDefault ()
  98. {
  99. for (int i = 0; i < 10; ++i)
  100. Assert.AreEqual (_src.ElementAtOrDefault<int> (i), _array.ElementAtOrDefault<int> (i));
  101. Assert.AreEqual (_src.ElementAtOrDefault<int> (100), _array.ElementAtOrDefault<int> (100));
  102. }
  103. [Test]
  104. public void Except ()
  105. {
  106. int [] except = { 1, 2, 3 };
  107. Assert.AreEqual (_src.Except<int> (except.AsQueryable ()).Count (), _array.Except<int> (except).Count ());
  108. }
  109. [Test]
  110. public void First ()
  111. {
  112. Assert.AreEqual (_src.First<int> (), _array.First<int> ());
  113. }
  114. [Test]
  115. public void FirstOrDefault ()
  116. {
  117. Assert.AreEqual (_src.FirstOrDefault<int> ((n) => n > 5), _array.FirstOrDefault<int> ((n) => n > 5));
  118. Assert.AreEqual (_src.FirstOrDefault<int> ((n) => n > 10), _array.FirstOrDefault<int> ((n) => n > 10));
  119. }
  120. [Test]
  121. public void GroupBy ()
  122. {
  123. IQueryable<IGrouping<bool, int>> grouping = _src.GroupBy<int, bool> ((n) => n > 5);
  124. Assert.AreEqual (grouping.Count(), 2);
  125. foreach (IGrouping<bool, int> group in grouping)
  126. {
  127. Assert.AreEqual(group.Count(), 5);
  128. }
  129. }
  130. [Test]
  131. public void Intersect ()
  132. {
  133. int [] subset = { 1, 2, 3 };
  134. int[] intersection = _src.Intersect<int> (subset.AsQueryable()).ToArray();
  135. Assert.AreEqual (subset, intersection);
  136. }
  137. [Test]
  138. public void Last ()
  139. {
  140. Assert.AreEqual (_src.Last<int> ((n) => n > 1), _array.Last<int> ((n) => n > 1));
  141. }
  142. [Test]
  143. public void LastOrDefault ()
  144. {
  145. Assert.AreEqual (_src.LastOrDefault<int> (), _array.LastOrDefault<int> ());
  146. }
  147. [Test]
  148. public void LongCount ()
  149. {
  150. Assert.AreEqual (_src.LongCount<int> (), _array.LongCount<int> ());
  151. }
  152. [Test]
  153. public void Max ()
  154. {
  155. Assert.AreEqual (_src.Max<int> (), _array.Max<int> ());
  156. }
  157. [Test]
  158. public void Min ()
  159. {
  160. Assert.AreEqual (_src.Min<int> (), _array.Min<int> ());
  161. }
  162. [Test]
  163. public void OfType ()
  164. {
  165. Assert.AreEqual (_src.OfType<int> ().Count (), _array.OfType<int> ().Count ());
  166. }
  167. [Test]
  168. public void OrderBy ()
  169. {
  170. int [] arr1 = _array.OrderBy<int, int> ((n) => n * -1).ToArray ();
  171. int [] arr2 = _src.OrderBy<int, int> ((n) => n * -1).ToArray ();
  172. Assert.AreEqual (arr1, arr2);
  173. }
  174. [Test]
  175. public void OrderByDescending ()
  176. {
  177. int [] arr1 = _array.OrderBy<int, int> ((n) => n).ToArray ();
  178. int [] arr2 = _src.OrderBy<int, int> ((n) => n).ToArray ();
  179. Assert.AreEqual (arr1, arr2);
  180. }
  181. [Test]
  182. public void Reverse ()
  183. {
  184. int [] arr1 = _array.Reverse<int> ().Reverse ().ToArray ();
  185. int [] arr2 = _src.Reverse<int> ().Reverse ().ToArray ();
  186. Assert.AreEqual (arr1, arr2);
  187. }
  188. [Test]
  189. public void Select ()
  190. {
  191. int [] arr1 = _array.Select<int, int> ((n) => n - 1).ToArray ();
  192. int [] arr2 = _src.Select<int, int> ((n) => n - 1).ToArray ();
  193. Assert.AreEqual (arr1, arr2);
  194. }
  195. [Test]
  196. public void SelectMany ()
  197. {
  198. int [] arr1 = _array.SelectMany<int, int> ((n) => new int [] { n, n, n }).ToArray ();
  199. int [] arr2 = _src.SelectMany<int, int> ((n) => new int [] { n, n, n }).ToArray ();
  200. Assert.AreEqual (arr1, arr2);
  201. }
  202. [Test]
  203. public void SequenceEqual ()
  204. {
  205. Assert.IsTrue (_src.SequenceEqual<int> (_src));
  206. }
  207. [Test]
  208. public void Single ()
  209. {
  210. Assert.AreEqual (_src.Single (n => n == 10), 10);
  211. }
  212. [Test]
  213. public void SingleOrDefault ()
  214. {
  215. Assert.AreEqual (_src.SingleOrDefault (n => n == 10), 10);
  216. Assert.AreEqual (_src.SingleOrDefault (n => n == 11), 0);
  217. }
  218. [Test]
  219. public void Skip ()
  220. {
  221. int [] arr1 = _array.Skip<int> (5).ToArray ();
  222. int [] arr2 = _src.Skip<int> (5).ToArray ();
  223. Assert.AreEqual (arr1, arr2);
  224. }
  225. [Test]
  226. public void SkipWhile ()
  227. {
  228. int[] arr1 = _src.SkipWhile<int> ((n) => n < 6).ToArray();
  229. int[] arr2 = _src.Skip<int> (5).ToArray();
  230. Assert.AreEqual (arr1, arr2);
  231. }
  232. [Test]
  233. public void Sum ()
  234. {
  235. Assert.AreEqual (_src.Sum<int> ((n) => n), _array.Sum<int> ((n) => n));
  236. Assert.AreEqual (_src.Sum<int> ((n) => n + 1), _array.Sum<int> ((n) => n + 1));
  237. }
  238. [Test]
  239. public void Take ()
  240. {
  241. int [] arr1 = _array.Take<int> (3).ToArray ();
  242. int [] arr2 = _src.Take<int> (3).ToArray ();
  243. Assert.AreEqual (arr1, arr2);
  244. }
  245. [Test]
  246. public void TakeWhile ()
  247. {
  248. int [] arr1 = _array.TakeWhile<int> (n => n < 6).ToArray ();
  249. int [] arr2 = _src.TakeWhile<int> (n => n < 6).ToArray ();
  250. Assert.AreEqual (arr1, arr2);
  251. }
  252. [Test]
  253. public void Union ()
  254. {
  255. int [] arr1 = _src.ToArray ();
  256. int[] arr2 = _src.Union (_src).ToArray ();
  257. Assert.AreEqual (arr1, arr2);
  258. int [] arr = { 11,12,13};
  259. Assert.AreEqual (_src.Union (arr).ToArray (), _array.Union (arr).ToArray ());
  260. }
  261. [Test]
  262. public void Where ()
  263. {
  264. int[] oddArray1 = _array.Where<int> ((n) => (n % 2) == 1).ToArray();
  265. int [] oddArray2 = _src.Where<int> ((n) => (n % 2) == 1).ToArray ();
  266. Assert.AreEqual (oddArray1, oddArray2);
  267. }
  268. [Test]
  269. public void UserExtensionMethod ()
  270. {
  271. BindingFlags extensionFlags = BindingFlags.Static | BindingFlags.Public;
  272. MethodInfo method = (from m in typeof (Ext).GetMethods (extensionFlags)
  273. where (m.Name == "UserQueryableExt1" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
  274. select m).FirstOrDefault ().MakeGenericMethod (typeof (int));
  275. Expression<Func<int, int>> exp = i => i;
  276. Expression e = Expression.Equal (
  277. Expression.Constant ("UserEnumerableExt1"),
  278. Expression.Call (method, _src.Expression, Expression.Quote (exp)));
  279. Assert.AreEqual (_src.Provider.Execute<bool> (e), true, "UserQueryableExt1");
  280. method = (from m in typeof (Ext).GetMethods (extensionFlags)
  281. where (m.Name == "UserQueryableExt2" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
  282. select m).FirstOrDefault ().MakeGenericMethod (typeof (int));
  283. e = Expression.Equal (
  284. Expression.Constant ("UserEnumerableExt2"),
  285. Expression.Call (method, _src.Expression, Expression.Quote (exp)));
  286. Assert.AreEqual (_src.Provider.Execute<bool> (e), true, "UserQueryableExt2");
  287. }
  288. [Test]
  289. [ExpectedException (typeof (InvalidOperationException))]
  290. public void UserExtensionMethodNegative ()
  291. {
  292. BindingFlags extensionFlags = BindingFlags.Static | BindingFlags.Public;
  293. MethodInfo method = (from m in typeof (Ext).GetMethods (extensionFlags)
  294. where (m.Name == "UserQueryableExt3" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
  295. select m).FirstOrDefault ().MakeGenericMethod (typeof (int));
  296. Expression<Func<int, int>> exp = i => i;
  297. Expression e = Expression.Call (method, _src.Expression, Expression.Quote (exp), Expression.Constant (10));
  298. _src.Provider.Execute (e);
  299. }
  300. [Test]
  301. public void NonGenericMethod () {
  302. BindingFlags extensionFlags = BindingFlags.Static | BindingFlags.Public;
  303. MethodInfo method = (from m in typeof (Ext).GetMethods (extensionFlags)
  304. where (m.Name == "NonGenericMethod" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
  305. select m).FirstOrDefault ();
  306. Expression<Func<int, int>> exp = i => i;
  307. Expression e = Expression.Call (method, _src.Expression);
  308. Assert.AreEqual (_src.Provider.Execute (e), "EnumerableNonGenericMethod", "NonGenericMethod");
  309. }
  310. [Test]
  311. [ExpectedException(typeof(InvalidOperationException))]
  312. public void InstantiatedGenericMethod () {
  313. BindingFlags extensionFlags = BindingFlags.Static | BindingFlags.Public;
  314. MethodInfo method = (from m in typeof (Ext).GetMethods (extensionFlags)
  315. where (m.Name == "InstantiatedGenericMethod" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
  316. select m).FirstOrDefault ().MakeGenericMethod (typeof (int));
  317. Expression<Func<int, int>> exp = i => i;
  318. Expression e = Expression.Call (method, _src.Expression, Expression.Constant(0));
  319. _src.Provider.Execute (e);
  320. }
  321. }
  322. class CustomEqualityComparer : IEqualityComparer<int> {
  323. public bool Equals (int x, int y)
  324. {
  325. return true;
  326. }
  327. public int GetHashCode (int obj)
  328. {
  329. return 0;
  330. }
  331. }
  332. public static class Ext {
  333. public static string UserQueryableExt1<T> (this IQueryable<T> e, Expression<Func<int, int>> ex)
  334. {
  335. return "UserQueryableExt1";
  336. }
  337. public static string UserQueryableExt2<T> (this IQueryable<T> e, Expression<Func<int, int>> ex)
  338. {
  339. return "UserQueryableExt2";
  340. }
  341. public static string UserQueryableExt3<T> (this IQueryable<T> e, Expression<Func<int, int>> ex, int dummy)
  342. {
  343. return "UserQueryableExt3";
  344. }
  345. public static string UserQueryableExt1<T> (this IEnumerable<T> e, Expression<Func<int, int>> ex)
  346. {
  347. return "UserEnumerableExt1";
  348. }
  349. public static string UserQueryableExt2<T> (this IEnumerable<T> e, Func<int, int> ex)
  350. {
  351. return "UserEnumerableExt2";
  352. }
  353. public static string NonGenericMethod (this IQueryable<int> iq)
  354. {
  355. return "QueryableNonGenericMethod";
  356. }
  357. public static string NonGenericMethod (this IEnumerable<int> iq)
  358. {
  359. return "EnumerableNonGenericMethod";
  360. }
  361. public static string InstantiatedGenericMethod<T> (this IQueryable<int> iq, T t)
  362. {
  363. return "QueryableInstantiatedGenericMethod";
  364. }
  365. public static string InstantiatedGenericMethod (this IEnumerable<int> ie, int t)
  366. {
  367. return "EnumerableInstantiatedGenericMethod";
  368. }
  369. }
  370. }