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. [Category("SRE")]
  40. public class EnumerableAsQueryableTest {
  41. int [] _array;
  42. IQueryable<int> _src;
  43. [SetUp]
  44. public void MyTestCleanup ()
  45. {
  46. _array = new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  47. _src = _array.AsQueryable<int> ();
  48. }
  49. [Test]
  50. public void NewQueryableExpression ()
  51. {
  52. var queryable = _array.AsQueryable ();
  53. var expression = queryable.Expression;
  54. Assert.AreEqual (ExpressionType.Constant, expression.NodeType);
  55. var constant = (ConstantExpression) expression;
  56. Assert.AreEqual (queryable, constant.Value);
  57. }
  58. [Test]
  59. public void Aggregate ()
  60. {
  61. Assert.AreEqual (_src.Aggregate<int> ((n, m) => n + m), _array.Aggregate<int> ((n, m) => n + m));
  62. }
  63. [Test]
  64. public void All ()
  65. {
  66. Assert.AreEqual (_src.All<int> ((n) => n < 11), _array.All<int> ((n) => n < 11));
  67. Assert.AreEqual (_src.All<int> ((n) => n < 10), _array.All<int> ((n) => n < 10));
  68. }
  69. [Test]
  70. public void Any ()
  71. {
  72. Assert.AreEqual (_src.Any<int> (i => i > 5), _array.Any<int> (i => i > 5));
  73. }
  74. [Test]
  75. public void Average ()
  76. {
  77. Assert.AreEqual (_src.Average<int> ((n) => 11), _array.Average<int> ((n) => 11));
  78. }
  79. [Test]
  80. public void Concat ()
  81. {
  82. Assert.AreEqual (_src.Concat<int> (_src).Count (), _array.Concat<int> (_src).Count ());
  83. }
  84. [Test]
  85. public void Contains ()
  86. {
  87. for (int i = 1; i < 20; ++i)
  88. Assert.AreEqual (_src.Contains<int> (i), _array.Contains<int> (i));
  89. }
  90. [Test]
  91. public void Count ()
  92. {
  93. Assert.AreEqual (_src.Count<int> (), _array.Count<int> ());
  94. }
  95. [Test]
  96. public void Distinct ()
  97. {
  98. Assert.AreEqual (_src.Distinct<int> ().Count (), _array.Distinct<int> ().Count ());
  99. Assert.AreEqual (_src.Distinct<int> (new CustomEqualityComparer ()).Count (), _array.Distinct<int> (new CustomEqualityComparer ()).Count ());
  100. }
  101. [Test]
  102. public void ElementAt ()
  103. {
  104. for (int i = 0; i < 10; ++i)
  105. Assert.AreEqual (_src.ElementAt<int> (i), _array.ElementAt<int> (i));
  106. }
  107. [Test]
  108. public void ElementAtOrDefault ()
  109. {
  110. for (int i = 0; i < 10; ++i)
  111. Assert.AreEqual (_src.ElementAtOrDefault<int> (i), _array.ElementAtOrDefault<int> (i));
  112. Assert.AreEqual (_src.ElementAtOrDefault<int> (100), _array.ElementAtOrDefault<int> (100));
  113. }
  114. [Test]
  115. public void Except ()
  116. {
  117. int [] except = { 1, 2, 3 };
  118. Assert.AreEqual (_src.Except<int> (except.AsQueryable ()).Count (), _array.Except<int> (except).Count ());
  119. }
  120. [Test]
  121. public void First ()
  122. {
  123. Assert.AreEqual (_src.First<int> (), _array.First<int> ());
  124. }
  125. [Test]
  126. public void FirstOrDefault ()
  127. {
  128. Assert.AreEqual (_src.FirstOrDefault<int> ((n) => n > 5), _array.FirstOrDefault<int> ((n) => n > 5));
  129. Assert.AreEqual (_src.FirstOrDefault<int> ((n) => n > 10), _array.FirstOrDefault<int> ((n) => n > 10));
  130. }
  131. [Test]
  132. public void GroupBy ()
  133. {
  134. IQueryable<IGrouping<bool, int>> grouping = _src.GroupBy<int, bool> ((n) => n > 5);
  135. Assert.AreEqual (grouping.Count(), 2);
  136. foreach (IGrouping<bool, int> group in grouping)
  137. {
  138. Assert.AreEqual(group.Count(), 5);
  139. }
  140. }
  141. [Test]
  142. public void Intersect ()
  143. {
  144. int [] subset = { 1, 2, 3 };
  145. int[] intersection = _src.Intersect<int> (subset.AsQueryable()).ToArray();
  146. Assert.AreEqual (subset, intersection);
  147. }
  148. [Test]
  149. public void Last ()
  150. {
  151. Assert.AreEqual (_src.Last<int> ((n) => n > 1), _array.Last<int> ((n) => n > 1));
  152. }
  153. [Test]
  154. public void LastOrDefault ()
  155. {
  156. Assert.AreEqual (_src.LastOrDefault<int> (), _array.LastOrDefault<int> ());
  157. }
  158. [Test]
  159. public void LongCount ()
  160. {
  161. Assert.AreEqual (_src.LongCount<int> (), _array.LongCount<int> ());
  162. }
  163. [Test]
  164. public void Max ()
  165. {
  166. Assert.AreEqual (_src.Max<int> (), _array.Max<int> ());
  167. }
  168. [Test]
  169. public void Min ()
  170. {
  171. Assert.AreEqual (_src.Min<int> (), _array.Min<int> ());
  172. }
  173. [Test]
  174. public void OfType ()
  175. {
  176. Assert.AreEqual (_src.OfType<int> ().Count (), _array.OfType<int> ().Count ());
  177. }
  178. [Test]
  179. public void OrderBy ()
  180. {
  181. int [] arr1 = _array.OrderBy<int, int> ((n) => n * -1).ToArray ();
  182. int [] arr2 = _src.OrderBy<int, int> ((n) => n * -1).ToArray ();
  183. Assert.AreEqual (arr1, arr2);
  184. }
  185. [Test]
  186. public void OrderByDescending ()
  187. {
  188. int [] arr1 = _array.OrderBy<int, int> ((n) => n).ToArray ();
  189. int [] arr2 = _src.OrderBy<int, int> ((n) => n).ToArray ();
  190. Assert.AreEqual (arr1, arr2);
  191. }
  192. [Test]
  193. public void Reverse ()
  194. {
  195. int [] arr1 = _array.Reverse<int> ().Reverse ().ToArray ();
  196. int [] arr2 = _src.Reverse<int> ().Reverse ().ToArray ();
  197. Assert.AreEqual (arr1, arr2);
  198. }
  199. [Test]
  200. public void Select ()
  201. {
  202. int [] arr1 = _array.Select<int, int> ((n) => n - 1).ToArray ();
  203. int [] arr2 = _src.Select<int, int> ((n) => n - 1).ToArray ();
  204. Assert.AreEqual (arr1, arr2);
  205. }
  206. [Test]
  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. }