2 // EnumerableAsQueryableTest.cs
5 // Roei Erez (roeie@mainsoft.com)
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections.Generic;
33 using NUnit.Framework;
34 using System.Linq.Expressions;
35 using System.Runtime.CompilerServices;
36 using System.Reflection;
37 using System.Collections;
39 namespace MonoTests.System.Linq {
42 public class EnumerableAsQueryableTest {
48 public void MyTestCleanup ()
50 _array = new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
51 _src = _array.AsQueryable<int> ();
55 public void NewQueryableExpression ()
57 var queryable = _array.AsQueryable ();
58 var expression = queryable.Expression;
60 Assert.AreEqual (ExpressionType.Constant, expression.NodeType);
62 var constant = (ConstantExpression) expression;
64 Assert.AreEqual (queryable, constant.Value);
68 public void Aggregate ()
70 Assert.AreEqual (_src.Aggregate<int> ((n, m) => n + m), _array.Aggregate<int> ((n, m) => n + m));
76 Assert.AreEqual (_src.All<int> ((n) => n < 11), _array.All<int> ((n) => n < 11));
77 Assert.AreEqual (_src.All<int> ((n) => n < 10), _array.All<int> ((n) => n < 10));
83 Assert.AreEqual (_src.Any<int> (i => i > 5), _array.Any<int> (i => i > 5));
87 public void Average ()
89 Assert.AreEqual (_src.Average<int> ((n) => 11), _array.Average<int> ((n) => 11));
95 Assert.AreEqual (_src.Concat<int> (_src).Count (), _array.Concat<int> (_src).Count ());
99 public void Contains ()
102 for (int i = 1; i < 20; ++i)
103 Assert.AreEqual (_src.Contains<int> (i), _array.Contains<int> (i));
109 Assert.AreEqual (_src.Count<int> (), _array.Count<int> ());
113 public void Distinct ()
115 Assert.AreEqual (_src.Distinct<int> ().Count (), _array.Distinct<int> ().Count ());
116 Assert.AreEqual (_src.Distinct<int> (new CustomEqualityComparer ()).Count (), _array.Distinct<int> (new CustomEqualityComparer ()).Count ());
120 public void ElementAt ()
122 for (int i = 0; i < 10; ++i)
123 Assert.AreEqual (_src.ElementAt<int> (i), _array.ElementAt<int> (i));
127 public void ElementAtOrDefault ()
129 for (int i = 0; i < 10; ++i)
130 Assert.AreEqual (_src.ElementAtOrDefault<int> (i), _array.ElementAtOrDefault<int> (i));
131 Assert.AreEqual (_src.ElementAtOrDefault<int> (100), _array.ElementAtOrDefault<int> (100));
135 public void Except ()
137 int [] except = { 1, 2, 3 };
138 Assert.AreEqual (_src.Except<int> (except.AsQueryable ()).Count (), _array.Except<int> (except).Count ());
144 Assert.AreEqual (_src.First<int> (), _array.First<int> ());
148 public void FirstOrDefault ()
150 Assert.AreEqual (_src.FirstOrDefault<int> ((n) => n > 5), _array.FirstOrDefault<int> ((n) => n > 5));
151 Assert.AreEqual (_src.FirstOrDefault<int> ((n) => n > 10), _array.FirstOrDefault<int> ((n) => n > 10));
155 public void GroupBy ()
157 IQueryable<IGrouping<bool, int>> grouping = _src.GroupBy<int, bool> ((n) => n > 5);
158 Assert.AreEqual (grouping.Count(), 2);
159 foreach (IGrouping<bool, int> group in grouping)
161 Assert.AreEqual(group.Count(), 5);
166 public void Intersect ()
168 int [] subset = { 1, 2, 3 };
169 int[] intersection = _src.Intersect<int> (subset.AsQueryable()).ToArray();
170 Assert.AreEqual (subset, intersection);
176 Assert.AreEqual (_src.Last<int> ((n) => n > 1), _array.Last<int> ((n) => n > 1));
180 public void LastOrDefault ()
182 Assert.AreEqual (_src.LastOrDefault<int> (), _array.LastOrDefault<int> ());
186 public void LongCount ()
188 Assert.AreEqual (_src.LongCount<int> (), _array.LongCount<int> ());
194 Assert.AreEqual (_src.Max<int> (), _array.Max<int> ());
200 Assert.AreEqual (_src.Min<int> (), _array.Min<int> ());
204 public void OfType ()
206 Assert.AreEqual (_src.OfType<int> ().Count (), _array.OfType<int> ().Count ());
210 public void OrderBy ()
212 int [] arr1 = _array.OrderBy<int, int> ((n) => n * -1).ToArray ();
213 int [] arr2 = _src.OrderBy<int, int> ((n) => n * -1).ToArray ();
214 Assert.AreEqual (arr1, arr2);
218 public void OrderByDescending ()
220 int [] arr1 = _array.OrderBy<int, int> ((n) => n).ToArray ();
221 int [] arr2 = _src.OrderBy<int, int> ((n) => n).ToArray ();
222 Assert.AreEqual (arr1, arr2);
226 public void Reverse ()
228 int [] arr1 = _array.Reverse<int> ().Reverse ().ToArray ();
229 int [] arr2 = _src.Reverse<int> ().Reverse ().ToArray ();
230 Assert.AreEqual (arr1, arr2);
234 public void Select ()
236 int [] arr1 = _array.Select<int, int> ((n) => n - 1).ToArray ();
237 int [] arr2 = _src.Select<int, int> ((n) => n - 1).ToArray ();
238 Assert.AreEqual (arr1, arr2);
242 public void SelectMany ()
244 int [] arr1 = _array.SelectMany<int, int> ((n) => new int [] { n, n, n }).ToArray ();
245 int [] arr2 = _src.SelectMany<int, int> ((n) => new int [] { n, n, n }).ToArray ();
246 Assert.AreEqual (arr1, arr2);
250 public void SequenceEqual ()
252 Assert.IsTrue (_src.SequenceEqual<int> (_src));
256 public void Single ()
258 Assert.AreEqual (_src.Single (n => n == 10), 10);
262 public void SingleOrDefault ()
264 Assert.AreEqual (_src.SingleOrDefault (n => n == 10), 10);
265 Assert.AreEqual (_src.SingleOrDefault (n => n == 11), 0);
271 int [] arr1 = _array.Skip<int> (5).ToArray ();
272 int [] arr2 = _src.Skip<int> (5).ToArray ();
273 Assert.AreEqual (arr1, arr2);
277 public void SkipWhile ()
279 int[] arr1 = _src.SkipWhile<int> ((n) => n < 6).ToArray();
280 int[] arr2 = _src.Skip<int> (5).ToArray();
281 Assert.AreEqual (arr1, arr2);
287 Assert.AreEqual (_src.Sum<int> ((n) => n), _array.Sum<int> ((n) => n));
288 Assert.AreEqual (_src.Sum<int> ((n) => n + 1), _array.Sum<int> ((n) => n + 1));
294 int [] arr1 = _array.Take<int> (3).ToArray ();
295 int [] arr2 = _src.Take<int> (3).ToArray ();
296 Assert.AreEqual (arr1, arr2);
300 public void TakeWhile ()
302 int [] arr1 = _array.TakeWhile<int> (n => n < 6).ToArray ();
303 int [] arr2 = _src.TakeWhile<int> (n => n < 6).ToArray ();
304 Assert.AreEqual (arr1, arr2);
310 int [] arr1 = _src.ToArray ();
311 int[] arr2 = _src.Union (_src).ToArray ();
312 Assert.AreEqual (arr1, arr2);
314 int [] arr = { 11,12,13};
315 Assert.AreEqual (_src.Union (arr).ToArray (), _array.Union (arr).ToArray ());
321 int[] oddArray1 = _array.Where<int> ((n) => (n % 2) == 1).ToArray();
322 int [] oddArray2 = _src.Where<int> ((n) => (n % 2) == 1).ToArray ();
323 Assert.AreEqual (oddArray1, oddArray2);
327 [Category ("NotWorkingInterpreter")]
328 public void UserExtensionMethod ()
330 BindingFlags extensionFlags = BindingFlags.Static | BindingFlags.Public;
331 MethodInfo method = (from m in typeof (Ext).GetMethods (extensionFlags)
332 where (m.Name == "UserQueryableExt1" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
333 select m).FirstOrDefault ().MakeGenericMethod (typeof (int));
334 Expression<Func<int, int>> exp = i => i;
335 Expression e = Expression.Equal (
336 Expression.Constant ("UserEnumerableExt1"),
337 Expression.Call (method, _src.Expression, Expression.Quote (exp)));
338 Assert.AreEqual (_src.Provider.Execute<bool> (e), true, "UserQueryableExt1");
340 method = (from m in typeof (Ext).GetMethods (extensionFlags)
341 where (m.Name == "UserQueryableExt2" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
342 select m).FirstOrDefault ().MakeGenericMethod (typeof (int));
343 e = Expression.Equal (
344 Expression.Constant ("UserEnumerableExt2"),
345 Expression.Call (method, _src.Expression, Expression.Quote (exp)));
346 Assert.AreEqual (_src.Provider.Execute<bool> (e), true, "UserQueryableExt2");
350 [ExpectedException (typeof (InvalidOperationException))]
351 public void UserExtensionMethodNegative ()
353 BindingFlags extensionFlags = BindingFlags.Static | BindingFlags.Public;
354 MethodInfo method = (from m in typeof (Ext).GetMethods (extensionFlags)
355 where (m.Name == "UserQueryableExt3" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
356 select m).FirstOrDefault ().MakeGenericMethod (typeof (int));
357 Expression<Func<int, int>> exp = i => i;
358 Expression e = Expression.Call (method, _src.Expression, Expression.Quote (exp), Expression.Constant (10));
359 _src.Provider.Execute (e);
363 public void NonGenericMethod () {
364 BindingFlags extensionFlags = BindingFlags.Static | BindingFlags.Public;
365 MethodInfo method = (from m in typeof (Ext).GetMethods (extensionFlags)
366 where (m.Name == "NonGenericMethod" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
367 select m).FirstOrDefault ();
369 Expression e = Expression.Call (method, _src.Expression);
370 Assert.AreEqual (_src.Provider.Execute (e), "EnumerableNonGenericMethod", "NonGenericMethod");
374 [ExpectedException(typeof(InvalidOperationException))]
375 public void InstantiatedGenericMethod () {
376 BindingFlags extensionFlags = BindingFlags.Static | BindingFlags.Public;
377 MethodInfo method = (from m in typeof (Ext).GetMethods (extensionFlags)
378 where (m.Name == "InstantiatedGenericMethod" && m.GetParameters () [0].ParameterType.GetGenericTypeDefinition () == typeof (IQueryable<>))
379 select m).FirstOrDefault ().MakeGenericMethod (typeof (int));
381 Expression e = Expression.Call (method, _src.Expression, Expression.Constant(0));
382 _src.Provider.Execute (e);
386 [ExpectedException (typeof (ArgumentNullException))]
387 public void NullEnumerable ()
389 IEnumerable<int> a = null;
394 [ExpectedException (typeof (ArgumentException))]
395 public void NonGenericEnumerable1 ()
397 new MyEnum ().AsQueryable ();
401 public void NonGenericEnumerable2 ()
403 IEnumerable<int> nonGen = new int[] { 1, 2, 3 };
404 Assert.IsTrue (nonGen.AsQueryable () is IQueryable<int>);
407 class Bar<T1, T2> : IEnumerable<T2> {
409 public IEnumerator<T2> GetEnumerator ()
414 IEnumerator IEnumerable.GetEnumerator ()
416 return GetEnumerator ();
421 public void NonGenericAsQueryableInstantiateProperQueryable ()
423 IEnumerable bar = new Bar<int, string> ();
424 IQueryable queryable = bar.AsQueryable ();
426 Assert.IsTrue (queryable is IQueryable<string>);
430 class MyEnum : IEnumerable
432 public IEnumerator GetEnumerator ()
434 throw new NotImplementedException ();
438 class CustomEqualityComparer : IEqualityComparer<int> {
440 public bool Equals (int x, int y)
445 public int GetHashCode (int obj)
451 public static class Ext {
453 public static string UserQueryableExt1<T> (this IQueryable<T> e, Expression<Func<int, int>> ex)
455 return "UserQueryableExt1";
458 public static string UserQueryableExt2<T> (this IQueryable<T> e, Expression<Func<int, int>> ex)
460 return "UserQueryableExt2";
463 public static string UserQueryableExt3<T> (this IQueryable<T> e, Expression<Func<int, int>> ex, int dummy)
465 return "UserQueryableExt3";
468 public static string UserQueryableExt1<T> (this IEnumerable<T> e, Expression<Func<int, int>> ex)
470 return "UserEnumerableExt1";
473 public static string UserQueryableExt2<T> (this IEnumerable<T> e, Func<int, int> ex)
475 return "UserEnumerableExt2";
478 public static string NonGenericMethod (this IQueryable<int> iq)
480 return "QueryableNonGenericMethod";
483 public static string NonGenericMethod (this IEnumerable<int> iq)
485 return "EnumerableNonGenericMethod";
488 public static string InstantiatedGenericMethod<T> (this IQueryable<int> iq, T t)
490 return "QueryableInstantiatedGenericMethod";
493 public static string InstantiatedGenericMethod (this IEnumerable<int> ie, int t)
495 return "EnumerableInstantiatedGenericMethod";