EnumerableAsQueryableTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 NewQueryableExpression ()
  49. {
  50. var queryable = _array.AsQueryable ();
  51. var expression = queryable.Expression;
  52. Assert.AreEqual (ExpressionType.Constant, expression.NodeType);
  53. var constant = (ConstantExpression) expression;
  54. Assert.AreEqual (queryable, constant.Value);
  55. }
  56. [Test]
  57. public void Aggregate ()
  58. {
  59. Assert.AreEqual (_src.Aggregate<int> ((n, m) => n + m), _array.Aggregate<int> ((n, m) => n + m));
  60. }
  61. [Test]
  62. public void All ()
  63. {
  64. Assert.AreEqual (_src.All<int> ((n) => n < 11), _array.All<int> ((n) => n < 11));
  65. Assert.AreEqual (_src.All<int> ((n) => n < 10), _array.All<int> ((n) => n < 10));
  66. }
  67. [Test]
  68. public void Any ()
  69. {
  70. Assert.AreEqual (_src.Any<int> (i => i > 5), _array.Any<int> (i => i > 5));
  71. }
  72. [Test]
  73. public void Average ()
  74. {
  75. Assert.AreEqual (_src.Average<int> ((n) => 11), _array.Average<int> ((n) => 11));
  76. }
  77. [Test]
  78. public void Concat ()
  79. {
  80. Assert.AreEqual (_src.Concat<int> (_src).Count (), _array.Concat<int> (_src).Count ());
  81. }
  82. [Test]
  83. public void Contains ()
  84. {
  85. for (int i = 1; i < 20; ++i)
  86. Assert.AreEqual (_src.Contains<int> (i), _array.Contains<int> (i));
  87. }
  88. [Test]
  89. public void Count ()
  90. {
  91. Assert.AreEqual (_src.Count<int> (), _array.Count<int> ());
  92. }
  93. [Test]
  94. public void Distinct ()
  95. {
  96. Assert.AreEqual (_src.Distinct<int> ().Count (), _array.Distinct<int> ().Count ());
  97. Assert.AreEqual (_src.Distinct<int> (new CustomEqualityComparer ()).Count (), _array.Distinct<int> (new CustomEqualityComparer ()).Count ());
  98. }
  99. [Test]
  100. public void ElementAt ()
  101. {
  102. for (int i = 0; i < 10; ++i)
  103. Assert.AreEqual (_src.ElementAt<int> (i), _array.ElementAt<int> (i));
  104. }
  105. [Test]
  106. public void ElementAtOrDefault ()
  107. {
  108. for (int i = 0; i < 10; ++i)
  109. Assert.AreEqual (_src.ElementAtOrDefault<int> (i), _array.ElementAtOrDefault<int> (i));
  110. Assert.AreEqual (_src.ElementAtOrDefault<int> (100), _array.ElementAtOrDefault<int> (100));
  111. }
  112. [Test]
  113. public void Except ()
  114. {
  115. int [] except = { 1, 2, 3 };
  116. Assert.AreEqual (_src.Except<int> (except.AsQueryable ()).Count (), _array.Except<int> (except).Count ());
  117. }
  118. [Test]
  119. public void First ()
  120. {
  121. Assert.AreEqual (_src.First<int> (), _array.First<int> ());
  122. }
  123. [Test]
  124. public void FirstOrDefault ()
  125. {
  126. Assert.AreEqual (_src.FirstOrDefault<int> ((n) => n > 5), _array.FirstOrDefault<int> ((n) => n > 5));
  127. Assert.AreEqual (_src.FirstOrDefault<int> ((n) => n > 10), _array.FirstOrDefault<int> ((n) => n > 10));
  128. }
  129. [Test]
  130. public void GroupBy ()
  131. {
  132. IQueryable<IGrouping<bool, int>> grouping = _src.GroupBy<int, bool> ((n) => n > 5);
  133. Assert.AreEqual (grouping.Count(), 2);
  134. foreach (IGrouping<bool, int> group in grouping)
  135. {
  136. Assert.AreEqual(group.Count(), 5);
  137. }
  138. }
  139. [Test]
  140. public void Intersect ()
  141. {
  142. int [] subset = { 1, 2, 3 };
  143. int[] intersection = _src.Intersect<int> (subset.AsQueryable()).ToArray();
  144. Assert.AreEqual (subset, intersection);
  145. }
  146. [Test]
  147. public void Last ()
  148. {
  149. Assert.AreEqual (_src.Last<int> ((n) => n > 1), _array.Last<int> ((n) => n > 1));
  150. }
  151. [Test]
  152. public void LastOrDefault ()
  153. {
  154. Assert.AreEqual (_src.LastOrDefault<int> (), _array.LastOrDefault<int> ());
  155. }
  156. [Test]
  157. public void LongCount ()
  158. {
  159. Assert.AreEqual (_src.LongCount<int> (), _array.LongCount<int> ());
  160. }
  161. [Test]
  162. public void Max ()
  163. {
  164. Assert.AreEqual (_src.Max<int> (), _array.Max<int> ());
  165. }
  166. [Test]
  167. public void Min ()
  168. {
  169. Assert.AreEqual (_src.Min<int> (), _array.Min<int> ());
  170. }
  171. [Test]
  172. public void OfType ()
  173. {
  174. Assert.AreEqual (_src.OfType<int> ().Count (), _array.OfType<int> ().Count ());
  175. }
  176. [Test]
  177. public void OrderBy ()
  178. {
  179. int [] arr1 = _array.OrderBy<int, int> ((n) => n * -1).ToArray ();
  180. int [] arr2 = _src.OrderBy<int, int> ((n) => n * -1).ToArray ();
  181. Assert.AreEqual (arr1, arr2);
  182. }
  183. [Test]
  184. public void OrderByDescending ()
  185. {
  186. int [] arr1 = _array.OrderBy<int, int> ((n) => n).ToArray ();
  187. int [] arr2 = _src.OrderBy<int, int> ((n) => n).ToArray ();
  188. Assert.AreEqual (arr1, arr2);
  189. }
  190. [Test]
  191. public void Reverse ()
  192. {
  193. int [] arr1 = _array.Reverse<int> ().Reverse ().ToArray ();
  194. int [] arr2 = _src.Reverse<int> ().Reverse ().ToArray ();
  195. Assert.AreEqual (arr1, arr2);
  196. }
  197. [Test]
  198. public void Select ()
  199. {
  200. int [] arr1 = _array.Select<int, int> ((n) => n - 1).ToArray ();
  201. int [] arr2 = _src.Select<int, int> ((n) => n - 1).ToArray ();
  202. Assert.AreEqual (arr1, arr2);
  203. }
  204. [Test]
  205. public void SelectMany ()
  206. {
  207. int [] arr1 = _array.SelectMany<int, int> ((n) => new int [] { n, n, n }).ToArray ();
  208. int [] arr2 = _src.SelectMany<int, int> ((n) => new int [] { n, n, n }).ToArray ();
  209. Assert.AreEqual (arr1, arr2);
  210. }
  211. [Test]
  212. public void SequenceEqual ()
  213. {
  214. Assert.IsTrue (_src.SequenceEqual<int> (_src));
  215. }
  216. [Test]
  217. public void Single ()
  218. {
  219. Assert.AreEqual (_src.Single (n => n == 10), 10);
  220. }
  221. [Test]
  222. public void SingleOrDefault ()
  223. {
  224. Assert.AreEqual (_src.SingleOrDefault (n => n == 10), 10);
  225. Assert.AreEqual (_src.SingleOrDefault (n => n == 11), 0);
  226. }
  227. [Test]
  228. public void Skip ()
  229. {
  230. int [] arr1 = _array.Skip<int> (5).ToArray ();
  231. int [] arr2 = _src.Skip<int> (5).ToArray ();
  232. Assert.AreEqual (arr1, arr2);
  233. }
  234. [Test]
  235. public void SkipWhile ()
  236. {
  237. int[] arr1 = _src.SkipWhile<int> ((n) => n < 6).ToArray();
  238. int[] arr2 = _src.Skip<int> (5).ToArray();
  239. Assert.AreEqual (arr1, arr2);
  240. }
  241. [Test]
  242. public void Sum ()
  243. {
  244. Assert.AreEqual (_src.Sum<int> ((n) => n), _array.Sum<int> ((n) => n));
  245. Assert.AreEqual (_src.Sum<int> ((n) => n + 1), _array.Sum<int> ((n) => n + 1));
  246. }
  247. [Test]
  248. public void Take ()
  249. {
  250. int [] arr1 = _array.Take<int> (3).ToArray ();
  251. int [] arr2 = _src.Take<int> (3).ToArray ();
  252. Assert.AreEqual (arr1, arr2);
  253. }
  254. [Test]
  255. public void TakeWhile ()
  256. {
  257. int [] arr1 = _array.TakeWhile<int> (n => n < 6).ToArray ();
  258. int [] arr2 = _src.TakeWhile<int> (n => n < 6).ToArray ();
  259. Assert.AreEqual (arr1, arr2);
  260. }
  261. [Test]
  262. public void Union ()
  263. {
  264. int [] arr1 = _src.ToArray ();
  265. int[] arr2 = _src.Union (_src).ToArray ();
  266. Assert.AreEqual (arr1, arr2);
  267. int [] arr = { 11,12,13};
  268. Assert.AreEqual (_src.Union (arr).ToArray (), _array.Union (arr).ToArray ());
  269. }
  270. [Test]
  271. public void Where ()
  272. {
  273. int[] oddArray1 = _array.Where<int> ((n) => (n % 2) == 1).ToArray();
  274. int [] oddArray2 = _src.Where<int> ((n) => (n % 2) == 1).ToArray ();
  275. Assert.AreEqual (oddArray1, oddArray2);
  276. }
  277. [Test]
  278. public void UserExtensionMethod ()
  279. {
  280. BindingFlags extensionFlags = BindingFlags.Static | BindingFlags.Public;
  281. MethodInfo method = (from m in typeof (Ext).GetMethods (extensionFlags)
  282. where (m.Name == "UserQueryableExt1" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
  283. select m).FirstOrDefault ().MakeGenericMethod (typeof (int));
  284. Expression<Func<int, int>> exp = i => i;
  285. Expression e = Expression.Equal (
  286. Expression.Constant ("UserEnumerableExt1"),
  287. Expression.Call (method, _src.Expression, Expression.Quote (exp)));
  288. Assert.AreEqual (_src.Provider.Execute<bool> (e), true, "UserQueryableExt1");
  289. method = (from m in typeof (Ext).GetMethods (extensionFlags)
  290. where (m.Name == "UserQueryableExt2" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
  291. select m).FirstOrDefault ().MakeGenericMethod (typeof (int));
  292. e = Expression.Equal (
  293. Expression.Constant ("UserEnumerableExt2"),
  294. Expression.Call (method, _src.Expression, Expression.Quote (exp)));
  295. Assert.AreEqual (_src.Provider.Execute<bool> (e), true, "UserQueryableExt2");
  296. }
  297. [Test]
  298. [ExpectedException (typeof (InvalidOperationException))]
  299. public void UserExtensionMethodNegative ()
  300. {
  301. BindingFlags extensionFlags = BindingFlags.Static | BindingFlags.Public;
  302. MethodInfo method = (from m in typeof (Ext).GetMethods (extensionFlags)
  303. where (m.Name == "UserQueryableExt3" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
  304. select m).FirstOrDefault ().MakeGenericMethod (typeof (int));
  305. Expression<Func<int, int>> exp = i => i;
  306. Expression e = Expression.Call (method, _src.Expression, Expression.Quote (exp), Expression.Constant (10));
  307. _src.Provider.Execute (e);
  308. }
  309. [Test]
  310. public void NonGenericMethod () {
  311. BindingFlags extensionFlags = BindingFlags.Static | BindingFlags.Public;
  312. MethodInfo method = (from m in typeof (Ext).GetMethods (extensionFlags)
  313. where (m.Name == "NonGenericMethod" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
  314. select m).FirstOrDefault ();
  315. Expression e = Expression.Call (method, _src.Expression);
  316. Assert.AreEqual (_src.Provider.Execute (e), "EnumerableNonGenericMethod", "NonGenericMethod");
  317. }
  318. [Test]
  319. [ExpectedException(typeof(InvalidOperationException))]
  320. public void InstantiatedGenericMethod () {
  321. BindingFlags extensionFlags = BindingFlags.Static | BindingFlags.Public;
  322. MethodInfo method = (from m in typeof (Ext).GetMethods (extensionFlags)
  323. where (m.Name == "InstantiatedGenericMethod" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
  324. select m).FirstOrDefault ().MakeGenericMethod (typeof (int));
  325. Expression e = Expression.Call (method, _src.Expression, Expression.Constant(0));
  326. _src.Provider.Execute (e);
  327. }
  328. }
  329. class CustomEqualityComparer : IEqualityComparer<int> {
  330. public bool Equals (int x, int y)
  331. {
  332. return true;
  333. }
  334. public int GetHashCode (int obj)
  335. {
  336. return 0;
  337. }
  338. }
  339. public static class Ext {
  340. public static string UserQueryableExt1<T> (this IQueryable<T> e, Expression<Func<int, int>> ex)
  341. {
  342. return "UserQueryableExt1";
  343. }
  344. public static string UserQueryableExt2<T> (this IQueryable<T> e, Expression<Func<int, int>> ex)
  345. {
  346. return "UserQueryableExt2";
  347. }
  348. public static string UserQueryableExt3<T> (this IQueryable<T> e, Expression<Func<int, int>> ex, int dummy)
  349. {
  350. return "UserQueryableExt3";
  351. }
  352. public static string UserQueryableExt1<T> (this IEnumerable<T> e, Expression<Func<int, int>> ex)
  353. {
  354. return "UserEnumerableExt1";
  355. }
  356. public static string UserQueryableExt2<T> (this IEnumerable<T> e, Func<int, int> ex)
  357. {
  358. return "UserEnumerableExt2";
  359. }
  360. public static string NonGenericMethod (this IQueryable<int> iq)
  361. {
  362. return "QueryableNonGenericMethod";
  363. }
  364. public static string NonGenericMethod (this IEnumerable<int> iq)
  365. {
  366. return "EnumerableNonGenericMethod";
  367. }
  368. public static string InstantiatedGenericMethod<T> (this IQueryable<int> iq, T t)
  369. {
  370. return "QueryableInstantiatedGenericMethod";
  371. }
  372. public static string InstantiatedGenericMethod (this IEnumerable<int> ie, int t)
  373. {
  374. return "EnumerableInstantiatedGenericMethod";
  375. }
  376. }
  377. }