EnumerableMoreTest.cs 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  1. //
  2. // EnumerableMoreTest.cs
  3. //
  4. // Author:
  5. // Andreas Noever <[email protected]>
  6. //
  7. // (C) 2007 Andreas Noever
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Linq;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Linq {
  34. [TestFixture]
  35. public class EnumerableMoreTest {
  36. class BigEnumerable : IEnumerable<int> {
  37. public readonly ulong Count;
  38. public BigEnumerable (ulong Count)
  39. {
  40. this.Count = Count;
  41. }
  42. #region IEnumerable<int> Members
  43. public IEnumerator<int> GetEnumerator ()
  44. {
  45. return new BigEnumerator (this);
  46. }
  47. #endregion
  48. #region IEnumerable Members
  49. IEnumerator IEnumerable.GetEnumerator ()
  50. {
  51. throw new NotImplementedException ();
  52. }
  53. #endregion
  54. }
  55. class BigEnumerator : IEnumerator<int> {
  56. BigEnumerable Parent;
  57. private ulong current;
  58. public BigEnumerator (BigEnumerable parent)
  59. {
  60. Parent = parent;
  61. }
  62. public int Current
  63. {
  64. get { return 3; }
  65. }
  66. public void Dispose ()
  67. {
  68. }
  69. object IEnumerator.Current
  70. {
  71. get { throw new NotImplementedException (); }
  72. }
  73. public bool MoveNext ()
  74. {
  75. if (current == Parent.Count)
  76. return false;
  77. current++;
  78. return true;
  79. }
  80. public void Reset ()
  81. {
  82. throw new NotImplementedException ();
  83. }
  84. }
  85. public static void AssertException<T> (Action action) where T : Exception
  86. {
  87. try {
  88. action ();
  89. }
  90. catch (T) {
  91. return;
  92. }
  93. Assert.Fail ("Expected: " + typeof (T).Name);
  94. }
  95. static void AssertAreSame<K, V> (K expectedKey, IEnumerable<V> expectedValues, IGrouping<K, V> actual)
  96. {
  97. if (expectedValues == null) {
  98. Assert.IsNull (actual);
  99. return;
  100. }
  101. Assert.IsNotNull (actual);
  102. Assert.AreEqual (expectedKey, actual.Key);
  103. var ee = expectedValues.GetEnumerator ();
  104. var ea = actual.GetEnumerator ();
  105. while (ee.MoveNext ()) {
  106. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
  107. Assert.AreEqual (ee.Current, ea.Current);
  108. }
  109. if (ea.MoveNext ())
  110. Assert.Fail ("Unexpected element: " + ee.Current);
  111. }
  112. static void AssertAreSame<K, V> (IDictionary<K, IEnumerable<V>> expected, IEnumerable<IGrouping<K, V>> actual)
  113. {
  114. if (expected == null) {
  115. Assert.IsNull (actual);
  116. return;
  117. }
  118. Assert.IsNotNull (actual);
  119. var ee = expected.GetEnumerator ();
  120. var ea = actual.GetEnumerator ();
  121. while (ee.MoveNext ()) {
  122. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + "' expected.");
  123. AssertAreSame (ee.Current.Key, ee.Current.Value, ea.Current);
  124. }
  125. if (ea.MoveNext ())
  126. Assert.Fail ("Unexpected element: " + ee.Current.Key);
  127. }
  128. static void AssertAreSame<K, V> (IDictionary<K, IEnumerable<V>> expected, ILookup<K, V> actual)
  129. {
  130. if (expected == null) {
  131. Assert.IsNull (actual);
  132. return;
  133. }
  134. Assert.IsNotNull (actual);
  135. var ee = expected.GetEnumerator ();
  136. var ea = actual.GetEnumerator ();
  137. while (ee.MoveNext ()) {
  138. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + "' expected.");
  139. AssertAreSame (ee.Current.Key, ee.Current.Value, ea.Current);
  140. }
  141. if (ea.MoveNext ())
  142. Assert.Fail ("Unexpected element: " + ee.Current.Key);
  143. }
  144. static void AssertAreSame<K, V> (IDictionary<K, V> expected, IDictionary<K, V> actual)
  145. {
  146. if (expected == null) {
  147. Assert.IsNull (actual);
  148. return;
  149. }
  150. Assert.IsNotNull (actual);
  151. var ee = expected.GetEnumerator ();
  152. var ea = actual.GetEnumerator ();
  153. while (ee.MoveNext ()) {
  154. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + ", " + ee.Current.Value + "' expected.");
  155. Assert.AreEqual (ee.Current.Key, ea.Current.Key);
  156. Assert.AreEqual (ee.Current.Value, ea.Current.Value);
  157. }
  158. if (ea.MoveNext ())
  159. Assert.Fail ("Unexpected element: " + ee.Current.Key + ", " + ee.Current.Value);
  160. }
  161. static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
  162. {
  163. if (expected == null) {
  164. Assert.IsNull (actual);
  165. return;
  166. }
  167. Assert.IsNotNull (actual);
  168. IEnumerator<T> ee = expected.GetEnumerator ();
  169. IEnumerator<T> ea = actual.GetEnumerator ();
  170. while (ee.MoveNext ()) {
  171. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
  172. Assert.AreEqual (ee.Current, ea.Current);
  173. }
  174. if (ea.MoveNext ())
  175. Assert.Fail ("Unexpected element: " + ea.Current);
  176. }
  177. [Test]
  178. public void FirstArgumentNullTest ()
  179. {
  180. string [] data = { "2", "1", "5", "3", "4" };
  181. // First<TSource> ()
  182. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).First (); });
  183. // First<TSource> (Func<TSource, bool>)
  184. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).First ((x => true)); });
  185. AssertException<ArgumentNullException> (delegate () { data.First ((Func<string, bool>) null); });
  186. }
  187. [Test]
  188. public void FirstTest ()
  189. {
  190. int [] data = { 2, 1, 5, 3, 4 };
  191. int [] empty = { };
  192. // First<TSource> ()
  193. Assert.AreEqual (2, data.First ());
  194. AssertException<InvalidOperationException> (delegate () { empty.First (); });
  195. // First<TSource> (Func<TSource, bool>)
  196. Assert.AreEqual (5, data.First (x => x == 5));
  197. AssertException<InvalidOperationException> (delegate () { empty.First (x => x == 5); });
  198. AssertException<InvalidOperationException> (delegate () { data.First (x => x == 6); });
  199. }
  200. [Test]
  201. public void FirstOrDefaultArgumentNullTest ()
  202. {
  203. string [] data = { "2", "1", "5", "3", "4" };
  204. // FirstOrDefault<TSource> ()
  205. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).FirstOrDefault (); });
  206. // FirstOrDefault<TSource> (Func<string, bool>)
  207. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).FirstOrDefault ((x => true)); });
  208. AssertException<ArgumentNullException> (delegate () { data.FirstOrDefault ((Func<string, bool>) null); });
  209. }
  210. [Test]
  211. public void FirstOrDefaultTest ()
  212. {
  213. int [] data = { 2, 1, 5, 3, 4 };
  214. int [] empty = { };
  215. // FirstOrDefault<TSource> ()
  216. Assert.AreEqual (2, data.FirstOrDefault ());
  217. Assert.AreEqual (0, empty.FirstOrDefault ());
  218. // FirstOrDefault<TSource> (Func<TSource, bool>)
  219. Assert.AreEqual (5, data.FirstOrDefault (x => x == 5));
  220. Assert.AreEqual (0, empty.FirstOrDefault (x => x == 5));
  221. Assert.AreEqual (0, data.FirstOrDefault (x => x == 6));
  222. }
  223. [Test]
  224. public void LastArgumentNullTest ()
  225. {
  226. string [] data = { "2", "1", "5", "3", "4" };
  227. // Last<TSource> ()
  228. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Last (); });
  229. // Last<TSource> (Func<TSource, bool>)
  230. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Last (x => true); });
  231. AssertException<ArgumentNullException> (delegate () { data.Last ((Func<string, bool>) null); });
  232. }
  233. [Test]
  234. public void LastTest ()
  235. {
  236. int [] data = { 2, 1, 1, 3, 4, 5 };
  237. int [] empty = { };
  238. // Last<TSource> ()
  239. Assert.AreEqual (5, data.Last ());
  240. AssertException<InvalidOperationException> (delegate () { empty.Last (); });
  241. // Last<TSource> (Func<TSource, bool>)
  242. Assert.AreEqual (4, data.Last (x => x < 5));
  243. AssertException<InvalidOperationException> (delegate () { empty.Last (x => x == 5); });
  244. AssertException<InvalidOperationException> (delegate () { data.Last (x => x == 6); });
  245. }
  246. [Test]
  247. public void LastOrDefaultArgumentNullTest ()
  248. {
  249. string [] data = { "2", "1", "5", "3", "4" };
  250. // LastOrDefault<TSource> ()
  251. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LastOrDefault (); });
  252. // LastOrDefault<TSource> (Func<TSource, bool>)
  253. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LastOrDefault (x => true); });
  254. AssertException<ArgumentNullException> (delegate () { data.LastOrDefault ((Func<string, bool>) null); });
  255. }
  256. [Test]
  257. public void LastOrDefaultTest ()
  258. {
  259. int [] data = { 2, 1, 5, 3, 4 };
  260. int [] empty = { };
  261. // LastOrDefault<TSource> ()
  262. Assert.AreEqual (4, data.LastOrDefault ());
  263. Assert.AreEqual (0, empty.LastOrDefault ());
  264. // LastOrDefault<TSource> (Func<TSource, bool>)
  265. Assert.AreEqual (3, data.LastOrDefault (x => x < 4));
  266. Assert.AreEqual (0, empty.LastOrDefault (x => x == 5));
  267. Assert.AreEqual (0, data.LastOrDefault (x => x == 6));
  268. }
  269. [Test]
  270. public void SingleArgumentNullTest ()
  271. {
  272. string [] data = { "2", "1", "5", "3", "4" };
  273. // Single<TSource> ()
  274. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Single (); });
  275. // Single<TSource> (Func<TSource, bool>)
  276. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Single ((x => true)); });
  277. AssertException<ArgumentNullException> (delegate () { data.Single ((Func<string, bool>) null); });
  278. }
  279. [Test]
  280. public void SingleTest ()
  281. {
  282. int [] data = { 2 };
  283. int [] data2 = { 2, 3, 5 };
  284. int [] empty = { };
  285. // Single<TSource> ()
  286. Assert.AreEqual (2, data.Single ());
  287. AssertException<InvalidOperationException> (delegate () { data2.Single (); });
  288. AssertException<InvalidOperationException> (delegate () { empty.Single (); });
  289. // Single<TSource> (Func<TSource, bool>)
  290. Assert.AreEqual (5, data2.Single (x => x == 5));
  291. AssertException<InvalidOperationException> (delegate () { data2.Single (x => false); });
  292. AssertException<InvalidOperationException> (delegate () { data2.Single (x => true); });
  293. AssertException<InvalidOperationException> (delegate () { empty.Single (x => true); });
  294. }
  295. [Test]
  296. public void SingleOrDefaultArgumentNullTest ()
  297. {
  298. string [] data = { "2", "1", "5", "3", "4" };
  299. // SingleOrDefault<TSource> ()
  300. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SingleOrDefault (); });
  301. // SingleOrDefault<TSource> (Func<TSource, bool>)
  302. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SingleOrDefault (x => true); });
  303. AssertException<ArgumentNullException> (delegate () { data.SingleOrDefault ((Func<string, bool>) null); });
  304. }
  305. [Test]
  306. public void SingleOrDefaultTest ()
  307. {
  308. int [] data = { 2 };
  309. int [] data2 = { 2, 3, 5 };
  310. int [] empty = { };
  311. // SingleOrDefault<TSource> ()
  312. Assert.AreEqual (2, data.SingleOrDefault ());
  313. Assert.AreEqual (0, empty.SingleOrDefault ());
  314. AssertException<InvalidOperationException> (delegate () { data2.SingleOrDefault (); });
  315. // SingleOrDefault<TSource> (Func<TSource, bool>)
  316. Assert.AreEqual (3, data2.SingleOrDefault (x => x == 3));
  317. Assert.AreEqual (0, data2.SingleOrDefault (x => false));
  318. AssertException<InvalidOperationException> (delegate () { data2.SingleOrDefault (x => true); });
  319. }
  320. [Test]
  321. public void ElementAtArgumentNullTest ()
  322. {
  323. //string [] data = { "2", "1", "5", "3", "4" };
  324. // ElementAt<TSource> (int)
  325. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ElementAt (0); });
  326. }
  327. [Test]
  328. public void ElementAtTest ()
  329. {
  330. int [] data = { 2, 3, 4, 5 };
  331. // ElementAt<string> (int)
  332. Assert.AreEqual (2, data.ElementAt (0));
  333. Assert.AreEqual (4, data.ElementAt (2));
  334. AssertException<ArgumentOutOfRangeException> (delegate () { data.ElementAt (-1); });
  335. AssertException<ArgumentOutOfRangeException> (delegate () { data.ElementAt (4); });
  336. AssertException<ArgumentOutOfRangeException> (delegate () { data.ElementAt (6); });
  337. }
  338. [Test]
  339. public void ElementAtOrDefaultArgumentNullTest ()
  340. {
  341. //string [] data = { "2", "1", "5", "3", "4" };
  342. // ElementAtOrDefault<TSource> (int)
  343. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ElementAtOrDefault (0); });
  344. }
  345. [Test]
  346. public void ElementAtOrDefaultTest ()
  347. {
  348. int [] data = { 2, 3, 4, 5 };
  349. int [] empty = { };
  350. // ElementAtOrDefault<TSource> (int)
  351. Assert.AreEqual (2, data.ElementAtOrDefault (0));
  352. Assert.AreEqual (4, data.ElementAtOrDefault (2));
  353. Assert.AreEqual (0, data.ElementAtOrDefault (-1));
  354. Assert.AreEqual (0, data.ElementAtOrDefault (4));
  355. Assert.AreEqual (0, empty.ElementAtOrDefault (4));
  356. }
  357. [Test]
  358. public void EmptyTest ()
  359. {
  360. IEnumerable<string> empty = Enumerable.Empty<string> ();
  361. Assert.IsFalse (empty.GetEnumerator ().MoveNext ());
  362. }
  363. [Test]
  364. public void AnyArgumentNullTest ()
  365. {
  366. string [] data = { "2", "1", "5", "3", "4" };
  367. // Any<TSource> ()
  368. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Any (); });
  369. // Any<TSource> (Func<TSource, bool>)
  370. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Any (x => true); });
  371. AssertException<ArgumentNullException> (delegate () { data.Any ((Func<string, bool>) null); });
  372. }
  373. [Test]
  374. public void AnyTest ()
  375. {
  376. int [] data = { 5, 2, 3, 1, 6 };
  377. int [] empty = { };
  378. // Any<TSource> ()
  379. Assert.IsTrue (data.Any ());
  380. Assert.IsFalse (empty.Any ());
  381. // Any<TSource> (Func<TSource, bool>)
  382. Assert.IsTrue (data.Any (x => x == 5));
  383. Assert.IsFalse (data.Any (x => x == 9));
  384. Assert.IsFalse (empty.Any (x => true));
  385. }
  386. [Test]
  387. public void AllArgumentNullTest ()
  388. {
  389. string [] data = { "2", "1", "5", "3", "4" };
  390. // All<TSource> (Func<TSource, bool>)
  391. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).All (x => true); });
  392. AssertException<ArgumentNullException> (delegate () { data.All ((Func<string, bool>) null); });
  393. }
  394. [Test]
  395. public void AllTest ()
  396. {
  397. int [] data = { 5, 2, 3, 1, 6 };
  398. int [] empty = { };
  399. // All<TSource> (Func<TSource, bool>)
  400. Assert.IsTrue (data.All (x => true));
  401. Assert.IsFalse (data.All (x => x != 1));
  402. Assert.IsTrue (empty.All (x => false));
  403. }
  404. [Test]
  405. public void CountArgumentNullTest ()
  406. {
  407. string [] data = { "2", "1", "5", "3", "4" };
  408. // Count<TSource> ()
  409. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Count (); });
  410. // Count<TSource> (Func<TSource, bool>)
  411. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Count (x => true); });
  412. AssertException<ArgumentNullException> (delegate () { data.Count ((Func<string, bool>) null); });
  413. }
  414. [Test]
  415. public void CountTest ()
  416. {
  417. int [] data = { 5, 2, 3, 1, 6 };
  418. // Count<TSource> ()
  419. Assert.AreEqual (5, data.Count ());
  420. // Count<TSource> (Func<TSource, bool>)
  421. Assert.AreEqual (3, data.Count (x => x < 5));
  422. }
  423. [Test]
  424. [Ignore ("Takes some time.")]
  425. public void CountOverflowTest ()
  426. {
  427. //BigEnumerable data = new BigEnumerable ((ulong) int.MaxValue + 1);
  428. // Count<TSource> ()
  429. //AssertException<OverflowException> (delegate () { data.Count (); });
  430. // Count<TSource> (Func<TSource, bool>)
  431. //AssertException<OverflowException> (delegate () { data.Count (x => 3 == x); });
  432. // Documentation error: http://msdn2.microsoft.com/en-us/library/bb535181.aspx
  433. // An exception is only rasied if count > int.MaxValue. Not if source contains more than int.MaxValue elements.
  434. // AssertException<OverflowException> (delegate () { data.Count (x => 5 == x); });
  435. }
  436. [Test]
  437. public void LongCountArgumentNullTest ()
  438. {
  439. string [] data = { "2", "1", "5", "3", "4" };
  440. // LongCount<TSource> ()
  441. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LongCount (); });
  442. // LongCount<TSource> (Func<TSource, bool>)
  443. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LongCount (x => true); });
  444. AssertException<ArgumentNullException> (delegate () { data.LongCount ((Func<string, bool>) null); });
  445. }
  446. [Test]
  447. public void LongCountTest ()
  448. {
  449. int [] data = { 5, 2, 3, 1, 6 };
  450. //TODO: Overflow test...
  451. // LongCount<TSource> ()
  452. Assert.AreEqual (5, data.LongCount ());
  453. Assert.AreEqual (5, Enumerable.Range (0, 5).LongCount ());
  454. // LongCount<TSource> (Func<TSource, bool>)
  455. Assert.AreEqual (3, data.LongCount (x => x < 5));
  456. }
  457. [Test]
  458. public void ContainsArgumentNullTest ()
  459. {
  460. //string [] data = { "2", "1", "5", "3", "4" };
  461. // Contains<TSource> (TSource)
  462. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Contains ("2"); });
  463. // Contains<TSource> (TSource, IEqualityComparer<TSource>)
  464. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Contains ("2", (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  465. }
  466. [Test]
  467. public void ContainsTest ()
  468. {
  469. int [] data = { 5, 2, 3, 1, 6 };
  470. // Contains<TSource> (TSource)
  471. Assert.IsTrue (data.Contains (2));
  472. Assert.IsFalse (data.Contains (0));
  473. // Contains<TSource> (TSource, IEqualityComparer<TSource>)
  474. Assert.IsTrue (data.Contains (2, EqualityComparer<int>.Default));
  475. Assert.IsFalse (data.Contains (0, EqualityComparer<int>.Default));
  476. }
  477. [Test]
  478. public void AggregateArgumentNullTest ()
  479. {
  480. string [] data = { "2", "1", "5", "3", "4" };
  481. // Aggregate<TSource> (Func<TSource, TSource, TSource>)
  482. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ((x, y) => "test"); });
  483. AssertException<ArgumentNullException> (delegate () { data.Aggregate ((Func<string, string, string>) null); });
  484. // Aggregate<TSource,TAccumulate> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>)
  485. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ("initial", (x, y) => "test"); });
  486. AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (Func<string, string, string>) null); });
  487. // Aggregate<TSource,TAccumulate,TResult> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>)
  488. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ("initial", (x, y) => "test", x => "test"); });
  489. AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (Func<string, string, string>) null, x => "test"); });
  490. AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (x, y) => "test", (Func<string, string>) null); });
  491. }
  492. [Test]
  493. public void AggregateTest ()
  494. {
  495. string [] data = { "2", "1", "5", "3", "4" };
  496. string [] empty = { };
  497. // Aggregate<TSource> (Func<TSource, TSource, TSource>)
  498. Assert.AreEqual ("21534", data.Aggregate ((x, y) => x + y));
  499. AssertException<InvalidOperationException> (delegate () { empty.Aggregate ((x, y) => x + y); }); //only this overload throws
  500. // Aggregate<TSource,TAccumulate> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>)
  501. Assert.AreEqual ("initial21534", (data.Aggregate ("initial", (x, y) => x + y)));
  502. // Aggregate<TSource,TAccumulate,TResult> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>)
  503. Assert.AreEqual ("INITIAL21534", data.Aggregate ("initial", (x, y) => x + y, (x => x.ToUpper ())));
  504. }
  505. [Test]
  506. public void SumArgumentNullTest ()
  507. {
  508. string [] data = { "2", "1", "5", "3", "4" };
  509. // Sum<TSource> (Func<TSource, int>)
  510. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, int>) (x => 0)); });
  511. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, int>) null); });
  512. // Sum<TSource> (Func<TSource, Nullable<int>>)
  513. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  514. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<int>>) null); });
  515. // Sum<TSource> (Func<TSource, Int64>)
  516. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Int64>) (x => 0L)); });
  517. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Int64>) null); });
  518. // Sum<TSource> (Func<TSource, Nullable<Int64>>)
  519. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  520. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Int64>>) null); });
  521. // Sum<TSource> (Func<TSource, Single>)
  522. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Single>) (x => 0f)); });
  523. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Single>) null); });
  524. // Sum<TSource> (Func<TSource, Nullable<Single>>)
  525. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  526. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Single>>) null); });
  527. // Sum<TSource> (Func<TSource, Double>)
  528. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Double>) (x => 0d)); });
  529. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Double>) null); });
  530. // Sum<TSource> (Func<TSource, Nullable<Double>>)
  531. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  532. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Double>>) null); });
  533. // Sum<TSource> (Func<TSource, Decimal>)
  534. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Decimal>) (x => 0m)); });
  535. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Decimal>) null); });
  536. // Sum<TSource> (Func<TSource, Nullable<Decimal>>)
  537. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  538. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Decimal>>) null); });
  539. // Sum (IEnumerable<int>)
  540. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Sum (); });
  541. // Sum (IEnumerable<int?>)
  542. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Sum (); });
  543. // Sum (IEnumerable<long>)
  544. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Sum (); });
  545. // Sum (IEnumerable<long?>)
  546. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Sum (); });
  547. // Sum (IEnumerable<float>)
  548. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Sum (); });
  549. // Sum (IEnumerable<float?>)
  550. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Sum (); });
  551. // Sum (IEnumerable<double>)
  552. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Sum (); });
  553. // Sum (IEnumerable<double?>)
  554. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Sum (); });
  555. // Sum (IEnumerable<decimal>)
  556. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Sum (); });
  557. // Sum (IEnumerable<decimal?>)
  558. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Sum (); });
  559. }
  560. [Test]
  561. public void SumTest ()
  562. {
  563. string [] data = { "2", "3", "5", "5" };
  564. //TODO: OverflowException
  565. // Sum<TSource> (Func<TSource, int>)
  566. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, int>) (x => int.Parse (x))));
  567. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, int>) (x => int.Parse (x))));
  568. // Sum<TSource> (Func<TSource, Nullable<int>>)
  569. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  570. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  571. // Sum<TSource> (Func<TSource, Int64>)
  572. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Int64>) (x => int.Parse (x))));
  573. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Int64>) (x => int.Parse (x))));
  574. // Sum<TSource> (Func<TSource, Nullable<Int64>>)
  575. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  576. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  577. // Sum<TSource> (Func<TSource, Single>)
  578. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Single>) (x => int.Parse (x))));
  579. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Single>) (x => int.Parse (x))));
  580. // Sum<TSource> (Func<TSource, Nullable<Single>>)
  581. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  582. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  583. // Sum<TSource> (Func<TSource, Double>)
  584. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Double>) (x => int.Parse (x))));
  585. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Double>) (x => int.Parse (x))));
  586. // Sum<TSource> (Func<TSource, Nullable<Double>>)
  587. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  588. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  589. // Sum<TSource> (Func<TSource, Decimal>)
  590. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  591. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  592. // Sum<TSource> (Func<TSource, Nullable<Decimal>>)
  593. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  594. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  595. // Sum<> ()
  596. Assert.AreEqual (6, ((IEnumerable<int>) new int [] { 1, 2, 3 }).Sum ());
  597. Assert.AreEqual (0, Enumerable.Empty<int> ().Sum ());
  598. // Sum<> ()
  599. Assert.AreEqual (6, ((IEnumerable<Nullable<int>>) new int? [] { 1, 2, 3 }).Sum ());
  600. Assert.AreEqual (0, Enumerable.Empty<int?> ().Sum ());
  601. // Sum<> ()
  602. Assert.AreEqual (6, ((IEnumerable<Int64>) new long [] { 1, 2, 3 }).Sum ());
  603. Assert.AreEqual (0, Enumerable.Empty<long> ().Sum ());
  604. // Sum<> ()
  605. Assert.AreEqual (6, ((IEnumerable<Nullable<Int64>>) new long? [] { 1, 2, 3 }).Sum ());
  606. Assert.AreEqual (0, Enumerable.Empty<long?> ().Sum ());
  607. // Sum<> ()
  608. Assert.AreEqual (6, ((IEnumerable<Single>) new float [] { 1, 2, 3 }).Sum ());
  609. Assert.AreEqual (0, Enumerable.Empty<float> ().Sum ());
  610. // Sum<> ()
  611. Assert.AreEqual (6, ((IEnumerable<Nullable<Single>>) new float? [] { 1, 2, 3 }).Sum ());
  612. Assert.AreEqual (0, Enumerable.Empty<float?> ().Sum ());
  613. // Sum<> ()
  614. Assert.AreEqual (6, ((IEnumerable<Double>) new double [] { 1, 2, 3 }).Sum ());
  615. Assert.AreEqual (0, Enumerable.Empty<double> ().Sum ());
  616. // Sum<> ()
  617. Assert.AreEqual (6, ((IEnumerable<Nullable<Double>>) new double? [] { 1, 2, 3 }).Sum ());
  618. Assert.AreEqual (0, Enumerable.Empty<double?> ().Sum ());
  619. // Sum<> ()
  620. Assert.AreEqual (6, ((IEnumerable<Decimal>) new decimal [] { 1, 2, 3 }).Sum ());
  621. Assert.AreEqual (0, Enumerable.Empty<decimal> ().Sum ());
  622. // Sum<> ()
  623. Assert.AreEqual (6, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 1, 2, 3 }).Sum ());
  624. Assert.AreEqual (0, Enumerable.Empty<decimal?> ().Sum ());
  625. }
  626. [Test]
  627. public void MinArgumentNullTest ()
  628. {
  629. string [] data = { "2", "1", "5", "3", "4" };
  630. // Min<TSource> ()
  631. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> (); });
  632. // Min<TSource> (Func<TSource, int>)
  633. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, int>) (x => 0)); });
  634. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, int>) null); });
  635. // Min<TSource> (Func<TSource, Nullable<int>>)
  636. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  637. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<int>>) null); });
  638. // Min<TSource> (Func<TSource, Int64>)
  639. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Int64>) (x => 0L)); });
  640. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Int64>) null); });
  641. // Min<TSource> (Func<TSource, Nullable<Int64>>)
  642. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  643. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Int64>>) null); });
  644. // Min<TSource> (Func<TSource, Single>)
  645. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Single>) (x => 0f)); });
  646. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Single>) null); });
  647. // Min<TSource> (Func<TSource, Nullable<Single>>)
  648. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  649. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Single>>) null); });
  650. // Min<TSource> (Func<TSource, Double>)
  651. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Double>) (x => 0d)); });
  652. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Double>) null); });
  653. // Min<TSource> (Func<TSource, Nullable<Double>>)
  654. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  655. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Double>>) null); });
  656. // Min<TSource> (Func<TSource, Decimal>)
  657. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Decimal>) (x => 0m)); });
  658. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Decimal>) null); });
  659. // Min<TSource> (Func<TSource, Nullable<Decimal>>)
  660. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  661. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Decimal>>) null); });
  662. // Min<TSource,TSource> (Func<TSource, string>)
  663. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string, string> ((Func<string, string>) (x => "test")); });
  664. AssertException<ArgumentNullException> (delegate () { data.Min<string, string> ((Func<string, string>) null); });
  665. // Min<> ()
  666. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Min (); });
  667. // Min<> ()
  668. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Min (); });
  669. // Min<> ()
  670. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Min (); });
  671. // Min<> ()
  672. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Min (); });
  673. // Min<> ()
  674. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Min (); });
  675. // Min<> ()
  676. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Min (); });
  677. // Min<> ()
  678. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Min (); });
  679. // Min<> ()
  680. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Min (); });
  681. // Min<> ()
  682. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Min (); });
  683. // Min<> ()
  684. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Min (); });
  685. }
  686. [Test]
  687. public void MinTest ()
  688. {
  689. string [] data = { "2", "1", "5", "3", "4" };
  690. // Min<TSource> ()
  691. Assert.AreEqual ("1", ((IEnumerable<string>) data).Min<string> ());
  692. // Min<TSource> (Func<TSource, int>)
  693. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, int>) (x => int.Parse (x))));
  694. // Min<TSource> (Func<TSource, Nullable<int>>)
  695. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  696. // Min<TSource> (Func<TSource, Int64>)
  697. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Int64>) (x => int.Parse (x))));
  698. // Min<TSource> (Func<TSource, Nullable<Int64>>)
  699. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  700. // Min<TSource> (Func<TSource, Single>)
  701. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Single>) (x => int.Parse (x))));
  702. // Min<TSource> (Func<TSource, Nullable<Single>>)
  703. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  704. // Min<TSource> (Func<TSource, Double>)
  705. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Double>) (x => int.Parse (x))));
  706. // Min<TSource> (Func<TSource, Nullable<Double>>)
  707. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  708. // Min<TSource> (Func<TSource, Decimal>)
  709. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  710. // Min<TSource> (Func<TSource, Nullable<Decimal>>)
  711. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  712. // Min<TSource,TSource> (Func<TSource, TSource>)
  713. Assert.AreEqual ("1", ((IEnumerable<string>) data).Min<string, string> ((Func<string, string>) (x => x)));
  714. // Min<> ()
  715. Assert.AreEqual (2, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Min ());
  716. // Min<> ()
  717. Assert.AreEqual (2, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Min ());
  718. // Min<> ()
  719. Assert.AreEqual (2, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Min ());
  720. // Min<> ()
  721. Assert.AreEqual (2, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Min ());
  722. // Min<> ()
  723. Assert.AreEqual (2, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Min ());
  724. // Min<> ()
  725. Assert.AreEqual (2, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Min ());
  726. // Min<> ()
  727. Assert.AreEqual (2, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Min ());
  728. // Min<> ()
  729. Assert.AreEqual (2, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Min ());
  730. // Min<> ()
  731. Assert.AreEqual (2, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Min ());
  732. // Min<> ()
  733. Assert.AreEqual (2, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Min ());
  734. }
  735. [Test]
  736. public void MaxArgumentNullTest ()
  737. {
  738. string [] data = { "2", "1", "5", "3", "4" };
  739. // Max<TSource> ()
  740. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> (); });
  741. // Max<TSource> (Func<TSource, int>)
  742. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, int>) (x => 0)); });
  743. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, int>) null); });
  744. // Max<TSource> (Func<TSource, Nullable<int>>)
  745. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  746. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<int>>) null); });
  747. // Max<TSource> (Func<TSource, Int64>)
  748. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Int64>) (x => 0L)); });
  749. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Int64>) null); });
  750. // Max<TSource> (Func<TSource, Nullable<Int64>>)
  751. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  752. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Int64>>) null); });
  753. // Max<TSource> (Func<TSource, Single>)
  754. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Single>) (x => 0f)); });
  755. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Single>) null); });
  756. // Max<TSource> (Func<TSource, Nullable<Single>>)
  757. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  758. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Single>>) null); });
  759. // Max<TSource> (Func<TSource, Double>)
  760. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Double>) (x => 0d)); });
  761. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Double>) null); });
  762. // Max<TSource> (Func<TSource, Nullable<Double>>)
  763. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  764. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Double>>) null); });
  765. // Max<TSource> (Func<TSource, Decimal>)
  766. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Decimal>) (x => 0m)); });
  767. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Decimal>) null); });
  768. // Max<TSource> (Func<TSource, Nullable<Decimal>>)
  769. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  770. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Decimal>>) null); });
  771. // Max<TSource,TSource> (Func<TSource, TSource>)
  772. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string, string> ((Func<string, string>) (x => "test")); });
  773. AssertException<ArgumentNullException> (delegate () { data.Max<string, string> ((Func<string, string>) null); });
  774. // Max<> ()
  775. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Max (); });
  776. // Max<> ()
  777. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Max (); });
  778. // Max<> ()
  779. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Max (); });
  780. // Max<> ()
  781. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Max (); });
  782. // Max<> ()
  783. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Max (); });
  784. // Max<> ()
  785. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Max (); });
  786. // Max<> ()
  787. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Max (); });
  788. // Max<> ()
  789. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Max (); });
  790. // Max<> ()
  791. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Max (); });
  792. // Max<> ()
  793. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Max (); });
  794. }
  795. [Test]
  796. public void MaxTest ()
  797. {
  798. string [] data = { "2", "1", "5", "3", "4" };
  799. // Max<string> ()
  800. Assert.AreEqual ("5", ((IEnumerable<string>) data).Max<string> ());
  801. // Max<TSource> (Func<TSource, int>)
  802. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, int>) (x => int.Parse (x))));
  803. // Max<TSource> (Func<TSource, Nullable<int>>)
  804. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  805. // Max<TSource> (Func<TSource, Int64>)
  806. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Int64>) (x => int.Parse (x))));
  807. // Max<TSource> (Func<TSource, Nullable<Int64>>)
  808. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  809. // Max<TSource> (Func<TSource, Single>)
  810. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Single>) (x => int.Parse (x))));
  811. // Max<TSource> (Func<TSource, Nullable<Single>>)
  812. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  813. // Max<TSource> (Func<TSource, Double>)
  814. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Double>) (x => int.Parse (x))));
  815. // Max<TSource> (Func<TSource, Nullable<Double>>)
  816. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  817. // Max<TSource> (Func<TSource, Decimal>)
  818. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  819. // Max<TSource> (Func<TSource, Nullable<Decimal>>)
  820. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  821. // Max<TSource,TSource> (Func<TSource, TSource>)
  822. Assert.AreEqual ("5", ((IEnumerable<string>) data).Max<string, string> ((Func<string, string>) (x => x)));
  823. // Max<> ()
  824. Assert.AreEqual (4, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Max ());
  825. // Max<> ()
  826. Assert.AreEqual (4, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Max ());
  827. // Max<> ()
  828. Assert.AreEqual (4, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Max ());
  829. // Max<> ()
  830. Assert.AreEqual (4, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Max ());
  831. // Max<> ()
  832. Assert.AreEqual (4, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Max ());
  833. // Max<> ()
  834. Assert.AreEqual (4, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Max ());
  835. // Max<> ()
  836. Assert.AreEqual (4, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Max ());
  837. // Max<> ()
  838. Assert.AreEqual (4, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Max ());
  839. // Max<> ()
  840. Assert.AreEqual (4, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Max ());
  841. // Max<> ()
  842. Assert.AreEqual (4, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Max ());
  843. }
  844. [Test]
  845. public void AverageArgumentNullTest ()
  846. {
  847. string [] data = { "2", "1", "5", "3", "4" };
  848. // Average<TSource> (Func<TSource, int>)
  849. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, int>) (x => 0)); });
  850. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, int>) null); });
  851. // Average<TSource> (Func<TSource, Nullable<int>>)
  852. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  853. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<int>>) null); });
  854. // Average<TSource> (Func<TSource, Int64>)
  855. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Int64>) (x => 0L)); });
  856. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Int64>) null); });
  857. // Average<TSource> (Func<TSource, Nullable<Int64>>)
  858. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  859. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Int64>>) null); });
  860. // Average<TSource> (Func<TSource, Single>)
  861. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Single>) (x => 0f)); });
  862. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Single>) null); });
  863. // Average<TSource> (Func<TSource, Nullable<Single>>)
  864. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  865. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Single>>) null); });
  866. // Average<TSource> (Func<TSource, Double>)
  867. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Double>) (x => 0d)); });
  868. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Double>) null); });
  869. // Average<TSource> (Func<TSource, Nullable<Double>>)
  870. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  871. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Double>>) null); });
  872. // Average<TSource> (Func<TSource, Decimal>)
  873. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Decimal>) (x => 0m)); });
  874. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Decimal>) null); });
  875. // Average<TSource> (Func<TSource, Nullable<Decimal>>)
  876. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  877. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Decimal>>) null); });
  878. // Average<> ()
  879. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Average (); });
  880. // Average<> ()
  881. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Average (); });
  882. // Average<> ()
  883. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Average (); });
  884. // Average<> ()
  885. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Average (); });
  886. // Average<> ()
  887. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Average (); });
  888. // Average<> ()
  889. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Average (); });
  890. // Average<> ()
  891. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Average (); });
  892. // Average<> ()
  893. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Average (); });
  894. // Average<> ()
  895. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Average (); });
  896. // Average<> ()
  897. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Average (); });
  898. }
  899. [Test]
  900. public void AverageTest ()
  901. {
  902. string [] data = { "2", "1", "5", "3", "4" };
  903. string [] empty = { };
  904. // Average<string> (Func<string, int>)
  905. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, int>) (x => int.Parse (x))));
  906. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, int>) (x => int.Parse (x))); });
  907. // Average<TSource> (Func<TSource, Nullable<int>>)
  908. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  909. // Average<TSource> (Func<TSource, Int64>)
  910. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, long>) (x => int.Parse (x))));
  911. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, long>) (x => int.Parse (x))); });
  912. // Average<TSource> (Func<TSource, Nullable<Int64>>)
  913. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, long?>) (x => (int?) int.Parse (x))));
  914. // Average<TSource> (Func<TSource, Single>)
  915. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, float>) (x => int.Parse (x))));
  916. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, float>) (x => int.Parse (x))); });
  917. // Average<TSource> (Func<TSource, Nullable<Single>>)
  918. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, float?>) (x => (int?) int.Parse (x))));
  919. // Average<TSource> (Func<TSource, Double>)
  920. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, double>) (x => int.Parse (x))));
  921. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, double>) (x => int.Parse (x))); });
  922. // Average<TSource> (Func<TSource, Nullable<Double>>)
  923. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, double?>) (x => (int?) int.Parse (x))));
  924. // Average<TSource> (Func<TSource, Decimal>)
  925. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, decimal>) (x => int.Parse (x))));
  926. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, decimal>) (x => int.Parse (x))); });
  927. // Average<TSource> (Func<TSource, Nullable<Decimal>>)
  928. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, decimal?>) (x => (int?) int.Parse (x))));
  929. // Average<> ()
  930. Assert.AreEqual (3, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Average ());
  931. AssertException<InvalidOperationException> (delegate () { new int [0].Average (); });
  932. // Average<> ()
  933. Assert.AreEqual (3, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Average ());
  934. // Average<> ()
  935. Assert.AreEqual (3, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Average ());
  936. AssertException<InvalidOperationException> (delegate () { new long [0].Average (); });
  937. // Average<> ()
  938. Assert.AreEqual (3, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Average ());
  939. // Average<> ()
  940. Assert.AreEqual (3, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Average ());
  941. AssertException<InvalidOperationException> (delegate () { new float [0].Average (); });
  942. // Average<> ()
  943. Assert.AreEqual (3, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Average ());
  944. // Average<> ()
  945. Assert.AreEqual (3, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Average ());
  946. AssertException<InvalidOperationException> (delegate () { new double [0].Average (); });
  947. // Average<> ()
  948. Assert.AreEqual (3, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Average ());
  949. // Average<> ()
  950. Assert.AreEqual (3, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Average ());
  951. AssertException<InvalidOperationException> (delegate () { new decimal [0].Average (); });
  952. // Average<> ()
  953. Assert.AreEqual (3, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Average ());
  954. }
  955. [Test]
  956. public void WhereArgumentNullTest ()
  957. {
  958. string [] data = { "2", "1", "5", "3", "4" };
  959. // Where<TSource> (Func<TSource, bool>)
  960. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Where (x => true); });
  961. AssertException<ArgumentNullException> (delegate () { data.Where ((Func<string, bool>) null); });
  962. // Where<TSource> (Func<TSource, int, bool>)
  963. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Where ((x, y) => true); });
  964. AssertException<ArgumentNullException> (delegate () { data.Where ((Func<string, int, bool>) null); });
  965. }
  966. [Test]
  967. public void WhereTest ()
  968. {
  969. int [] data = { 2, 1, 5, 3, 4 };
  970. int [] expected1 = { 2, 1 };
  971. int [] expected2 = { 2 };
  972. // Where<TSource> (Func<TSource, bool>)
  973. AssertAreSame (expected1, data.Where (x => x < 3));
  974. // Where<TSource> (Func<TSource, int, bool>)
  975. AssertAreSame (expected2, data.Where ((x, y) => x < 3 && y != 1));
  976. }
  977. [Test]
  978. public void SelectArgumentNullTest ()
  979. {
  980. string [] data = { "2", "1", "5", "3", "4" };
  981. // Select<TSource,TResult> (Func<TSource, TResult>)
  982. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Select (x => "test"); });
  983. AssertException<ArgumentNullException> (delegate () { data.Select ((Func<string, string>) null); });
  984. // Select<TSource,TResult> (Func<TSource, int, TResult>)
  985. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Select ((x, y) => "test"); });
  986. AssertException<ArgumentNullException> (delegate () { data.Select ((Func<string, int, string>) null); });
  987. }
  988. [Test]
  989. public void SelectTest ()
  990. {
  991. string [] data = { "2", "1", "5", "3", "4" };
  992. string [] expected1 = { "2x", "1x", "5x", "3x", "4x" };
  993. string [] expected2 = { "2x0", "1x1", "5x2", "3x3", "4x4" };
  994. // Select<TSource,TResult> (Func<TSource, TResult>)
  995. AssertAreSame (expected1, data.Select<string, string> (x => x + "x"));
  996. // Select<TSource,TResult> (Func<TSource, int, TResult>)
  997. AssertAreSame (expected2, data.Select<string, string> ((x, y) => x + "x" + y));
  998. }
  999. [Test]
  1000. public void SelectManyArgumentNullTest ()
  1001. {
  1002. string [] data = { "2", "1", "5", "3", "4" };
  1003. // SelectMany<TSource,TResult> (Func<TSource, IEnumerable<TResult>>)
  1004. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany (x => data); });
  1005. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, IEnumerable<string>>) null); });
  1006. // SelectMany<TSource,TResult> (Func<TSource, int, IEnumerable<TResult>>)
  1007. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany ((x, y) => data); });
  1008. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, int, IEnumerable<string>>) null); });
  1009. // SelectMany<TSource,TCollection,TResult> (Func<string, int, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1010. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany ((x, y) => data, (x, y) => "test"); });
  1011. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, int, IEnumerable<string>>) null, (x, y) => "test"); });
  1012. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((x, y) => data, (Func<string, string, string>) null); });
  1013. // SelectMany<TSource,TCollection,TResult> (Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1014. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany (x => data, (x, y) => "test"); });
  1015. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, IEnumerable<string>>) null, (x, y) => "test"); });
  1016. AssertException<ArgumentNullException> (delegate () { data.SelectMany (x => data, (Func<string, string, string>) null); });
  1017. }
  1018. [Test]
  1019. public void SelectManyTest ()
  1020. {
  1021. string [] data = { "0", "1" };
  1022. string [] expected = { "0", "00", "1", "11" };
  1023. // SelectMany<TSource,TResult> (Func<TSource, IEnumerable<TResult>>)
  1024. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany (x => new string [] { x, x + x }));
  1025. // SelectMany<TSource,TResult> (Func<TSource, int, IEnumerable<TResult>>)
  1026. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany ((x, y) => new string [] { x, x + y }));
  1027. // SelectMany<TSource,TCollection,TResult> (Func<string, int, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1028. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany ((x, y) => new string [] { x, x + y }, (x, y) => y));
  1029. // SelectMany<TSource,TCollection,TResult> (Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1030. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany (x => new string [] { x, x + x }, (x, y) => y));
  1031. }
  1032. [Test]
  1033. public void TakeArgumentNullTest ()
  1034. {
  1035. //string [] data = { "2", "1", "5", "3", "4" };
  1036. // Take<TSource> (int)
  1037. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Take (0); });
  1038. }
  1039. [Test]
  1040. public void TakeTest ()
  1041. {
  1042. int [] data = { 2, 1, 5, 3, 1 };
  1043. int [] expected = { 2, 1 };
  1044. int [] empty = { };
  1045. // Take<TSource> (int)
  1046. AssertAreSame (expected, data.Take (2));
  1047. AssertAreSame (empty, data.Take (-2));
  1048. }
  1049. [Test]
  1050. public void TakeWhileArgumentNullTest ()
  1051. {
  1052. string [] data = { "2", "1", "5", "3", "4" };
  1053. // TakeWhile<TSource> (Func<TSource, bool>)
  1054. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).TakeWhile (x => true); });
  1055. AssertException<ArgumentNullException> (delegate () { data.TakeWhile ((Func<string, bool>) null); });
  1056. // TakeWhile<TSource> (Func<TSource, int, bool>)
  1057. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).TakeWhile ((x, y) => true); });
  1058. AssertException<ArgumentNullException> (delegate () { data.TakeWhile ((Func<string, int, bool>) null); });
  1059. }
  1060. [Test]
  1061. public void TakeWhileTest ()
  1062. {
  1063. int [] data = { 2, 1, 5, 3, 1 };
  1064. int [] expected = { 2, 1 };
  1065. // TakeWhile<TSource> (Func<TSource, bool>)
  1066. AssertAreSame (expected, data.TakeWhile (x => x != 5));
  1067. // TakeWhile<TSource> (Func<TSource, int, bool>)
  1068. AssertAreSame (expected, data.TakeWhile ((x, y) => y != 2));
  1069. }
  1070. [Test]
  1071. public void SkipArgumentNullTest ()
  1072. {
  1073. //string [] data = { "2", "1", "5", "3", "4" };
  1074. // Skip<TSource> (int)
  1075. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Skip (0); });
  1076. }
  1077. [Test]
  1078. public void SkipTest ()
  1079. {
  1080. int [] data = { 2, 1, 5, 3, 1 };
  1081. int [] expected = { 5, 3, 1 };
  1082. // Skip<string> (TSource)
  1083. AssertAreSame (expected, data.Skip (2));
  1084. AssertAreSame (data, data.Skip (-2));
  1085. }
  1086. [Test]
  1087. public void SkipWhileArgumentNullTest ()
  1088. {
  1089. string [] data = { "2", "1", "5", "3", "4" };
  1090. // SkipWhile<TSource> (Func<TSource, bool>)
  1091. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SkipWhile (x => true); });
  1092. AssertException<ArgumentNullException> (delegate () { data.SkipWhile ((Func<string, bool>) null); });
  1093. // SkipWhile<TSource> (Func<TSource, int, bool>)
  1094. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SkipWhile ((x, y) => true); });
  1095. AssertException<ArgumentNullException> (delegate () { data.SkipWhile ((Func<string, int, bool>) null); });
  1096. }
  1097. [Test]
  1098. public void SkipWhileTest ()
  1099. {
  1100. int [] data = { 2, 1, 5, 3, 1 };
  1101. int [] expected = { 5, 3, 1 };
  1102. // SkipWhile<TSource> (Func<TSource, bool>)
  1103. AssertAreSame (expected, data.SkipWhile (x => x != 5));
  1104. // SkipWhile<TSource> (Func<TSource, int, bool>)
  1105. AssertAreSame (expected, data.SkipWhile ((x, y) => y != 2));
  1106. }
  1107. [Test]
  1108. public void JoinArgumentNullTest ()
  1109. {
  1110. string [] data = { "2", "1", "5", "3", "4" };
  1111. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)
  1112. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Join (data, x => "test", x => "test", (x, y) => "test"); });
  1113. AssertException<ArgumentNullException> (delegate () { data.Join ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test"); });
  1114. AssertException<ArgumentNullException> (delegate () { data.Join (data, (Func<string, string>) null, x => "test", (x, y) => "test"); });
  1115. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", (Func<string, string>) null, (x, y) => "test"); });
  1116. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", x => "test", (Func<string, string, string>) null); });
  1117. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<string>)
  1118. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Join (data, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1119. AssertException<ArgumentNullException> (delegate () { data.Join ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1120. AssertException<ArgumentNullException> (delegate () { data.Join (data, (Func<string, string>) null, x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1121. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", (Func<string, string>) null, (x, y) => "test", EqualityComparer<string>.Default); });
  1122. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", x => "test", (Func<string, string, string>) null, EqualityComparer<string>.Default); });
  1123. }
  1124. [Test]
  1125. public void JoinTest ()
  1126. {
  1127. string [] dataOuter1 = { "2", "1", "5", "3", "4" };
  1128. string [] dataInner1 = { "7", "3", "5", "8", "9" };
  1129. string [] expected1 = { "55", "33" };
  1130. string [] dataOuter2 = { "2", "1", "3", "4" };
  1131. string [] dataInner2 = { "7", "5", "8", "9" };
  1132. string [] expected2 = { };
  1133. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)
  1134. AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y));
  1135. AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y));
  1136. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<string>)
  1137. AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y, EqualityComparer<string>.Default));
  1138. AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y, EqualityComparer<string>.Default));
  1139. }
  1140. [Test]
  1141. public void GroupJoinArgumentNullTest ()
  1142. {
  1143. string [] data = { "2", "1", "5", "3", "4" };
  1144. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
  1145. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test"); });
  1146. AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test"); });
  1147. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test"); });
  1148. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test"); });
  1149. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null); });
  1150. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
  1151. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1152. AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1153. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1154. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test", EqualityComparer<string>.Default); });
  1155. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null, EqualityComparer<string>.Default); });
  1156. }
  1157. [Test]
  1158. public void GroupJoinTest ()
  1159. {
  1160. string [] dataOuter1 = { "2", "1", "5", "3", "4" };
  1161. string [] dataInner1 = { "7", "3", "5", "3", "9" };
  1162. string [] expected1 = { "2", "1", "55", "333", "4" };
  1163. string [] dataOuter2 = { "2", "1", "5", "8", "4" };
  1164. string [] dataInner2 = { "7", "3", "6", "3", "9" };
  1165. string [] expected2 = { "2", "1", "5", "8", "4" };
  1166. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
  1167. AssertAreSame (expected1, (dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
  1168. AssertAreSame (expected2, (dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
  1169. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
  1170. AssertAreSame (expected1, dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1171. AssertAreSame (expected2, dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1172. }
  1173. [Test]
  1174. public void OrderByArgumentNullTest ()
  1175. {
  1176. string [] data = { "2", "1", "5", "3", "4" };
  1177. // OrderBy<TSource,TKey> (Func<TSource, TKey>)
  1178. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test"); });
  1179. AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null); });
  1180. // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1181. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test", Comparer<string>.Default); });
  1182. AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null, Comparer<string>.Default); });
  1183. }
  1184. [Test]
  1185. public void OrderByTest ()
  1186. {
  1187. int [] data = { 2, 1, 5, 3, 4 };
  1188. int [] expected = { 1, 2, 3, 4, 5 };
  1189. // OrderBy<TSource,TKey> (Func<TSource, TKey>)
  1190. AssertAreSame (expected, data.OrderBy (x => x));
  1191. // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1192. AssertAreSame (expected, data.OrderBy (x => x, Comparer<int>.Default));
  1193. }
  1194. [Test]
  1195. public void OrderByDescendingArgumentNullTest ()
  1196. {
  1197. string [] data = { "2", "1", "5", "3", "4" };
  1198. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
  1199. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test"); });
  1200. AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null); });
  1201. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1202. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test", Comparer<string>.Default); });
  1203. AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null, Comparer<string>.Default); });
  1204. }
  1205. [Test]
  1206. public void OrderByDescendingTest ()
  1207. {
  1208. int [] data = { 2, 1, 5, 3, 4 };
  1209. int [] expected = { 5, 4, 3, 2, 1 };
  1210. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
  1211. AssertAreSame (expected, data.OrderByDescending (x => x));
  1212. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1213. AssertAreSame (expected, data.OrderByDescending (x => x, Comparer<int>.Default));
  1214. }
  1215. [Test]
  1216. public void ThenByArgumentNullTest ()
  1217. {
  1218. string [] data = { "2", "1", "5", "3", "4" };
  1219. // ThenBy<TSource,TKey> (Func<TSource, TKey>)
  1220. AssertException<ArgumentNullException> (delegate () {
  1221. ((IOrderedEnumerable<string>) null).ThenBy (x => "test");
  1222. });
  1223. AssertException<ArgumentNullException> (delegate () {
  1224. data.OrderBy (x => x).ThenBy ((Func<string, string>) null);
  1225. });
  1226. // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1227. AssertException<ArgumentNullException> (delegate () {
  1228. ((IOrderedEnumerable<string>) null).ThenBy (x => "test", Comparer<string>.Default);
  1229. });
  1230. AssertException<ArgumentNullException> (delegate () {
  1231. data.OrderBy (x => x).ThenBy ((Func<string, string>) null, Comparer<string>.Default);
  1232. });
  1233. }
  1234. [Test]
  1235. public void ThenByTest ()
  1236. {
  1237. int [] data = { 2, 1, 5, 3, 4 };
  1238. int [] expected = { 1, 2, 3, 4, 5 };
  1239. // ThenBy<TSource,TKey> (Func<TSource, TKey>)
  1240. AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x));
  1241. // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1242. AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x, Comparer<int>.Default));
  1243. }
  1244. [Test]
  1245. public void ThenByDescendingArgumentNullTest ()
  1246. {
  1247. string [] data = { "2", "1", "5", "3", "4" };
  1248. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
  1249. AssertException<ArgumentNullException> (delegate () {
  1250. ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test");
  1251. });
  1252. AssertException<ArgumentNullException> (delegate () {
  1253. data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null);
  1254. });
  1255. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1256. AssertException<ArgumentNullException> (delegate () {
  1257. ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test", Comparer<string>.Default);
  1258. });
  1259. AssertException<ArgumentNullException> (delegate () {
  1260. data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null, Comparer<string>.Default);
  1261. });
  1262. }
  1263. [Test]
  1264. public void ThenByDescendingTest ()
  1265. {
  1266. int [] data = { 2, 1, 5, 3, 4 };
  1267. int [] expected = { 5, 4, 3, 2, 1 };
  1268. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
  1269. AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x));
  1270. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1271. AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x, Comparer<int>.Default));
  1272. }
  1273. [Test]
  1274. [Category ("NotWorking")]
  1275. public void GroupByArgumentNullTest ()
  1276. {
  1277. string [] data = { "2", "1", "5", "3", "4" };
  1278. // GroupBy<string,string> (Func<string, string>)
  1279. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test")); });
  1280. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null); });
  1281. // GroupBy<string,string> (Func<string, string>, IEqualityComparer<string>)
  1282. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1283. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1284. // GroupBy<string,string,string> (Func<string, string>, Func<string, string>)
  1285. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
  1286. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
  1287. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
  1288. // GroupBy<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
  1289. 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); });
  1290. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1291. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1292. // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>)
  1293. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
  1294. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
  1295. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null); });
  1296. // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>)
  1297. 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")); });
  1298. 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")); });
  1299. 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")); });
  1300. 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); });
  1301. // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
  1302. 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); });
  1303. 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); });
  1304. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1305. // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
  1306. 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); });
  1307. 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); });
  1308. 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); });
  1309. 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); });
  1310. }
  1311. [Test]
  1312. [Category ("NotWorking")]
  1313. public void GroupByTest ()
  1314. {
  1315. string [] data = { "2", "1", "5", "3", "4", "3" };
  1316. Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
  1317. expected.Add ("2", new List<string> () { "2" });
  1318. expected.Add ("1", new List<string> () { "1" });
  1319. expected.Add ("5", new List<string> () { "5" });
  1320. expected.Add ("3", new List<string> () { "3", "3" });
  1321. expected.Add ("4", new List<string> () { "4" });
  1322. Dictionary<string, IEnumerable<string>> expected2 = new Dictionary<string, IEnumerable<string>> ();
  1323. expected2.Add ("2", new List<string> () { "22" });
  1324. expected2.Add ("1", new List<string> () { "11" });
  1325. expected2.Add ("5", new List<string> () { "55" });
  1326. expected2.Add ("3", new List<string> () { "33", "33" });
  1327. expected2.Add ("4", new List<string> () { "44" });
  1328. string [] expected3 = new string [] { "22", "11", "55", "333", "44" };
  1329. // GroupBy<int,int> (Func<int, int>)
  1330. AssertAreSame (expected, data.GroupBy (x => x));
  1331. // GroupBy<int,int> (Func<int, int>, IEqualityComparer<int>)
  1332. AssertAreSame (expected, data.GroupBy (x => x, EqualityComparer<string>.Default));
  1333. // GroupBy<int,int,int> (Func<int, int>, Func<int, int>)
  1334. AssertAreSame (expected2, data.GroupBy (x => x, x => x + x));
  1335. // GroupBy<int,int,int> (Func<int, int>, Func<int, int>, IEqualityComparer<int>)
  1336. AssertAreSame (expected2, data.GroupBy (x => x, x => x + x, EqualityComparer<string>.Default));
  1337. // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>)
  1338. AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
  1339. // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>)
  1340. AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
  1341. // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
  1342. AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1343. // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
  1344. AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1345. }
  1346. [Test]
  1347. public void ConcatArgumentNullTest ()
  1348. {
  1349. string [] data = { "2", "1", "5", "3", "4" };
  1350. // Concat<TSource> (IEnumerable<TSource>)
  1351. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Concat (data); });
  1352. AssertException<ArgumentNullException> (delegate () { data.Concat ((IEnumerable<string>) null); });
  1353. }
  1354. [Test]
  1355. public void ConcatTest ()
  1356. {
  1357. int [] data1 = { 2, 1, 5, 3, 4 };
  1358. int [] data2 = { 1, 2, 3, 4, 5 };
  1359. int [] expected = { 2, 1, 5, 3, 4, 1, 2, 3, 4, 5 };
  1360. // Concat<TSource> (IEnumerable<TSource>)
  1361. AssertAreSame (expected, data1.Concat (data2));
  1362. }
  1363. [Test]
  1364. public void DistinctArgumentNullTest ()
  1365. {
  1366. //string [] data = { "2", "1", "5", "3", "4" };
  1367. // Distinct<TSource> ()
  1368. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (); });
  1369. // Distinct<TSource> (IEqualityComparer<TSource>)
  1370. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (EqualityComparer<string>.Default); });
  1371. }
  1372. [Test]
  1373. public void DistinctTest ()
  1374. {
  1375. int [] data = { 2, 1, 5, 3, 4, 2, 5, 3, 1, 8 };
  1376. int [] expected = { 2, 1, 5, 3, 4, 8 };
  1377. // Distinct<TSource> ()
  1378. AssertAreSame (expected, data.Distinct ());
  1379. // Distinct<iTSourcent> (IEqualityComparer<TSource>)
  1380. AssertAreSame (expected, data.Distinct (EqualityComparer<int>.Default));
  1381. }
  1382. [Test]
  1383. public void UnionArgumentNullTest ()
  1384. {
  1385. string [] data = { "2", "1", "5", "3", "4" };
  1386. // Union<TSource> (IEnumerable<TSource>)
  1387. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data); });
  1388. AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null); });
  1389. // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1390. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data, EqualityComparer<string>.Default); });
  1391. AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1392. }
  1393. [Test]
  1394. public void UnionTest ()
  1395. {
  1396. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1397. int [] data2 = { 1, 2, 3, 8, 4, 5 };
  1398. int [] expected = { 2, 1, 5, 7, 3, 4, 8 };
  1399. // Union<TSource> (IEnumerable<TSource>)
  1400. AssertAreSame (expected, data1.Union (data2));
  1401. // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1402. AssertAreSame (expected, data1.Union (data2, EqualityComparer<int>.Default));
  1403. }
  1404. [Test]
  1405. public void IntersectArgumentNullTest ()
  1406. {
  1407. string [] data = { "2", "1", "5", "3", "4" };
  1408. // Intersect<TSource> (IEnumerable<TSource>)
  1409. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data); });
  1410. AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null); });
  1411. // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1412. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data, EqualityComparer<string>.Default); });
  1413. AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1414. }
  1415. [Test]
  1416. public void IntersectTest ()
  1417. {
  1418. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1419. int [] data2 = { 1, 2, 3, 8, 4, 5 };
  1420. int [] expected = { 2, 1, 5, 3, 4 };
  1421. // Intersect<TSource> (IEnumerable<TSource>)
  1422. AssertAreSame (expected, data1.Intersect (data2));
  1423. // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1424. AssertAreSame (expected, data1.Intersect (data2, EqualityComparer<int>.Default));
  1425. }
  1426. [Test]
  1427. public void ExceptArgumentNullTest ()
  1428. {
  1429. string [] data = { "2", "1", "5", "3", "4" };
  1430. // Except<TSource> (IEnumerable<TSource>)
  1431. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data); });
  1432. AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null); });
  1433. // Except<TSource> (IEnumerable<string>, IEqualityComparer<TSource>)
  1434. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data, EqualityComparer<string>.Default); });
  1435. AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1436. }
  1437. [Test]
  1438. public void ExceptTest ()
  1439. {
  1440. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1441. int [] data2 = { 1, 2, 3, 8, 4, 5 };
  1442. int [] expected = { 7 };
  1443. // Except<TSource> (IEnumerable<TSource>)
  1444. AssertAreSame (expected, data1.Except (data2));
  1445. // Except<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1446. AssertAreSame (expected, data1.Except (data2, EqualityComparer<int>.Default));
  1447. }
  1448. [Test]
  1449. public void ReverseArgumentNullTest ()
  1450. {
  1451. //string [] data = { "2", "1", "5", "3", "4" };
  1452. // Reverse<TSource> ()
  1453. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Reverse (); });
  1454. }
  1455. [Test]
  1456. public void ReverseTest ()
  1457. {
  1458. int [] data = { 2, 1, 5, 7, 3, 4 };
  1459. int [] expected = { 4, 3, 7, 5, 1, 2 };
  1460. // Reverse<TSource> ()
  1461. AssertAreSame (expected, data.Reverse ());
  1462. }
  1463. [Test]
  1464. public void SequenceEqualArgumentNullTest ()
  1465. {
  1466. string [] data = { "2", "1", "5", "3", "4" };
  1467. // SequenceEqual<TSource> (IEnumerable<TSource>)
  1468. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data); });
  1469. AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null); });
  1470. // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1471. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data, EqualityComparer<string>.Default); });
  1472. AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1473. }
  1474. [Test]
  1475. public void SequenceEqualTest ()
  1476. {
  1477. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1478. int [] data2 = { 2, 1, 5, 7, 3, 4 };
  1479. int [] data3 = { 2, 1, 5, 7, 3, 4, 5 };
  1480. int [] data4 = { 2, 1, 5, 7, 3 };
  1481. int [] data5 = { 2, 1, 5, 8, 3, 4 };
  1482. // SequenceEqual<TSource> (IEnumerable<TSource>)
  1483. Assert.IsTrue (data1.SequenceEqual (data2));
  1484. Assert.IsFalse (data1.SequenceEqual (data3));
  1485. Assert.IsFalse (data1.SequenceEqual (data4));
  1486. Assert.IsFalse (data1.SequenceEqual (data5));
  1487. // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1488. Assert.IsTrue (data1.SequenceEqual (data2, EqualityComparer<int>.Default));
  1489. Assert.IsFalse (data1.SequenceEqual (data3, EqualityComparer<int>.Default));
  1490. Assert.IsFalse (data1.SequenceEqual (data4, EqualityComparer<int>.Default));
  1491. Assert.IsFalse (data1.SequenceEqual (data5, EqualityComparer<int>.Default));
  1492. }
  1493. [Test]
  1494. public void AsEnumerableArgumentNullTest ()
  1495. {
  1496. //string [] data = { "2", "1", "5", "3", "4" };
  1497. }
  1498. [Test]
  1499. public void AsEnumerableTest ()
  1500. {
  1501. int [] data = { 2, 1, 5, 7, 3, 4 };
  1502. // AsEnumerable<TSource> ()
  1503. Assert.AreSame (data, data.AsEnumerable ());
  1504. }
  1505. [Test]
  1506. public void ToArrayArgumentNullTest ()
  1507. {
  1508. //string [] data = { "2", "1", "5", "3", "4" };
  1509. // ToArray<TSource> ()
  1510. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToArray (); });
  1511. }
  1512. [Test]
  1513. public void ToArrayTest ()
  1514. {
  1515. int [] data = { 2, 3, 4, 5 };
  1516. int [] expected = { 2, 3, 4, 5 };
  1517. // ToArray<TSource> ()
  1518. AssertAreSame (expected, data.ToArray ());
  1519. }
  1520. [Test]
  1521. public void ToListArgumentNullTest ()
  1522. {
  1523. //string [] data = { "2", "1", "5", "3", "4" };
  1524. // ToList<string> ()
  1525. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToList (); });
  1526. }
  1527. [Test]
  1528. public void ToListTest ()
  1529. {
  1530. int [] data = { 2, 4, 5, 1 };
  1531. int [] expected = { 2, 4, 5, 1 };
  1532. // ToList<int> ()
  1533. AssertAreSame (expected, data.ToList ());
  1534. }
  1535. [Test]
  1536. public void ToDictionaryArgumentNullTest ()
  1537. {
  1538. string [] data = { "2", "1", "5", "3", "4" };
  1539. // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
  1540. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test"); });
  1541. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null); });
  1542. // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
  1543. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", EqualityComparer<string>.Default); });
  1544. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, EqualityComparer<string>.Default); });
  1545. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
  1546. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test"); });
  1547. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test"); });
  1548. AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null); });
  1549. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
  1550. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test", EqualityComparer<string>.Default); });
  1551. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test", EqualityComparer<string>.Default); });
  1552. AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null, EqualityComparer<string>.Default); });
  1553. }
  1554. [Test]
  1555. public void ToDictionaryTest ()
  1556. {
  1557. string [] data = { "2", "1", "5", "3", "4" };
  1558. Dictionary<string, string> expected = new Dictionary<string, string> ();
  1559. expected.Add ("k2", "2");
  1560. expected.Add ("k1", "1");
  1561. expected.Add ("k5", "5");
  1562. expected.Add ("k3", "3");
  1563. expected.Add ("k4", "4");
  1564. // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
  1565. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x));
  1566. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key"); });
  1567. // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
  1568. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, EqualityComparer<string>.Default));
  1569. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", EqualityComparer<string>.Default); });
  1570. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
  1571. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x));
  1572. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x); });
  1573. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
  1574. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x, EqualityComparer<string>.Default));
  1575. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x, EqualityComparer<string>.Default); });
  1576. }
  1577. [Test]
  1578. public void ToLookupArgumentNullTest ()
  1579. {
  1580. string [] data = { "2", "1", "5", "3", "4" };
  1581. // ToLookup<string,string> (Func<string, string>)
  1582. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test")); });
  1583. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null); });
  1584. // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
  1585. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1586. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1587. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
  1588. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
  1589. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
  1590. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
  1591. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
  1592. 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); });
  1593. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1594. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1595. }
  1596. [Test]
  1597. public void ToLookupTest ()
  1598. {
  1599. string [] data = { "23", "12", "55", "42", "41" };
  1600. Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
  1601. expected.Add ("2", new List<string> () { "23" });
  1602. expected.Add ("1", new List<string> () { "12" });
  1603. expected.Add ("5", new List<string> () { "55" });
  1604. expected.Add ("4", new List<string> () { "42", "41" });
  1605. // ToLookup<string,string> (Func<string, string>)
  1606. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup ((x => x [0].ToString ())));
  1607. // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
  1608. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), EqualityComparer<string>.Default));
  1609. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
  1610. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x));
  1611. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
  1612. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x, EqualityComparer<string>.Default));
  1613. }
  1614. [Test]
  1615. public void DefaultIfEmptyArgumentNullTest ()
  1616. {
  1617. //string [] data = { "2", "1", "5", "3", "4" };
  1618. // DefaultIfEmpty<string> ()
  1619. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> (); });
  1620. // DefaultIfEmpty<string> (string)
  1621. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> ((string) "default"); });
  1622. }
  1623. [Test]
  1624. public void DefaultIfEmptyTest ()
  1625. {
  1626. string [] data = { "2", "1", "5", "3", "4" };
  1627. string [] empty = { };
  1628. string [] default1 = { null };
  1629. string [] default2 = { "default" };
  1630. // DefaultIfEmpty<string> ()
  1631. AssertAreSame (data, data.DefaultIfEmpty ());
  1632. AssertAreSame (default1, empty.DefaultIfEmpty ());
  1633. // DefaultIfEmpty<string> (string)
  1634. AssertAreSame (data, data.DefaultIfEmpty ("default"));
  1635. AssertAreSame (default2, empty.DefaultIfEmpty ("default"));
  1636. }
  1637. [Test]
  1638. public void OfTypeArgumentNullTest ()
  1639. {
  1640. //string [] data = { "2", "1", "5", "3", "4" };
  1641. // OfType<string> ()
  1642. AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).OfType<string> (); });
  1643. }
  1644. [Test]
  1645. public void OfTypeTest ()
  1646. {
  1647. object [] data = { "2", 2, "1", "5", "3", "4" };
  1648. string [] expected = { "2", "1", "5", "3", "4" };
  1649. // OfType<string> ()
  1650. AssertAreSame (expected, data.OfType<string> ());
  1651. }
  1652. [Test]
  1653. public void CastArgumentNullTest ()
  1654. {
  1655. //string [] data = { "2", "1", "5", "3", "4" };
  1656. // Cast<string> ()
  1657. AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).Cast<string> (); });
  1658. }
  1659. [Test]
  1660. public void CastTest ()
  1661. {
  1662. object [] data = { 1, 2, 3 };
  1663. int [] expected = { 1, 2, 3 };
  1664. // Cast<string> ()
  1665. AssertAreSame (expected, data.Cast<int> ());
  1666. AssertException<InvalidCastException> (delegate () { data.Cast<IEnumerable> ().GetEnumerator ().MoveNext (); });
  1667. data.Cast<IEnumerable> ();
  1668. }
  1669. [Test]
  1670. public void RangeArgumentNullTest ()
  1671. {
  1672. //string [] data = { "2", "1", "5", "3", "4" };
  1673. }
  1674. [Test]
  1675. public void RangeTest ()
  1676. {
  1677. int [] expected = { 2, 3, 4, 5 };
  1678. // Range<> (int)
  1679. AssertAreSame (expected, Enumerable.Range (2, 4));
  1680. AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (2, -3); });
  1681. AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (int.MaxValue - 5, 7); });
  1682. Enumerable.Range (int.MaxValue - 5, 6);
  1683. }
  1684. }
  1685. }