EnumerableAsQueryableTest.cs 14 KB

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