EnumerableMoreTest.cs 98 KB

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