EnumerableMoreTest.cs 99 KB

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