2 // EnumerableMoreTest.cs
5 // Andreas Noever <andreas.noever@gmail.com>
7 // (C) 2007 Andreas Noever
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;
31 using System.Collections.Generic;
33 using NUnit.Framework;
36 namespace MonoTests.System.Linq {
39 public class EnumerableMoreTest {
41 class BigEnumerable : IEnumerable<int> {
42 public readonly ulong Count;
43 public BigEnumerable (ulong Count)
49 #region IEnumerable<int> Members
51 public IEnumerator<int> GetEnumerator ()
53 return new BigEnumerator (this);
58 #region IEnumerable Members
60 IEnumerator IEnumerable.GetEnumerator ()
62 throw new NotImplementedException ();
68 class BigEnumerator : IEnumerator<int> {
70 private ulong current;
72 public BigEnumerator (BigEnumerable parent)
82 public void Dispose ()
86 object IEnumerator.Current
88 get { throw new NotImplementedException (); }
91 public bool MoveNext ()
93 if (current == Parent.Count)
101 throw new NotImplementedException ();
106 public static void AssertException<T> (Action action) where T : Exception
114 Assert.Fail ("Expected: " + typeof (T).Name);
117 static void AssertAreSame<K, V> (K expectedKey, IEnumerable<V> expectedValues, IGrouping<K, V> actual)
119 if (expectedValues == null) {
120 Assert.IsNull (actual);
124 Assert.IsNotNull (actual);
126 Assert.AreEqual (expectedKey, actual.Key);
128 var ee = expectedValues.GetEnumerator ();
129 var ea = actual.GetEnumerator ();
131 while (ee.MoveNext ()) {
132 Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
133 Assert.AreEqual (ee.Current, ea.Current);
137 Assert.Fail ("Unexpected element: " + ee.Current);
140 static void AssertAreSame<K, V> (IDictionary<K, IEnumerable<V>> expected, IEnumerable<IGrouping<K, V>> actual)
142 if (expected == null) {
143 Assert.IsNull (actual);
147 Assert.IsNotNull (actual);
149 var ee = expected.GetEnumerator ();
150 var ea = actual.GetEnumerator ();
152 while (ee.MoveNext ()) {
153 Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + "' expected.");
154 AssertAreSame (ee.Current.Key, ee.Current.Value, ea.Current);
158 Assert.Fail ("Unexpected element: " + ee.Current.Key);
161 static void AssertAreSame<K, V> (IDictionary<K, IEnumerable<V>> expected, ILookup<K, V> actual)
163 if (expected == null) {
164 Assert.IsNull (actual);
168 Assert.IsNotNull (actual);
170 var ee = expected.GetEnumerator ();
171 var ea = actual.GetEnumerator ();
173 while (ee.MoveNext ()) {
174 Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + "' expected.");
175 AssertAreSame (ee.Current.Key, ee.Current.Value, ea.Current);
179 Assert.Fail ("Unexpected element: " + ee.Current.Key);
182 static void AssertAreSame<K, V> (IDictionary<K, V> expected, IDictionary<K, V> actual)
184 if (expected == null) {
185 Assert.IsNull (actual);
189 Assert.IsNotNull (actual);
191 var ee = expected.GetEnumerator ();
192 var ea = actual.GetEnumerator ();
194 while (ee.MoveNext ()) {
195 Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + ", " + ee.Current.Value + "' expected.");
196 Assert.AreEqual (ee.Current.Key, ea.Current.Key);
197 Assert.AreEqual (ee.Current.Value, ea.Current.Value);
201 Assert.Fail ("Unexpected element: " + ee.Current.Key + ", " + ee.Current.Value);
204 static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
206 if (expected == null) {
207 Assert.IsNull (actual);
211 Assert.IsNotNull (actual);
213 IEnumerator<T> ee = expected.GetEnumerator ();
214 IEnumerator<T> ea = actual.GetEnumerator ();
216 while (ee.MoveNext ()) {
217 Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
218 Assert.AreEqual (ee.Current, ea.Current);
222 Assert.Fail ("Unexpected element: " + ea.Current);
226 public void FirstArgumentNullTest ()
228 string [] data = { "2", "1", "5", "3", "4" };
232 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).First (); });
234 // First<TSource> (Func<TSource, bool>)
235 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).First ((x => true)); });
236 AssertException<ArgumentNullException> (delegate () { data.First ((Func<string, bool>) null); });
240 public void FirstTest ()
242 int [] data = { 2, 1, 5, 3, 4 };
246 Assert.AreEqual (2, data.First ());
247 AssertException<InvalidOperationException> (delegate () { empty.First (); });
249 // First<TSource> (Func<TSource, bool>)
250 Assert.AreEqual (5, data.First (x => x == 5));
251 AssertException<InvalidOperationException> (delegate () { empty.First (x => x == 5); });
252 AssertException<InvalidOperationException> (delegate () { data.First (x => x == 6); });
256 public void FirstOrDefaultArgumentNullTest ()
258 string [] data = { "2", "1", "5", "3", "4" };
261 // FirstOrDefault<TSource> ()
262 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).FirstOrDefault (); });
264 // FirstOrDefault<TSource> (Func<string, bool>)
265 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).FirstOrDefault ((x => true)); });
266 AssertException<ArgumentNullException> (delegate () { data.FirstOrDefault ((Func<string, bool>) null); });
270 public void FirstOrDefaultTest ()
272 int [] data = { 2, 1, 5, 3, 4 };
276 // FirstOrDefault<TSource> ()
277 Assert.AreEqual (2, data.FirstOrDefault ());
278 Assert.AreEqual (0, empty.FirstOrDefault ());
280 // FirstOrDefault<TSource> (Func<TSource, bool>)
281 Assert.AreEqual (5, data.FirstOrDefault (x => x == 5));
282 Assert.AreEqual (0, empty.FirstOrDefault (x => x == 5));
283 Assert.AreEqual (0, data.FirstOrDefault (x => x == 6));
288 public void LastArgumentNullTest ()
290 string [] data = { "2", "1", "5", "3", "4" };
294 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Last (); });
296 // Last<TSource> (Func<TSource, bool>)
297 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Last (x => true); });
298 AssertException<ArgumentNullException> (delegate () { data.Last ((Func<string, bool>) null); });
302 public void LastTest ()
304 int [] data = { 2, 1, 1, 3, 4, 5 };
308 Assert.AreEqual (5, data.Last ());
309 AssertException<InvalidOperationException> (delegate () { empty.Last (); });
311 // Last<TSource> (Func<TSource, bool>)
312 Assert.AreEqual (4, data.Last (x => x < 5));
313 AssertException<InvalidOperationException> (delegate () { empty.Last (x => x == 5); });
314 AssertException<InvalidOperationException> (delegate () { data.Last (x => x == 6); });
318 public void LastOrDefaultArgumentNullTest ()
320 string [] data = { "2", "1", "5", "3", "4" };
323 // LastOrDefault<TSource> ()
324 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LastOrDefault (); });
326 // LastOrDefault<TSource> (Func<TSource, bool>)
327 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LastOrDefault (x => true); });
328 AssertException<ArgumentNullException> (delegate () { data.LastOrDefault ((Func<string, bool>) null); });
332 public void LastOrDefaultTest ()
334 int [] data = { 2, 1, 5, 3, 4 };
338 // LastOrDefault<TSource> ()
339 Assert.AreEqual (4, data.LastOrDefault ());
340 Assert.AreEqual (0, empty.LastOrDefault ());
342 // LastOrDefault<TSource> (Func<TSource, bool>)
343 Assert.AreEqual (3, data.LastOrDefault (x => x < 4));
344 Assert.AreEqual (0, empty.LastOrDefault (x => x == 5));
345 Assert.AreEqual (0, data.LastOrDefault (x => x == 6));
349 public void SingleArgumentNullTest ()
351 string [] data = { "2", "1", "5", "3", "4" };
354 // Single<TSource> ()
355 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Single (); });
357 // Single<TSource> (Func<TSource, bool>)
358 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Single ((x => true)); });
359 AssertException<ArgumentNullException> (delegate () { data.Single ((Func<string, bool>) null); });
363 public void SingleTest ()
366 int [] data2 = { 2, 3, 5 };
370 // Single<TSource> ()
371 Assert.AreEqual (2, data.Single ());
372 AssertException<InvalidOperationException> (delegate () { data2.Single (); });
373 AssertException<InvalidOperationException> (delegate () { empty.Single (); });
375 // Single<TSource> (Func<TSource, bool>)
376 Assert.AreEqual (5, data2.Single (x => x == 5));
377 AssertException<InvalidOperationException> (delegate () { data2.Single (x => false); });
378 AssertException<InvalidOperationException> (delegate () { data2.Single (x => true); });
379 AssertException<InvalidOperationException> (delegate () { empty.Single (x => true); });
383 public void SingleOrDefaultArgumentNullTest ()
385 string [] data = { "2", "1", "5", "3", "4" };
388 // SingleOrDefault<TSource> ()
389 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SingleOrDefault (); });
391 // SingleOrDefault<TSource> (Func<TSource, bool>)
392 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SingleOrDefault (x => true); });
393 AssertException<ArgumentNullException> (delegate () { data.SingleOrDefault ((Func<string, bool>) null); });
397 public void SingleOrDefaultTest ()
400 int [] data2 = { 2, 3, 5 };
404 // SingleOrDefault<TSource> ()
405 Assert.AreEqual (2, data.SingleOrDefault ());
406 Assert.AreEqual (0, empty.SingleOrDefault ());
407 AssertException<InvalidOperationException> (delegate () { data2.SingleOrDefault (); });
410 // SingleOrDefault<TSource> (Func<TSource, bool>)
411 Assert.AreEqual (3, data2.SingleOrDefault (x => x == 3));
412 Assert.AreEqual (0, data2.SingleOrDefault (x => false));
413 AssertException<InvalidOperationException> (delegate () { data2.SingleOrDefault (x => true); });
417 public void ElementAtArgumentNullTest ()
419 //string [] data = { "2", "1", "5", "3", "4" };
422 // ElementAt<TSource> (int)
423 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ElementAt (0); });
427 public void ElementAtTest ()
429 int [] data = { 2, 3, 4, 5 };
431 // ElementAt<string> (int)
432 Assert.AreEqual (2, data.ElementAt (0));
433 Assert.AreEqual (4, data.ElementAt (2));
434 AssertException<ArgumentOutOfRangeException> (delegate () { data.ElementAt (-1); });
435 AssertException<ArgumentOutOfRangeException> (delegate () { data.ElementAt (4); });
436 AssertException<ArgumentOutOfRangeException> (delegate () { data.ElementAt (6); });
440 public void ElementAtOrDefaultArgumentNullTest ()
442 //string [] data = { "2", "1", "5", "3", "4" };
445 // ElementAtOrDefault<TSource> (int)
446 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ElementAtOrDefault (0); });
450 public void ElementAtOrDefaultTest ()
452 int [] data = { 2, 3, 4, 5 };
456 // ElementAtOrDefault<TSource> (int)
457 Assert.AreEqual (2, data.ElementAtOrDefault (0));
458 Assert.AreEqual (4, data.ElementAtOrDefault (2));
459 Assert.AreEqual (0, data.ElementAtOrDefault (-1));
460 Assert.AreEqual (0, data.ElementAtOrDefault (4));
461 Assert.AreEqual (0, empty.ElementAtOrDefault (4));
465 public void EmptyTest ()
467 IEnumerable<string> empty = Enumerable.Empty<string> ();
468 Assert.IsFalse (empty.GetEnumerator ().MoveNext ());
473 public void AnyArgumentNullTest ()
475 string [] data = { "2", "1", "5", "3", "4" };
479 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Any (); });
481 // Any<TSource> (Func<TSource, bool>)
482 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Any (x => true); });
483 AssertException<ArgumentNullException> (delegate () { data.Any ((Func<string, bool>) null); });
487 public void AnyTest ()
489 int [] data = { 5, 2, 3, 1, 6 };
494 Assert.IsTrue (data.Any ());
495 Assert.IsFalse (empty.Any ());
497 // Any<TSource> (Func<TSource, bool>)
498 Assert.IsTrue (data.Any (x => x == 5));
499 Assert.IsFalse (data.Any (x => x == 9));
500 Assert.IsFalse (empty.Any (x => true));
504 public void AllArgumentNullTest ()
506 string [] data = { "2", "1", "5", "3", "4" };
509 // All<TSource> (Func<TSource, bool>)
510 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).All (x => true); });
511 AssertException<ArgumentNullException> (delegate () { data.All ((Func<string, bool>) null); });
515 public void AllTest ()
517 int [] data = { 5, 2, 3, 1, 6 };
520 // All<TSource> (Func<TSource, bool>)
521 Assert.IsTrue (data.All (x => true));
522 Assert.IsFalse (data.All (x => x != 1));
523 Assert.IsTrue (empty.All (x => false));
527 public void CountArgumentNullTest ()
529 string [] data = { "2", "1", "5", "3", "4" };
533 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Count (); });
535 // Count<TSource> (Func<TSource, bool>)
536 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Count (x => true); });
537 AssertException<ArgumentNullException> (delegate () { data.Count ((Func<string, bool>) null); });
541 public void CountTest ()
543 int [] data = { 5, 2, 3, 1, 6 };
546 Assert.AreEqual (5, data.Count ());
548 // Count<TSource> (Func<TSource, bool>)
549 Assert.AreEqual (3, data.Count (x => x < 5));
554 public void CountOverflowTest ()
556 //BigEnumerable data = new BigEnumerable ((ulong) int.MaxValue + 1);
559 //AssertException<OverflowException> (delegate () { data.Count (); });
561 // Count<TSource> (Func<TSource, bool>)
562 //AssertException<OverflowException> (delegate () { data.Count (x => 3 == x); });
564 // Documentation error: http://msdn2.microsoft.com/en-us/library/bb535181.aspx
565 // An exception is only rasied if count > int.MaxValue. Not if source contains more than int.MaxValue elements.
566 // AssertException<OverflowException> (delegate () { data.Count (x => 5 == x); });
570 public void LongCountArgumentNullTest ()
572 string [] data = { "2", "1", "5", "3", "4" };
575 // LongCount<TSource> ()
576 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LongCount (); });
578 // LongCount<TSource> (Func<TSource, bool>)
579 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LongCount (x => true); });
580 AssertException<ArgumentNullException> (delegate () { data.LongCount ((Func<string, bool>) null); });
584 public void LongCountTest ()
586 int [] data = { 5, 2, 3, 1, 6 };
588 //TODO: Overflow test...
590 // LongCount<TSource> ()
591 Assert.AreEqual (5, data.LongCount ());
592 Assert.AreEqual (5, Enumerable.Range (0, 5).LongCount ());
594 // LongCount<TSource> (Func<TSource, bool>)
595 Assert.AreEqual (3, data.LongCount (x => x < 5));
599 public void ContainsArgumentNullTest ()
601 //string [] data = { "2", "1", "5", "3", "4" };
604 // Contains<TSource> (TSource)
605 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Contains ("2"); });
607 // Contains<TSource> (TSource, IEqualityComparer<TSource>)
608 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Contains ("2", (IEqualityComparer<string>) EqualityComparer<string>.Default); });
611 static void IsFalse(bool b, int[] data) {
613 Console.WriteLine (data.Contains (0));
616 Assert.IsFalse (true);
618 //Console.WriteLine ("HIT!");
622 public void ContainsTest ()
624 int [] data = { 5, 2, 3, 1, 6 };
625 ICollection<int> icoll = data;
627 // Contains<TSource> (TSource)
628 Assert.IsTrue (data.Contains (2));
629 for (int i = 0; i < 50; ++i)
630 Console.WriteLine (icoll.Contains (0));//Console.WriteLine (data.Contains (0));
631 IsFalse (data.Contains (0), data);
633 // Contains<TSource> (TSource, IEqualityComparer<TSource>)
634 Assert.IsTrue (data.Contains (2, EqualityComparer<int>.Default));
635 Assert.IsFalse (data.Contains (0, EqualityComparer<int>.Default));
639 public void AggregateArgumentNullTest ()
641 string [] data = { "2", "1", "5", "3", "4" };
644 // Aggregate<TSource> (Func<TSource, TSource, TSource>)
645 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ((x, y) => "test"); });
646 AssertException<ArgumentNullException> (delegate () { data.Aggregate ((Func<string, string, string>) null); });
648 // Aggregate<TSource,TAccumulate> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>)
649 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ("initial", (x, y) => "test"); });
650 AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (Func<string, string, string>) null); });
652 // Aggregate<TSource,TAccumulate,TResult> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>)
653 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ("initial", (x, y) => "test", x => "test"); });
654 AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (Func<string, string, string>) null, x => "test"); });
655 AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (x, y) => "test", (Func<string, string>) null); });
659 public void AggregateTest ()
661 string [] data = { "2", "1", "5", "3", "4" };
662 string [] empty = { };
664 // Aggregate<TSource> (Func<TSource, TSource, TSource>)
665 Assert.AreEqual ("21534", data.Aggregate ((x, y) => x + y));
666 AssertException<InvalidOperationException> (delegate () { empty.Aggregate ((x, y) => x + y); }); //only this overload throws
668 // Aggregate<TSource,TAccumulate> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>)
669 Assert.AreEqual ("initial21534", (data.Aggregate ("initial", (x, y) => x + y)));
671 // Aggregate<TSource,TAccumulate,TResult> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>)
672 Assert.AreEqual ("INITIAL21534", data.Aggregate ("initial", (x, y) => x + y, (x => x.ToUpper ())));
676 public void SumArgumentNullTest ()
678 string [] data = { "2", "1", "5", "3", "4" };
681 // Sum<TSource> (Func<TSource, int>)
682 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, int>) (x => 0)); });
683 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, int>) null); });
685 // Sum<TSource> (Func<TSource, Nullable<int>>)
686 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
687 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<int>>) null); });
689 // Sum<TSource> (Func<TSource, Int64>)
690 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Int64>) (x => 0L)); });
691 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Int64>) null); });
693 // Sum<TSource> (Func<TSource, Nullable<Int64>>)
694 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
695 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Int64>>) null); });
697 // Sum<TSource> (Func<TSource, Single>)
698 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Single>) (x => 0f)); });
699 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Single>) null); });
701 // Sum<TSource> (Func<TSource, Nullable<Single>>)
702 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
703 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Single>>) null); });
705 // Sum<TSource> (Func<TSource, Double>)
706 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Double>) (x => 0d)); });
707 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Double>) null); });
709 // Sum<TSource> (Func<TSource, Nullable<Double>>)
710 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
711 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Double>>) null); });
713 // Sum<TSource> (Func<TSource, Decimal>)
714 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Decimal>) (x => 0m)); });
715 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Decimal>) null); });
717 // Sum<TSource> (Func<TSource, Nullable<Decimal>>)
718 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
719 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Decimal>>) null); });
721 // Sum (IEnumerable<int>)
722 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Sum (); });
724 // Sum (IEnumerable<int?>)
725 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Sum (); });
727 // Sum (IEnumerable<long>)
728 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Sum (); });
730 // Sum (IEnumerable<long?>)
731 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Sum (); });
733 // Sum (IEnumerable<float>)
734 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Sum (); });
736 // Sum (IEnumerable<float?>)
737 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Sum (); });
739 // Sum (IEnumerable<double>)
740 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Sum (); });
742 // Sum (IEnumerable<double?>)
743 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Sum (); });
745 // Sum (IEnumerable<decimal>)
746 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Sum (); });
748 // Sum (IEnumerable<decimal?>)
749 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Sum (); });
753 public void SumTest ()
755 string [] data = { "2", "3", "5", "5" };
757 //TODO: OverflowException
759 // Sum<TSource> (Func<TSource, int>)
760 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, int>) (x => int.Parse (x))));
761 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, int>) (x => int.Parse (x))));
763 // Sum<TSource> (Func<TSource, Nullable<int>>)
764 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
765 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
767 // Sum<TSource> (Func<TSource, Int64>)
768 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Int64>) (x => int.Parse (x))));
769 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Int64>) (x => int.Parse (x))));
771 // Sum<TSource> (Func<TSource, Nullable<Int64>>)
772 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
773 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
775 // Sum<TSource> (Func<TSource, Single>)
776 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Single>) (x => int.Parse (x))));
777 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Single>) (x => int.Parse (x))));
779 // Sum<TSource> (Func<TSource, Nullable<Single>>)
780 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
781 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
783 // Sum<TSource> (Func<TSource, Double>)
784 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Double>) (x => int.Parse (x))));
785 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Double>) (x => int.Parse (x))));
787 // Sum<TSource> (Func<TSource, Nullable<Double>>)
788 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
789 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
791 // Sum<TSource> (Func<TSource, Decimal>)
792 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Decimal>) (x => int.Parse (x))));
793 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Decimal>) (x => int.Parse (x))));
795 // Sum<TSource> (Func<TSource, Nullable<Decimal>>)
796 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
797 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
800 Assert.AreEqual (6, ((IEnumerable<int>) new int [] { 1, 2, 3 }).Sum ());
801 Assert.AreEqual (0, Enumerable.Empty<int> ().Sum ());
804 Assert.AreEqual (6, ((IEnumerable<Nullable<int>>) new int? [] { 1, 2, 3 }).Sum ());
805 Assert.AreEqual (0, Enumerable.Empty<int?> ().Sum ());
808 Assert.AreEqual (6, ((IEnumerable<Int64>) new long [] { 1, 2, 3 }).Sum ());
809 Assert.AreEqual (0, Enumerable.Empty<long> ().Sum ());
812 Assert.AreEqual (6, ((IEnumerable<Nullable<Int64>>) new long? [] { 1, 2, 3 }).Sum ());
813 Assert.AreEqual (0, Enumerable.Empty<long?> ().Sum ());
816 Assert.AreEqual (6, ((IEnumerable<Single>) new float [] { 1, 2, 3 }).Sum ());
817 Assert.AreEqual (0, Enumerable.Empty<float> ().Sum ());
820 Assert.AreEqual (6, ((IEnumerable<Nullable<Single>>) new float? [] { 1, 2, 3 }).Sum ());
821 Assert.AreEqual (0, Enumerable.Empty<float?> ().Sum ());
824 Assert.AreEqual (6, ((IEnumerable<Double>) new double [] { 1, 2, 3 }).Sum ());
825 Assert.AreEqual (0, Enumerable.Empty<double> ().Sum ());
828 Assert.AreEqual (6, ((IEnumerable<Nullable<Double>>) new double? [] { 1, 2, 3 }).Sum ());
829 Assert.AreEqual (0, Enumerable.Empty<double?> ().Sum ());
832 Assert.AreEqual (6, ((IEnumerable<Decimal>) new decimal [] { 1, 2, 3 }).Sum ());
833 Assert.AreEqual (0, Enumerable.Empty<decimal> ().Sum ());
836 Assert.AreEqual (6, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 1, 2, 3 }).Sum ());
837 Assert.AreEqual (0, Enumerable.Empty<decimal?> ().Sum ());
841 public void MinArgumentNullTest ()
843 string [] data = { "2", "1", "5", "3", "4" };
847 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> (); });
849 // Min<TSource> (Func<TSource, int>)
850 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, int>) (x => 0)); });
851 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, int>) null); });
853 // Min<TSource> (Func<TSource, Nullable<int>>)
854 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
855 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<int>>) null); });
857 // Min<TSource> (Func<TSource, Int64>)
858 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Int64>) (x => 0L)); });
859 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Int64>) null); });
861 // Min<TSource> (Func<TSource, Nullable<Int64>>)
862 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
863 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Int64>>) null); });
865 // Min<TSource> (Func<TSource, Single>)
866 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Single>) (x => 0f)); });
867 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Single>) null); });
869 // Min<TSource> (Func<TSource, Nullable<Single>>)
870 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
871 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Single>>) null); });
873 // Min<TSource> (Func<TSource, Double>)
874 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Double>) (x => 0d)); });
875 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Double>) null); });
877 // Min<TSource> (Func<TSource, Nullable<Double>>)
878 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
879 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Double>>) null); });
881 // Min<TSource> (Func<TSource, Decimal>)
882 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Decimal>) (x => 0m)); });
883 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Decimal>) null); });
885 // Min<TSource> (Func<TSource, Nullable<Decimal>>)
886 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
887 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Decimal>>) null); });
889 // Min<TSource,TSource> (Func<TSource, string>)
890 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string, string> ((Func<string, string>) (x => "test")); });
891 AssertException<ArgumentNullException> (delegate () { data.Min<string, string> ((Func<string, string>) null); });
894 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Min (); });
897 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Min (); });
900 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Min (); });
903 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Min (); });
906 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Min (); });
909 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Min (); });
912 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Min (); });
915 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Min (); });
918 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Min (); });
921 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Min (); });
925 public void MinTest ()
927 string [] data = { "2", "1", "5", "3", "4" };
931 Assert.AreEqual ("1", ((IEnumerable<string>) data).Min<string> ());
934 // Min<TSource> (Func<TSource, int>)
935 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, int>) (x => int.Parse (x))));
937 // Min<TSource> (Func<TSource, Nullable<int>>)
938 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
940 // Min<TSource> (Func<TSource, Int64>)
941 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Int64>) (x => int.Parse (x))));
943 // Min<TSource> (Func<TSource, Nullable<Int64>>)
944 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
946 // Min<TSource> (Func<TSource, Single>)
947 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Single>) (x => int.Parse (x))));
949 // Min<TSource> (Func<TSource, Nullable<Single>>)
950 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
952 // Min<TSource> (Func<TSource, Double>)
953 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Double>) (x => int.Parse (x))));
955 // Min<TSource> (Func<TSource, Nullable<Double>>)
956 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
958 // Min<TSource> (Func<TSource, Decimal>)
959 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Decimal>) (x => int.Parse (x))));
961 // Min<TSource> (Func<TSource, Nullable<Decimal>>)
962 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
964 // Min<TSource,TSource> (Func<TSource, TSource>)
965 Assert.AreEqual ("1", ((IEnumerable<string>) data).Min<string, string> ((Func<string, string>) (x => x)));
968 Assert.AreEqual (2, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Min ());
971 Assert.AreEqual (2, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Min ());
974 Assert.AreEqual (2, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Min ());
977 Assert.AreEqual (2, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Min ());
980 Assert.AreEqual (2, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Min ());
983 Assert.AreEqual (2, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Min ());
986 Assert.AreEqual (2, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Min ());
989 Assert.AreEqual (2, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Min ());
992 Assert.AreEqual (2, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Min ());
995 Assert.AreEqual (2, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Min ());
999 public void MaxArgumentNullTest ()
1001 string [] data = { "2", "1", "5", "3", "4" };
1005 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> (); });
1007 // Max<TSource> (Func<TSource, int>)
1008 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, int>) (x => 0)); });
1009 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, int>) null); });
1011 // Max<TSource> (Func<TSource, Nullable<int>>)
1012 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
1013 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<int>>) null); });
1015 // Max<TSource> (Func<TSource, Int64>)
1016 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Int64>) (x => 0L)); });
1017 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Int64>) null); });
1019 // Max<TSource> (Func<TSource, Nullable<Int64>>)
1020 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
1021 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Int64>>) null); });
1023 // Max<TSource> (Func<TSource, Single>)
1024 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Single>) (x => 0f)); });
1025 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Single>) null); });
1027 // Max<TSource> (Func<TSource, Nullable<Single>>)
1028 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
1029 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Single>>) null); });
1031 // Max<TSource> (Func<TSource, Double>)
1032 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Double>) (x => 0d)); });
1033 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Double>) null); });
1035 // Max<TSource> (Func<TSource, Nullable<Double>>)
1036 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
1037 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Double>>) null); });
1039 // Max<TSource> (Func<TSource, Decimal>)
1040 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Decimal>) (x => 0m)); });
1041 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Decimal>) null); });
1043 // Max<TSource> (Func<TSource, Nullable<Decimal>>)
1044 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
1045 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Decimal>>) null); });
1047 // Max<TSource,TSource> (Func<TSource, TSource>)
1048 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string, string> ((Func<string, string>) (x => "test")); });
1049 AssertException<ArgumentNullException> (delegate () { data.Max<string, string> ((Func<string, string>) null); });
1052 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Max (); });
1055 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Max (); });
1058 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Max (); });
1061 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Max (); });
1064 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Max (); });
1067 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Max (); });
1070 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Max (); });
1073 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Max (); });
1076 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Max (); });
1079 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Max (); });
1083 public void MaxTest ()
1085 string [] data = { "2", "1", "5", "3", "4" };
1089 Assert.AreEqual ("5", ((IEnumerable<string>) data).Max<string> ());
1091 // Max<TSource> (Func<TSource, int>)
1092 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, int>) (x => int.Parse (x))));
1094 // Max<TSource> (Func<TSource, Nullable<int>>)
1095 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
1097 // Max<TSource> (Func<TSource, Int64>)
1098 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Int64>) (x => int.Parse (x))));
1100 // Max<TSource> (Func<TSource, Nullable<Int64>>)
1101 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
1103 // Max<TSource> (Func<TSource, Single>)
1104 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Single>) (x => int.Parse (x))));
1106 // Max<TSource> (Func<TSource, Nullable<Single>>)
1107 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
1109 // Max<TSource> (Func<TSource, Double>)
1110 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Double>) (x => int.Parse (x))));
1112 // Max<TSource> (Func<TSource, Nullable<Double>>)
1113 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
1115 // Max<TSource> (Func<TSource, Decimal>)
1116 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Decimal>) (x => int.Parse (x))));
1118 // Max<TSource> (Func<TSource, Nullable<Decimal>>)
1119 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
1121 // Max<TSource,TSource> (Func<TSource, TSource>)
1122 Assert.AreEqual ("5", ((IEnumerable<string>) data).Max<string, string> ((Func<string, string>) (x => x)));
1125 Assert.AreEqual (4, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Max ());
1128 Assert.AreEqual (4, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Max ());
1131 Assert.AreEqual (4, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Max ());
1134 Assert.AreEqual (4, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Max ());
1137 Assert.AreEqual (4, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Max ());
1140 Assert.AreEqual (4, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Max ());
1143 Assert.AreEqual (4, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Max ());
1146 Assert.AreEqual (4, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Max ());
1149 Assert.AreEqual (4, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Max ());
1152 Assert.AreEqual (4, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Max ());
1156 public void AverageArgumentNullTest ()
1158 string [] data = { "2", "1", "5", "3", "4" };
1161 // Average<TSource> (Func<TSource, int>)
1162 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, int>) (x => 0)); });
1163 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, int>) null); });
1165 // Average<TSource> (Func<TSource, Nullable<int>>)
1166 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
1167 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<int>>) null); });
1169 // Average<TSource> (Func<TSource, Int64>)
1170 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Int64>) (x => 0L)); });
1171 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Int64>) null); });
1173 // Average<TSource> (Func<TSource, Nullable<Int64>>)
1174 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
1175 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Int64>>) null); });
1177 // Average<TSource> (Func<TSource, Single>)
1178 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Single>) (x => 0f)); });
1179 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Single>) null); });
1181 // Average<TSource> (Func<TSource, Nullable<Single>>)
1182 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
1183 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Single>>) null); });
1185 // Average<TSource> (Func<TSource, Double>)
1186 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Double>) (x => 0d)); });
1187 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Double>) null); });
1189 // Average<TSource> (Func<TSource, Nullable<Double>>)
1190 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
1191 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Double>>) null); });
1193 // Average<TSource> (Func<TSource, Decimal>)
1194 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Decimal>) (x => 0m)); });
1195 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Decimal>) null); });
1197 // Average<TSource> (Func<TSource, Nullable<Decimal>>)
1198 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
1199 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Decimal>>) null); });
1202 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Average (); });
1205 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Average (); });
1208 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Average (); });
1211 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Average (); });
1214 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Average (); });
1217 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Average (); });
1220 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Average (); });
1223 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Average (); });
1226 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Average (); });
1229 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Average (); });
1233 public void AverageTest ()
1235 string [] data = { "2", "1", "5", "3", "4" };
1236 string [] empty = { };
1238 // Average<string> (Func<string, int>)
1239 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, int>) (x => int.Parse (x))));
1240 AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, int>) (x => int.Parse (x))); });
1242 // Average<TSource> (Func<TSource, Nullable<int>>)
1243 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
1245 // Average<TSource> (Func<TSource, Int64>)
1246 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, long>) (x => int.Parse (x))));
1247 AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, long>) (x => int.Parse (x))); });
1249 // Average<TSource> (Func<TSource, Nullable<Int64>>)
1250 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, long?>) (x => (int?) int.Parse (x))));
1252 // Average<TSource> (Func<TSource, Single>)
1253 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, float>) (x => int.Parse (x))));
1254 AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, float>) (x => int.Parse (x))); });
1256 // Average<TSource> (Func<TSource, Nullable<Single>>)
1257 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, float?>) (x => (int?) int.Parse (x))));
1259 // Average<TSource> (Func<TSource, Double>)
1260 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, double>) (x => int.Parse (x))));
1261 AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, double>) (x => int.Parse (x))); });
1263 // Average<TSource> (Func<TSource, Nullable<Double>>)
1264 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, double?>) (x => (int?) int.Parse (x))));
1266 // Average<TSource> (Func<TSource, Decimal>)
1267 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, decimal>) (x => int.Parse (x))));
1268 AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, decimal>) (x => int.Parse (x))); });
1270 // Average<TSource> (Func<TSource, Nullable<Decimal>>)
1271 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, decimal?>) (x => (int?) int.Parse (x))));
1275 Assert.AreEqual (3, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Average ());
1276 AssertException<InvalidOperationException> (delegate () { new int [0].Average (); });
1279 Assert.AreEqual (3, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Average ());
1282 Assert.AreEqual (3, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Average ());
1283 AssertException<InvalidOperationException> (delegate () { new long [0].Average (); });
1286 Assert.AreEqual (3, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Average ());
1289 Assert.AreEqual (3, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Average ());
1290 AssertException<InvalidOperationException> (delegate () { new float [0].Average (); });
1293 Assert.AreEqual (3, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Average ());
1296 Assert.AreEqual (3, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Average ());
1297 AssertException<InvalidOperationException> (delegate () { new double [0].Average (); });
1300 Assert.AreEqual (3, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Average ());
1303 Assert.AreEqual (3, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Average ());
1304 AssertException<InvalidOperationException> (delegate () { new decimal [0].Average (); });
1307 Assert.AreEqual (3, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Average ());
1311 public void WhereArgumentNullTest ()
1313 string [] data = { "2", "1", "5", "3", "4" };
1315 // Where<TSource> (Func<TSource, bool>)
1316 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Where (x => true); });
1317 AssertException<ArgumentNullException> (delegate () { data.Where ((Func<string, bool>) null); });
1319 // Where<TSource> (Func<TSource, int, bool>)
1320 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Where ((x, y) => true); });
1321 AssertException<ArgumentNullException> (delegate () { data.Where ((Func<string, int, bool>) null); });
1325 public void WhereTest ()
1327 int [] data = { 2, 1, 5, 3, 4 };
1328 int [] expected1 = { 2, 1 };
1329 int [] expected2 = { 2 };
1331 // Where<TSource> (Func<TSource, bool>)
1332 AssertAreSame (expected1, data.Where (x => x < 3));
1334 // Where<TSource> (Func<TSource, int, bool>)
1335 AssertAreSame (expected2, data.Where ((x, y) => x < 3 && y != 1));
1339 public void SelectArgumentNullTest ()
1341 string [] data = { "2", "1", "5", "3", "4" };
1344 // Select<TSource,TResult> (Func<TSource, TResult>)
1345 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Select (x => "test"); });
1346 AssertException<ArgumentNullException> (delegate () { data.Select ((Func<string, string>) null); });
1348 // Select<TSource,TResult> (Func<TSource, int, TResult>)
1349 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Select ((x, y) => "test"); });
1350 AssertException<ArgumentNullException> (delegate () { data.Select ((Func<string, int, string>) null); });
1354 public void SelectTest ()
1356 string [] data = { "2", "1", "5", "3", "4" };
1357 string [] expected1 = { "2x", "1x", "5x", "3x", "4x" };
1358 string [] expected2 = { "2x0", "1x1", "5x2", "3x3", "4x4" };
1361 // Select<TSource,TResult> (Func<TSource, TResult>)
1362 AssertAreSame (expected1, data.Select<string, string> (x => x + "x"));
1364 // Select<TSource,TResult> (Func<TSource, int, TResult>)
1365 AssertAreSame (expected2, data.Select<string, string> ((x, y) => x + "x" + y));
1369 public void SelectManyArgumentNullTest ()
1371 string [] data = { "2", "1", "5", "3", "4" };
1374 // SelectMany<TSource,TResult> (Func<TSource, IEnumerable<TResult>>)
1375 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany (x => data); });
1376 AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, IEnumerable<string>>) null); });
1378 // SelectMany<TSource,TResult> (Func<TSource, int, IEnumerable<TResult>>)
1379 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany ((x, y) => data); });
1380 AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, int, IEnumerable<string>>) null); });
1382 // SelectMany<TSource,TCollection,TResult> (Func<string, int, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
1383 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany ((x, y) => data, (x, y) => "test"); });
1384 AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, int, IEnumerable<string>>) null, (x, y) => "test"); });
1385 AssertException<ArgumentNullException> (delegate () { data.SelectMany ((x, y) => data, (Func<string, string, string>) null); });
1387 // SelectMany<TSource,TCollection,TResult> (Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
1388 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany (x => data, (x, y) => "test"); });
1389 AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, IEnumerable<string>>) null, (x, y) => "test"); });
1390 AssertException<ArgumentNullException> (delegate () { data.SelectMany (x => data, (Func<string, string, string>) null); });
1394 public void SelectManyTest ()
1396 string [] data = { "0", "1" };
1397 string [] expected = { "0", "00", "1", "11" };
1399 // SelectMany<TSource,TResult> (Func<TSource, IEnumerable<TResult>>)
1400 AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany (x => new string [] { x, x + x }));
1402 // SelectMany<TSource,TResult> (Func<TSource, int, IEnumerable<TResult>>)
1403 AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany ((x, y) => new string [] { x, x + y }));
1405 // SelectMany<TSource,TCollection,TResult> (Func<string, int, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
1406 AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany ((x, y) => new string [] { x, x + y }, (x, y) => y));
1408 // SelectMany<TSource,TCollection,TResult> (Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
1409 AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany (x => new string [] { x, x + x }, (x, y) => y));
1413 public void TakeArgumentNullTest ()
1415 //string [] data = { "2", "1", "5", "3", "4" };
1418 // Take<TSource> (int)
1419 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Take (0); });
1423 public void TakeTest ()
1425 int [] data = { 2, 1, 5, 3, 1 };
1426 int [] expected = { 2, 1 };
1429 // Take<TSource> (int)
1430 AssertAreSame (expected, data.Take (2));
1431 AssertAreSame (empty, data.Take (-2));
1435 public void TakeWhileArgumentNullTest ()
1437 string [] data = { "2", "1", "5", "3", "4" };
1440 // TakeWhile<TSource> (Func<TSource, bool>)
1441 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).TakeWhile (x => true); });
1442 AssertException<ArgumentNullException> (delegate () { data.TakeWhile ((Func<string, bool>) null); });
1444 // TakeWhile<TSource> (Func<TSource, int, bool>)
1445 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).TakeWhile ((x, y) => true); });
1446 AssertException<ArgumentNullException> (delegate () { data.TakeWhile ((Func<string, int, bool>) null); });
1450 public void TakeWhileTest ()
1452 int [] data = { 2, 1, 5, 3, 1 };
1453 int [] expected = { 2, 1 };
1456 // TakeWhile<TSource> (Func<TSource, bool>)
1457 AssertAreSame (expected, data.TakeWhile (x => x != 5));
1459 // TakeWhile<TSource> (Func<TSource, int, bool>)
1460 AssertAreSame (expected, data.TakeWhile ((x, y) => y != 2));
1464 public void SkipArgumentNullTest ()
1466 //string [] data = { "2", "1", "5", "3", "4" };
1469 // Skip<TSource> (int)
1470 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Skip (0); });
1474 public void SkipTest ()
1476 int [] data = { 2, 1, 5, 3, 1 };
1477 int [] expected = { 5, 3, 1 };
1479 // Skip<string> (TSource)
1480 AssertAreSame (expected, data.Skip (2));
1481 AssertAreSame (data, data.Skip (-2));
1485 public void SkipWhileArgumentNullTest ()
1487 string [] data = { "2", "1", "5", "3", "4" };
1490 // SkipWhile<TSource> (Func<TSource, bool>)
1491 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SkipWhile (x => true); });
1492 AssertException<ArgumentNullException> (delegate () { data.SkipWhile ((Func<string, bool>) null); });
1494 // SkipWhile<TSource> (Func<TSource, int, bool>)
1495 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SkipWhile ((x, y) => true); });
1496 AssertException<ArgumentNullException> (delegate () { data.SkipWhile ((Func<string, int, bool>) null); });
1500 public void SkipWhileTest ()
1502 int [] data = { 2, 1, 5, 3, 1 };
1503 int [] expected = { 5, 3, 1 };
1507 // SkipWhile<TSource> (Func<TSource, bool>)
1508 AssertAreSame (expected, data.SkipWhile (x => x != 5));
1510 // SkipWhile<TSource> (Func<TSource, int, bool>)
1511 AssertAreSame (expected, data.SkipWhile ((x, y) => y != 2));
1515 public void JoinArgumentNullTest ()
1517 string [] data = { "2", "1", "5", "3", "4" };
1520 // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)
1521 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Join (data, x => "test", x => "test", (x, y) => "test"); });
1522 AssertException<ArgumentNullException> (delegate () { data.Join ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test"); });
1523 AssertException<ArgumentNullException> (delegate () { data.Join (data, (Func<string, string>) null, x => "test", (x, y) => "test"); });
1524 AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", (Func<string, string>) null, (x, y) => "test"); });
1525 AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", x => "test", (Func<string, string, string>) null); });
1527 // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<string>)
1528 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Join (data, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1529 AssertException<ArgumentNullException> (delegate () { data.Join ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1530 AssertException<ArgumentNullException> (delegate () { data.Join (data, (Func<string, string>) null, x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1531 AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", (Func<string, string>) null, (x, y) => "test", EqualityComparer<string>.Default); });
1532 AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", x => "test", (Func<string, string, string>) null, EqualityComparer<string>.Default); });
1536 public void JoinTest ()
1538 string [] dataOuter1 = { "2", "1", "5", "3", "4" };
1539 string [] dataInner1 = { "7", "3", "5", "8", "9" };
1540 string [] expected1 = { "55", "33" };
1542 string [] dataOuter2 = { "2", "1", "3", "4" };
1543 string [] dataInner2 = { "7", "5", "8", "9" };
1544 string [] expected2 = { };
1547 // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)
1548 AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y));
1549 AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y));
1551 // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<string>)
1552 AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y, EqualityComparer<string>.Default));
1553 AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y, EqualityComparer<string>.Default));
1557 public void JoinTestNullKeys ()
1560 new { Name = "name1", Nullable = (int?) null },
1561 new { Name = "name2", Nullable = (int?) null }
1564 var count = l1.Join (l1, i => i.Nullable, i => i.Nullable, (x, y) => x.Name).Count ();
1565 Assert.AreEqual (0, count);
1569 public void GroupJoinArgumentNullTest ()
1571 string [] data = { "2", "1", "5", "3", "4" };
1574 // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
1575 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test"); });
1576 AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test"); });
1577 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test"); });
1578 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test"); });
1579 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null); });
1581 // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
1582 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1583 AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1584 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1585 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test", EqualityComparer<string>.Default); });
1586 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null, EqualityComparer<string>.Default); });
1590 public void GroupJoinTest ()
1592 string [] dataOuter1 = { "2", "1", "5", "3", "4" };
1593 string [] dataInner1 = { "7", "3", "5", "3", "9" };
1594 string [] expected1 = { "2", "1", "55", "333", "4" };
1596 string [] dataOuter2 = { "2", "1", "5", "8", "4" };
1597 string [] dataInner2 = { "7", "3", "6", "3", "9" };
1598 string [] expected2 = { "2", "1", "5", "8", "4" };
1601 // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
1602 AssertAreSame (expected1, (dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
1603 AssertAreSame (expected2, (dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
1605 // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
1606 AssertAreSame (expected1, dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
1607 AssertAreSame (expected2, dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
1611 public void GroupJoinWithNullKeys ()
1613 string[] l1 = { null };
1614 string[] l2 = { null, null };
1615 var res = l1.GroupJoin (l2, x => x, y => y, (a, b) => new { Key = a, Count = b.Count () }).ToArray ();
1616 Assert.AreEqual (1, res.Length);
1617 Assert.AreEqual (0, res [0].Count);
1621 public void OrderByArgumentNullTest ()
1623 string [] data = { "2", "1", "5", "3", "4" };
1626 // OrderBy<TSource,TKey> (Func<TSource, TKey>)
1627 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test"); });
1628 AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null); });
1630 // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1631 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test", Comparer<string>.Default); });
1632 AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null, Comparer<string>.Default); });
1636 public void OrderByTest ()
1638 int [] data = { 2, 1, 5, 3, 4 };
1639 int [] expected = { 1, 2, 3, 4, 5 };
1642 // OrderBy<TSource,TKey> (Func<TSource, TKey>)
1643 AssertAreSame (expected, data.OrderBy (x => x));
1645 // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1646 AssertAreSame (expected, data.OrderBy (x => x, Comparer<int>.Default));
1650 public void OrderByDescendingArgumentNullTest ()
1652 string [] data = { "2", "1", "5", "3", "4" };
1655 // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
1656 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test"); });
1657 AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null); });
1659 // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1660 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test", Comparer<string>.Default); });
1661 AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null, Comparer<string>.Default); });
1665 public void OrderByDescendingTest ()
1667 int [] data = { 2, 1, 5, 3, 4 };
1668 int [] expected = { 5, 4, 3, 2, 1 };
1671 // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
1672 AssertAreSame (expected, data.OrderByDescending (x => x));
1674 // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1675 AssertAreSame (expected, data.OrderByDescending (x => x, Comparer<int>.Default));
1679 public void ThenByArgumentNullTest ()
1681 string [] data = { "2", "1", "5", "3", "4" };
1684 // ThenBy<TSource,TKey> (Func<TSource, TKey>)
1685 AssertException<ArgumentNullException> (delegate () {
1686 ((IOrderedEnumerable<string>) null).ThenBy (x => "test");
1688 AssertException<ArgumentNullException> (delegate () {
1689 data.OrderBy (x => x).ThenBy ((Func<string, string>) null);
1692 // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1693 AssertException<ArgumentNullException> (delegate () {
1695 ((IOrderedEnumerable<string>) null).ThenBy (x => "test", Comparer<string>.Default);
1697 AssertException<ArgumentNullException> (delegate () {
1699 data.OrderBy (x => x).ThenBy ((Func<string, string>) null, Comparer<string>.Default);
1704 public void ThenByTest ()
1706 int [] data = { 2, 1, 5, 3, 4 };
1707 int [] expected = { 1, 2, 3, 4, 5 };
1710 // ThenBy<TSource,TKey> (Func<TSource, TKey>)
1711 AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x));
1713 // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1714 AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x, Comparer<int>.Default));
1718 public void ThenByDescendingArgumentNullTest ()
1720 string [] data = { "2", "1", "5", "3", "4" };
1723 // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
1724 AssertException<ArgumentNullException> (delegate () {
1725 ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test");
1727 AssertException<ArgumentNullException> (delegate () {
1728 data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null);
1731 // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1732 AssertException<ArgumentNullException> (delegate () {
1734 ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test", Comparer<string>.Default);
1736 AssertException<ArgumentNullException> (delegate () {
1738 data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null, Comparer<string>.Default);
1743 public void ThenByDescendingTest ()
1745 int [] data = { 2, 1, 5, 3, 4 };
1746 int [] expected = { 5, 4, 3, 2, 1 };
1749 // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
1750 AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x));
1752 // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1753 AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x, Comparer<int>.Default));
1757 public void GroupByArgumentNullTest ()
1759 string [] data = { "2", "1", "5", "3", "4" };
1762 // GroupBy<string,string> (Func<string, string>)
1763 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test")); });
1764 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null); });
1766 // GroupBy<string,string> (Func<string, string>, IEqualityComparer<string>)
1767 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1768 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1770 // GroupBy<string,string,string> (Func<string, string>, Func<string, string>)
1771 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
1772 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
1773 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
1775 // GroupBy<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
1776 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1777 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1778 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1780 // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>)
1781 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
1782 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
1783 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null); });
1785 // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>)
1786 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
1787 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
1788 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
1789 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null); });
1791 // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
1792 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1793 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, IEnumerable<string>, string>) ((x, y) => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1794 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1796 // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
1797 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1798 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1799 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (Func<string, IEnumerable<string>, string>) ((x, y) => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1800 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1804 public void GroupByTest ()
1806 string [] data = { "2", "1", "5", "3", "4", "3" };
1808 Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
1809 expected.Add ("2", new List<string> () { "2" });
1810 expected.Add ("1", new List<string> () { "1" });
1811 expected.Add ("5", new List<string> () { "5" });
1812 expected.Add ("3", new List<string> () { "3", "3" });
1813 expected.Add ("4", new List<string> () { "4" });
1815 Dictionary<string, IEnumerable<string>> expected2 = new Dictionary<string, IEnumerable<string>> ();
1816 expected2.Add ("2", new List<string> () { "22" });
1817 expected2.Add ("1", new List<string> () { "11" });
1818 expected2.Add ("5", new List<string> () { "55" });
1819 expected2.Add ("3", new List<string> () { "33", "33" });
1820 expected2.Add ("4", new List<string> () { "44" });
1822 string [] expected3 = new string [] { "22", "11", "55", "333", "44" };
1824 // GroupBy<int,int> (Func<int, int>)
1825 AssertAreSame (expected, data.GroupBy (x => x));
1827 // GroupBy<int,int> (Func<int, int>, IEqualityComparer<int>)
1828 AssertAreSame (expected, data.GroupBy (x => x, EqualityComparer<string>.Default));
1830 // GroupBy<int,int,int> (Func<int, int>, Func<int, int>)
1831 AssertAreSame (expected2, data.GroupBy (x => x, x => x + x));
1833 // GroupBy<int,int,int> (Func<int, int>, Func<int, int>, IEqualityComparer<int>)
1834 AssertAreSame (expected2, data.GroupBy (x => x, x => x + x, EqualityComparer<string>.Default));
1836 // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>)
1837 AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
1839 // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>)
1840 AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
1842 // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
1843 AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
1845 // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
1846 AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
1853 public string String;
1855 public Data (int number, string str)
1863 public void GroupByLastNullGroup ()
1865 var values = new List<Data> ();
1867 values.Add (new Data (0, "a"));
1868 values.Add (new Data (1, "a"));
1869 values.Add (new Data (2, "b"));
1870 values.Add (new Data (3, "b"));
1871 values.Add (new Data (4, null));
1873 var groups = values.GroupBy (d => d.String);
1875 Assert.AreEqual (3, groups.Count ());
1879 public void ConcatArgumentNullTest ()
1881 string [] data = { "2", "1", "5", "3", "4" };
1883 // Concat<TSource> (IEnumerable<TSource>)
1884 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Concat (data); });
1885 AssertException<ArgumentNullException> (delegate () { data.Concat ((IEnumerable<string>) null); });
1889 public void ConcatTest ()
1891 int [] data1 = { 2, 1, 5, 3, 4 };
1892 int [] data2 = { 1, 2, 3, 4, 5 };
1893 int [] expected = { 2, 1, 5, 3, 4, 1, 2, 3, 4, 5 };
1896 // Concat<TSource> (IEnumerable<TSource>)
1897 AssertAreSame (expected, data1.Concat (data2));
1901 public void DistinctArgumentNullTest ()
1903 //string [] data = { "2", "1", "5", "3", "4" };
1906 // Distinct<TSource> ()
1907 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (); });
1909 // Distinct<TSource> (IEqualityComparer<TSource>)
1910 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (EqualityComparer<string>.Default); });
1914 public void DistinctTest ()
1916 int [] data = { 2, 1, 5, 3, 4, 2, 5, 3, 1, 8 };
1917 int [] expected = { 2, 1, 5, 3, 4, 8 };
1920 // Distinct<TSource> ()
1921 AssertAreSame (expected, data.Distinct ());
1923 // Distinct<iTSourcent> (IEqualityComparer<TSource>)
1924 AssertAreSame (expected, data.Distinct (EqualityComparer<int>.Default));
1928 public void UnionArgumentNullTest ()
1930 string [] data = { "2", "1", "5", "3", "4" };
1933 // Union<TSource> (IEnumerable<TSource>)
1934 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data); });
1935 AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null); });
1937 // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1938 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data, EqualityComparer<string>.Default); });
1939 AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
1943 public void UnionTest ()
1945 int [] data1 = { 2, 1, 5, 7, 3, 4 };
1946 int [] data2 = { 1, 2, 3, 8, 4, 5 };
1947 int [] expected = { 2, 1, 5, 7, 3, 4, 8 };
1949 // Union<TSource> (IEnumerable<TSource>)
1950 AssertAreSame (expected, data1.Union (data2));
1952 // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1953 AssertAreSame (expected, data1.Union (data2, EqualityComparer<int>.Default));
1957 public void IntersectArgumentNullTest ()
1959 string [] data = { "2", "1", "5", "3", "4" };
1962 // Intersect<TSource> (IEnumerable<TSource>)
1963 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data); });
1964 AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null); });
1966 // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1967 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data, EqualityComparer<string>.Default); });
1968 AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
1972 public void IntersectTest ()
1974 int [] data1 = { 2, 1, 5, 7, 3, 4 };
1975 int [] data2 = { 1, 2, 3, 8, 4, 5 };
1976 int [] expected = { 2, 1, 5, 3, 4 };
1979 // Intersect<TSource> (IEnumerable<TSource>)
1980 AssertAreSame (expected, data1.Intersect (data2));
1982 // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1983 AssertAreSame (expected, data1.Intersect (data2, EqualityComparer<int>.Default));
1987 public void ExceptArgumentNullTest ()
1989 string [] data = { "2", "1", "5", "3", "4" };
1992 // Except<TSource> (IEnumerable<TSource>)
1993 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data); });
1994 AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null); });
1996 // Except<TSource> (IEnumerable<string>, IEqualityComparer<TSource>)
1997 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data, EqualityComparer<string>.Default); });
1998 AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
2002 public void ExceptTest ()
2004 int [] data1 = { 2, 1, 5, 7, 3, 4 };
2005 int [] data2 = { 1, 2, 3, 8, 4, 5 };
2006 int [] expected = { 7 };
2009 // Except<TSource> (IEnumerable<TSource>)
2010 AssertAreSame (expected, data1.Except (data2));
2012 // Except<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
2013 AssertAreSame (expected, data1.Except (data2, EqualityComparer<int>.Default));
2017 public void ReverseArgumentNullTest ()
2019 //string [] data = { "2", "1", "5", "3", "4" };
2022 // Reverse<TSource> ()
2023 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Reverse (); });
2027 public void ReverseTest ()
2029 int [] data = { 2, 1, 5, 7, 3, 4 };
2030 int [] expected = { 4, 3, 7, 5, 1, 2 };
2034 // Reverse<TSource> ()
2035 AssertAreSame (expected, data.Reverse ());
2039 public void SequenceEqualArgumentNullTest ()
2041 string [] data = { "2", "1", "5", "3", "4" };
2044 // SequenceEqual<TSource> (IEnumerable<TSource>)
2045 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data); });
2046 AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null); });
2048 // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
2049 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data, EqualityComparer<string>.Default); });
2050 AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
2054 public void SequenceEqualTest ()
2056 int [] data1 = { 2, 1, 5, 7, 3, 4 };
2057 int [] data2 = { 2, 1, 5, 7, 3, 4 };
2058 int [] data3 = { 2, 1, 5, 7, 3, 4, 5 };
2059 int [] data4 = { 2, 1, 5, 7, 3 };
2060 int [] data5 = { 2, 1, 5, 8, 3, 4 };
2063 // SequenceEqual<TSource> (IEnumerable<TSource>)
2064 Assert.IsTrue (data1.SequenceEqual (data2));
2065 Assert.IsFalse (data1.SequenceEqual (data3));
2066 Assert.IsFalse (data1.SequenceEqual (data4));
2067 Assert.IsFalse (data1.SequenceEqual (data5));
2069 // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
2070 Assert.IsTrue (data1.SequenceEqual (data2, EqualityComparer<int>.Default));
2071 Assert.IsFalse (data1.SequenceEqual (data3, EqualityComparer<int>.Default));
2072 Assert.IsFalse (data1.SequenceEqual (data4, EqualityComparer<int>.Default));
2073 Assert.IsFalse (data1.SequenceEqual (data5, EqualityComparer<int>.Default));
2077 public void AsEnumerableArgumentNullTest ()
2079 //string [] data = { "2", "1", "5", "3", "4" };
2084 public void AsEnumerableTest ()
2086 int [] data = { 2, 1, 5, 7, 3, 4 };
2089 // AsEnumerable<TSource> ()
2090 Assert.AreSame (data, data.AsEnumerable ());
2094 public void ToArrayArgumentNullTest ()
2096 //string [] data = { "2", "1", "5", "3", "4" };
2099 // ToArray<TSource> ()
2100 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToArray (); });
2104 public void ToArrayTest ()
2106 int [] data = { 2, 3, 4, 5 };
2107 int [] expected = { 2, 3, 4, 5 };
2110 // ToArray<TSource> ()
2111 AssertAreSame (expected, data.ToArray ());
2115 public void ToListArgumentNullTest ()
2117 //string [] data = { "2", "1", "5", "3", "4" };
2120 // ToList<string> ()
2121 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToList (); });
2125 public void ToListTest ()
2127 int [] data = { 2, 4, 5, 1 };
2128 int [] expected = { 2, 4, 5, 1 };
2132 AssertAreSame (expected, data.ToList ());
2136 public void ToDictionaryArgumentNullTest ()
2138 string [] data = { "2", "1", "5", "3", "4" };
2141 // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
2142 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test"); });
2143 AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null); });
2145 // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
2146 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", EqualityComparer<string>.Default); });
2147 AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, EqualityComparer<string>.Default); });
2149 // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
2150 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test"); });
2151 AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test"); });
2152 AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null); });
2154 // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
2155 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test", EqualityComparer<string>.Default); });
2156 AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test", EqualityComparer<string>.Default); });
2157 AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null, EqualityComparer<string>.Default); });
2161 public void ToDictionaryTest ()
2163 string [] data = { "2", "1", "5", "3", "4" };
2164 Dictionary<string, string> expected = new Dictionary<string, string> ();
2165 expected.Add ("k2", "2");
2166 expected.Add ("k1", "1");
2167 expected.Add ("k5", "5");
2168 expected.Add ("k3", "3");
2169 expected.Add ("k4", "4");
2172 // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
2173 AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x));
2174 AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key"); });
2176 // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
2177 AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, EqualityComparer<string>.Default));
2178 AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", EqualityComparer<string>.Default); });
2180 // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
2181 AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x));
2182 AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x); });
2184 // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
2185 AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x, EqualityComparer<string>.Default));
2186 AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x, EqualityComparer<string>.Default); });
2190 public void ToLookupArgumentNullTest ()
2192 string [] data = { "2", "1", "5", "3", "4" };
2195 // ToLookup<string,string> (Func<string, string>)
2196 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test")); });
2197 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null); });
2199 // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
2200 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
2201 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
2203 // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
2204 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
2205 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
2206 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
2208 // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
2209 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
2210 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
2211 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
2215 public void ToLookupTest ()
2217 string [] data = { "23", "12", "55", "42", "41" };
2218 Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
2219 expected.Add ("2", new List<string> () { "23" });
2220 expected.Add ("1", new List<string> () { "12" });
2221 expected.Add ("5", new List<string> () { "55" });
2222 expected.Add ("4", new List<string> () { "42", "41" });
2224 Assert.AreEqual (expected.Count, ((IEnumerable<string>)data).ToLookup ((x => x [0].ToString ())).Count);
2226 // ToLookup<string,string> (Func<string, string>)
2227 AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup ((x => x [0].ToString ())));
2229 // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
2230 AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), EqualityComparer<string>.Default));
2232 // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
2233 AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x));
2235 // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
2236 AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x, EqualityComparer<string>.Default));
2240 public void ToLookupNullKeyTest ()
2242 string[] strs = new string[] { "one", null, "two", null, "three" };
2245 var l = strs.ToLookup (s => (s == null) ? null : "numbers", s => (s == null) ? (++i).ToString() : s);
2247 Assert.AreEqual (2, l.Count);
2248 Assert.AreEqual (2, l [null].Count());
2249 Assert.IsTrue (l [null].Contains ("1"));
2250 Assert.IsTrue (l [null].Contains ("2"));
2252 Assert.AreEqual (3, l ["numbers"].Count());
2253 Assert.IsTrue (l ["numbers"].Contains ("one"));
2254 Assert.IsTrue (l ["numbers"].Contains ("two"));
2255 Assert.IsTrue (l ["numbers"].Contains ("three"));
2259 public void DefaultIfEmptyArgumentNullTest ()
2261 //string [] data = { "2", "1", "5", "3", "4" };
2264 // DefaultIfEmpty<string> ()
2265 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> (); });
2267 // DefaultIfEmpty<string> (string)
2268 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> ((string) "default"); });
2272 public void DefaultIfEmptyTest ()
2274 string [] data = { "2", "1", "5", "3", "4" };
2275 string [] empty = { };
2276 string [] default1 = { null };
2277 string [] default2 = { "default" };
2280 // DefaultIfEmpty<string> ()
2281 AssertAreSame (data, data.DefaultIfEmpty ());
2282 AssertAreSame (default1, empty.DefaultIfEmpty ());
2284 // DefaultIfEmpty<string> (string)
2285 AssertAreSame (data, data.DefaultIfEmpty ("default"));
2286 AssertAreSame (default2, empty.DefaultIfEmpty ("default"));
2290 public void OfTypeArgumentNullTest ()
2292 //string [] data = { "2", "1", "5", "3", "4" };
2295 // OfType<string> ()
2296 AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).OfType<string> (); });
2300 public void OfTypeTest ()
2302 object [] data = { "2", 2, "1", "5", "3", "4" };
2303 string [] expected = { "2", "1", "5", "3", "4" };
2306 // OfType<string> ()
2307 AssertAreSame (expected, data.OfType<string> ());
2311 public void CastArgumentNullTest ()
2313 //string [] data = { "2", "1", "5", "3", "4" };
2317 AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).Cast<string> (); });
2321 public void CastTest ()
2323 object [] data = { 1, 2, 3 };
2324 int [] expected = { 1, 2, 3 };
2328 AssertAreSame (expected, data.Cast<int> ());
2329 AssertException<InvalidCastException> (delegate () { data.Cast<IEnumerable> ().GetEnumerator ().MoveNext (); });
2330 data.Cast<IEnumerable> ();
2334 public void RangeArgumentNullTest ()
2336 //string [] data = { "2", "1", "5", "3", "4" };
2341 public void RangeTest ()
2343 int [] expected = { 2, 3, 4, 5 };
2346 AssertAreSame (expected, Enumerable.Range (2, 4));
2347 AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (2, -3); });
2348 AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (int.MaxValue - 5, 7); });
2349 Enumerable.Range (int.MaxValue - 5, 6);
2353 public void ExceptMultipleItems ()
2355 var data = new [] { 1, 2, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10 };
2356 var expected = new [] { 2, 4, 6, 8, 10 };
2358 AssertAreSame (expected, data.Except (new [] { 1, 3, 5, 7, 9 }));