EnumerableAsQueryableTest.cs 14 KB

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