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 GroupJoinArgumentNullTest ()
1559 string [] data = { "2", "1", "5", "3", "4" };
1562 // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
1563 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test"); });
1564 AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test"); });
1565 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test"); });
1566 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test"); });
1567 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null); });
1569 // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
1570 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1571 AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1572 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1573 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test", EqualityComparer<string>.Default); });
1574 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null, EqualityComparer<string>.Default); });
1578 public void GroupJoinTest ()
1580 string [] dataOuter1 = { "2", "1", "5", "3", "4" };
1581 string [] dataInner1 = { "7", "3", "5", "3", "9" };
1582 string [] expected1 = { "2", "1", "55", "333", "4" };
1584 string [] dataOuter2 = { "2", "1", "5", "8", "4" };
1585 string [] dataInner2 = { "7", "3", "6", "3", "9" };
1586 string [] expected2 = { "2", "1", "5", "8", "4" };
1589 // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
1590 AssertAreSame (expected1, (dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
1591 AssertAreSame (expected2, (dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
1593 // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
1594 AssertAreSame (expected1, dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
1595 AssertAreSame (expected2, dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
1599 public void OrderByArgumentNullTest ()
1601 string [] data = { "2", "1", "5", "3", "4" };
1604 // OrderBy<TSource,TKey> (Func<TSource, TKey>)
1605 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test"); });
1606 AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null); });
1608 // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1609 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test", Comparer<string>.Default); });
1610 AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null, Comparer<string>.Default); });
1614 public void OrderByTest ()
1616 int [] data = { 2, 1, 5, 3, 4 };
1617 int [] expected = { 1, 2, 3, 4, 5 };
1620 // OrderBy<TSource,TKey> (Func<TSource, TKey>)
1621 AssertAreSame (expected, data.OrderBy (x => x));
1623 // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1624 AssertAreSame (expected, data.OrderBy (x => x, Comparer<int>.Default));
1628 public void OrderByDescendingArgumentNullTest ()
1630 string [] data = { "2", "1", "5", "3", "4" };
1633 // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
1634 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test"); });
1635 AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null); });
1637 // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1638 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test", Comparer<string>.Default); });
1639 AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null, Comparer<string>.Default); });
1643 public void OrderByDescendingTest ()
1645 int [] data = { 2, 1, 5, 3, 4 };
1646 int [] expected = { 5, 4, 3, 2, 1 };
1649 // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
1650 AssertAreSame (expected, data.OrderByDescending (x => x));
1652 // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1653 AssertAreSame (expected, data.OrderByDescending (x => x, Comparer<int>.Default));
1657 public void ThenByArgumentNullTest ()
1659 string [] data = { "2", "1", "5", "3", "4" };
1662 // ThenBy<TSource,TKey> (Func<TSource, TKey>)
1663 AssertException<ArgumentNullException> (delegate () {
1664 ((IOrderedEnumerable<string>) null).ThenBy (x => "test");
1666 AssertException<ArgumentNullException> (delegate () {
1667 data.OrderBy (x => x).ThenBy ((Func<string, string>) null);
1670 // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1671 AssertException<ArgumentNullException> (delegate () {
1673 ((IOrderedEnumerable<string>) null).ThenBy (x => "test", Comparer<string>.Default);
1675 AssertException<ArgumentNullException> (delegate () {
1677 data.OrderBy (x => x).ThenBy ((Func<string, string>) null, Comparer<string>.Default);
1682 public void ThenByTest ()
1684 int [] data = { 2, 1, 5, 3, 4 };
1685 int [] expected = { 1, 2, 3, 4, 5 };
1688 // ThenBy<TSource,TKey> (Func<TSource, TKey>)
1689 AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x));
1691 // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1692 AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x, Comparer<int>.Default));
1696 public void ThenByDescendingArgumentNullTest ()
1698 string [] data = { "2", "1", "5", "3", "4" };
1701 // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
1702 AssertException<ArgumentNullException> (delegate () {
1703 ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test");
1705 AssertException<ArgumentNullException> (delegate () {
1706 data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null);
1709 // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1710 AssertException<ArgumentNullException> (delegate () {
1712 ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test", Comparer<string>.Default);
1714 AssertException<ArgumentNullException> (delegate () {
1716 data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null, Comparer<string>.Default);
1721 public void ThenByDescendingTest ()
1723 int [] data = { 2, 1, 5, 3, 4 };
1724 int [] expected = { 5, 4, 3, 2, 1 };
1727 // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
1728 AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x));
1730 // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1731 AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x, Comparer<int>.Default));
1735 public void GroupByArgumentNullTest ()
1737 string [] data = { "2", "1", "5", "3", "4" };
1740 // GroupBy<string,string> (Func<string, string>)
1741 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test")); });
1742 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null); });
1744 // GroupBy<string,string> (Func<string, string>, IEqualityComparer<string>)
1745 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1746 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1748 // GroupBy<string,string,string> (Func<string, string>, Func<string, string>)
1749 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
1750 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
1751 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
1753 // GroupBy<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
1754 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); });
1755 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1756 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1758 // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>)
1759 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
1760 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
1761 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null); });
1763 // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>)
1764 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")); });
1765 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")); });
1766 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")); });
1767 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); });
1769 // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
1770 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); });
1771 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); });
1772 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1774 // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
1775 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); });
1776 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); });
1777 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); });
1778 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); });
1782 public void GroupByTest ()
1784 string [] data = { "2", "1", "5", "3", "4", "3" };
1786 Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
1787 expected.Add ("2", new List<string> () { "2" });
1788 expected.Add ("1", new List<string> () { "1" });
1789 expected.Add ("5", new List<string> () { "5" });
1790 expected.Add ("3", new List<string> () { "3", "3" });
1791 expected.Add ("4", new List<string> () { "4" });
1793 Dictionary<string, IEnumerable<string>> expected2 = new Dictionary<string, IEnumerable<string>> ();
1794 expected2.Add ("2", new List<string> () { "22" });
1795 expected2.Add ("1", new List<string> () { "11" });
1796 expected2.Add ("5", new List<string> () { "55" });
1797 expected2.Add ("3", new List<string> () { "33", "33" });
1798 expected2.Add ("4", new List<string> () { "44" });
1800 string [] expected3 = new string [] { "22", "11", "55", "333", "44" };
1802 // GroupBy<int,int> (Func<int, int>)
1803 AssertAreSame (expected, data.GroupBy (x => x));
1805 // GroupBy<int,int> (Func<int, int>, IEqualityComparer<int>)
1806 AssertAreSame (expected, data.GroupBy (x => x, EqualityComparer<string>.Default));
1808 // GroupBy<int,int,int> (Func<int, int>, Func<int, int>)
1809 AssertAreSame (expected2, data.GroupBy (x => x, x => x + x));
1811 // GroupBy<int,int,int> (Func<int, int>, Func<int, int>, IEqualityComparer<int>)
1812 AssertAreSame (expected2, data.GroupBy (x => x, x => x + x, EqualityComparer<string>.Default));
1814 // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>)
1815 AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
1817 // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>)
1818 AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
1820 // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
1821 AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
1823 // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
1824 AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
1831 public string String;
1833 public Data (int number, string str)
1841 public void GroupByLastNullGroup ()
1843 var values = new List<Data> ();
1845 values.Add (new Data (0, "a"));
1846 values.Add (new Data (1, "a"));
1847 values.Add (new Data (2, "b"));
1848 values.Add (new Data (3, "b"));
1849 values.Add (new Data (4, null));
1851 var groups = values.GroupBy (d => d.String);
1853 Assert.AreEqual (3, groups.Count ());
1857 public void ConcatArgumentNullTest ()
1859 string [] data = { "2", "1", "5", "3", "4" };
1861 // Concat<TSource> (IEnumerable<TSource>)
1862 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Concat (data); });
1863 AssertException<ArgumentNullException> (delegate () { data.Concat ((IEnumerable<string>) null); });
1867 public void ConcatTest ()
1869 int [] data1 = { 2, 1, 5, 3, 4 };
1870 int [] data2 = { 1, 2, 3, 4, 5 };
1871 int [] expected = { 2, 1, 5, 3, 4, 1, 2, 3, 4, 5 };
1874 // Concat<TSource> (IEnumerable<TSource>)
1875 AssertAreSame (expected, data1.Concat (data2));
1879 public void DistinctArgumentNullTest ()
1881 //string [] data = { "2", "1", "5", "3", "4" };
1884 // Distinct<TSource> ()
1885 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (); });
1887 // Distinct<TSource> (IEqualityComparer<TSource>)
1888 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (EqualityComparer<string>.Default); });
1892 public void DistinctTest ()
1894 int [] data = { 2, 1, 5, 3, 4, 2, 5, 3, 1, 8 };
1895 int [] expected = { 2, 1, 5, 3, 4, 8 };
1898 // Distinct<TSource> ()
1899 AssertAreSame (expected, data.Distinct ());
1901 // Distinct<iTSourcent> (IEqualityComparer<TSource>)
1902 AssertAreSame (expected, data.Distinct (EqualityComparer<int>.Default));
1906 public void UnionArgumentNullTest ()
1908 string [] data = { "2", "1", "5", "3", "4" };
1911 // Union<TSource> (IEnumerable<TSource>)
1912 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data); });
1913 AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null); });
1915 // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1916 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data, EqualityComparer<string>.Default); });
1917 AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
1921 public void UnionTest ()
1923 int [] data1 = { 2, 1, 5, 7, 3, 4 };
1924 int [] data2 = { 1, 2, 3, 8, 4, 5 };
1925 int [] expected = { 2, 1, 5, 7, 3, 4, 8 };
1927 // Union<TSource> (IEnumerable<TSource>)
1928 AssertAreSame (expected, data1.Union (data2));
1930 // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1931 AssertAreSame (expected, data1.Union (data2, EqualityComparer<int>.Default));
1935 public void IntersectArgumentNullTest ()
1937 string [] data = { "2", "1", "5", "3", "4" };
1940 // Intersect<TSource> (IEnumerable<TSource>)
1941 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data); });
1942 AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null); });
1944 // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1945 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data, EqualityComparer<string>.Default); });
1946 AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
1950 public void IntersectTest ()
1952 int [] data1 = { 2, 1, 5, 7, 3, 4 };
1953 int [] data2 = { 1, 2, 3, 8, 4, 5 };
1954 int [] expected = { 2, 1, 5, 3, 4 };
1957 // Intersect<TSource> (IEnumerable<TSource>)
1958 AssertAreSame (expected, data1.Intersect (data2));
1960 // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1961 AssertAreSame (expected, data1.Intersect (data2, EqualityComparer<int>.Default));
1965 public void ExceptArgumentNullTest ()
1967 string [] data = { "2", "1", "5", "3", "4" };
1970 // Except<TSource> (IEnumerable<TSource>)
1971 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data); });
1972 AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null); });
1974 // Except<TSource> (IEnumerable<string>, IEqualityComparer<TSource>)
1975 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data, EqualityComparer<string>.Default); });
1976 AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
1980 public void ExceptTest ()
1982 int [] data1 = { 2, 1, 5, 7, 3, 4 };
1983 int [] data2 = { 1, 2, 3, 8, 4, 5 };
1984 int [] expected = { 7 };
1987 // Except<TSource> (IEnumerable<TSource>)
1988 AssertAreSame (expected, data1.Except (data2));
1990 // Except<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1991 AssertAreSame (expected, data1.Except (data2, EqualityComparer<int>.Default));
1995 public void ReverseArgumentNullTest ()
1997 //string [] data = { "2", "1", "5", "3", "4" };
2000 // Reverse<TSource> ()
2001 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Reverse (); });
2005 public void ReverseTest ()
2007 int [] data = { 2, 1, 5, 7, 3, 4 };
2008 int [] expected = { 4, 3, 7, 5, 1, 2 };
2012 // Reverse<TSource> ()
2013 AssertAreSame (expected, data.Reverse ());
2017 public void SequenceEqualArgumentNullTest ()
2019 string [] data = { "2", "1", "5", "3", "4" };
2022 // SequenceEqual<TSource> (IEnumerable<TSource>)
2023 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data); });
2024 AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null); });
2026 // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
2027 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data, EqualityComparer<string>.Default); });
2028 AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
2032 public void SequenceEqualTest ()
2034 int [] data1 = { 2, 1, 5, 7, 3, 4 };
2035 int [] data2 = { 2, 1, 5, 7, 3, 4 };
2036 int [] data3 = { 2, 1, 5, 7, 3, 4, 5 };
2037 int [] data4 = { 2, 1, 5, 7, 3 };
2038 int [] data5 = { 2, 1, 5, 8, 3, 4 };
2041 // SequenceEqual<TSource> (IEnumerable<TSource>)
2042 Assert.IsTrue (data1.SequenceEqual (data2));
2043 Assert.IsFalse (data1.SequenceEqual (data3));
2044 Assert.IsFalse (data1.SequenceEqual (data4));
2045 Assert.IsFalse (data1.SequenceEqual (data5));
2047 // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
2048 Assert.IsTrue (data1.SequenceEqual (data2, EqualityComparer<int>.Default));
2049 Assert.IsFalse (data1.SequenceEqual (data3, EqualityComparer<int>.Default));
2050 Assert.IsFalse (data1.SequenceEqual (data4, EqualityComparer<int>.Default));
2051 Assert.IsFalse (data1.SequenceEqual (data5, EqualityComparer<int>.Default));
2055 public void AsEnumerableArgumentNullTest ()
2057 //string [] data = { "2", "1", "5", "3", "4" };
2062 public void AsEnumerableTest ()
2064 int [] data = { 2, 1, 5, 7, 3, 4 };
2067 // AsEnumerable<TSource> ()
2068 Assert.AreSame (data, data.AsEnumerable ());
2072 public void ToArrayArgumentNullTest ()
2074 //string [] data = { "2", "1", "5", "3", "4" };
2077 // ToArray<TSource> ()
2078 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToArray (); });
2082 public void ToArrayTest ()
2084 int [] data = { 2, 3, 4, 5 };
2085 int [] expected = { 2, 3, 4, 5 };
2088 // ToArray<TSource> ()
2089 AssertAreSame (expected, data.ToArray ());
2093 public void ToListArgumentNullTest ()
2095 //string [] data = { "2", "1", "5", "3", "4" };
2098 // ToList<string> ()
2099 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToList (); });
2103 public void ToListTest ()
2105 int [] data = { 2, 4, 5, 1 };
2106 int [] expected = { 2, 4, 5, 1 };
2110 AssertAreSame (expected, data.ToList ());
2114 public void ToDictionaryArgumentNullTest ()
2116 string [] data = { "2", "1", "5", "3", "4" };
2119 // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
2120 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test"); });
2121 AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null); });
2123 // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
2124 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", EqualityComparer<string>.Default); });
2125 AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, EqualityComparer<string>.Default); });
2127 // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
2128 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test"); });
2129 AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test"); });
2130 AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null); });
2132 // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
2133 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test", EqualityComparer<string>.Default); });
2134 AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test", EqualityComparer<string>.Default); });
2135 AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null, EqualityComparer<string>.Default); });
2139 public void ToDictionaryTest ()
2141 string [] data = { "2", "1", "5", "3", "4" };
2142 Dictionary<string, string> expected = new Dictionary<string, string> ();
2143 expected.Add ("k2", "2");
2144 expected.Add ("k1", "1");
2145 expected.Add ("k5", "5");
2146 expected.Add ("k3", "3");
2147 expected.Add ("k4", "4");
2150 // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
2151 AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x));
2152 AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key"); });
2154 // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
2155 AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, EqualityComparer<string>.Default));
2156 AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", EqualityComparer<string>.Default); });
2158 // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
2159 AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x));
2160 AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x); });
2162 // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
2163 AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x, EqualityComparer<string>.Default));
2164 AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x, EqualityComparer<string>.Default); });
2168 public void ToLookupArgumentNullTest ()
2170 string [] data = { "2", "1", "5", "3", "4" };
2173 // ToLookup<string,string> (Func<string, string>)
2174 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test")); });
2175 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null); });
2177 // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
2178 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
2179 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
2181 // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
2182 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
2183 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
2184 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
2186 // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
2187 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); });
2188 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
2189 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
2193 public void ToLookupTest ()
2195 string [] data = { "23", "12", "55", "42", "41" };
2196 Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
2197 expected.Add ("2", new List<string> () { "23" });
2198 expected.Add ("1", new List<string> () { "12" });
2199 expected.Add ("5", new List<string> () { "55" });
2200 expected.Add ("4", new List<string> () { "42", "41" });
2202 Assert.AreEqual (expected.Count, ((IEnumerable<string>)data).ToLookup ((x => x [0].ToString ())).Count);
2204 // ToLookup<string,string> (Func<string, string>)
2205 AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup ((x => x [0].ToString ())));
2207 // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
2208 AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), EqualityComparer<string>.Default));
2210 // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
2211 AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x));
2213 // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
2214 AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x, EqualityComparer<string>.Default));
2218 public void ToLookupNullKeyTest ()
2220 string[] strs = new string[] { "one", null, "two", null, "three" };
2223 var l = strs.ToLookup (s => (s == null) ? null : "numbers", s => (s == null) ? (++i).ToString() : s);
2225 Assert.AreEqual (2, l.Count);
2226 Assert.AreEqual (2, l [null].Count());
2227 Assert.IsTrue (l [null].Contains ("1"));
2228 Assert.IsTrue (l [null].Contains ("2"));
2230 Assert.AreEqual (3, l ["numbers"].Count());
2231 Assert.IsTrue (l ["numbers"].Contains ("one"));
2232 Assert.IsTrue (l ["numbers"].Contains ("two"));
2233 Assert.IsTrue (l ["numbers"].Contains ("three"));
2237 public void DefaultIfEmptyArgumentNullTest ()
2239 //string [] data = { "2", "1", "5", "3", "4" };
2242 // DefaultIfEmpty<string> ()
2243 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> (); });
2245 // DefaultIfEmpty<string> (string)
2246 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> ((string) "default"); });
2250 public void DefaultIfEmptyTest ()
2252 string [] data = { "2", "1", "5", "3", "4" };
2253 string [] empty = { };
2254 string [] default1 = { null };
2255 string [] default2 = { "default" };
2258 // DefaultIfEmpty<string> ()
2259 AssertAreSame (data, data.DefaultIfEmpty ());
2260 AssertAreSame (default1, empty.DefaultIfEmpty ());
2262 // DefaultIfEmpty<string> (string)
2263 AssertAreSame (data, data.DefaultIfEmpty ("default"));
2264 AssertAreSame (default2, empty.DefaultIfEmpty ("default"));
2268 public void OfTypeArgumentNullTest ()
2270 //string [] data = { "2", "1", "5", "3", "4" };
2273 // OfType<string> ()
2274 AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).OfType<string> (); });
2278 public void OfTypeTest ()
2280 object [] data = { "2", 2, "1", "5", "3", "4" };
2281 string [] expected = { "2", "1", "5", "3", "4" };
2284 // OfType<string> ()
2285 AssertAreSame (expected, data.OfType<string> ());
2289 public void CastArgumentNullTest ()
2291 //string [] data = { "2", "1", "5", "3", "4" };
2295 AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).Cast<string> (); });
2299 public void CastTest ()
2301 object [] data = { 1, 2, 3 };
2302 int [] expected = { 1, 2, 3 };
2306 AssertAreSame (expected, data.Cast<int> ());
2307 AssertException<InvalidCastException> (delegate () { data.Cast<IEnumerable> ().GetEnumerator ().MoveNext (); });
2308 data.Cast<IEnumerable> ();
2312 public void RangeArgumentNullTest ()
2314 //string [] data = { "2", "1", "5", "3", "4" };
2319 public void RangeTest ()
2321 int [] expected = { 2, 3, 4, 5 };
2324 AssertAreSame (expected, Enumerable.Range (2, 4));
2325 AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (2, -3); });
2326 AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (int.MaxValue - 5, 7); });
2327 Enumerable.Range (int.MaxValue - 5, 6);
2331 public void ExceptMultipleItems ()
2333 var data = new [] { 1, 2, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10 };
2334 var expected = new [] { 2, 4, 6, 8, 10 };
2336 AssertAreSame (expected, data.Except (new [] { 1, 3, 5, 7, 9 }));