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); });
612 public void ContainsTest ()
614 int [] data = { 5, 2, 3, 1, 6 };
617 // Contains<TSource> (TSource)
618 Assert.IsTrue (data.Contains (2));
619 Assert.IsFalse (data.Contains (0));
621 // Contains<TSource> (TSource, IEqualityComparer<TSource>)
622 Assert.IsTrue (data.Contains (2, EqualityComparer<int>.Default));
623 Assert.IsFalse (data.Contains (0, EqualityComparer<int>.Default));
627 public void AggregateArgumentNullTest ()
629 string [] data = { "2", "1", "5", "3", "4" };
632 // Aggregate<TSource> (Func<TSource, TSource, TSource>)
633 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ((x, y) => "test"); });
634 AssertException<ArgumentNullException> (delegate () { data.Aggregate ((Func<string, string, string>) null); });
636 // Aggregate<TSource,TAccumulate> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>)
637 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ("initial", (x, y) => "test"); });
638 AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (Func<string, string, string>) null); });
640 // Aggregate<TSource,TAccumulate,TResult> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>)
641 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ("initial", (x, y) => "test", x => "test"); });
642 AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (Func<string, string, string>) null, x => "test"); });
643 AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (x, y) => "test", (Func<string, string>) null); });
647 public void AggregateTest ()
649 string [] data = { "2", "1", "5", "3", "4" };
650 string [] empty = { };
652 // Aggregate<TSource> (Func<TSource, TSource, TSource>)
653 Assert.AreEqual ("21534", data.Aggregate ((x, y) => x + y));
654 AssertException<InvalidOperationException> (delegate () { empty.Aggregate ((x, y) => x + y); }); //only this overload throws
656 // Aggregate<TSource,TAccumulate> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>)
657 Assert.AreEqual ("initial21534", (data.Aggregate ("initial", (x, y) => x + y)));
659 // Aggregate<TSource,TAccumulate,TResult> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>)
660 Assert.AreEqual ("INITIAL21534", data.Aggregate ("initial", (x, y) => x + y, (x => x.ToUpper ())));
664 public void SumArgumentNullTest ()
666 string [] data = { "2", "1", "5", "3", "4" };
669 // Sum<TSource> (Func<TSource, int>)
670 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, int>) (x => 0)); });
671 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, int>) null); });
673 // Sum<TSource> (Func<TSource, Nullable<int>>)
674 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
675 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<int>>) null); });
677 // Sum<TSource> (Func<TSource, Int64>)
678 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Int64>) (x => 0L)); });
679 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Int64>) null); });
681 // Sum<TSource> (Func<TSource, Nullable<Int64>>)
682 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
683 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Int64>>) null); });
685 // Sum<TSource> (Func<TSource, Single>)
686 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Single>) (x => 0f)); });
687 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Single>) null); });
689 // Sum<TSource> (Func<TSource, Nullable<Single>>)
690 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
691 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Single>>) null); });
693 // Sum<TSource> (Func<TSource, Double>)
694 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Double>) (x => 0d)); });
695 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Double>) null); });
697 // Sum<TSource> (Func<TSource, Nullable<Double>>)
698 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
699 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Double>>) null); });
701 // Sum<TSource> (Func<TSource, Decimal>)
702 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Decimal>) (x => 0m)); });
703 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Decimal>) null); });
705 // Sum<TSource> (Func<TSource, Nullable<Decimal>>)
706 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
707 AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Decimal>>) null); });
709 // Sum (IEnumerable<int>)
710 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Sum (); });
712 // Sum (IEnumerable<int?>)
713 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Sum (); });
715 // Sum (IEnumerable<long>)
716 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Sum (); });
718 // Sum (IEnumerable<long?>)
719 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Sum (); });
721 // Sum (IEnumerable<float>)
722 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Sum (); });
724 // Sum (IEnumerable<float?>)
725 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Sum (); });
727 // Sum (IEnumerable<double>)
728 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Sum (); });
730 // Sum (IEnumerable<double?>)
731 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Sum (); });
733 // Sum (IEnumerable<decimal>)
734 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Sum (); });
736 // Sum (IEnumerable<decimal?>)
737 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Sum (); });
741 public void SumTest ()
743 string [] data = { "2", "3", "5", "5" };
745 //TODO: OverflowException
747 // Sum<TSource> (Func<TSource, int>)
748 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, int>) (x => int.Parse (x))));
749 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, int>) (x => int.Parse (x))));
751 // Sum<TSource> (Func<TSource, Nullable<int>>)
752 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
753 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
755 // Sum<TSource> (Func<TSource, Int64>)
756 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Int64>) (x => int.Parse (x))));
757 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Int64>) (x => int.Parse (x))));
759 // Sum<TSource> (Func<TSource, Nullable<Int64>>)
760 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
761 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
763 // Sum<TSource> (Func<TSource, Single>)
764 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Single>) (x => int.Parse (x))));
765 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Single>) (x => int.Parse (x))));
767 // Sum<TSource> (Func<TSource, Nullable<Single>>)
768 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
769 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
771 // Sum<TSource> (Func<TSource, Double>)
772 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Double>) (x => int.Parse (x))));
773 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Double>) (x => int.Parse (x))));
775 // Sum<TSource> (Func<TSource, Nullable<Double>>)
776 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
777 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
779 // Sum<TSource> (Func<TSource, Decimal>)
780 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Decimal>) (x => int.Parse (x))));
781 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Decimal>) (x => int.Parse (x))));
783 // Sum<TSource> (Func<TSource, Nullable<Decimal>>)
784 Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
785 Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
788 Assert.AreEqual (6, ((IEnumerable<int>) new int [] { 1, 2, 3 }).Sum ());
789 Assert.AreEqual (0, Enumerable.Empty<int> ().Sum ());
792 Assert.AreEqual (6, ((IEnumerable<Nullable<int>>) new int? [] { 1, 2, 3 }).Sum ());
793 Assert.AreEqual (0, Enumerable.Empty<int?> ().Sum ());
796 Assert.AreEqual (6, ((IEnumerable<Int64>) new long [] { 1, 2, 3 }).Sum ());
797 Assert.AreEqual (0, Enumerable.Empty<long> ().Sum ());
800 Assert.AreEqual (6, ((IEnumerable<Nullable<Int64>>) new long? [] { 1, 2, 3 }).Sum ());
801 Assert.AreEqual (0, Enumerable.Empty<long?> ().Sum ());
804 Assert.AreEqual (6, ((IEnumerable<Single>) new float [] { 1, 2, 3 }).Sum ());
805 Assert.AreEqual (0, Enumerable.Empty<float> ().Sum ());
808 Assert.AreEqual (6, ((IEnumerable<Nullable<Single>>) new float? [] { 1, 2, 3 }).Sum ());
809 Assert.AreEqual (0, Enumerable.Empty<float?> ().Sum ());
812 Assert.AreEqual (6, ((IEnumerable<Double>) new double [] { 1, 2, 3 }).Sum ());
813 Assert.AreEqual (0, Enumerable.Empty<double> ().Sum ());
816 Assert.AreEqual (6, ((IEnumerable<Nullable<Double>>) new double? [] { 1, 2, 3 }).Sum ());
817 Assert.AreEqual (0, Enumerable.Empty<double?> ().Sum ());
820 Assert.AreEqual (6, ((IEnumerable<Decimal>) new decimal [] { 1, 2, 3 }).Sum ());
821 Assert.AreEqual (0, Enumerable.Empty<decimal> ().Sum ());
824 Assert.AreEqual (6, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 1, 2, 3 }).Sum ());
825 Assert.AreEqual (0, Enumerable.Empty<decimal?> ().Sum ());
829 public void MinArgumentNullTest ()
831 string [] data = { "2", "1", "5", "3", "4" };
835 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> (); });
837 // Min<TSource> (Func<TSource, int>)
838 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, int>) (x => 0)); });
839 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, int>) null); });
841 // Min<TSource> (Func<TSource, Nullable<int>>)
842 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
843 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<int>>) null); });
845 // Min<TSource> (Func<TSource, Int64>)
846 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Int64>) (x => 0L)); });
847 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Int64>) null); });
849 // Min<TSource> (Func<TSource, Nullable<Int64>>)
850 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
851 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Int64>>) null); });
853 // Min<TSource> (Func<TSource, Single>)
854 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Single>) (x => 0f)); });
855 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Single>) null); });
857 // Min<TSource> (Func<TSource, Nullable<Single>>)
858 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
859 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Single>>) null); });
861 // Min<TSource> (Func<TSource, Double>)
862 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Double>) (x => 0d)); });
863 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Double>) null); });
865 // Min<TSource> (Func<TSource, Nullable<Double>>)
866 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
867 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Double>>) null); });
869 // Min<TSource> (Func<TSource, Decimal>)
870 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Decimal>) (x => 0m)); });
871 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Decimal>) null); });
873 // Min<TSource> (Func<TSource, Nullable<Decimal>>)
874 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
875 AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Decimal>>) null); });
877 // Min<TSource,TSource> (Func<TSource, string>)
878 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string, string> ((Func<string, string>) (x => "test")); });
879 AssertException<ArgumentNullException> (delegate () { data.Min<string, string> ((Func<string, string>) null); });
882 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Min (); });
885 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Min (); });
888 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Min (); });
891 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Min (); });
894 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Min (); });
897 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Min (); });
900 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Min (); });
903 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Min (); });
906 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Min (); });
909 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Min (); });
913 public void MinTest ()
915 string [] data = { "2", "1", "5", "3", "4" };
919 Assert.AreEqual ("1", ((IEnumerable<string>) data).Min<string> ());
922 // Min<TSource> (Func<TSource, int>)
923 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, int>) (x => int.Parse (x))));
925 // Min<TSource> (Func<TSource, Nullable<int>>)
926 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
928 // Min<TSource> (Func<TSource, Int64>)
929 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Int64>) (x => int.Parse (x))));
931 // Min<TSource> (Func<TSource, Nullable<Int64>>)
932 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
934 // Min<TSource> (Func<TSource, Single>)
935 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Single>) (x => int.Parse (x))));
937 // Min<TSource> (Func<TSource, Nullable<Single>>)
938 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
940 // Min<TSource> (Func<TSource, Double>)
941 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Double>) (x => int.Parse (x))));
943 // Min<TSource> (Func<TSource, Nullable<Double>>)
944 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
946 // Min<TSource> (Func<TSource, Decimal>)
947 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Decimal>) (x => int.Parse (x))));
949 // Min<TSource> (Func<TSource, Nullable<Decimal>>)
950 Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
952 // Min<TSource,TSource> (Func<TSource, TSource>)
953 Assert.AreEqual ("1", ((IEnumerable<string>) data).Min<string, string> ((Func<string, string>) (x => x)));
956 Assert.AreEqual (2, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Min ());
959 Assert.AreEqual (2, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Min ());
962 Assert.AreEqual (2, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Min ());
965 Assert.AreEqual (2, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Min ());
968 Assert.AreEqual (2, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Min ());
971 Assert.AreEqual (2, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Min ());
974 Assert.AreEqual (2, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Min ());
977 Assert.AreEqual (2, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Min ());
980 Assert.AreEqual (2, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Min ());
983 Assert.AreEqual (2, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Min ());
987 public void MaxArgumentNullTest ()
989 string [] data = { "2", "1", "5", "3", "4" };
993 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> (); });
995 // Max<TSource> (Func<TSource, int>)
996 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, int>) (x => 0)); });
997 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, int>) null); });
999 // Max<TSource> (Func<TSource, Nullable<int>>)
1000 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
1001 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<int>>) null); });
1003 // Max<TSource> (Func<TSource, Int64>)
1004 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Int64>) (x => 0L)); });
1005 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Int64>) null); });
1007 // Max<TSource> (Func<TSource, Nullable<Int64>>)
1008 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
1009 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Int64>>) null); });
1011 // Max<TSource> (Func<TSource, Single>)
1012 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Single>) (x => 0f)); });
1013 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Single>) null); });
1015 // Max<TSource> (Func<TSource, Nullable<Single>>)
1016 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
1017 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Single>>) null); });
1019 // Max<TSource> (Func<TSource, Double>)
1020 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Double>) (x => 0d)); });
1021 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Double>) null); });
1023 // Max<TSource> (Func<TSource, Nullable<Double>>)
1024 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
1025 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Double>>) null); });
1027 // Max<TSource> (Func<TSource, Decimal>)
1028 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Decimal>) (x => 0m)); });
1029 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Decimal>) null); });
1031 // Max<TSource> (Func<TSource, Nullable<Decimal>>)
1032 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
1033 AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Decimal>>) null); });
1035 // Max<TSource,TSource> (Func<TSource, TSource>)
1036 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string, string> ((Func<string, string>) (x => "test")); });
1037 AssertException<ArgumentNullException> (delegate () { data.Max<string, string> ((Func<string, string>) null); });
1040 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Max (); });
1043 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Max (); });
1046 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Max (); });
1049 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Max (); });
1052 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Max (); });
1055 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Max (); });
1058 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Max (); });
1061 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Max (); });
1064 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Max (); });
1067 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Max (); });
1071 public void MaxTest ()
1073 string [] data = { "2", "1", "5", "3", "4" };
1077 Assert.AreEqual ("5", ((IEnumerable<string>) data).Max<string> ());
1079 // Max<TSource> (Func<TSource, int>)
1080 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, int>) (x => int.Parse (x))));
1082 // Max<TSource> (Func<TSource, Nullable<int>>)
1083 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
1085 // Max<TSource> (Func<TSource, Int64>)
1086 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Int64>) (x => int.Parse (x))));
1088 // Max<TSource> (Func<TSource, Nullable<Int64>>)
1089 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
1091 // Max<TSource> (Func<TSource, Single>)
1092 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Single>) (x => int.Parse (x))));
1094 // Max<TSource> (Func<TSource, Nullable<Single>>)
1095 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
1097 // Max<TSource> (Func<TSource, Double>)
1098 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Double>) (x => int.Parse (x))));
1100 // Max<TSource> (Func<TSource, Nullable<Double>>)
1101 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
1103 // Max<TSource> (Func<TSource, Decimal>)
1104 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Decimal>) (x => int.Parse (x))));
1106 // Max<TSource> (Func<TSource, Nullable<Decimal>>)
1107 Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
1109 // Max<TSource,TSource> (Func<TSource, TSource>)
1110 Assert.AreEqual ("5", ((IEnumerable<string>) data).Max<string, string> ((Func<string, string>) (x => x)));
1113 Assert.AreEqual (4, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Max ());
1116 Assert.AreEqual (4, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Max ());
1119 Assert.AreEqual (4, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Max ());
1122 Assert.AreEqual (4, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Max ());
1125 Assert.AreEqual (4, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Max ());
1128 Assert.AreEqual (4, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Max ());
1131 Assert.AreEqual (4, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Max ());
1134 Assert.AreEqual (4, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Max ());
1137 Assert.AreEqual (4, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Max ());
1140 Assert.AreEqual (4, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Max ());
1144 public void AverageArgumentNullTest ()
1146 string [] data = { "2", "1", "5", "3", "4" };
1149 // Average<TSource> (Func<TSource, int>)
1150 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, int>) (x => 0)); });
1151 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, int>) null); });
1153 // Average<TSource> (Func<TSource, Nullable<int>>)
1154 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
1155 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<int>>) null); });
1157 // Average<TSource> (Func<TSource, Int64>)
1158 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Int64>) (x => 0L)); });
1159 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Int64>) null); });
1161 // Average<TSource> (Func<TSource, Nullable<Int64>>)
1162 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
1163 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Int64>>) null); });
1165 // Average<TSource> (Func<TSource, Single>)
1166 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Single>) (x => 0f)); });
1167 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Single>) null); });
1169 // Average<TSource> (Func<TSource, Nullable<Single>>)
1170 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
1171 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Single>>) null); });
1173 // Average<TSource> (Func<TSource, Double>)
1174 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Double>) (x => 0d)); });
1175 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Double>) null); });
1177 // Average<TSource> (Func<TSource, Nullable<Double>>)
1178 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
1179 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Double>>) null); });
1181 // Average<TSource> (Func<TSource, Decimal>)
1182 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Decimal>) (x => 0m)); });
1183 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Decimal>) null); });
1185 // Average<TSource> (Func<TSource, Nullable<Decimal>>)
1186 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
1187 AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Decimal>>) null); });
1190 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Average (); });
1193 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Average (); });
1196 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Average (); });
1199 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Average (); });
1202 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Average (); });
1205 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Average (); });
1208 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Average (); });
1211 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Average (); });
1214 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Average (); });
1217 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Average (); });
1221 public void AverageTest ()
1223 string [] data = { "2", "1", "5", "3", "4" };
1224 string [] empty = { };
1226 // Average<string> (Func<string, int>)
1227 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, int>) (x => int.Parse (x))));
1228 AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, int>) (x => int.Parse (x))); });
1230 // Average<TSource> (Func<TSource, Nullable<int>>)
1231 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
1233 // Average<TSource> (Func<TSource, Int64>)
1234 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, long>) (x => int.Parse (x))));
1235 AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, long>) (x => int.Parse (x))); });
1237 // Average<TSource> (Func<TSource, Nullable<Int64>>)
1238 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, long?>) (x => (int?) int.Parse (x))));
1240 // Average<TSource> (Func<TSource, Single>)
1241 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, float>) (x => int.Parse (x))));
1242 AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, float>) (x => int.Parse (x))); });
1244 // Average<TSource> (Func<TSource, Nullable<Single>>)
1245 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, float?>) (x => (int?) int.Parse (x))));
1247 // Average<TSource> (Func<TSource, Double>)
1248 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, double>) (x => int.Parse (x))));
1249 AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, double>) (x => int.Parse (x))); });
1251 // Average<TSource> (Func<TSource, Nullable<Double>>)
1252 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, double?>) (x => (int?) int.Parse (x))));
1254 // Average<TSource> (Func<TSource, Decimal>)
1255 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, decimal>) (x => int.Parse (x))));
1256 AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, decimal>) (x => int.Parse (x))); });
1258 // Average<TSource> (Func<TSource, Nullable<Decimal>>)
1259 Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, decimal?>) (x => (int?) int.Parse (x))));
1263 Assert.AreEqual (3, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Average ());
1264 AssertException<InvalidOperationException> (delegate () { new int [0].Average (); });
1267 Assert.AreEqual (3, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Average ());
1270 Assert.AreEqual (3, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Average ());
1271 AssertException<InvalidOperationException> (delegate () { new long [0].Average (); });
1274 Assert.AreEqual (3, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Average ());
1277 Assert.AreEqual (3, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Average ());
1278 AssertException<InvalidOperationException> (delegate () { new float [0].Average (); });
1281 Assert.AreEqual (3, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Average ());
1284 Assert.AreEqual (3, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Average ());
1285 AssertException<InvalidOperationException> (delegate () { new double [0].Average (); });
1288 Assert.AreEqual (3, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Average ());
1291 Assert.AreEqual (3, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Average ());
1292 AssertException<InvalidOperationException> (delegate () { new decimal [0].Average (); });
1295 Assert.AreEqual (3, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Average ());
1299 public void WhereArgumentNullTest ()
1301 string [] data = { "2", "1", "5", "3", "4" };
1303 // Where<TSource> (Func<TSource, bool>)
1304 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Where (x => true); });
1305 AssertException<ArgumentNullException> (delegate () { data.Where ((Func<string, bool>) null); });
1307 // Where<TSource> (Func<TSource, int, bool>)
1308 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Where ((x, y) => true); });
1309 AssertException<ArgumentNullException> (delegate () { data.Where ((Func<string, int, bool>) null); });
1313 public void WhereTest ()
1315 int [] data = { 2, 1, 5, 3, 4 };
1316 int [] expected1 = { 2, 1 };
1317 int [] expected2 = { 2 };
1319 // Where<TSource> (Func<TSource, bool>)
1320 AssertAreSame (expected1, data.Where (x => x < 3));
1322 // Where<TSource> (Func<TSource, int, bool>)
1323 AssertAreSame (expected2, data.Where ((x, y) => x < 3 && y != 1));
1327 public void SelectArgumentNullTest ()
1329 string [] data = { "2", "1", "5", "3", "4" };
1332 // Select<TSource,TResult> (Func<TSource, TResult>)
1333 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Select (x => "test"); });
1334 AssertException<ArgumentNullException> (delegate () { data.Select ((Func<string, string>) null); });
1336 // Select<TSource,TResult> (Func<TSource, int, TResult>)
1337 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Select ((x, y) => "test"); });
1338 AssertException<ArgumentNullException> (delegate () { data.Select ((Func<string, int, string>) null); });
1342 public void SelectTest ()
1344 string [] data = { "2", "1", "5", "3", "4" };
1345 string [] expected1 = { "2x", "1x", "5x", "3x", "4x" };
1346 string [] expected2 = { "2x0", "1x1", "5x2", "3x3", "4x4" };
1349 // Select<TSource,TResult> (Func<TSource, TResult>)
1350 AssertAreSame (expected1, data.Select<string, string> (x => x + "x"));
1352 // Select<TSource,TResult> (Func<TSource, int, TResult>)
1353 AssertAreSame (expected2, data.Select<string, string> ((x, y) => x + "x" + y));
1357 public void SelectManyArgumentNullTest ()
1359 string [] data = { "2", "1", "5", "3", "4" };
1362 // SelectMany<TSource,TResult> (Func<TSource, IEnumerable<TResult>>)
1363 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany (x => data); });
1364 AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, IEnumerable<string>>) null); });
1366 // SelectMany<TSource,TResult> (Func<TSource, int, IEnumerable<TResult>>)
1367 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany ((x, y) => data); });
1368 AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, int, IEnumerable<string>>) null); });
1370 // SelectMany<TSource,TCollection,TResult> (Func<string, int, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
1371 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany ((x, y) => data, (x, y) => "test"); });
1372 AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, int, IEnumerable<string>>) null, (x, y) => "test"); });
1373 AssertException<ArgumentNullException> (delegate () { data.SelectMany ((x, y) => data, (Func<string, string, string>) null); });
1375 // SelectMany<TSource,TCollection,TResult> (Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
1376 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany (x => data, (x, y) => "test"); });
1377 AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, IEnumerable<string>>) null, (x, y) => "test"); });
1378 AssertException<ArgumentNullException> (delegate () { data.SelectMany (x => data, (Func<string, string, string>) null); });
1382 public void SelectManyTest ()
1384 string [] data = { "0", "1" };
1385 string [] expected = { "0", "00", "1", "11" };
1387 // SelectMany<TSource,TResult> (Func<TSource, IEnumerable<TResult>>)
1388 AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany (x => new string [] { x, x + x }));
1390 // SelectMany<TSource,TResult> (Func<TSource, int, IEnumerable<TResult>>)
1391 AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany ((x, y) => new string [] { x, x + y }));
1393 // SelectMany<TSource,TCollection,TResult> (Func<string, int, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
1394 AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany ((x, y) => new string [] { x, x + y }, (x, y) => y));
1396 // SelectMany<TSource,TCollection,TResult> (Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
1397 AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany (x => new string [] { x, x + x }, (x, y) => y));
1401 public void TakeArgumentNullTest ()
1403 //string [] data = { "2", "1", "5", "3", "4" };
1406 // Take<TSource> (int)
1407 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Take (0); });
1411 public void TakeTest ()
1413 int [] data = { 2, 1, 5, 3, 1 };
1414 int [] expected = { 2, 1 };
1417 // Take<TSource> (int)
1418 AssertAreSame (expected, data.Take (2));
1419 AssertAreSame (empty, data.Take (-2));
1423 public void TakeWhileArgumentNullTest ()
1425 string [] data = { "2", "1", "5", "3", "4" };
1428 // TakeWhile<TSource> (Func<TSource, bool>)
1429 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).TakeWhile (x => true); });
1430 AssertException<ArgumentNullException> (delegate () { data.TakeWhile ((Func<string, bool>) null); });
1432 // TakeWhile<TSource> (Func<TSource, int, bool>)
1433 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).TakeWhile ((x, y) => true); });
1434 AssertException<ArgumentNullException> (delegate () { data.TakeWhile ((Func<string, int, bool>) null); });
1438 public void TakeWhileTest ()
1440 int [] data = { 2, 1, 5, 3, 1 };
1441 int [] expected = { 2, 1 };
1444 // TakeWhile<TSource> (Func<TSource, bool>)
1445 AssertAreSame (expected, data.TakeWhile (x => x != 5));
1447 // TakeWhile<TSource> (Func<TSource, int, bool>)
1448 AssertAreSame (expected, data.TakeWhile ((x, y) => y != 2));
1452 public void SkipArgumentNullTest ()
1454 //string [] data = { "2", "1", "5", "3", "4" };
1457 // Skip<TSource> (int)
1458 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Skip (0); });
1462 public void SkipTest ()
1464 int [] data = { 2, 1, 5, 3, 1 };
1465 int [] expected = { 5, 3, 1 };
1467 // Skip<string> (TSource)
1468 AssertAreSame (expected, data.Skip (2));
1469 AssertAreSame (data, data.Skip (-2));
1473 public void SkipWhileArgumentNullTest ()
1475 string [] data = { "2", "1", "5", "3", "4" };
1478 // SkipWhile<TSource> (Func<TSource, bool>)
1479 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SkipWhile (x => true); });
1480 AssertException<ArgumentNullException> (delegate () { data.SkipWhile ((Func<string, bool>) null); });
1482 // SkipWhile<TSource> (Func<TSource, int, bool>)
1483 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SkipWhile ((x, y) => true); });
1484 AssertException<ArgumentNullException> (delegate () { data.SkipWhile ((Func<string, int, bool>) null); });
1488 public void SkipWhileTest ()
1490 int [] data = { 2, 1, 5, 3, 1 };
1491 int [] expected = { 5, 3, 1 };
1495 // SkipWhile<TSource> (Func<TSource, bool>)
1496 AssertAreSame (expected, data.SkipWhile (x => x != 5));
1498 // SkipWhile<TSource> (Func<TSource, int, bool>)
1499 AssertAreSame (expected, data.SkipWhile ((x, y) => y != 2));
1503 public void JoinArgumentNullTest ()
1505 string [] data = { "2", "1", "5", "3", "4" };
1508 // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)
1509 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Join (data, x => "test", x => "test", (x, y) => "test"); });
1510 AssertException<ArgumentNullException> (delegate () { data.Join ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test"); });
1511 AssertException<ArgumentNullException> (delegate () { data.Join (data, (Func<string, string>) null, x => "test", (x, y) => "test"); });
1512 AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", (Func<string, string>) null, (x, y) => "test"); });
1513 AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", x => "test", (Func<string, string, string>) null); });
1515 // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<string>)
1516 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Join (data, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1517 AssertException<ArgumentNullException> (delegate () { data.Join ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1518 AssertException<ArgumentNullException> (delegate () { data.Join (data, (Func<string, string>) null, x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1519 AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", (Func<string, string>) null, (x, y) => "test", EqualityComparer<string>.Default); });
1520 AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", x => "test", (Func<string, string, string>) null, EqualityComparer<string>.Default); });
1524 public void JoinTest ()
1526 string [] dataOuter1 = { "2", "1", "5", "3", "4" };
1527 string [] dataInner1 = { "7", "3", "5", "8", "9" };
1528 string [] expected1 = { "55", "33" };
1530 string [] dataOuter2 = { "2", "1", "3", "4" };
1531 string [] dataInner2 = { "7", "5", "8", "9" };
1532 string [] expected2 = { };
1535 // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)
1536 AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y));
1537 AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y));
1539 // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<string>)
1540 AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y, EqualityComparer<string>.Default));
1541 AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y, EqualityComparer<string>.Default));
1545 public void GroupJoinArgumentNullTest ()
1547 string [] data = { "2", "1", "5", "3", "4" };
1550 // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
1551 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test"); });
1552 AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test"); });
1553 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test"); });
1554 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test"); });
1555 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null); });
1557 // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
1558 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1559 AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1560 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
1561 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test", EqualityComparer<string>.Default); });
1562 AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null, EqualityComparer<string>.Default); });
1566 public void GroupJoinTest ()
1568 string [] dataOuter1 = { "2", "1", "5", "3", "4" };
1569 string [] dataInner1 = { "7", "3", "5", "3", "9" };
1570 string [] expected1 = { "2", "1", "55", "333", "4" };
1572 string [] dataOuter2 = { "2", "1", "5", "8", "4" };
1573 string [] dataInner2 = { "7", "3", "6", "3", "9" };
1574 string [] expected2 = { "2", "1", "5", "8", "4" };
1577 // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
1578 AssertAreSame (expected1, (dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
1579 AssertAreSame (expected2, (dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
1581 // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
1582 AssertAreSame (expected1, dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
1583 AssertAreSame (expected2, dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
1587 public void OrderByArgumentNullTest ()
1589 string [] data = { "2", "1", "5", "3", "4" };
1592 // OrderBy<TSource,TKey> (Func<TSource, TKey>)
1593 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test"); });
1594 AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null); });
1596 // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1597 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test", Comparer<string>.Default); });
1598 AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null, Comparer<string>.Default); });
1602 public void OrderByTest ()
1604 int [] data = { 2, 1, 5, 3, 4 };
1605 int [] expected = { 1, 2, 3, 4, 5 };
1608 // OrderBy<TSource,TKey> (Func<TSource, TKey>)
1609 AssertAreSame (expected, data.OrderBy (x => x));
1611 // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1612 AssertAreSame (expected, data.OrderBy (x => x, Comparer<int>.Default));
1616 public void OrderByDescendingArgumentNullTest ()
1618 string [] data = { "2", "1", "5", "3", "4" };
1621 // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
1622 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test"); });
1623 AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null); });
1625 // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1626 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test", Comparer<string>.Default); });
1627 AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null, Comparer<string>.Default); });
1631 public void OrderByDescendingTest ()
1633 int [] data = { 2, 1, 5, 3, 4 };
1634 int [] expected = { 5, 4, 3, 2, 1 };
1637 // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
1638 AssertAreSame (expected, data.OrderByDescending (x => x));
1640 // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1641 AssertAreSame (expected, data.OrderByDescending (x => x, Comparer<int>.Default));
1645 public void ThenByArgumentNullTest ()
1647 string [] data = { "2", "1", "5", "3", "4" };
1650 // ThenBy<TSource,TKey> (Func<TSource, TKey>)
1651 AssertException<ArgumentNullException> (delegate () {
1652 ((IOrderedEnumerable<string>) null).ThenBy (x => "test");
1654 AssertException<ArgumentNullException> (delegate () {
1655 data.OrderBy (x => x).ThenBy ((Func<string, string>) null);
1658 // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1659 AssertException<ArgumentNullException> (delegate () {
1661 ((IOrderedEnumerable<string>) null).ThenBy (x => "test", Comparer<string>.Default);
1663 AssertException<ArgumentNullException> (delegate () {
1665 data.OrderBy (x => x).ThenBy ((Func<string, string>) null, Comparer<string>.Default);
1670 public void ThenByTest ()
1672 int [] data = { 2, 1, 5, 3, 4 };
1673 int [] expected = { 1, 2, 3, 4, 5 };
1676 // ThenBy<TSource,TKey> (Func<TSource, TKey>)
1677 AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x));
1679 // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1680 AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x, Comparer<int>.Default));
1684 public void ThenByDescendingArgumentNullTest ()
1686 string [] data = { "2", "1", "5", "3", "4" };
1689 // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
1690 AssertException<ArgumentNullException> (delegate () {
1691 ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test");
1693 AssertException<ArgumentNullException> (delegate () {
1694 data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null);
1697 // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1698 AssertException<ArgumentNullException> (delegate () {
1700 ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test", Comparer<string>.Default);
1702 AssertException<ArgumentNullException> (delegate () {
1704 data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null, Comparer<string>.Default);
1709 public void ThenByDescendingTest ()
1711 int [] data = { 2, 1, 5, 3, 4 };
1712 int [] expected = { 5, 4, 3, 2, 1 };
1715 // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
1716 AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x));
1718 // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
1719 AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x, Comparer<int>.Default));
1723 public void GroupByArgumentNullTest ()
1725 string [] data = { "2", "1", "5", "3", "4" };
1728 // GroupBy<string,string> (Func<string, string>)
1729 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test")); });
1730 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null); });
1732 // GroupBy<string,string> (Func<string, string>, IEqualityComparer<string>)
1733 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1734 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1736 // GroupBy<string,string,string> (Func<string, string>, Func<string, string>)
1737 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
1738 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
1739 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
1741 // GroupBy<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
1742 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); });
1743 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1744 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1746 // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>)
1747 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
1748 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
1749 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null); });
1751 // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>)
1752 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")); });
1753 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")); });
1754 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")); });
1755 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); });
1757 // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
1758 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); });
1759 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); });
1760 AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
1762 // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
1763 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); });
1764 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); });
1765 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); });
1766 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); });
1770 public void GroupByTest ()
1772 string [] data = { "2", "1", "5", "3", "4", "3" };
1774 Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
1775 expected.Add ("2", new List<string> () { "2" });
1776 expected.Add ("1", new List<string> () { "1" });
1777 expected.Add ("5", new List<string> () { "5" });
1778 expected.Add ("3", new List<string> () { "3", "3" });
1779 expected.Add ("4", new List<string> () { "4" });
1781 Dictionary<string, IEnumerable<string>> expected2 = new Dictionary<string, IEnumerable<string>> ();
1782 expected2.Add ("2", new List<string> () { "22" });
1783 expected2.Add ("1", new List<string> () { "11" });
1784 expected2.Add ("5", new List<string> () { "55" });
1785 expected2.Add ("3", new List<string> () { "33", "33" });
1786 expected2.Add ("4", new List<string> () { "44" });
1788 string [] expected3 = new string [] { "22", "11", "55", "333", "44" };
1790 // GroupBy<int,int> (Func<int, int>)
1791 AssertAreSame (expected, data.GroupBy (x => x));
1793 // GroupBy<int,int> (Func<int, int>, IEqualityComparer<int>)
1794 AssertAreSame (expected, data.GroupBy (x => x, EqualityComparer<string>.Default));
1796 // GroupBy<int,int,int> (Func<int, int>, Func<int, int>)
1797 AssertAreSame (expected2, data.GroupBy (x => x, x => x + x));
1799 // GroupBy<int,int,int> (Func<int, int>, Func<int, int>, IEqualityComparer<int>)
1800 AssertAreSame (expected2, data.GroupBy (x => x, x => x + x, EqualityComparer<string>.Default));
1802 // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>)
1803 AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
1805 // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>)
1806 AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
1808 // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
1809 AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
1811 // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
1812 AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
1819 public string String;
1821 public Data (int number, string str)
1829 public void GroupByLastNullGroup ()
1831 var values = new List<Data> ();
1833 values.Add (new Data (0, "a"));
1834 values.Add (new Data (1, "a"));
1835 values.Add (new Data (2, "b"));
1836 values.Add (new Data (3, "b"));
1837 values.Add (new Data (4, null));
1839 var groups = values.GroupBy (d => d.String);
1841 Assert.AreEqual (3, groups.Count ());
1845 public void ConcatArgumentNullTest ()
1847 string [] data = { "2", "1", "5", "3", "4" };
1849 // Concat<TSource> (IEnumerable<TSource>)
1850 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Concat (data); });
1851 AssertException<ArgumentNullException> (delegate () { data.Concat ((IEnumerable<string>) null); });
1855 public void ConcatTest ()
1857 int [] data1 = { 2, 1, 5, 3, 4 };
1858 int [] data2 = { 1, 2, 3, 4, 5 };
1859 int [] expected = { 2, 1, 5, 3, 4, 1, 2, 3, 4, 5 };
1862 // Concat<TSource> (IEnumerable<TSource>)
1863 AssertAreSame (expected, data1.Concat (data2));
1867 public void DistinctArgumentNullTest ()
1869 //string [] data = { "2", "1", "5", "3", "4" };
1872 // Distinct<TSource> ()
1873 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (); });
1875 // Distinct<TSource> (IEqualityComparer<TSource>)
1876 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (EqualityComparer<string>.Default); });
1880 public void DistinctTest ()
1882 int [] data = { 2, 1, 5, 3, 4, 2, 5, 3, 1, 8 };
1883 int [] expected = { 2, 1, 5, 3, 4, 8 };
1886 // Distinct<TSource> ()
1887 AssertAreSame (expected, data.Distinct ());
1889 // Distinct<iTSourcent> (IEqualityComparer<TSource>)
1890 AssertAreSame (expected, data.Distinct (EqualityComparer<int>.Default));
1894 public void UnionArgumentNullTest ()
1896 string [] data = { "2", "1", "5", "3", "4" };
1899 // Union<TSource> (IEnumerable<TSource>)
1900 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data); });
1901 AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null); });
1903 // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1904 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data, EqualityComparer<string>.Default); });
1905 AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
1909 public void UnionTest ()
1911 int [] data1 = { 2, 1, 5, 7, 3, 4 };
1912 int [] data2 = { 1, 2, 3, 8, 4, 5 };
1913 int [] expected = { 2, 1, 5, 7, 3, 4, 8 };
1915 // Union<TSource> (IEnumerable<TSource>)
1916 AssertAreSame (expected, data1.Union (data2));
1918 // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1919 AssertAreSame (expected, data1.Union (data2, EqualityComparer<int>.Default));
1923 public void IntersectArgumentNullTest ()
1925 string [] data = { "2", "1", "5", "3", "4" };
1928 // Intersect<TSource> (IEnumerable<TSource>)
1929 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data); });
1930 AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null); });
1932 // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1933 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data, EqualityComparer<string>.Default); });
1934 AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
1938 public void IntersectTest ()
1940 int [] data1 = { 2, 1, 5, 7, 3, 4 };
1941 int [] data2 = { 1, 2, 3, 8, 4, 5 };
1942 int [] expected = { 2, 1, 5, 3, 4 };
1945 // Intersect<TSource> (IEnumerable<TSource>)
1946 AssertAreSame (expected, data1.Intersect (data2));
1948 // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1949 AssertAreSame (expected, data1.Intersect (data2, EqualityComparer<int>.Default));
1953 public void ExceptArgumentNullTest ()
1955 string [] data = { "2", "1", "5", "3", "4" };
1958 // Except<TSource> (IEnumerable<TSource>)
1959 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data); });
1960 AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null); });
1962 // Except<TSource> (IEnumerable<string>, IEqualityComparer<TSource>)
1963 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data, EqualityComparer<string>.Default); });
1964 AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
1968 public void ExceptTest ()
1970 int [] data1 = { 2, 1, 5, 7, 3, 4 };
1971 int [] data2 = { 1, 2, 3, 8, 4, 5 };
1972 int [] expected = { 7 };
1975 // Except<TSource> (IEnumerable<TSource>)
1976 AssertAreSame (expected, data1.Except (data2));
1978 // Except<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
1979 AssertAreSame (expected, data1.Except (data2, EqualityComparer<int>.Default));
1983 public void ReverseArgumentNullTest ()
1985 //string [] data = { "2", "1", "5", "3", "4" };
1988 // Reverse<TSource> ()
1989 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Reverse (); });
1993 public void ReverseTest ()
1995 int [] data = { 2, 1, 5, 7, 3, 4 };
1996 int [] expected = { 4, 3, 7, 5, 1, 2 };
2000 // Reverse<TSource> ()
2001 AssertAreSame (expected, data.Reverse ());
2005 public void SequenceEqualArgumentNullTest ()
2007 string [] data = { "2", "1", "5", "3", "4" };
2010 // SequenceEqual<TSource> (IEnumerable<TSource>)
2011 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data); });
2012 AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null); });
2014 // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
2015 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data, EqualityComparer<string>.Default); });
2016 AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
2020 public void SequenceEqualTest ()
2022 int [] data1 = { 2, 1, 5, 7, 3, 4 };
2023 int [] data2 = { 2, 1, 5, 7, 3, 4 };
2024 int [] data3 = { 2, 1, 5, 7, 3, 4, 5 };
2025 int [] data4 = { 2, 1, 5, 7, 3 };
2026 int [] data5 = { 2, 1, 5, 8, 3, 4 };
2029 // SequenceEqual<TSource> (IEnumerable<TSource>)
2030 Assert.IsTrue (data1.SequenceEqual (data2));
2031 Assert.IsFalse (data1.SequenceEqual (data3));
2032 Assert.IsFalse (data1.SequenceEqual (data4));
2033 Assert.IsFalse (data1.SequenceEqual (data5));
2035 // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
2036 Assert.IsTrue (data1.SequenceEqual (data2, EqualityComparer<int>.Default));
2037 Assert.IsFalse (data1.SequenceEqual (data3, EqualityComparer<int>.Default));
2038 Assert.IsFalse (data1.SequenceEqual (data4, EqualityComparer<int>.Default));
2039 Assert.IsFalse (data1.SequenceEqual (data5, EqualityComparer<int>.Default));
2043 public void AsEnumerableArgumentNullTest ()
2045 //string [] data = { "2", "1", "5", "3", "4" };
2050 public void AsEnumerableTest ()
2052 int [] data = { 2, 1, 5, 7, 3, 4 };
2055 // AsEnumerable<TSource> ()
2056 Assert.AreSame (data, data.AsEnumerable ());
2060 public void ToArrayArgumentNullTest ()
2062 //string [] data = { "2", "1", "5", "3", "4" };
2065 // ToArray<TSource> ()
2066 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToArray (); });
2070 public void ToArrayTest ()
2072 int [] data = { 2, 3, 4, 5 };
2073 int [] expected = { 2, 3, 4, 5 };
2076 // ToArray<TSource> ()
2077 AssertAreSame (expected, data.ToArray ());
2081 public void ToListArgumentNullTest ()
2083 //string [] data = { "2", "1", "5", "3", "4" };
2086 // ToList<string> ()
2087 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToList (); });
2091 public void ToListTest ()
2093 int [] data = { 2, 4, 5, 1 };
2094 int [] expected = { 2, 4, 5, 1 };
2098 AssertAreSame (expected, data.ToList ());
2102 public void ToDictionaryArgumentNullTest ()
2104 string [] data = { "2", "1", "5", "3", "4" };
2107 // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
2108 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test"); });
2109 AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null); });
2111 // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
2112 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", EqualityComparer<string>.Default); });
2113 AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, EqualityComparer<string>.Default); });
2115 // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
2116 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test"); });
2117 AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test"); });
2118 AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null); });
2120 // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
2121 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test", EqualityComparer<string>.Default); });
2122 AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test", EqualityComparer<string>.Default); });
2123 AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null, EqualityComparer<string>.Default); });
2127 public void ToDictionaryTest ()
2129 string [] data = { "2", "1", "5", "3", "4" };
2130 Dictionary<string, string> expected = new Dictionary<string, string> ();
2131 expected.Add ("k2", "2");
2132 expected.Add ("k1", "1");
2133 expected.Add ("k5", "5");
2134 expected.Add ("k3", "3");
2135 expected.Add ("k4", "4");
2138 // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
2139 AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x));
2140 AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key"); });
2142 // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
2143 AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, EqualityComparer<string>.Default));
2144 AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", EqualityComparer<string>.Default); });
2146 // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
2147 AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x));
2148 AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x); });
2150 // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
2151 AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x, EqualityComparer<string>.Default));
2152 AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x, EqualityComparer<string>.Default); });
2156 public void ToLookupArgumentNullTest ()
2158 string [] data = { "2", "1", "5", "3", "4" };
2161 // ToLookup<string,string> (Func<string, string>)
2162 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test")); });
2163 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null); });
2165 // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
2166 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
2167 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
2169 // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
2170 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
2171 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
2172 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
2174 // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
2175 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); });
2176 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
2177 AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
2181 public void ToLookupTest ()
2183 string [] data = { "23", "12", "55", "42", "41" };
2184 Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
2185 expected.Add ("2", new List<string> () { "23" });
2186 expected.Add ("1", new List<string> () { "12" });
2187 expected.Add ("5", new List<string> () { "55" });
2188 expected.Add ("4", new List<string> () { "42", "41" });
2190 Assert.AreEqual (expected.Count, ((IEnumerable<string>)data).ToLookup ((x => x [0].ToString ())).Count);
2192 // ToLookup<string,string> (Func<string, string>)
2193 AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup ((x => x [0].ToString ())));
2195 // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
2196 AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), EqualityComparer<string>.Default));
2198 // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
2199 AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x));
2201 // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
2202 AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x, EqualityComparer<string>.Default));
2206 public void ToLookupNullKeyTest ()
2208 string[] strs = new string[] { "one", null, "two", null, "three" };
2211 var l = strs.ToLookup (s => (s == null) ? null : "numbers", s => (s == null) ? (++i).ToString() : s);
2213 Assert.AreEqual (2, l.Count);
2214 Assert.AreEqual (2, l [null].Count());
2215 Assert.IsTrue (l [null].Contains ("1"));
2216 Assert.IsTrue (l [null].Contains ("2"));
2218 Assert.AreEqual (3, l ["numbers"].Count());
2219 Assert.IsTrue (l ["numbers"].Contains ("one"));
2220 Assert.IsTrue (l ["numbers"].Contains ("two"));
2221 Assert.IsTrue (l ["numbers"].Contains ("three"));
2225 public void DefaultIfEmptyArgumentNullTest ()
2227 //string [] data = { "2", "1", "5", "3", "4" };
2230 // DefaultIfEmpty<string> ()
2231 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> (); });
2233 // DefaultIfEmpty<string> (string)
2234 AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> ((string) "default"); });
2238 public void DefaultIfEmptyTest ()
2240 string [] data = { "2", "1", "5", "3", "4" };
2241 string [] empty = { };
2242 string [] default1 = { null };
2243 string [] default2 = { "default" };
2246 // DefaultIfEmpty<string> ()
2247 AssertAreSame (data, data.DefaultIfEmpty ());
2248 AssertAreSame (default1, empty.DefaultIfEmpty ());
2250 // DefaultIfEmpty<string> (string)
2251 AssertAreSame (data, data.DefaultIfEmpty ("default"));
2252 AssertAreSame (default2, empty.DefaultIfEmpty ("default"));
2256 public void OfTypeArgumentNullTest ()
2258 //string [] data = { "2", "1", "5", "3", "4" };
2261 // OfType<string> ()
2262 AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).OfType<string> (); });
2266 public void OfTypeTest ()
2268 object [] data = { "2", 2, "1", "5", "3", "4" };
2269 string [] expected = { "2", "1", "5", "3", "4" };
2272 // OfType<string> ()
2273 AssertAreSame (expected, data.OfType<string> ());
2277 public void CastArgumentNullTest ()
2279 //string [] data = { "2", "1", "5", "3", "4" };
2283 AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).Cast<string> (); });
2287 public void CastTest ()
2289 object [] data = { 1, 2, 3 };
2290 int [] expected = { 1, 2, 3 };
2294 AssertAreSame (expected, data.Cast<int> ());
2295 AssertException<InvalidCastException> (delegate () { data.Cast<IEnumerable> ().GetEnumerator ().MoveNext (); });
2296 data.Cast<IEnumerable> ();
2300 public void RangeArgumentNullTest ()
2302 //string [] data = { "2", "1", "5", "3", "4" };
2307 public void RangeTest ()
2309 int [] expected = { 2, 3, 4, 5 };
2312 AssertAreSame (expected, Enumerable.Range (2, 4));
2313 AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (2, -3); });
2314 AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (int.MaxValue - 5, 7); });
2315 Enumerable.Range (int.MaxValue - 5, 6);
2319 public void ExceptMultipleItems ()
2321 var data = new [] { 1, 2, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10 };
2322 var expected = new [] { 2, 4, 6, 8, 10 };
2324 AssertAreSame (expected, data.Except (new [] { 1, 3, 5, 7, 9 }));