EnumerableMoreTest.cs 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  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. [Category ("NotWorking")]
  562. public void SumTest ()
  563. {
  564. string [] data = { "2", "3", "5", "5" };
  565. //TODO: OverflowException
  566. // Sum<TSource> (Func<TSource, int>)
  567. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, int>) (x => int.Parse (x))));
  568. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, int>) (x => int.Parse (x))));
  569. // Sum<TSource> (Func<TSource, Nullable<int>>)
  570. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  571. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  572. // Sum<TSource> (Func<TSource, Int64>)
  573. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Int64>) (x => int.Parse (x))));
  574. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Int64>) (x => int.Parse (x))));
  575. // Sum<TSource> (Func<TSource, Nullable<Int64>>)
  576. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  577. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  578. // Sum<TSource> (Func<TSource, Single>)
  579. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Single>) (x => int.Parse (x))));
  580. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Single>) (x => int.Parse (x))));
  581. // Sum<TSource> (Func<TSource, Nullable<Single>>)
  582. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  583. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  584. // Sum<TSource> (Func<TSource, Double>)
  585. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Double>) (x => int.Parse (x))));
  586. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Double>) (x => int.Parse (x))));
  587. // Sum<TSource> (Func<TSource, Nullable<Double>>)
  588. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  589. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  590. // Sum<TSource> (Func<TSource, Decimal>)
  591. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  592. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  593. // Sum<TSource> (Func<TSource, Nullable<Decimal>>)
  594. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  595. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  596. // Sum<> ()
  597. Assert.AreEqual (6, ((IEnumerable<int>) new int [] { 1, 2, 3 }).Sum ());
  598. Assert.AreEqual (0, Enumerable.Empty<int> ().Sum ());
  599. // Sum<> ()
  600. Assert.AreEqual (6, ((IEnumerable<Nullable<int>>) new int? [] { 1, 2, 3 }).Sum ());
  601. Assert.AreEqual (0, Enumerable.Empty<int?> ().Sum ());
  602. // Sum<> ()
  603. Assert.AreEqual (6, ((IEnumerable<Int64>) new long [] { 1, 2, 3 }).Sum ());
  604. Assert.AreEqual (0, Enumerable.Empty<long> ().Sum ());
  605. // Sum<> ()
  606. Assert.AreEqual (6, ((IEnumerable<Nullable<Int64>>) new long? [] { 1, 2, 3 }).Sum ());
  607. Assert.AreEqual (0, Enumerable.Empty<long?> ().Sum ());
  608. // Sum<> ()
  609. Assert.AreEqual (6, ((IEnumerable<Single>) new float [] { 1, 2, 3 }).Sum ());
  610. Assert.AreEqual (0, Enumerable.Empty<float> ().Sum ());
  611. // Sum<> ()
  612. Assert.AreEqual (6, ((IEnumerable<Nullable<Single>>) new float? [] { 1, 2, 3 }).Sum ());
  613. Assert.AreEqual (0, Enumerable.Empty<float?> ().Sum ());
  614. // Sum<> ()
  615. Assert.AreEqual (6, ((IEnumerable<Double>) new double [] { 1, 2, 3 }).Sum ());
  616. Assert.AreEqual (0, Enumerable.Empty<double> ().Sum ());
  617. // Sum<> ()
  618. Assert.AreEqual (6, ((IEnumerable<Nullable<Double>>) new double? [] { 1, 2, 3 }).Sum ());
  619. Assert.AreEqual (0, Enumerable.Empty<double?> ().Sum ());
  620. // Sum<> ()
  621. Assert.AreEqual (6, ((IEnumerable<Decimal>) new decimal [] { 1, 2, 3 }).Sum ());
  622. Assert.AreEqual (0, Enumerable.Empty<decimal> ().Sum ());
  623. // Sum<> ()
  624. Assert.AreEqual (6, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 1, 2, 3 }).Sum ());
  625. Assert.AreEqual (0, Enumerable.Empty<decimal?> ().Sum ());
  626. }
  627. [Test]
  628. public void MinArgumentNullTest ()
  629. {
  630. string [] data = { "2", "1", "5", "3", "4" };
  631. // Min<TSource> ()
  632. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> (); });
  633. // Min<TSource> (Func<TSource, int>)
  634. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, int>) (x => 0)); });
  635. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, int>) null); });
  636. // Min<TSource> (Func<TSource, Nullable<int>>)
  637. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  638. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<int>>) null); });
  639. // Min<TSource> (Func<TSource, Int64>)
  640. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Int64>) (x => 0L)); });
  641. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Int64>) null); });
  642. // Min<TSource> (Func<TSource, Nullable<Int64>>)
  643. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  644. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Int64>>) null); });
  645. // Min<TSource> (Func<TSource, Single>)
  646. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Single>) (x => 0f)); });
  647. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Single>) null); });
  648. // Min<TSource> (Func<TSource, Nullable<Single>>)
  649. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  650. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Single>>) null); });
  651. // Min<TSource> (Func<TSource, Double>)
  652. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Double>) (x => 0d)); });
  653. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Double>) null); });
  654. // Min<TSource> (Func<TSource, Nullable<Double>>)
  655. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  656. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Double>>) null); });
  657. // Min<TSource> (Func<TSource, Decimal>)
  658. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Decimal>) (x => 0m)); });
  659. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Decimal>) null); });
  660. // Min<TSource> (Func<TSource, Nullable<Decimal>>)
  661. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  662. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Decimal>>) null); });
  663. // Min<TSource,TSource> (Func<TSource, string>)
  664. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string, string> ((Func<string, string>) (x => "test")); });
  665. AssertException<ArgumentNullException> (delegate () { data.Min<string, string> ((Func<string, string>) null); });
  666. // Min<> ()
  667. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Min (); });
  668. // Min<> ()
  669. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Min (); });
  670. // Min<> ()
  671. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Min (); });
  672. // Min<> ()
  673. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Min (); });
  674. // Min<> ()
  675. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Min (); });
  676. // Min<> ()
  677. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Min (); });
  678. // Min<> ()
  679. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Min (); });
  680. // Min<> ()
  681. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Min (); });
  682. // Min<> ()
  683. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Min (); });
  684. // Min<> ()
  685. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Min (); });
  686. }
  687. [Test]
  688. [Category ("NotWorking")]
  689. public void MinTest ()
  690. {
  691. string [] data = { "2", "1", "5", "3", "4" };
  692. // Min<TSource> ()
  693. Assert.AreEqual ("1", ((IEnumerable<string>) data).Min<string> ());
  694. // Min<TSource> (Func<TSource, int>)
  695. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, int>) (x => int.Parse (x))));
  696. // Min<TSource> (Func<TSource, Nullable<int>>)
  697. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  698. // Min<TSource> (Func<TSource, Int64>)
  699. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Int64>) (x => int.Parse (x))));
  700. // Min<TSource> (Func<TSource, Nullable<Int64>>)
  701. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  702. // Min<TSource> (Func<TSource, Single>)
  703. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Single>) (x => int.Parse (x))));
  704. // Min<TSource> (Func<TSource, Nullable<Single>>)
  705. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  706. // Min<TSource> (Func<TSource, Double>)
  707. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Double>) (x => int.Parse (x))));
  708. // Min<TSource> (Func<TSource, Nullable<Double>>)
  709. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  710. // Min<TSource> (Func<TSource, Decimal>)
  711. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  712. // Min<TSource> (Func<TSource, Nullable<Decimal>>)
  713. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  714. // Min<TSource,TSource> (Func<TSource, TSource>)
  715. Assert.AreEqual ("1", ((IEnumerable<string>) data).Min<string, string> ((Func<string, string>) (x => x)));
  716. // Min<> ()
  717. Assert.AreEqual (2, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Min ());
  718. // Min<> ()
  719. Assert.AreEqual (2, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Min ());
  720. // Min<> ()
  721. Assert.AreEqual (2, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Min ());
  722. // Min<> ()
  723. Assert.AreEqual (2, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Min ());
  724. // Min<> ()
  725. Assert.AreEqual (2, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Min ());
  726. // Min<> ()
  727. Assert.AreEqual (2, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Min ());
  728. // Min<> ()
  729. Assert.AreEqual (2, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Min ());
  730. // Min<> ()
  731. Assert.AreEqual (2, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Min ());
  732. // Min<> ()
  733. Assert.AreEqual (2, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Min ());
  734. // Min<> ()
  735. Assert.AreEqual (2, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Min ());
  736. }
  737. [Test]
  738. public void MaxArgumentNullTest ()
  739. {
  740. string [] data = { "2", "1", "5", "3", "4" };
  741. // Max<TSource> ()
  742. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> (); });
  743. // Max<TSource> (Func<TSource, int>)
  744. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, int>) (x => 0)); });
  745. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, int>) null); });
  746. // Max<TSource> (Func<TSource, Nullable<int>>)
  747. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  748. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<int>>) null); });
  749. // Max<TSource> (Func<TSource, Int64>)
  750. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Int64>) (x => 0L)); });
  751. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Int64>) null); });
  752. // Max<TSource> (Func<TSource, Nullable<Int64>>)
  753. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  754. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Int64>>) null); });
  755. // Max<TSource> (Func<TSource, Single>)
  756. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Single>) (x => 0f)); });
  757. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Single>) null); });
  758. // Max<TSource> (Func<TSource, Nullable<Single>>)
  759. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  760. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Single>>) null); });
  761. // Max<TSource> (Func<TSource, Double>)
  762. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Double>) (x => 0d)); });
  763. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Double>) null); });
  764. // Max<TSource> (Func<TSource, Nullable<Double>>)
  765. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  766. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Double>>) null); });
  767. // Max<TSource> (Func<TSource, Decimal>)
  768. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Decimal>) (x => 0m)); });
  769. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Decimal>) null); });
  770. // Max<TSource> (Func<TSource, Nullable<Decimal>>)
  771. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  772. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Decimal>>) null); });
  773. // Max<TSource,TSource> (Func<TSource, TSource>)
  774. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string, string> ((Func<string, string>) (x => "test")); });
  775. AssertException<ArgumentNullException> (delegate () { data.Max<string, string> ((Func<string, string>) null); });
  776. // Max<> ()
  777. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Max (); });
  778. // Max<> ()
  779. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Max (); });
  780. // Max<> ()
  781. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Max (); });
  782. // Max<> ()
  783. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Max (); });
  784. // Max<> ()
  785. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Max (); });
  786. // Max<> ()
  787. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Max (); });
  788. // Max<> ()
  789. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Max (); });
  790. // Max<> ()
  791. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Max (); });
  792. // Max<> ()
  793. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Max (); });
  794. // Max<> ()
  795. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Max (); });
  796. }
  797. [Test]
  798. public void MaxTest ()
  799. {
  800. string [] data = { "2", "1", "5", "3", "4" };
  801. // Max<string> ()
  802. Assert.AreEqual ("5", ((IEnumerable<string>) data).Max<string> ());
  803. // Max<TSource> (Func<TSource, int>)
  804. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, int>) (x => int.Parse (x))));
  805. // Max<TSource> (Func<TSource, Nullable<int>>)
  806. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  807. // Max<TSource> (Func<TSource, Int64>)
  808. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Int64>) (x => int.Parse (x))));
  809. // Max<TSource> (Func<TSource, Nullable<Int64>>)
  810. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  811. // Max<TSource> (Func<TSource, Single>)
  812. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Single>) (x => int.Parse (x))));
  813. // Max<TSource> (Func<TSource, Nullable<Single>>)
  814. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  815. // Max<TSource> (Func<TSource, Double>)
  816. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Double>) (x => int.Parse (x))));
  817. // Max<TSource> (Func<TSource, Nullable<Double>>)
  818. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  819. // Max<TSource> (Func<TSource, Decimal>)
  820. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  821. // Max<TSource> (Func<TSource, Nullable<Decimal>>)
  822. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  823. // Max<TSource,TSource> (Func<TSource, TSource>)
  824. Assert.AreEqual ("5", ((IEnumerable<string>) data).Max<string, string> ((Func<string, string>) (x => x)));
  825. // Max<> ()
  826. Assert.AreEqual (4, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Max ());
  827. // Max<> ()
  828. Assert.AreEqual (4, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Max ());
  829. // Max<> ()
  830. Assert.AreEqual (4, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Max ());
  831. // Max<> ()
  832. Assert.AreEqual (4, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Max ());
  833. // Max<> ()
  834. Assert.AreEqual (4, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Max ());
  835. // Max<> ()
  836. Assert.AreEqual (4, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Max ());
  837. // Max<> ()
  838. Assert.AreEqual (4, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Max ());
  839. // Max<> ()
  840. Assert.AreEqual (4, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Max ());
  841. // Max<> ()
  842. Assert.AreEqual (4, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Max ());
  843. // Max<> ()
  844. Assert.AreEqual (4, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Max ());
  845. }
  846. [Test]
  847. public void AverageArgumentNullTest ()
  848. {
  849. string [] data = { "2", "1", "5", "3", "4" };
  850. // Average<TSource> (Func<TSource, int>)
  851. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, int>) (x => 0)); });
  852. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, int>) null); });
  853. // Average<TSource> (Func<TSource, Nullable<int>>)
  854. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  855. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<int>>) null); });
  856. // Average<TSource> (Func<TSource, Int64>)
  857. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Int64>) (x => 0L)); });
  858. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Int64>) null); });
  859. // Average<TSource> (Func<TSource, Nullable<Int64>>)
  860. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  861. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Int64>>) null); });
  862. // Average<TSource> (Func<TSource, Single>)
  863. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Single>) (x => 0f)); });
  864. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Single>) null); });
  865. // Average<TSource> (Func<TSource, Nullable<Single>>)
  866. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  867. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Single>>) null); });
  868. // Average<TSource> (Func<TSource, Double>)
  869. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Double>) (x => 0d)); });
  870. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Double>) null); });
  871. // Average<TSource> (Func<TSource, Nullable<Double>>)
  872. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  873. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Double>>) null); });
  874. // Average<TSource> (Func<TSource, Decimal>)
  875. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Decimal>) (x => 0m)); });
  876. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Decimal>) null); });
  877. // Average<TSource> (Func<TSource, Nullable<Decimal>>)
  878. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  879. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Decimal>>) null); });
  880. // Average<> ()
  881. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Average (); });
  882. // Average<> ()
  883. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Average (); });
  884. // Average<> ()
  885. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Average (); });
  886. // Average<> ()
  887. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Average (); });
  888. // Average<> ()
  889. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Average (); });
  890. // Average<> ()
  891. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Average (); });
  892. // Average<> ()
  893. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Average (); });
  894. // Average<> ()
  895. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Average (); });
  896. // Average<> ()
  897. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Average (); });
  898. // Average<> ()
  899. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Average (); });
  900. }
  901. [Test]
  902. public void AverageTest ()
  903. {
  904. string [] data = { "2", "1", "5", "3", "4" };
  905. string [] empty = { };
  906. // Average<string> (Func<string, int>)
  907. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, int>) (x => int.Parse (x))));
  908. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, int>) (x => int.Parse (x))); });
  909. // Average<TSource> (Func<TSource, Nullable<int>>)
  910. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  911. // Average<TSource> (Func<TSource, Int64>)
  912. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, long>) (x => int.Parse (x))));
  913. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, long>) (x => int.Parse (x))); });
  914. // Average<TSource> (Func<TSource, Nullable<Int64>>)
  915. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, long?>) (x => (int?) int.Parse (x))));
  916. // Average<TSource> (Func<TSource, Single>)
  917. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, float>) (x => int.Parse (x))));
  918. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, float>) (x => int.Parse (x))); });
  919. // Average<TSource> (Func<TSource, Nullable<Single>>)
  920. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, float?>) (x => (int?) int.Parse (x))));
  921. // Average<TSource> (Func<TSource, Double>)
  922. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, double>) (x => int.Parse (x))));
  923. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, double>) (x => int.Parse (x))); });
  924. // Average<TSource> (Func<TSource, Nullable<Double>>)
  925. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, double?>) (x => (int?) int.Parse (x))));
  926. // Average<TSource> (Func<TSource, Decimal>)
  927. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, decimal>) (x => int.Parse (x))));
  928. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, decimal>) (x => int.Parse (x))); });
  929. // Average<TSource> (Func<TSource, Nullable<Decimal>>)
  930. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, decimal?>) (x => (int?) int.Parse (x))));
  931. // Average<> ()
  932. Assert.AreEqual (3, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Average ());
  933. AssertException<InvalidOperationException> (delegate () { new int [0].Average (); });
  934. // Average<> ()
  935. Assert.AreEqual (3, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Average ());
  936. // Average<> ()
  937. Assert.AreEqual (3, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Average ());
  938. AssertException<InvalidOperationException> (delegate () { new long [0].Average (); });
  939. // Average<> ()
  940. Assert.AreEqual (3, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Average ());
  941. // Average<> ()
  942. Assert.AreEqual (3, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Average ());
  943. AssertException<InvalidOperationException> (delegate () { new float [0].Average (); });
  944. // Average<> ()
  945. Assert.AreEqual (3, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Average ());
  946. // Average<> ()
  947. Assert.AreEqual (3, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Average ());
  948. AssertException<InvalidOperationException> (delegate () { new double [0].Average (); });
  949. // Average<> ()
  950. Assert.AreEqual (3, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Average ());
  951. // Average<> ()
  952. Assert.AreEqual (3, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Average ());
  953. AssertException<InvalidOperationException> (delegate () { new decimal [0].Average (); });
  954. // Average<> ()
  955. Assert.AreEqual (3, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Average ());
  956. }
  957. [Test]
  958. public void WhereArgumentNullTest ()
  959. {
  960. string [] data = { "2", "1", "5", "3", "4" };
  961. // Where<TSource> (Func<TSource, bool>)
  962. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Where (x => true); });
  963. AssertException<ArgumentNullException> (delegate () { data.Where ((Func<string, bool>) null); });
  964. // Where<TSource> (Func<TSource, int, bool>)
  965. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Where ((x, y) => true); });
  966. AssertException<ArgumentNullException> (delegate () { data.Where ((Func<string, int, bool>) null); });
  967. }
  968. [Test]
  969. public void WhereTest ()
  970. {
  971. int [] data = { 2, 1, 5, 3, 4 };
  972. int [] expected1 = { 2, 1 };
  973. int [] expected2 = { 2 };
  974. // Where<TSource> (Func<TSource, bool>)
  975. AssertAreSame (expected1, data.Where (x => x < 3));
  976. // Where<TSource> (Func<TSource, int, bool>)
  977. AssertAreSame (expected2, data.Where ((x, y) => x < 3 && y != 1));
  978. }
  979. [Test]
  980. public void SelectArgumentNullTest ()
  981. {
  982. string [] data = { "2", "1", "5", "3", "4" };
  983. // Select<TSource,TResult> (Func<TSource, TResult>)
  984. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Select (x => "test"); });
  985. AssertException<ArgumentNullException> (delegate () { data.Select ((Func<string, string>) null); });
  986. // Select<TSource,TResult> (Func<TSource, int, TResult>)
  987. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Select ((x, y) => "test"); });
  988. AssertException<ArgumentNullException> (delegate () { data.Select ((Func<string, int, string>) null); });
  989. }
  990. [Test]
  991. public void SelectTest ()
  992. {
  993. string [] data = { "2", "1", "5", "3", "4" };
  994. string [] expected1 = { "2x", "1x", "5x", "3x", "4x" };
  995. string [] expected2 = { "2x0", "1x1", "5x2", "3x3", "4x4" };
  996. // Select<TSource,TResult> (Func<TSource, TResult>)
  997. AssertAreSame (expected1, data.Select<string, string> (x => x + "x"));
  998. // Select<TSource,TResult> (Func<TSource, int, TResult>)
  999. AssertAreSame (expected2, data.Select<string, string> ((x, y) => x + "x" + y));
  1000. }
  1001. [Test]
  1002. public void SelectManyArgumentNullTest ()
  1003. {
  1004. string [] data = { "2", "1", "5", "3", "4" };
  1005. // SelectMany<TSource,TResult> (Func<TSource, IEnumerable<TResult>>)
  1006. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany (x => data); });
  1007. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, IEnumerable<string>>) null); });
  1008. // SelectMany<TSource,TResult> (Func<TSource, int, IEnumerable<TResult>>)
  1009. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany ((x, y) => data); });
  1010. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, int, IEnumerable<string>>) null); });
  1011. // SelectMany<TSource,TCollection,TResult> (Func<string, int, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1012. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany ((x, y) => data, (x, y) => "test"); });
  1013. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, int, IEnumerable<string>>) null, (x, y) => "test"); });
  1014. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((x, y) => data, (Func<string, string, string>) null); });
  1015. // SelectMany<TSource,TCollection,TResult> (Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1016. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany (x => data, (x, y) => "test"); });
  1017. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, IEnumerable<string>>) null, (x, y) => "test"); });
  1018. AssertException<ArgumentNullException> (delegate () { data.SelectMany (x => data, (Func<string, string, string>) null); });
  1019. }
  1020. [Test]
  1021. public void SelectManyTest ()
  1022. {
  1023. string [] data = { "0", "1" };
  1024. string [] expected = { "0", "00", "1", "11" };
  1025. // SelectMany<TSource,TResult> (Func<TSource, IEnumerable<TResult>>)
  1026. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany (x => new string [] { x, x + x }));
  1027. // SelectMany<TSource,TResult> (Func<TSource, int, IEnumerable<TResult>>)
  1028. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany ((x, y) => new string [] { x, x + y }));
  1029. // SelectMany<TSource,TCollection,TResult> (Func<string, int, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1030. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany ((x, y) => new string [] { x, x + y }, (x, y) => y));
  1031. // SelectMany<TSource,TCollection,TResult> (Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1032. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany (x => new string [] { x, x + x }, (x, y) => y));
  1033. }
  1034. [Test]
  1035. public void TakeArgumentNullTest ()
  1036. {
  1037. //string [] data = { "2", "1", "5", "3", "4" };
  1038. // Take<TSource> (int)
  1039. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Take (0); });
  1040. }
  1041. [Test]
  1042. public void TakeTest ()
  1043. {
  1044. int [] data = { 2, 1, 5, 3, 1 };
  1045. int [] expected = { 2, 1 };
  1046. int [] empty = { };
  1047. // Take<TSource> (int)
  1048. AssertAreSame (expected, data.Take (2));
  1049. AssertAreSame (empty, data.Take (-2));
  1050. }
  1051. [Test]
  1052. public void TakeWhileArgumentNullTest ()
  1053. {
  1054. string [] data = { "2", "1", "5", "3", "4" };
  1055. // TakeWhile<TSource> (Func<TSource, bool>)
  1056. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).TakeWhile (x => true); });
  1057. AssertException<ArgumentNullException> (delegate () { data.TakeWhile ((Func<string, bool>) null); });
  1058. // TakeWhile<TSource> (Func<TSource, int, bool>)
  1059. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).TakeWhile ((x, y) => true); });
  1060. AssertException<ArgumentNullException> (delegate () { data.TakeWhile ((Func<string, int, bool>) null); });
  1061. }
  1062. [Test]
  1063. public void TakeWhileTest ()
  1064. {
  1065. int [] data = { 2, 1, 5, 3, 1 };
  1066. int [] expected = { 2, 1 };
  1067. // TakeWhile<TSource> (Func<TSource, bool>)
  1068. AssertAreSame (expected, data.TakeWhile (x => x != 5));
  1069. // TakeWhile<TSource> (Func<TSource, int, bool>)
  1070. AssertAreSame (expected, data.TakeWhile ((x, y) => y != 2));
  1071. }
  1072. [Test]
  1073. public void SkipArgumentNullTest ()
  1074. {
  1075. //string [] data = { "2", "1", "5", "3", "4" };
  1076. // Skip<TSource> (int)
  1077. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Skip (0); });
  1078. }
  1079. [Test]
  1080. public void SkipTest ()
  1081. {
  1082. int [] data = { 2, 1, 5, 3, 1 };
  1083. int [] expected = { 5, 3, 1 };
  1084. // Skip<string> (TSource)
  1085. AssertAreSame (expected, data.Skip (2));
  1086. AssertAreSame (data, data.Skip (-2));
  1087. }
  1088. [Test]
  1089. public void SkipWhileArgumentNullTest ()
  1090. {
  1091. string [] data = { "2", "1", "5", "3", "4" };
  1092. // SkipWhile<TSource> (Func<TSource, bool>)
  1093. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SkipWhile (x => true); });
  1094. AssertException<ArgumentNullException> (delegate () { data.SkipWhile ((Func<string, bool>) null); });
  1095. // SkipWhile<TSource> (Func<TSource, int, bool>)
  1096. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SkipWhile ((x, y) => true); });
  1097. AssertException<ArgumentNullException> (delegate () { data.SkipWhile ((Func<string, int, bool>) null); });
  1098. }
  1099. [Test]
  1100. public void SkipWhileTest ()
  1101. {
  1102. int [] data = { 2, 1, 5, 3, 1 };
  1103. int [] expected = { 5, 3, 1 };
  1104. // SkipWhile<TSource> (Func<TSource, bool>)
  1105. AssertAreSame (expected, data.SkipWhile (x => x != 5));
  1106. // SkipWhile<TSource> (Func<TSource, int, bool>)
  1107. AssertAreSame (expected, data.SkipWhile ((x, y) => y != 2));
  1108. }
  1109. [Test]
  1110. [Category ("NotWorking")]
  1111. public void JoinArgumentNullTest ()
  1112. {
  1113. string [] data = { "2", "1", "5", "3", "4" };
  1114. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)
  1115. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Join (data, x => "test", x => "test", (x, y) => "test"); });
  1116. AssertException<ArgumentNullException> (delegate () { data.Join ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test"); });
  1117. AssertException<ArgumentNullException> (delegate () { data.Join (data, (Func<string, string>) null, x => "test", (x, y) => "test"); });
  1118. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", (Func<string, string>) null, (x, y) => "test"); });
  1119. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", x => "test", (Func<string, string, string>) null); });
  1120. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<string>)
  1121. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Join (data, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1122. AssertException<ArgumentNullException> (delegate () { data.Join ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1123. AssertException<ArgumentNullException> (delegate () { data.Join (data, (Func<string, string>) null, x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1124. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", (Func<string, string>) null, (x, y) => "test", EqualityComparer<string>.Default); });
  1125. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", x => "test", (Func<string, string, string>) null, EqualityComparer<string>.Default); });
  1126. }
  1127. [Test]
  1128. public void JoinTest ()
  1129. {
  1130. string [] dataOuter1 = { "2", "1", "5", "3", "4" };
  1131. string [] dataInner1 = { "7", "3", "5", "8", "9" };
  1132. string [] expected1 = { "55", "33" };
  1133. string [] dataOuter2 = { "2", "1", "3", "4" };
  1134. string [] dataInner2 = { "7", "5", "8", "9" };
  1135. string [] expected2 = { };
  1136. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)
  1137. AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y));
  1138. AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y));
  1139. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<string>)
  1140. AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y, EqualityComparer<string>.Default));
  1141. AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y, EqualityComparer<string>.Default));
  1142. }
  1143. [Test]
  1144. [Category ("NotWorking")]
  1145. public void GroupJoinArgumentNullTest ()
  1146. {
  1147. string [] data = { "2", "1", "5", "3", "4" };
  1148. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
  1149. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test"); });
  1150. AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test"); });
  1151. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test"); });
  1152. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test"); });
  1153. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null); });
  1154. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
  1155. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1156. AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1157. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1158. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test", EqualityComparer<string>.Default); });
  1159. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null, EqualityComparer<string>.Default); });
  1160. }
  1161. [Test]
  1162. public void GroupJoinTest ()
  1163. {
  1164. string [] dataOuter1 = { "2", "1", "5", "3", "4" };
  1165. string [] dataInner1 = { "7", "3", "5", "3", "9" };
  1166. string [] expected1 = { "2", "1", "55", "333", "4" };
  1167. string [] dataOuter2 = { "2", "1", "5", "8", "4" };
  1168. string [] dataInner2 = { "7", "3", "6", "3", "9" };
  1169. string [] expected2 = { "2", "1", "5", "8", "4" };
  1170. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
  1171. AssertAreSame (expected1, (dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
  1172. AssertAreSame (expected2, (dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
  1173. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
  1174. AssertAreSame (expected1, dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1175. AssertAreSame (expected2, dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1176. }
  1177. [Test]
  1178. public void OrderByArgumentNullTest ()
  1179. {
  1180. string [] data = { "2", "1", "5", "3", "4" };
  1181. // OrderBy<TSource,TKey> (Func<TSource, TKey>)
  1182. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test"); });
  1183. AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null); });
  1184. // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1185. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test", Comparer<string>.Default); });
  1186. AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null, Comparer<string>.Default); });
  1187. }
  1188. [Test]
  1189. public void OrderByTest ()
  1190. {
  1191. int [] data = { 2, 1, 5, 3, 4 };
  1192. int [] expected = { 1, 2, 3, 4, 5 };
  1193. // OrderBy<TSource,TKey> (Func<TSource, TKey>)
  1194. AssertAreSame (expected, data.OrderBy (x => x));
  1195. // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1196. AssertAreSame (expected, data.OrderBy (x => x, Comparer<int>.Default));
  1197. }
  1198. [Test]
  1199. public void OrderByDescendingArgumentNullTest ()
  1200. {
  1201. string [] data = { "2", "1", "5", "3", "4" };
  1202. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
  1203. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test"); });
  1204. AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null); });
  1205. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1206. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test", Comparer<string>.Default); });
  1207. AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null, Comparer<string>.Default); });
  1208. }
  1209. [Test]
  1210. public void OrderByDescendingTest ()
  1211. {
  1212. int [] data = { 2, 1, 5, 3, 4 };
  1213. int [] expected = { 5, 4, 3, 2, 1 };
  1214. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
  1215. AssertAreSame (expected, data.OrderByDescending (x => x));
  1216. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1217. AssertAreSame (expected, data.OrderByDescending (x => x, Comparer<int>.Default));
  1218. }
  1219. [Test]
  1220. public void ThenByArgumentNullTest ()
  1221. {
  1222. string [] data = { "2", "1", "5", "3", "4" };
  1223. // ThenBy<TSource,TKey> (Func<TSource, TKey>)
  1224. AssertException<ArgumentNullException> (delegate () {
  1225. ((IOrderedEnumerable<string>) null).ThenBy (x => "test");
  1226. });
  1227. AssertException<ArgumentNullException> (delegate () {
  1228. data.OrderBy (x => x).ThenBy ((Func<string, string>) null);
  1229. });
  1230. // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1231. AssertException<ArgumentNullException> (delegate () {
  1232. ((IOrderedEnumerable<string>) null).ThenBy (x => "test", Comparer<string>.Default);
  1233. });
  1234. AssertException<ArgumentNullException> (delegate () {
  1235. data.OrderBy (x => x).ThenBy ((Func<string, string>) null, Comparer<string>.Default);
  1236. });
  1237. }
  1238. [Test]
  1239. public void ThenByTest ()
  1240. {
  1241. int [] data = { 2, 1, 5, 3, 4 };
  1242. int [] expected = { 1, 2, 3, 4, 5 };
  1243. // ThenBy<TSource,TKey> (Func<TSource, TKey>)
  1244. AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x));
  1245. // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1246. AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x, Comparer<int>.Default));
  1247. }
  1248. [Test]
  1249. public void ThenByDescendingArgumentNullTest ()
  1250. {
  1251. string [] data = { "2", "1", "5", "3", "4" };
  1252. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
  1253. AssertException<ArgumentNullException> (delegate () {
  1254. ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test");
  1255. });
  1256. AssertException<ArgumentNullException> (delegate () {
  1257. data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null);
  1258. });
  1259. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1260. AssertException<ArgumentNullException> (delegate () {
  1261. ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test", Comparer<string>.Default);
  1262. });
  1263. AssertException<ArgumentNullException> (delegate () {
  1264. data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null, Comparer<string>.Default);
  1265. });
  1266. }
  1267. [Test]
  1268. public void ThenByDescendingTest ()
  1269. {
  1270. int [] data = { 2, 1, 5, 3, 4 };
  1271. int [] expected = { 5, 4, 3, 2, 1 };
  1272. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
  1273. AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x));
  1274. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1275. AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x, Comparer<int>.Default));
  1276. }
  1277. [Test]
  1278. [Category ("NotWorking")]
  1279. public void GroupByArgumentNullTest ()
  1280. {
  1281. string [] data = { "2", "1", "5", "3", "4" };
  1282. // GroupBy<string,string> (Func<string, string>)
  1283. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test")); });
  1284. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null); });
  1285. // GroupBy<string,string> (Func<string, string>, IEqualityComparer<string>)
  1286. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1287. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1288. // GroupBy<string,string,string> (Func<string, string>, Func<string, string>)
  1289. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
  1290. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
  1291. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
  1292. // GroupBy<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
  1293. 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); });
  1294. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1295. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1296. // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>)
  1297. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
  1298. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
  1299. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null); });
  1300. // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>)
  1301. 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")); });
  1302. 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")); });
  1303. 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")); });
  1304. 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); });
  1305. // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
  1306. 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); });
  1307. 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); });
  1308. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1309. // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
  1310. 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); });
  1311. 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); });
  1312. 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); });
  1313. 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); });
  1314. }
  1315. [Test]
  1316. [Category ("NotWorking")]
  1317. public void GroupByTest ()
  1318. {
  1319. string [] data = { "2", "1", "5", "3", "4", "3" };
  1320. Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
  1321. expected.Add ("2", new List<string> () { "2" });
  1322. expected.Add ("1", new List<string> () { "1" });
  1323. expected.Add ("5", new List<string> () { "5" });
  1324. expected.Add ("3", new List<string> () { "3", "3" });
  1325. expected.Add ("4", new List<string> () { "4" });
  1326. Dictionary<string, IEnumerable<string>> expected2 = new Dictionary<string, IEnumerable<string>> ();
  1327. expected2.Add ("2", new List<string> () { "22" });
  1328. expected2.Add ("1", new List<string> () { "11" });
  1329. expected2.Add ("5", new List<string> () { "55" });
  1330. expected2.Add ("3", new List<string> () { "33", "33" });
  1331. expected2.Add ("4", new List<string> () { "44" });
  1332. string [] expected3 = new string [] { "22", "11", "55", "333", "44" };
  1333. // GroupBy<int,int> (Func<int, int>)
  1334. AssertAreSame (expected, data.GroupBy (x => x));
  1335. // GroupBy<int,int> (Func<int, int>, IEqualityComparer<int>)
  1336. AssertAreSame (expected, data.GroupBy (x => x, EqualityComparer<string>.Default));
  1337. // GroupBy<int,int,int> (Func<int, int>, Func<int, int>)
  1338. AssertAreSame (expected2, data.GroupBy (x => x, x => x + x));
  1339. // GroupBy<int,int,int> (Func<int, int>, Func<int, int>, IEqualityComparer<int>)
  1340. AssertAreSame (expected2, data.GroupBy (x => x, x => x + x, EqualityComparer<string>.Default));
  1341. // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>)
  1342. AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
  1343. // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>)
  1344. AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
  1345. // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
  1346. AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1347. // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
  1348. AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1349. }
  1350. [Test]
  1351. public void ConcatArgumentNullTest ()
  1352. {
  1353. string [] data = { "2", "1", "5", "3", "4" };
  1354. // Concat<TSource> (IEnumerable<TSource>)
  1355. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Concat (data); });
  1356. AssertException<ArgumentNullException> (delegate () { data.Concat ((IEnumerable<string>) null); });
  1357. }
  1358. [Test]
  1359. public void ConcatTest ()
  1360. {
  1361. int [] data1 = { 2, 1, 5, 3, 4 };
  1362. int [] data2 = { 1, 2, 3, 4, 5 };
  1363. int [] expected = { 2, 1, 5, 3, 4, 1, 2, 3, 4, 5 };
  1364. // Concat<TSource> (IEnumerable<TSource>)
  1365. AssertAreSame (expected, data1.Concat (data2));
  1366. }
  1367. [Test]
  1368. public void DistinctArgumentNullTest ()
  1369. {
  1370. //string [] data = { "2", "1", "5", "3", "4" };
  1371. // Distinct<TSource> ()
  1372. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (); });
  1373. // Distinct<TSource> (IEqualityComparer<TSource>)
  1374. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (EqualityComparer<string>.Default); });
  1375. }
  1376. [Test]
  1377. public void DistinctTest ()
  1378. {
  1379. int [] data = { 2, 1, 5, 3, 4, 2, 5, 3, 1, 8 };
  1380. int [] expected = { 2, 1, 5, 3, 4, 8 };
  1381. // Distinct<TSource> ()
  1382. AssertAreSame (expected, data.Distinct ());
  1383. // Distinct<iTSourcent> (IEqualityComparer<TSource>)
  1384. AssertAreSame (expected, data.Distinct (EqualityComparer<int>.Default));
  1385. }
  1386. [Test]
  1387. public void UnionArgumentNullTest ()
  1388. {
  1389. string [] data = { "2", "1", "5", "3", "4" };
  1390. // Union<TSource> (IEnumerable<TSource>)
  1391. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data); });
  1392. AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null); });
  1393. // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1394. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data, EqualityComparer<string>.Default); });
  1395. AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1396. }
  1397. [Test]
  1398. public void UnionTest ()
  1399. {
  1400. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1401. int [] data2 = { 1, 2, 3, 8, 4, 5 };
  1402. int [] expected = { 2, 1, 5, 7, 3, 4, 8 };
  1403. // Union<TSource> (IEnumerable<TSource>)
  1404. AssertAreSame (expected, data1.Union (data2));
  1405. // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1406. AssertAreSame (expected, data1.Union (data2, EqualityComparer<int>.Default));
  1407. }
  1408. [Test]
  1409. public void IntersectArgumentNullTest ()
  1410. {
  1411. string [] data = { "2", "1", "5", "3", "4" };
  1412. // Intersect<TSource> (IEnumerable<TSource>)
  1413. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data); });
  1414. AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null); });
  1415. // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1416. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data, EqualityComparer<string>.Default); });
  1417. AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1418. }
  1419. [Test]
  1420. public void IntersectTest ()
  1421. {
  1422. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1423. int [] data2 = { 1, 2, 3, 8, 4, 5 };
  1424. int [] expected = { 2, 1, 5, 3, 4 };
  1425. // Intersect<TSource> (IEnumerable<TSource>)
  1426. AssertAreSame (expected, data1.Intersect (data2));
  1427. // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1428. AssertAreSame (expected, data1.Intersect (data2, EqualityComparer<int>.Default));
  1429. }
  1430. [Test]
  1431. public void ExceptArgumentNullTest ()
  1432. {
  1433. string [] data = { "2", "1", "5", "3", "4" };
  1434. // Except<TSource> (IEnumerable<TSource>)
  1435. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data); });
  1436. AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null); });
  1437. // Except<TSource> (IEnumerable<string>, IEqualityComparer<TSource>)
  1438. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data, EqualityComparer<string>.Default); });
  1439. AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1440. }
  1441. [Test]
  1442. public void ExceptTest ()
  1443. {
  1444. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1445. int [] data2 = { 1, 2, 3, 8, 4, 5 };
  1446. int [] expected = { 7 };
  1447. // Except<TSource> (IEnumerable<TSource>)
  1448. AssertAreSame (expected, data1.Except (data2));
  1449. // Except<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1450. AssertAreSame (expected, data1.Except (data2, EqualityComparer<int>.Default));
  1451. }
  1452. [Test]
  1453. public void ReverseArgumentNullTest ()
  1454. {
  1455. //string [] data = { "2", "1", "5", "3", "4" };
  1456. // Reverse<TSource> ()
  1457. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Reverse (); });
  1458. }
  1459. [Test]
  1460. public void ReverseTest ()
  1461. {
  1462. int [] data = { 2, 1, 5, 7, 3, 4 };
  1463. int [] expected = { 4, 3, 7, 5, 1, 2 };
  1464. // Reverse<TSource> ()
  1465. AssertAreSame (expected, data.Reverse ());
  1466. }
  1467. [Test]
  1468. public void SequenceEqualArgumentNullTest ()
  1469. {
  1470. string [] data = { "2", "1", "5", "3", "4" };
  1471. // SequenceEqual<TSource> (IEnumerable<TSource>)
  1472. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data); });
  1473. AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null); });
  1474. // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1475. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data, EqualityComparer<string>.Default); });
  1476. AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1477. }
  1478. [Test]
  1479. public void SequenceEqualTest ()
  1480. {
  1481. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1482. int [] data2 = { 2, 1, 5, 7, 3, 4 };
  1483. int [] data3 = { 2, 1, 5, 7, 3, 4, 5 };
  1484. int [] data4 = { 2, 1, 5, 7, 3 };
  1485. int [] data5 = { 2, 1, 5, 8, 3, 4 };
  1486. // SequenceEqual<TSource> (IEnumerable<TSource>)
  1487. Assert.IsTrue (data1.SequenceEqual (data2));
  1488. Assert.IsFalse (data1.SequenceEqual (data3));
  1489. Assert.IsFalse (data1.SequenceEqual (data4));
  1490. Assert.IsFalse (data1.SequenceEqual (data5));
  1491. // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1492. Assert.IsTrue (data1.SequenceEqual (data2, EqualityComparer<int>.Default));
  1493. Assert.IsFalse (data1.SequenceEqual (data3, EqualityComparer<int>.Default));
  1494. Assert.IsFalse (data1.SequenceEqual (data4, EqualityComparer<int>.Default));
  1495. Assert.IsFalse (data1.SequenceEqual (data5, EqualityComparer<int>.Default));
  1496. }
  1497. [Test]
  1498. public void AsEnumerableArgumentNullTest ()
  1499. {
  1500. //string [] data = { "2", "1", "5", "3", "4" };
  1501. }
  1502. [Test]
  1503. public void AsEnumerableTest ()
  1504. {
  1505. int [] data = { 2, 1, 5, 7, 3, 4 };
  1506. // AsEnumerable<TSource> ()
  1507. Assert.AreSame (data, data.AsEnumerable ());
  1508. }
  1509. [Test]
  1510. public void ToArrayArgumentNullTest ()
  1511. {
  1512. //string [] data = { "2", "1", "5", "3", "4" };
  1513. // ToArray<TSource> ()
  1514. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToArray (); });
  1515. }
  1516. [Test]
  1517. public void ToArrayTest ()
  1518. {
  1519. int [] data = { 2, 3, 4, 5 };
  1520. int [] expected = { 2, 3, 4, 5 };
  1521. // ToArray<TSource> ()
  1522. AssertAreSame (expected, data.ToArray ());
  1523. }
  1524. [Test]
  1525. public void ToListArgumentNullTest ()
  1526. {
  1527. //string [] data = { "2", "1", "5", "3", "4" };
  1528. // ToList<string> ()
  1529. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToList (); });
  1530. }
  1531. [Test]
  1532. public void ToListTest ()
  1533. {
  1534. int [] data = { 2, 4, 5, 1 };
  1535. int [] expected = { 2, 4, 5, 1 };
  1536. // ToList<int> ()
  1537. AssertAreSame (expected, data.ToList ());
  1538. }
  1539. [Test]
  1540. [Category ("NotWorking")]
  1541. public void ToDictionaryArgumentNullTest ()
  1542. {
  1543. string [] data = { "2", "1", "5", "3", "4" };
  1544. // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
  1545. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test"); });
  1546. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null); });
  1547. // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
  1548. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", EqualityComparer<string>.Default); });
  1549. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, EqualityComparer<string>.Default); });
  1550. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
  1551. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test"); });
  1552. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test"); });
  1553. AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null); });
  1554. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
  1555. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test", EqualityComparer<string>.Default); });
  1556. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test", EqualityComparer<string>.Default); });
  1557. AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null, EqualityComparer<string>.Default); });
  1558. }
  1559. [Test]
  1560. [Category ("NotWorking")]
  1561. public void ToDictionaryTest ()
  1562. {
  1563. string [] data = { "2", "1", "5", "3", "4" };
  1564. Dictionary<string, string> expected = new Dictionary<string, string> ();
  1565. expected.Add ("k2", "2");
  1566. expected.Add ("k1", "1");
  1567. expected.Add ("k5", "5");
  1568. expected.Add ("k3", "3");
  1569. expected.Add ("k4", "4");
  1570. // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
  1571. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x));
  1572. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key"); });
  1573. // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
  1574. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, EqualityComparer<string>.Default));
  1575. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", EqualityComparer<string>.Default); });
  1576. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
  1577. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x));
  1578. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x); });
  1579. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
  1580. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x, EqualityComparer<string>.Default));
  1581. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x, EqualityComparer<string>.Default); });
  1582. }
  1583. [Test]
  1584. public void ToLookupArgumentNullTest ()
  1585. {
  1586. string [] data = { "2", "1", "5", "3", "4" };
  1587. // ToLookup<string,string> (Func<string, string>)
  1588. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test")); });
  1589. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null); });
  1590. // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
  1591. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1592. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1593. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
  1594. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
  1595. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
  1596. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
  1597. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
  1598. 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); });
  1599. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1600. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1601. }
  1602. [Test]
  1603. public void ToLookupTest ()
  1604. {
  1605. string [] data = { "23", "12", "55", "42", "41" };
  1606. Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
  1607. expected.Add ("2", new List<string> () { "23" });
  1608. expected.Add ("1", new List<string> () { "12" });
  1609. expected.Add ("5", new List<string> () { "55" });
  1610. expected.Add ("4", new List<string> () { "42", "41" });
  1611. // ToLookup<string,string> (Func<string, string>)
  1612. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup ((x => x [0].ToString ())));
  1613. // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
  1614. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), EqualityComparer<string>.Default));
  1615. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
  1616. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x));
  1617. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
  1618. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x, EqualityComparer<string>.Default));
  1619. }
  1620. [Test]
  1621. public void DefaultIfEmptyArgumentNullTest ()
  1622. {
  1623. //string [] data = { "2", "1", "5", "3", "4" };
  1624. // DefaultIfEmpty<string> ()
  1625. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> (); });
  1626. // DefaultIfEmpty<string> (string)
  1627. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> ((string) "default"); });
  1628. }
  1629. [Test]
  1630. public void DefaultIfEmptyTest ()
  1631. {
  1632. string [] data = { "2", "1", "5", "3", "4" };
  1633. string [] empty = { };
  1634. string [] default1 = { null };
  1635. string [] default2 = { "default" };
  1636. // DefaultIfEmpty<string> ()
  1637. AssertAreSame (data, data.DefaultIfEmpty ());
  1638. AssertAreSame (default1, empty.DefaultIfEmpty ());
  1639. // DefaultIfEmpty<string> (string)
  1640. AssertAreSame (data, data.DefaultIfEmpty ("default"));
  1641. AssertAreSame (default2, empty.DefaultIfEmpty ("default"));
  1642. }
  1643. [Test]
  1644. public void OfTypeArgumentNullTest ()
  1645. {
  1646. //string [] data = { "2", "1", "5", "3", "4" };
  1647. // OfType<string> ()
  1648. AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).OfType<string> (); });
  1649. }
  1650. [Test]
  1651. public void OfTypeTest ()
  1652. {
  1653. object [] data = { "2", 2, "1", "5", "3", "4" };
  1654. string [] expected = { "2", "1", "5", "3", "4" };
  1655. // OfType<string> ()
  1656. AssertAreSame (expected, data.OfType<string> ());
  1657. }
  1658. [Test]
  1659. public void CastArgumentNullTest ()
  1660. {
  1661. //string [] data = { "2", "1", "5", "3", "4" };
  1662. // Cast<string> ()
  1663. AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).Cast<string> (); });
  1664. }
  1665. [Test]
  1666. public void CastTest ()
  1667. {
  1668. object [] data = { 1, 2, 3 };
  1669. int [] expected = { 1, 2, 3 };
  1670. // Cast<string> ()
  1671. AssertAreSame (expected, data.Cast<int> ());
  1672. AssertException<InvalidCastException> (delegate () { data.Cast<IEnumerable> ().GetEnumerator ().MoveNext (); });
  1673. data.Cast<IEnumerable> ();
  1674. }
  1675. [Test]
  1676. public void RangeArgumentNullTest ()
  1677. {
  1678. //string [] data = { "2", "1", "5", "3", "4" };
  1679. }
  1680. [Test]
  1681. public void RangeTest ()
  1682. {
  1683. int [] expected = { 2, 3, 4, 5 };
  1684. // Range<> (int)
  1685. AssertAreSame (expected, Enumerable.Range (2, 4));
  1686. AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (2, -3); });
  1687. AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (int.MaxValue - 5, 7); });
  1688. Enumerable.Range (int.MaxValue - 5, 6);
  1689. }
  1690. }
  1691. }