OrderBy.Generated.cs 274 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. partial class OrderBy
  12. {
  13. [Fact]
  14. public async Task OrderBy_OrderBy()
  15. {
  16. var rand = new Random(42);
  17. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  18. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2);
  19. var syncRes = xs.OrderBy(x => x % 2);
  20. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  21. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  22. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  23. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  24. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  25. for (var i = 0; i < 100; i++)
  26. {
  27. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  28. }
  29. }
  30. [Fact]
  31. public async Task OrderBy_OrderByAsync()
  32. {
  33. var rand = new Random(42);
  34. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  35. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2));
  36. var syncRes = xs.OrderBy(x => x % 2);
  37. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  38. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  39. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  40. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  41. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  42. for (var i = 0; i < 100; i++)
  43. {
  44. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  45. }
  46. }
  47. [Fact]
  48. public async Task OrderBy_OrderByAsyncWithCancellation()
  49. {
  50. var rand = new Random(42);
  51. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  52. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2));
  53. var syncRes = xs.OrderBy(x => x % 2);
  54. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  55. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  56. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  57. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  58. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  59. for (var i = 0; i < 100; i++)
  60. {
  61. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  62. }
  63. }
  64. [Fact]
  65. public async Task OrderBy_OrderByDescending()
  66. {
  67. var rand = new Random(42);
  68. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  69. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2);
  70. var syncRes = xs.OrderByDescending(x => x % 2);
  71. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  72. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  73. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  74. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  75. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  76. for (var i = 0; i < 100; i++)
  77. {
  78. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  79. }
  80. }
  81. [Fact]
  82. public async Task OrderBy_OrderByDescendingAsync()
  83. {
  84. var rand = new Random(42);
  85. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  86. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2));
  87. var syncRes = xs.OrderByDescending(x => x % 2);
  88. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  89. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  90. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  91. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  92. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  93. for (var i = 0; i < 100; i++)
  94. {
  95. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  96. }
  97. }
  98. [Fact]
  99. public async Task OrderBy_OrderByDescendingAsyncWithCancellation()
  100. {
  101. var rand = new Random(42);
  102. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  103. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2));
  104. var syncRes = xs.OrderByDescending(x => x % 2);
  105. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  106. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  107. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  108. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  109. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  110. for (var i = 0; i < 100; i++)
  111. {
  112. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  113. }
  114. }
  115. [Fact]
  116. public async Task OrderBy_OrderBy_ThenBy()
  117. {
  118. var rand = new Random(42);
  119. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  120. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3);
  121. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  122. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  123. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  124. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  125. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  126. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  127. for (var i = 0; i < 100; i++)
  128. {
  129. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  130. }
  131. }
  132. [Fact]
  133. public async Task OrderBy_OrderBy_ThenByAsync()
  134. {
  135. var rand = new Random(42);
  136. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  137. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3));
  138. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  139. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  140. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  141. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  142. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  143. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  144. for (var i = 0; i < 100; i++)
  145. {
  146. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  147. }
  148. }
  149. [Fact]
  150. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation()
  151. {
  152. var rand = new Random(42);
  153. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  154. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3));
  155. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  156. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  157. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  158. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  159. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  160. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  161. for (var i = 0; i < 100; i++)
  162. {
  163. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  164. }
  165. }
  166. [Fact]
  167. public async Task OrderBy_OrderBy_ThenByDescending()
  168. {
  169. var rand = new Random(42);
  170. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  171. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  172. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  173. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  174. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  175. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  176. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  177. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  178. for (var i = 0; i < 100; i++)
  179. {
  180. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  181. }
  182. }
  183. [Fact]
  184. public async Task OrderBy_OrderBy_ThenByDescendingAsync()
  185. {
  186. var rand = new Random(42);
  187. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  188. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3));
  189. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  190. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  191. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  192. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  193. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  194. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  195. for (var i = 0; i < 100; i++)
  196. {
  197. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  198. }
  199. }
  200. [Fact]
  201. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation()
  202. {
  203. var rand = new Random(42);
  204. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  205. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3));
  206. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  207. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  208. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  209. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  210. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  211. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  212. for (var i = 0; i < 100; i++)
  213. {
  214. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  215. }
  216. }
  217. [Fact]
  218. public async Task OrderBy_OrderByAsync_ThenBy()
  219. {
  220. var rand = new Random(42);
  221. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  222. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3);
  223. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  224. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  225. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  226. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  227. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  228. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  229. for (var i = 0; i < 100; i++)
  230. {
  231. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  232. }
  233. }
  234. [Fact]
  235. public async Task OrderBy_OrderByAsync_ThenByAsync()
  236. {
  237. var rand = new Random(42);
  238. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  239. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3));
  240. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  241. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  242. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  243. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  244. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  245. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  246. for (var i = 0; i < 100; i++)
  247. {
  248. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  249. }
  250. }
  251. [Fact]
  252. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation()
  253. {
  254. var rand = new Random(42);
  255. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  256. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3));
  257. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  258. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  259. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  260. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  261. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  262. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  263. for (var i = 0; i < 100; i++)
  264. {
  265. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  266. }
  267. }
  268. [Fact]
  269. public async Task OrderBy_OrderByAsync_ThenByDescending()
  270. {
  271. var rand = new Random(42);
  272. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  273. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3);
  274. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  275. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  276. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  277. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  278. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  279. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  280. for (var i = 0; i < 100; i++)
  281. {
  282. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  283. }
  284. }
  285. [Fact]
  286. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync()
  287. {
  288. var rand = new Random(42);
  289. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  290. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3));
  291. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  292. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  293. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  294. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  295. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  296. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  297. for (var i = 0; i < 100; i++)
  298. {
  299. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  300. }
  301. }
  302. [Fact]
  303. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation()
  304. {
  305. var rand = new Random(42);
  306. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  307. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3));
  308. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  309. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  310. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  311. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  312. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  313. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  314. for (var i = 0; i < 100; i++)
  315. {
  316. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  317. }
  318. }
  319. [Fact]
  320. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy()
  321. {
  322. var rand = new Random(42);
  323. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  324. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3);
  325. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  326. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  327. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  328. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  329. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  330. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  331. for (var i = 0; i < 100; i++)
  332. {
  333. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  334. }
  335. }
  336. [Fact]
  337. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync()
  338. {
  339. var rand = new Random(42);
  340. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  341. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3));
  342. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  343. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  344. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  345. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  346. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  347. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  348. for (var i = 0; i < 100; i++)
  349. {
  350. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  351. }
  352. }
  353. [Fact]
  354. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation()
  355. {
  356. var rand = new Random(42);
  357. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  358. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3));
  359. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  360. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  361. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  362. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  363. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  364. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  365. for (var i = 0; i < 100; i++)
  366. {
  367. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  368. }
  369. }
  370. [Fact]
  371. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending()
  372. {
  373. var rand = new Random(42);
  374. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  375. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3);
  376. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  377. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  378. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  379. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  380. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  381. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  382. for (var i = 0; i < 100; i++)
  383. {
  384. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  385. }
  386. }
  387. [Fact]
  388. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync()
  389. {
  390. var rand = new Random(42);
  391. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  392. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3));
  393. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  394. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  395. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  396. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  397. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  398. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  399. for (var i = 0; i < 100; i++)
  400. {
  401. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  402. }
  403. }
  404. [Fact]
  405. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  406. {
  407. var rand = new Random(42);
  408. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  409. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3));
  410. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  411. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  412. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  413. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  414. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  415. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  416. for (var i = 0; i < 100; i++)
  417. {
  418. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  419. }
  420. }
  421. [Fact]
  422. public async Task OrderBy_OrderByDescending_ThenBy()
  423. {
  424. var rand = new Random(42);
  425. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  426. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  427. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  428. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  429. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  430. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  431. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  432. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  433. for (var i = 0; i < 100; i++)
  434. {
  435. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  436. }
  437. }
  438. [Fact]
  439. public async Task OrderBy_OrderByDescending_ThenByAsync()
  440. {
  441. var rand = new Random(42);
  442. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  443. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3));
  444. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  445. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  446. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  447. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  448. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  449. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  450. for (var i = 0; i < 100; i++)
  451. {
  452. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  453. }
  454. }
  455. [Fact]
  456. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation()
  457. {
  458. var rand = new Random(42);
  459. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  460. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3));
  461. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  462. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  463. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  464. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  465. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  466. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  467. for (var i = 0; i < 100; i++)
  468. {
  469. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  470. }
  471. }
  472. [Fact]
  473. public async Task OrderBy_OrderByDescending_ThenByDescending()
  474. {
  475. var rand = new Random(42);
  476. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  477. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  478. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  479. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  480. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  481. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  482. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  483. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  484. for (var i = 0; i < 100; i++)
  485. {
  486. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  487. }
  488. }
  489. [Fact]
  490. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync()
  491. {
  492. var rand = new Random(42);
  493. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  494. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3));
  495. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  496. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  497. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  498. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  499. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  500. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  501. for (var i = 0; i < 100; i++)
  502. {
  503. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  504. }
  505. }
  506. [Fact]
  507. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation()
  508. {
  509. var rand = new Random(42);
  510. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  511. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3));
  512. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  513. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  514. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  515. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  516. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  517. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  518. for (var i = 0; i < 100; i++)
  519. {
  520. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  521. }
  522. }
  523. [Fact]
  524. public async Task OrderBy_OrderByDescendingAsync_ThenBy()
  525. {
  526. var rand = new Random(42);
  527. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  528. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3);
  529. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  530. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  531. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  532. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  533. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  534. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  535. for (var i = 0; i < 100; i++)
  536. {
  537. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  538. }
  539. }
  540. [Fact]
  541. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync()
  542. {
  543. var rand = new Random(42);
  544. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  545. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3));
  546. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  547. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  548. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  549. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  550. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  551. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  552. for (var i = 0; i < 100; i++)
  553. {
  554. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  555. }
  556. }
  557. [Fact]
  558. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation()
  559. {
  560. var rand = new Random(42);
  561. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  562. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3));
  563. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  564. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  565. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  566. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  567. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  568. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  569. for (var i = 0; i < 100; i++)
  570. {
  571. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  572. }
  573. }
  574. [Fact]
  575. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending()
  576. {
  577. var rand = new Random(42);
  578. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  579. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3);
  580. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  581. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  582. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  583. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  584. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  585. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  586. for (var i = 0; i < 100; i++)
  587. {
  588. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  589. }
  590. }
  591. [Fact]
  592. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync()
  593. {
  594. var rand = new Random(42);
  595. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  596. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3));
  597. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  598. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  599. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  600. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  601. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  602. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  603. for (var i = 0; i < 100; i++)
  604. {
  605. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  606. }
  607. }
  608. [Fact]
  609. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  610. {
  611. var rand = new Random(42);
  612. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  613. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3));
  614. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  615. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  616. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  617. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  618. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  619. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  620. for (var i = 0; i < 100; i++)
  621. {
  622. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  623. }
  624. }
  625. [Fact]
  626. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy()
  627. {
  628. var rand = new Random(42);
  629. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  630. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3);
  631. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  632. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  633. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  634. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  635. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  636. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  637. for (var i = 0; i < 100; i++)
  638. {
  639. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  640. }
  641. }
  642. [Fact]
  643. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync()
  644. {
  645. var rand = new Random(42);
  646. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  647. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3));
  648. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  649. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  650. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  651. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  652. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  653. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  654. for (var i = 0; i < 100; i++)
  655. {
  656. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  657. }
  658. }
  659. [Fact]
  660. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  661. {
  662. var rand = new Random(42);
  663. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  664. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3));
  665. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  666. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  667. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  668. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  669. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  670. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  671. for (var i = 0; i < 100; i++)
  672. {
  673. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  674. }
  675. }
  676. [Fact]
  677. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending()
  678. {
  679. var rand = new Random(42);
  680. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  681. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3);
  682. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  683. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  684. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  685. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  686. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  687. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  688. for (var i = 0; i < 100; i++)
  689. {
  690. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  691. }
  692. }
  693. [Fact]
  694. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  695. {
  696. var rand = new Random(42);
  697. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  698. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3));
  699. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  700. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  701. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  702. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  703. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  704. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  705. for (var i = 0; i < 100; i++)
  706. {
  707. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  708. }
  709. }
  710. [Fact]
  711. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  712. {
  713. var rand = new Random(42);
  714. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  715. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3));
  716. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  717. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  718. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  719. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  720. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  721. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  722. for (var i = 0; i < 100; i++)
  723. {
  724. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  725. }
  726. }
  727. [Fact]
  728. public async Task OrderBy_OrderBy_ThenBy_ThenBy()
  729. {
  730. var rand = new Random(42);
  731. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  732. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  733. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  734. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  735. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  736. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  737. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  738. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  739. for (var i = 0; i < 100; i++)
  740. {
  741. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  742. }
  743. }
  744. [Fact]
  745. public async Task OrderBy_OrderBy_ThenBy_ThenByAsync()
  746. {
  747. var rand = new Random(42);
  748. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  749. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  750. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  751. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  752. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  753. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  754. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  755. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  756. for (var i = 0; i < 100; i++)
  757. {
  758. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  759. }
  760. }
  761. [Fact]
  762. public async Task OrderBy_OrderBy_ThenBy_ThenByAsyncWithCancellation()
  763. {
  764. var rand = new Random(42);
  765. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  766. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  767. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  768. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  769. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  770. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  771. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  772. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  773. for (var i = 0; i < 100; i++)
  774. {
  775. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  776. }
  777. }
  778. [Fact]
  779. public async Task OrderBy_OrderBy_ThenBy_ThenByDescending()
  780. {
  781. var rand = new Random(42);
  782. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  783. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  784. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  785. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  786. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  787. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  788. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  789. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  790. for (var i = 0; i < 100; i++)
  791. {
  792. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  793. }
  794. }
  795. [Fact]
  796. public async Task OrderBy_OrderBy_ThenBy_ThenByDescendingAsync()
  797. {
  798. var rand = new Random(42);
  799. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  800. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  801. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  802. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  803. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  804. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  805. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  806. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  807. for (var i = 0; i < 100; i++)
  808. {
  809. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  810. }
  811. }
  812. [Fact]
  813. public async Task OrderBy_OrderBy_ThenBy_ThenByDescendingAsyncWithCancellation()
  814. {
  815. var rand = new Random(42);
  816. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  817. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  818. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  819. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  820. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  821. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  822. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  823. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  824. for (var i = 0; i < 100; i++)
  825. {
  826. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  827. }
  828. }
  829. [Fact]
  830. public async Task OrderBy_OrderBy_ThenByAsync_ThenBy()
  831. {
  832. var rand = new Random(42);
  833. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  834. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  835. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  836. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  837. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  838. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  839. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  840. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  841. for (var i = 0; i < 100; i++)
  842. {
  843. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  844. }
  845. }
  846. [Fact]
  847. public async Task OrderBy_OrderBy_ThenByAsync_ThenByAsync()
  848. {
  849. var rand = new Random(42);
  850. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  851. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  852. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  853. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  854. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  855. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  856. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  857. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  858. for (var i = 0; i < 100; i++)
  859. {
  860. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  861. }
  862. }
  863. [Fact]
  864. public async Task OrderBy_OrderBy_ThenByAsync_ThenByAsyncWithCancellation()
  865. {
  866. var rand = new Random(42);
  867. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  868. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  869. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  870. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  871. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  872. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  873. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  874. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  875. for (var i = 0; i < 100; i++)
  876. {
  877. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  878. }
  879. }
  880. [Fact]
  881. public async Task OrderBy_OrderBy_ThenByAsync_ThenByDescending()
  882. {
  883. var rand = new Random(42);
  884. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  885. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  886. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  887. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  888. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  889. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  890. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  891. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  892. for (var i = 0; i < 100; i++)
  893. {
  894. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  895. }
  896. }
  897. [Fact]
  898. public async Task OrderBy_OrderBy_ThenByAsync_ThenByDescendingAsync()
  899. {
  900. var rand = new Random(42);
  901. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  902. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  903. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  904. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  905. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  906. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  907. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  908. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  909. for (var i = 0; i < 100; i++)
  910. {
  911. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  912. }
  913. }
  914. [Fact]
  915. public async Task OrderBy_OrderBy_ThenByAsync_ThenByDescendingAsyncWithCancellation()
  916. {
  917. var rand = new Random(42);
  918. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  919. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  920. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  921. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  922. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  923. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  924. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  925. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  926. for (var i = 0; i < 100; i++)
  927. {
  928. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  929. }
  930. }
  931. [Fact]
  932. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation_ThenBy()
  933. {
  934. var rand = new Random(42);
  935. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  936. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  937. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  938. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  939. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  940. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  941. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  942. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  943. for (var i = 0; i < 100; i++)
  944. {
  945. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  946. }
  947. }
  948. [Fact]
  949. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation_ThenByAsync()
  950. {
  951. var rand = new Random(42);
  952. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  953. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  954. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  955. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  956. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  957. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  958. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  959. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  960. for (var i = 0; i < 100; i++)
  961. {
  962. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  963. }
  964. }
  965. [Fact]
  966. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation_ThenByAsyncWithCancellation()
  967. {
  968. var rand = new Random(42);
  969. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  970. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  971. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  972. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  973. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  974. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  975. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  976. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  977. for (var i = 0; i < 100; i++)
  978. {
  979. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  980. }
  981. }
  982. [Fact]
  983. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation_ThenByDescending()
  984. {
  985. var rand = new Random(42);
  986. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  987. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  988. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  989. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  990. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  991. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  992. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  993. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  994. for (var i = 0; i < 100; i++)
  995. {
  996. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  997. }
  998. }
  999. [Fact]
  1000. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation_ThenByDescendingAsync()
  1001. {
  1002. var rand = new Random(42);
  1003. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1004. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  1005. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1006. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1007. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1008. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1009. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1010. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1011. for (var i = 0; i < 100; i++)
  1012. {
  1013. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1014. }
  1015. }
  1016. [Fact]
  1017. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  1018. {
  1019. var rand = new Random(42);
  1020. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1021. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1022. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1023. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1024. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1025. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1026. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1027. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1028. for (var i = 0; i < 100; i++)
  1029. {
  1030. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1031. }
  1032. }
  1033. [Fact]
  1034. public async Task OrderBy_OrderBy_ThenByDescending_ThenBy()
  1035. {
  1036. var rand = new Random(42);
  1037. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1038. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1039. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1040. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1041. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1042. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1043. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1044. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1045. for (var i = 0; i < 100; i++)
  1046. {
  1047. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1048. }
  1049. }
  1050. [Fact]
  1051. public async Task OrderBy_OrderBy_ThenByDescending_ThenByAsync()
  1052. {
  1053. var rand = new Random(42);
  1054. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1055. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  1056. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1057. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1058. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1059. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1060. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1061. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1062. for (var i = 0; i < 100; i++)
  1063. {
  1064. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1065. }
  1066. }
  1067. [Fact]
  1068. public async Task OrderBy_OrderBy_ThenByDescending_ThenByAsyncWithCancellation()
  1069. {
  1070. var rand = new Random(42);
  1071. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1072. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1073. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1074. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1075. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1076. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1077. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1078. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1079. for (var i = 0; i < 100; i++)
  1080. {
  1081. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1082. }
  1083. }
  1084. [Fact]
  1085. public async Task OrderBy_OrderBy_ThenByDescending_ThenByDescending()
  1086. {
  1087. var rand = new Random(42);
  1088. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1089. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1090. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1091. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1092. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1093. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1094. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1095. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1096. for (var i = 0; i < 100; i++)
  1097. {
  1098. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1099. }
  1100. }
  1101. [Fact]
  1102. public async Task OrderBy_OrderBy_ThenByDescending_ThenByDescendingAsync()
  1103. {
  1104. var rand = new Random(42);
  1105. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1106. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  1107. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1108. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1109. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1110. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1111. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1112. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1113. for (var i = 0; i < 100; i++)
  1114. {
  1115. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1116. }
  1117. }
  1118. [Fact]
  1119. public async Task OrderBy_OrderBy_ThenByDescending_ThenByDescendingAsyncWithCancellation()
  1120. {
  1121. var rand = new Random(42);
  1122. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1123. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1124. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1125. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1126. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1127. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1128. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1129. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1130. for (var i = 0; i < 100; i++)
  1131. {
  1132. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1133. }
  1134. }
  1135. [Fact]
  1136. public async Task OrderBy_OrderBy_ThenByDescendingAsync_ThenBy()
  1137. {
  1138. var rand = new Random(42);
  1139. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1140. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  1141. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1142. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1143. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1144. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1145. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1146. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1147. for (var i = 0; i < 100; i++)
  1148. {
  1149. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1150. }
  1151. }
  1152. [Fact]
  1153. public async Task OrderBy_OrderBy_ThenByDescendingAsync_ThenByAsync()
  1154. {
  1155. var rand = new Random(42);
  1156. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1157. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  1158. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1159. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1160. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1161. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1162. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1163. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1164. for (var i = 0; i < 100; i++)
  1165. {
  1166. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1167. }
  1168. }
  1169. [Fact]
  1170. public async Task OrderBy_OrderBy_ThenByDescendingAsync_ThenByAsyncWithCancellation()
  1171. {
  1172. var rand = new Random(42);
  1173. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1174. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1175. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1176. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1177. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1178. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1179. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1180. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1181. for (var i = 0; i < 100; i++)
  1182. {
  1183. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1184. }
  1185. }
  1186. [Fact]
  1187. public async Task OrderBy_OrderBy_ThenByDescendingAsync_ThenByDescending()
  1188. {
  1189. var rand = new Random(42);
  1190. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1191. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  1192. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1193. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1194. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1195. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1196. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1197. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1198. for (var i = 0; i < 100; i++)
  1199. {
  1200. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1201. }
  1202. }
  1203. [Fact]
  1204. public async Task OrderBy_OrderBy_ThenByDescendingAsync_ThenByDescendingAsync()
  1205. {
  1206. var rand = new Random(42);
  1207. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1208. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  1209. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1210. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1211. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1212. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1213. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1214. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1215. for (var i = 0; i < 100; i++)
  1216. {
  1217. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1218. }
  1219. }
  1220. [Fact]
  1221. public async Task OrderBy_OrderBy_ThenByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  1222. {
  1223. var rand = new Random(42);
  1224. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1225. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1226. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1227. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1228. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1229. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1230. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1231. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1232. for (var i = 0; i < 100; i++)
  1233. {
  1234. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1235. }
  1236. }
  1237. [Fact]
  1238. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation_ThenBy()
  1239. {
  1240. var rand = new Random(42);
  1241. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1242. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  1243. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1244. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1245. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1246. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1247. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1248. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1249. for (var i = 0; i < 100; i++)
  1250. {
  1251. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1252. }
  1253. }
  1254. [Fact]
  1255. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation_ThenByAsync()
  1256. {
  1257. var rand = new Random(42);
  1258. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1259. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  1260. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1261. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1262. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1263. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1264. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1265. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1266. for (var i = 0; i < 100; i++)
  1267. {
  1268. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1269. }
  1270. }
  1271. [Fact]
  1272. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  1273. {
  1274. var rand = new Random(42);
  1275. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1276. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1277. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1278. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1279. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1280. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1281. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1282. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1283. for (var i = 0; i < 100; i++)
  1284. {
  1285. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1286. }
  1287. }
  1288. [Fact]
  1289. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation_ThenByDescending()
  1290. {
  1291. var rand = new Random(42);
  1292. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1293. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  1294. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1295. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1296. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1297. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1298. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1299. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1300. for (var i = 0; i < 100; i++)
  1301. {
  1302. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1303. }
  1304. }
  1305. [Fact]
  1306. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  1307. {
  1308. var rand = new Random(42);
  1309. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1310. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  1311. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1312. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1313. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1314. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1315. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1316. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1317. for (var i = 0; i < 100; i++)
  1318. {
  1319. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1320. }
  1321. }
  1322. [Fact]
  1323. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  1324. {
  1325. var rand = new Random(42);
  1326. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1327. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1328. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1329. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1330. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1331. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1332. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1333. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1334. for (var i = 0; i < 100; i++)
  1335. {
  1336. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1337. }
  1338. }
  1339. [Fact]
  1340. public async Task OrderBy_OrderByAsync_ThenBy_ThenBy()
  1341. {
  1342. var rand = new Random(42);
  1343. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1344. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1345. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1346. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1347. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1348. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1349. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1350. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1351. for (var i = 0; i < 100; i++)
  1352. {
  1353. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1354. }
  1355. }
  1356. [Fact]
  1357. public async Task OrderBy_OrderByAsync_ThenBy_ThenByAsync()
  1358. {
  1359. var rand = new Random(42);
  1360. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1361. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  1362. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1363. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1364. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1365. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1366. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1367. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1368. for (var i = 0; i < 100; i++)
  1369. {
  1370. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1371. }
  1372. }
  1373. [Fact]
  1374. public async Task OrderBy_OrderByAsync_ThenBy_ThenByAsyncWithCancellation()
  1375. {
  1376. var rand = new Random(42);
  1377. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1378. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1379. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1380. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1381. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1382. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1383. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1384. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1385. for (var i = 0; i < 100; i++)
  1386. {
  1387. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1388. }
  1389. }
  1390. [Fact]
  1391. public async Task OrderBy_OrderByAsync_ThenBy_ThenByDescending()
  1392. {
  1393. var rand = new Random(42);
  1394. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1395. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1396. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1397. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1398. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1399. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1400. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1401. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1402. for (var i = 0; i < 100; i++)
  1403. {
  1404. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1405. }
  1406. }
  1407. [Fact]
  1408. public async Task OrderBy_OrderByAsync_ThenBy_ThenByDescendingAsync()
  1409. {
  1410. var rand = new Random(42);
  1411. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1412. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  1413. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1414. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1415. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1416. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1417. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1418. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1419. for (var i = 0; i < 100; i++)
  1420. {
  1421. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1422. }
  1423. }
  1424. [Fact]
  1425. public async Task OrderBy_OrderByAsync_ThenBy_ThenByDescendingAsyncWithCancellation()
  1426. {
  1427. var rand = new Random(42);
  1428. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1429. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1430. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1431. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1432. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1433. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1434. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1435. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1436. for (var i = 0; i < 100; i++)
  1437. {
  1438. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1439. }
  1440. }
  1441. [Fact]
  1442. public async Task OrderBy_OrderByAsync_ThenByAsync_ThenBy()
  1443. {
  1444. var rand = new Random(42);
  1445. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1446. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  1447. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1448. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1449. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1450. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1451. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1452. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1453. for (var i = 0; i < 100; i++)
  1454. {
  1455. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1456. }
  1457. }
  1458. [Fact]
  1459. public async Task OrderBy_OrderByAsync_ThenByAsync_ThenByAsync()
  1460. {
  1461. var rand = new Random(42);
  1462. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1463. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  1464. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1465. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1466. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1467. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1468. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1469. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1470. for (var i = 0; i < 100; i++)
  1471. {
  1472. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1473. }
  1474. }
  1475. [Fact]
  1476. public async Task OrderBy_OrderByAsync_ThenByAsync_ThenByAsyncWithCancellation()
  1477. {
  1478. var rand = new Random(42);
  1479. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1480. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1481. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1482. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1483. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1484. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1485. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1486. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1487. for (var i = 0; i < 100; i++)
  1488. {
  1489. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1490. }
  1491. }
  1492. [Fact]
  1493. public async Task OrderBy_OrderByAsync_ThenByAsync_ThenByDescending()
  1494. {
  1495. var rand = new Random(42);
  1496. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1497. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  1498. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1499. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1500. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1501. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1502. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1503. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1504. for (var i = 0; i < 100; i++)
  1505. {
  1506. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1507. }
  1508. }
  1509. [Fact]
  1510. public async Task OrderBy_OrderByAsync_ThenByAsync_ThenByDescendingAsync()
  1511. {
  1512. var rand = new Random(42);
  1513. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1514. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  1515. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1516. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1517. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1518. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1519. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1520. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1521. for (var i = 0; i < 100; i++)
  1522. {
  1523. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1524. }
  1525. }
  1526. [Fact]
  1527. public async Task OrderBy_OrderByAsync_ThenByAsync_ThenByDescendingAsyncWithCancellation()
  1528. {
  1529. var rand = new Random(42);
  1530. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1531. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1532. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1533. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1534. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1535. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1536. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1537. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1538. for (var i = 0; i < 100; i++)
  1539. {
  1540. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1541. }
  1542. }
  1543. [Fact]
  1544. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation_ThenBy()
  1545. {
  1546. var rand = new Random(42);
  1547. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1548. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  1549. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1550. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1551. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1552. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1553. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1554. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1555. for (var i = 0; i < 100; i++)
  1556. {
  1557. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1558. }
  1559. }
  1560. [Fact]
  1561. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation_ThenByAsync()
  1562. {
  1563. var rand = new Random(42);
  1564. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1565. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  1566. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1567. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1568. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1569. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1570. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1571. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1572. for (var i = 0; i < 100; i++)
  1573. {
  1574. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1575. }
  1576. }
  1577. [Fact]
  1578. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation_ThenByAsyncWithCancellation()
  1579. {
  1580. var rand = new Random(42);
  1581. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1582. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1583. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1584. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1585. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1586. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1587. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1588. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1589. for (var i = 0; i < 100; i++)
  1590. {
  1591. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1592. }
  1593. }
  1594. [Fact]
  1595. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation_ThenByDescending()
  1596. {
  1597. var rand = new Random(42);
  1598. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1599. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  1600. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1601. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1602. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1603. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1604. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1605. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1606. for (var i = 0; i < 100; i++)
  1607. {
  1608. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1609. }
  1610. }
  1611. [Fact]
  1612. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation_ThenByDescendingAsync()
  1613. {
  1614. var rand = new Random(42);
  1615. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1616. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  1617. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1618. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1619. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1620. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1621. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1622. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1623. for (var i = 0; i < 100; i++)
  1624. {
  1625. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1626. }
  1627. }
  1628. [Fact]
  1629. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  1630. {
  1631. var rand = new Random(42);
  1632. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1633. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1634. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1635. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1636. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1637. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1638. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1639. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1640. for (var i = 0; i < 100; i++)
  1641. {
  1642. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1643. }
  1644. }
  1645. [Fact]
  1646. public async Task OrderBy_OrderByAsync_ThenByDescending_ThenBy()
  1647. {
  1648. var rand = new Random(42);
  1649. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1650. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1651. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1652. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1653. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1654. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1655. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1656. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1657. for (var i = 0; i < 100; i++)
  1658. {
  1659. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1660. }
  1661. }
  1662. [Fact]
  1663. public async Task OrderBy_OrderByAsync_ThenByDescending_ThenByAsync()
  1664. {
  1665. var rand = new Random(42);
  1666. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1667. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  1668. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1669. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1670. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1671. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1672. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1673. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1674. for (var i = 0; i < 100; i++)
  1675. {
  1676. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1677. }
  1678. }
  1679. [Fact]
  1680. public async Task OrderBy_OrderByAsync_ThenByDescending_ThenByAsyncWithCancellation()
  1681. {
  1682. var rand = new Random(42);
  1683. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1684. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1685. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1686. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1687. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1688. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1689. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1690. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1691. for (var i = 0; i < 100; i++)
  1692. {
  1693. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1694. }
  1695. }
  1696. [Fact]
  1697. public async Task OrderBy_OrderByAsync_ThenByDescending_ThenByDescending()
  1698. {
  1699. var rand = new Random(42);
  1700. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1701. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1702. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1703. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1704. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1705. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1706. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1707. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1708. for (var i = 0; i < 100; i++)
  1709. {
  1710. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1711. }
  1712. }
  1713. [Fact]
  1714. public async Task OrderBy_OrderByAsync_ThenByDescending_ThenByDescendingAsync()
  1715. {
  1716. var rand = new Random(42);
  1717. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1718. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  1719. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1720. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1721. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1722. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1723. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1724. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1725. for (var i = 0; i < 100; i++)
  1726. {
  1727. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1728. }
  1729. }
  1730. [Fact]
  1731. public async Task OrderBy_OrderByAsync_ThenByDescending_ThenByDescendingAsyncWithCancellation()
  1732. {
  1733. var rand = new Random(42);
  1734. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1735. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1736. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1737. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1738. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1739. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1740. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1741. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1742. for (var i = 0; i < 100; i++)
  1743. {
  1744. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1745. }
  1746. }
  1747. [Fact]
  1748. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync_ThenBy()
  1749. {
  1750. var rand = new Random(42);
  1751. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1752. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  1753. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1754. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1755. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1756. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1757. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1758. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1759. for (var i = 0; i < 100; i++)
  1760. {
  1761. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1762. }
  1763. }
  1764. [Fact]
  1765. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync_ThenByAsync()
  1766. {
  1767. var rand = new Random(42);
  1768. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1769. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  1770. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1771. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1772. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1773. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1774. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1775. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1776. for (var i = 0; i < 100; i++)
  1777. {
  1778. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1779. }
  1780. }
  1781. [Fact]
  1782. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync_ThenByAsyncWithCancellation()
  1783. {
  1784. var rand = new Random(42);
  1785. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1786. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1787. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1788. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1789. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1790. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1791. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1792. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1793. for (var i = 0; i < 100; i++)
  1794. {
  1795. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1796. }
  1797. }
  1798. [Fact]
  1799. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync_ThenByDescending()
  1800. {
  1801. var rand = new Random(42);
  1802. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1803. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  1804. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1805. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1806. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1807. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1808. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1809. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1810. for (var i = 0; i < 100; i++)
  1811. {
  1812. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1813. }
  1814. }
  1815. [Fact]
  1816. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync_ThenByDescendingAsync()
  1817. {
  1818. var rand = new Random(42);
  1819. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1820. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  1821. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1822. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1823. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1824. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1825. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1826. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1827. for (var i = 0; i < 100; i++)
  1828. {
  1829. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1830. }
  1831. }
  1832. [Fact]
  1833. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  1834. {
  1835. var rand = new Random(42);
  1836. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1837. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1838. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1839. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1840. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1841. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1842. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1843. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1844. for (var i = 0; i < 100; i++)
  1845. {
  1846. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1847. }
  1848. }
  1849. [Fact]
  1850. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation_ThenBy()
  1851. {
  1852. var rand = new Random(42);
  1853. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1854. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  1855. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1856. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1857. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1858. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1859. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1860. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1861. for (var i = 0; i < 100; i++)
  1862. {
  1863. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1864. }
  1865. }
  1866. [Fact]
  1867. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation_ThenByAsync()
  1868. {
  1869. var rand = new Random(42);
  1870. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1871. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  1872. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1873. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1874. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1875. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1876. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1877. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1878. for (var i = 0; i < 100; i++)
  1879. {
  1880. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1881. }
  1882. }
  1883. [Fact]
  1884. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  1885. {
  1886. var rand = new Random(42);
  1887. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1888. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1889. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1890. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1891. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1892. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1893. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1894. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1895. for (var i = 0; i < 100; i++)
  1896. {
  1897. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1898. }
  1899. }
  1900. [Fact]
  1901. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation_ThenByDescending()
  1902. {
  1903. var rand = new Random(42);
  1904. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1905. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  1906. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1907. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1908. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1909. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1910. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1911. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1912. for (var i = 0; i < 100; i++)
  1913. {
  1914. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1915. }
  1916. }
  1917. [Fact]
  1918. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  1919. {
  1920. var rand = new Random(42);
  1921. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1922. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  1923. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1924. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1925. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1926. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1927. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1928. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1929. for (var i = 0; i < 100; i++)
  1930. {
  1931. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1932. }
  1933. }
  1934. [Fact]
  1935. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  1936. {
  1937. var rand = new Random(42);
  1938. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1939. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1940. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1941. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1942. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1943. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1944. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1945. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1946. for (var i = 0; i < 100; i++)
  1947. {
  1948. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1949. }
  1950. }
  1951. [Fact]
  1952. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy_ThenBy()
  1953. {
  1954. var rand = new Random(42);
  1955. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1956. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1957. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1958. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1959. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1960. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1961. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1962. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1963. for (var i = 0; i < 100; i++)
  1964. {
  1965. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1966. }
  1967. }
  1968. [Fact]
  1969. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy_ThenByAsync()
  1970. {
  1971. var rand = new Random(42);
  1972. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1973. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  1974. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1975. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1976. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1977. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1978. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1979. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1980. for (var i = 0; i < 100; i++)
  1981. {
  1982. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  1983. }
  1984. }
  1985. [Fact]
  1986. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy_ThenByAsyncWithCancellation()
  1987. {
  1988. var rand = new Random(42);
  1989. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1990. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1991. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1992. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  1993. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  1994. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  1995. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  1996. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  1997. for (var i = 0; i < 100; i++)
  1998. {
  1999. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2000. }
  2001. }
  2002. [Fact]
  2003. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy_ThenByDescending()
  2004. {
  2005. var rand = new Random(42);
  2006. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2007. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2008. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2009. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2010. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2011. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2012. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2013. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2014. for (var i = 0; i < 100; i++)
  2015. {
  2016. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2017. }
  2018. }
  2019. [Fact]
  2020. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy_ThenByDescendingAsync()
  2021. {
  2022. var rand = new Random(42);
  2023. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2024. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  2025. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2026. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2027. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2028. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2029. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2030. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2031. for (var i = 0; i < 100; i++)
  2032. {
  2033. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2034. }
  2035. }
  2036. [Fact]
  2037. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy_ThenByDescendingAsyncWithCancellation()
  2038. {
  2039. var rand = new Random(42);
  2040. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2041. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  2042. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2043. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2044. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2045. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2046. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2047. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2048. for (var i = 0; i < 100; i++)
  2049. {
  2050. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2051. }
  2052. }
  2053. [Fact]
  2054. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync_ThenBy()
  2055. {
  2056. var rand = new Random(42);
  2057. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2058. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  2059. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2060. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2061. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2062. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2063. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2064. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2065. for (var i = 0; i < 100; i++)
  2066. {
  2067. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2068. }
  2069. }
  2070. [Fact]
  2071. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync_ThenByAsync()
  2072. {
  2073. var rand = new Random(42);
  2074. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2075. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  2076. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2077. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2078. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2079. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2080. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2081. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2082. for (var i = 0; i < 100; i++)
  2083. {
  2084. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2085. }
  2086. }
  2087. [Fact]
  2088. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync_ThenByAsyncWithCancellation()
  2089. {
  2090. var rand = new Random(42);
  2091. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2092. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  2093. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2094. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2095. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2096. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2097. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2098. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2099. for (var i = 0; i < 100; i++)
  2100. {
  2101. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2102. }
  2103. }
  2104. [Fact]
  2105. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync_ThenByDescending()
  2106. {
  2107. var rand = new Random(42);
  2108. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2109. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  2110. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2111. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2112. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2113. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2114. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2115. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2116. for (var i = 0; i < 100; i++)
  2117. {
  2118. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2119. }
  2120. }
  2121. [Fact]
  2122. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync_ThenByDescendingAsync()
  2123. {
  2124. var rand = new Random(42);
  2125. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2126. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  2127. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2128. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2129. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2130. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2131. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2132. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2133. for (var i = 0; i < 100; i++)
  2134. {
  2135. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2136. }
  2137. }
  2138. [Fact]
  2139. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync_ThenByDescendingAsyncWithCancellation()
  2140. {
  2141. var rand = new Random(42);
  2142. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2143. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  2144. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2145. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2146. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2147. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2148. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2149. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2150. for (var i = 0; i < 100; i++)
  2151. {
  2152. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2153. }
  2154. }
  2155. [Fact]
  2156. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation_ThenBy()
  2157. {
  2158. var rand = new Random(42);
  2159. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2160. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  2161. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2162. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2163. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2164. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2165. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2166. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2167. for (var i = 0; i < 100; i++)
  2168. {
  2169. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2170. }
  2171. }
  2172. [Fact]
  2173. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByAsync()
  2174. {
  2175. var rand = new Random(42);
  2176. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2177. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  2178. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2179. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2180. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2181. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2182. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2183. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2184. for (var i = 0; i < 100; i++)
  2185. {
  2186. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2187. }
  2188. }
  2189. [Fact]
  2190. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByAsyncWithCancellation()
  2191. {
  2192. var rand = new Random(42);
  2193. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2194. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  2195. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2196. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2197. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2198. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2199. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2200. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2201. for (var i = 0; i < 100; i++)
  2202. {
  2203. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2204. }
  2205. }
  2206. [Fact]
  2207. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByDescending()
  2208. {
  2209. var rand = new Random(42);
  2210. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2211. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  2212. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2213. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2214. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2215. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2216. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2217. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2218. for (var i = 0; i < 100; i++)
  2219. {
  2220. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2221. }
  2222. }
  2223. [Fact]
  2224. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByDescendingAsync()
  2225. {
  2226. var rand = new Random(42);
  2227. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2228. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  2229. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2230. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2231. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2232. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2233. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2234. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2235. for (var i = 0; i < 100; i++)
  2236. {
  2237. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2238. }
  2239. }
  2240. [Fact]
  2241. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  2242. {
  2243. var rand = new Random(42);
  2244. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2245. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  2246. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2247. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2248. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2249. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2250. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2251. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2252. for (var i = 0; i < 100; i++)
  2253. {
  2254. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2255. }
  2256. }
  2257. [Fact]
  2258. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending_ThenBy()
  2259. {
  2260. var rand = new Random(42);
  2261. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2262. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2263. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2264. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2265. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2266. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2267. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2268. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2269. for (var i = 0; i < 100; i++)
  2270. {
  2271. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2272. }
  2273. }
  2274. [Fact]
  2275. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending_ThenByAsync()
  2276. {
  2277. var rand = new Random(42);
  2278. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2279. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  2280. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2281. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2282. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2283. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2284. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2285. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2286. for (var i = 0; i < 100; i++)
  2287. {
  2288. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2289. }
  2290. }
  2291. [Fact]
  2292. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending_ThenByAsyncWithCancellation()
  2293. {
  2294. var rand = new Random(42);
  2295. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2296. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  2297. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2298. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2299. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2300. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2301. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2302. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2303. for (var i = 0; i < 100; i++)
  2304. {
  2305. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2306. }
  2307. }
  2308. [Fact]
  2309. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending_ThenByDescending()
  2310. {
  2311. var rand = new Random(42);
  2312. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2313. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2314. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2315. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2316. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2317. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2318. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2319. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2320. for (var i = 0; i < 100; i++)
  2321. {
  2322. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2323. }
  2324. }
  2325. [Fact]
  2326. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending_ThenByDescendingAsync()
  2327. {
  2328. var rand = new Random(42);
  2329. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2330. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  2331. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2332. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2333. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2334. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2335. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2336. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2337. for (var i = 0; i < 100; i++)
  2338. {
  2339. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2340. }
  2341. }
  2342. [Fact]
  2343. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending_ThenByDescendingAsyncWithCancellation()
  2344. {
  2345. var rand = new Random(42);
  2346. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2347. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  2348. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2349. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2350. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2351. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2352. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2353. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2354. for (var i = 0; i < 100; i++)
  2355. {
  2356. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2357. }
  2358. }
  2359. [Fact]
  2360. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync_ThenBy()
  2361. {
  2362. var rand = new Random(42);
  2363. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2364. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  2365. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2366. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2367. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2368. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2369. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2370. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2371. for (var i = 0; i < 100; i++)
  2372. {
  2373. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2374. }
  2375. }
  2376. [Fact]
  2377. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync_ThenByAsync()
  2378. {
  2379. var rand = new Random(42);
  2380. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2381. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  2382. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2383. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2384. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2385. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2386. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2387. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2388. for (var i = 0; i < 100; i++)
  2389. {
  2390. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2391. }
  2392. }
  2393. [Fact]
  2394. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync_ThenByAsyncWithCancellation()
  2395. {
  2396. var rand = new Random(42);
  2397. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2398. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  2399. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2400. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2401. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2402. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2403. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2404. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2405. for (var i = 0; i < 100; i++)
  2406. {
  2407. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2408. }
  2409. }
  2410. [Fact]
  2411. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync_ThenByDescending()
  2412. {
  2413. var rand = new Random(42);
  2414. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2415. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  2416. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2417. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2418. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2419. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2420. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2421. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2422. for (var i = 0; i < 100; i++)
  2423. {
  2424. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2425. }
  2426. }
  2427. [Fact]
  2428. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync_ThenByDescendingAsync()
  2429. {
  2430. var rand = new Random(42);
  2431. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2432. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  2433. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2434. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2435. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2436. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2437. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2438. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2439. for (var i = 0; i < 100; i++)
  2440. {
  2441. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2442. }
  2443. }
  2444. [Fact]
  2445. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  2446. {
  2447. var rand = new Random(42);
  2448. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2449. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  2450. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2451. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2452. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2453. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2454. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2455. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2456. for (var i = 0; i < 100; i++)
  2457. {
  2458. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2459. }
  2460. }
  2461. [Fact]
  2462. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenBy()
  2463. {
  2464. var rand = new Random(42);
  2465. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2466. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  2467. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2468. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2469. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2470. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2471. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2472. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2473. for (var i = 0; i < 100; i++)
  2474. {
  2475. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2476. }
  2477. }
  2478. [Fact]
  2479. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByAsync()
  2480. {
  2481. var rand = new Random(42);
  2482. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2483. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  2484. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2485. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2486. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2487. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2488. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2489. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2490. for (var i = 0; i < 100; i++)
  2491. {
  2492. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2493. }
  2494. }
  2495. [Fact]
  2496. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  2497. {
  2498. var rand = new Random(42);
  2499. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2500. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  2501. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2502. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2503. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2504. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2505. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2506. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2507. for (var i = 0; i < 100; i++)
  2508. {
  2509. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2510. }
  2511. }
  2512. [Fact]
  2513. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByDescending()
  2514. {
  2515. var rand = new Random(42);
  2516. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2517. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  2518. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2519. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2520. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2521. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2522. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2523. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2524. for (var i = 0; i < 100; i++)
  2525. {
  2526. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2527. }
  2528. }
  2529. [Fact]
  2530. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  2531. {
  2532. var rand = new Random(42);
  2533. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2534. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  2535. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2536. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2537. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2538. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2539. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2540. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2541. for (var i = 0; i < 100; i++)
  2542. {
  2543. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2544. }
  2545. }
  2546. [Fact]
  2547. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  2548. {
  2549. var rand = new Random(42);
  2550. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2551. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  2552. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2553. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2554. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2555. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2556. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2557. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2558. for (var i = 0; i < 100; i++)
  2559. {
  2560. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2561. }
  2562. }
  2563. [Fact]
  2564. public async Task OrderBy_OrderByDescending_ThenBy_ThenBy()
  2565. {
  2566. var rand = new Random(42);
  2567. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2568. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2569. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2570. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2571. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2572. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2573. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2574. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2575. for (var i = 0; i < 100; i++)
  2576. {
  2577. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2578. }
  2579. }
  2580. [Fact]
  2581. public async Task OrderBy_OrderByDescending_ThenBy_ThenByAsync()
  2582. {
  2583. var rand = new Random(42);
  2584. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2585. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  2586. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2587. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2588. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2589. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2590. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2591. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2592. for (var i = 0; i < 100; i++)
  2593. {
  2594. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2595. }
  2596. }
  2597. [Fact]
  2598. public async Task OrderBy_OrderByDescending_ThenBy_ThenByAsyncWithCancellation()
  2599. {
  2600. var rand = new Random(42);
  2601. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2602. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  2603. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2604. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2605. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2606. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2607. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2608. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2609. for (var i = 0; i < 100; i++)
  2610. {
  2611. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2612. }
  2613. }
  2614. [Fact]
  2615. public async Task OrderBy_OrderByDescending_ThenBy_ThenByDescending()
  2616. {
  2617. var rand = new Random(42);
  2618. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2619. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2620. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2621. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2622. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2623. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2624. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2625. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2626. for (var i = 0; i < 100; i++)
  2627. {
  2628. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2629. }
  2630. }
  2631. [Fact]
  2632. public async Task OrderBy_OrderByDescending_ThenBy_ThenByDescendingAsync()
  2633. {
  2634. var rand = new Random(42);
  2635. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2636. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  2637. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2638. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2639. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2640. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2641. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2642. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2643. for (var i = 0; i < 100; i++)
  2644. {
  2645. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2646. }
  2647. }
  2648. [Fact]
  2649. public async Task OrderBy_OrderByDescending_ThenBy_ThenByDescendingAsyncWithCancellation()
  2650. {
  2651. var rand = new Random(42);
  2652. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2653. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  2654. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2655. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2656. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2657. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2658. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2659. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2660. for (var i = 0; i < 100; i++)
  2661. {
  2662. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2663. }
  2664. }
  2665. [Fact]
  2666. public async Task OrderBy_OrderByDescending_ThenByAsync_ThenBy()
  2667. {
  2668. var rand = new Random(42);
  2669. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2670. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  2671. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2672. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2673. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2674. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2675. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2676. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2677. for (var i = 0; i < 100; i++)
  2678. {
  2679. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2680. }
  2681. }
  2682. [Fact]
  2683. public async Task OrderBy_OrderByDescending_ThenByAsync_ThenByAsync()
  2684. {
  2685. var rand = new Random(42);
  2686. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2687. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  2688. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2689. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2690. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2691. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2692. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2693. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2694. for (var i = 0; i < 100; i++)
  2695. {
  2696. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2697. }
  2698. }
  2699. [Fact]
  2700. public async Task OrderBy_OrderByDescending_ThenByAsync_ThenByAsyncWithCancellation()
  2701. {
  2702. var rand = new Random(42);
  2703. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2704. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  2705. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2706. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2707. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2708. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2709. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2710. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2711. for (var i = 0; i < 100; i++)
  2712. {
  2713. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2714. }
  2715. }
  2716. [Fact]
  2717. public async Task OrderBy_OrderByDescending_ThenByAsync_ThenByDescending()
  2718. {
  2719. var rand = new Random(42);
  2720. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2721. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  2722. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2723. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2724. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2725. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2726. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2727. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2728. for (var i = 0; i < 100; i++)
  2729. {
  2730. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2731. }
  2732. }
  2733. [Fact]
  2734. public async Task OrderBy_OrderByDescending_ThenByAsync_ThenByDescendingAsync()
  2735. {
  2736. var rand = new Random(42);
  2737. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2738. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  2739. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2740. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2741. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2742. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2743. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2744. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2745. for (var i = 0; i < 100; i++)
  2746. {
  2747. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2748. }
  2749. }
  2750. [Fact]
  2751. public async Task OrderBy_OrderByDescending_ThenByAsync_ThenByDescendingAsyncWithCancellation()
  2752. {
  2753. var rand = new Random(42);
  2754. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2755. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  2756. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2757. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2758. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2759. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2760. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2761. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2762. for (var i = 0; i < 100; i++)
  2763. {
  2764. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2765. }
  2766. }
  2767. [Fact]
  2768. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation_ThenBy()
  2769. {
  2770. var rand = new Random(42);
  2771. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2772. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  2773. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2774. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2775. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2776. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2777. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2778. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2779. for (var i = 0; i < 100; i++)
  2780. {
  2781. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2782. }
  2783. }
  2784. [Fact]
  2785. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation_ThenByAsync()
  2786. {
  2787. var rand = new Random(42);
  2788. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2789. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  2790. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2791. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2792. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2793. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2794. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2795. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2796. for (var i = 0; i < 100; i++)
  2797. {
  2798. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2799. }
  2800. }
  2801. [Fact]
  2802. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation_ThenByAsyncWithCancellation()
  2803. {
  2804. var rand = new Random(42);
  2805. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2806. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  2807. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2808. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2809. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2810. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2811. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2812. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2813. for (var i = 0; i < 100; i++)
  2814. {
  2815. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2816. }
  2817. }
  2818. [Fact]
  2819. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation_ThenByDescending()
  2820. {
  2821. var rand = new Random(42);
  2822. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2823. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  2824. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2825. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2826. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2827. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2828. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2829. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2830. for (var i = 0; i < 100; i++)
  2831. {
  2832. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2833. }
  2834. }
  2835. [Fact]
  2836. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation_ThenByDescendingAsync()
  2837. {
  2838. var rand = new Random(42);
  2839. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2840. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  2841. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2842. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2843. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2844. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2845. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2846. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2847. for (var i = 0; i < 100; i++)
  2848. {
  2849. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2850. }
  2851. }
  2852. [Fact]
  2853. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  2854. {
  2855. var rand = new Random(42);
  2856. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2857. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  2858. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2859. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2860. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2861. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2862. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2863. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2864. for (var i = 0; i < 100; i++)
  2865. {
  2866. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2867. }
  2868. }
  2869. [Fact]
  2870. public async Task OrderBy_OrderByDescending_ThenByDescending_ThenBy()
  2871. {
  2872. var rand = new Random(42);
  2873. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2874. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2875. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2876. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2877. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2878. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2879. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2880. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2881. for (var i = 0; i < 100; i++)
  2882. {
  2883. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2884. }
  2885. }
  2886. [Fact]
  2887. public async Task OrderBy_OrderByDescending_ThenByDescending_ThenByAsync()
  2888. {
  2889. var rand = new Random(42);
  2890. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2891. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  2892. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2893. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2894. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2895. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2896. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2897. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2898. for (var i = 0; i < 100; i++)
  2899. {
  2900. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2901. }
  2902. }
  2903. [Fact]
  2904. public async Task OrderBy_OrderByDescending_ThenByDescending_ThenByAsyncWithCancellation()
  2905. {
  2906. var rand = new Random(42);
  2907. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2908. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  2909. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2910. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2911. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2912. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2913. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2914. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2915. for (var i = 0; i < 100; i++)
  2916. {
  2917. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2918. }
  2919. }
  2920. [Fact]
  2921. public async Task OrderBy_OrderByDescending_ThenByDescending_ThenByDescending()
  2922. {
  2923. var rand = new Random(42);
  2924. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2925. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2926. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2927. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2928. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2929. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2930. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2931. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2932. for (var i = 0; i < 100; i++)
  2933. {
  2934. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2935. }
  2936. }
  2937. [Fact]
  2938. public async Task OrderBy_OrderByDescending_ThenByDescending_ThenByDescendingAsync()
  2939. {
  2940. var rand = new Random(42);
  2941. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2942. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  2943. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2944. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2945. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2946. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2947. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2948. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2949. for (var i = 0; i < 100; i++)
  2950. {
  2951. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2952. }
  2953. }
  2954. [Fact]
  2955. public async Task OrderBy_OrderByDescending_ThenByDescending_ThenByDescendingAsyncWithCancellation()
  2956. {
  2957. var rand = new Random(42);
  2958. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2959. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  2960. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2961. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2962. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2963. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2964. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2965. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2966. for (var i = 0; i < 100; i++)
  2967. {
  2968. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2969. }
  2970. }
  2971. [Fact]
  2972. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync_ThenBy()
  2973. {
  2974. var rand = new Random(42);
  2975. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2976. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  2977. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2978. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2979. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2980. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2981. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2982. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  2983. for (var i = 0; i < 100; i++)
  2984. {
  2985. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  2986. }
  2987. }
  2988. [Fact]
  2989. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync_ThenByAsync()
  2990. {
  2991. var rand = new Random(42);
  2992. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2993. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  2994. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2995. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  2996. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  2997. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  2998. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  2999. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3000. for (var i = 0; i < 100; i++)
  3001. {
  3002. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3003. }
  3004. }
  3005. [Fact]
  3006. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync_ThenByAsyncWithCancellation()
  3007. {
  3008. var rand = new Random(42);
  3009. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3010. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3011. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3012. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3013. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3014. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3015. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3016. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3017. for (var i = 0; i < 100; i++)
  3018. {
  3019. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3020. }
  3021. }
  3022. [Fact]
  3023. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync_ThenByDescending()
  3024. {
  3025. var rand = new Random(42);
  3026. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3027. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  3028. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3029. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3030. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3031. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3032. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3033. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3034. for (var i = 0; i < 100; i++)
  3035. {
  3036. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3037. }
  3038. }
  3039. [Fact]
  3040. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync_ThenByDescendingAsync()
  3041. {
  3042. var rand = new Random(42);
  3043. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3044. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  3045. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3046. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3047. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3048. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3049. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3050. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3051. for (var i = 0; i < 100; i++)
  3052. {
  3053. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3054. }
  3055. }
  3056. [Fact]
  3057. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  3058. {
  3059. var rand = new Random(42);
  3060. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3061. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3062. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3063. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3064. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3065. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3066. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3067. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3068. for (var i = 0; i < 100; i++)
  3069. {
  3070. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3071. }
  3072. }
  3073. [Fact]
  3074. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation_ThenBy()
  3075. {
  3076. var rand = new Random(42);
  3077. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3078. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  3079. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3080. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3081. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3082. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3083. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3084. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3085. for (var i = 0; i < 100; i++)
  3086. {
  3087. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3088. }
  3089. }
  3090. [Fact]
  3091. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation_ThenByAsync()
  3092. {
  3093. var rand = new Random(42);
  3094. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3095. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  3096. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3097. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3098. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3099. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3100. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3101. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3102. for (var i = 0; i < 100; i++)
  3103. {
  3104. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3105. }
  3106. }
  3107. [Fact]
  3108. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  3109. {
  3110. var rand = new Random(42);
  3111. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3112. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3113. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3114. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3115. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3116. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3117. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3118. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3119. for (var i = 0; i < 100; i++)
  3120. {
  3121. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3122. }
  3123. }
  3124. [Fact]
  3125. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation_ThenByDescending()
  3126. {
  3127. var rand = new Random(42);
  3128. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3129. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  3130. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3131. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3132. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3133. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3134. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3135. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3136. for (var i = 0; i < 100; i++)
  3137. {
  3138. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3139. }
  3140. }
  3141. [Fact]
  3142. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  3143. {
  3144. var rand = new Random(42);
  3145. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3146. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  3147. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3148. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3149. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3150. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3151. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3152. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3153. for (var i = 0; i < 100; i++)
  3154. {
  3155. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3156. }
  3157. }
  3158. [Fact]
  3159. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  3160. {
  3161. var rand = new Random(42);
  3162. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3163. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3164. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3165. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3166. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3167. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3168. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3169. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3170. for (var i = 0; i < 100; i++)
  3171. {
  3172. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3173. }
  3174. }
  3175. [Fact]
  3176. public async Task OrderBy_OrderByDescendingAsync_ThenBy_ThenBy()
  3177. {
  3178. var rand = new Random(42);
  3179. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3180. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3181. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3182. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3183. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3184. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3185. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3186. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3187. for (var i = 0; i < 100; i++)
  3188. {
  3189. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3190. }
  3191. }
  3192. [Fact]
  3193. public async Task OrderBy_OrderByDescendingAsync_ThenBy_ThenByAsync()
  3194. {
  3195. var rand = new Random(42);
  3196. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3197. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  3198. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3199. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3200. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3201. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3202. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3203. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3204. for (var i = 0; i < 100; i++)
  3205. {
  3206. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3207. }
  3208. }
  3209. [Fact]
  3210. public async Task OrderBy_OrderByDescendingAsync_ThenBy_ThenByAsyncWithCancellation()
  3211. {
  3212. var rand = new Random(42);
  3213. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3214. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3215. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3216. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3217. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3218. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3219. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3220. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3221. for (var i = 0; i < 100; i++)
  3222. {
  3223. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3224. }
  3225. }
  3226. [Fact]
  3227. public async Task OrderBy_OrderByDescendingAsync_ThenBy_ThenByDescending()
  3228. {
  3229. var rand = new Random(42);
  3230. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3231. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3232. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3233. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3234. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3235. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3236. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3237. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3238. for (var i = 0; i < 100; i++)
  3239. {
  3240. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3241. }
  3242. }
  3243. [Fact]
  3244. public async Task OrderBy_OrderByDescendingAsync_ThenBy_ThenByDescendingAsync()
  3245. {
  3246. var rand = new Random(42);
  3247. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3248. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  3249. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3250. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3251. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3252. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3253. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3254. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3255. for (var i = 0; i < 100; i++)
  3256. {
  3257. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3258. }
  3259. }
  3260. [Fact]
  3261. public async Task OrderBy_OrderByDescendingAsync_ThenBy_ThenByDescendingAsyncWithCancellation()
  3262. {
  3263. var rand = new Random(42);
  3264. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3265. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3266. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3267. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3268. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3269. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3270. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3271. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3272. for (var i = 0; i < 100; i++)
  3273. {
  3274. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3275. }
  3276. }
  3277. [Fact]
  3278. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync_ThenBy()
  3279. {
  3280. var rand = new Random(42);
  3281. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3282. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  3283. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3284. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3285. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3286. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3287. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3288. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3289. for (var i = 0; i < 100; i++)
  3290. {
  3291. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3292. }
  3293. }
  3294. [Fact]
  3295. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync_ThenByAsync()
  3296. {
  3297. var rand = new Random(42);
  3298. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3299. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  3300. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3301. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3302. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3303. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3304. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3305. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3306. for (var i = 0; i < 100; i++)
  3307. {
  3308. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3309. }
  3310. }
  3311. [Fact]
  3312. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync_ThenByAsyncWithCancellation()
  3313. {
  3314. var rand = new Random(42);
  3315. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3316. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3317. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3318. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3319. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3320. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3321. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3322. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3323. for (var i = 0; i < 100; i++)
  3324. {
  3325. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3326. }
  3327. }
  3328. [Fact]
  3329. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync_ThenByDescending()
  3330. {
  3331. var rand = new Random(42);
  3332. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3333. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  3334. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3335. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3336. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3337. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3338. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3339. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3340. for (var i = 0; i < 100; i++)
  3341. {
  3342. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3343. }
  3344. }
  3345. [Fact]
  3346. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync_ThenByDescendingAsync()
  3347. {
  3348. var rand = new Random(42);
  3349. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3350. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  3351. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3352. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3353. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3354. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3355. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3356. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3357. for (var i = 0; i < 100; i++)
  3358. {
  3359. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3360. }
  3361. }
  3362. [Fact]
  3363. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync_ThenByDescendingAsyncWithCancellation()
  3364. {
  3365. var rand = new Random(42);
  3366. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3367. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3368. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3369. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3370. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3371. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3372. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3373. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3374. for (var i = 0; i < 100; i++)
  3375. {
  3376. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3377. }
  3378. }
  3379. [Fact]
  3380. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation_ThenBy()
  3381. {
  3382. var rand = new Random(42);
  3383. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3384. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  3385. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3386. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3387. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3388. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3389. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3390. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3391. for (var i = 0; i < 100; i++)
  3392. {
  3393. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3394. }
  3395. }
  3396. [Fact]
  3397. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation_ThenByAsync()
  3398. {
  3399. var rand = new Random(42);
  3400. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3401. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  3402. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3403. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3404. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3405. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3406. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3407. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3408. for (var i = 0; i < 100; i++)
  3409. {
  3410. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3411. }
  3412. }
  3413. [Fact]
  3414. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation_ThenByAsyncWithCancellation()
  3415. {
  3416. var rand = new Random(42);
  3417. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3418. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3419. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3420. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3421. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3422. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3423. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3424. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3425. for (var i = 0; i < 100; i++)
  3426. {
  3427. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3428. }
  3429. }
  3430. [Fact]
  3431. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation_ThenByDescending()
  3432. {
  3433. var rand = new Random(42);
  3434. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3435. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  3436. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3437. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3438. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3439. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3440. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3441. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3442. for (var i = 0; i < 100; i++)
  3443. {
  3444. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3445. }
  3446. }
  3447. [Fact]
  3448. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation_ThenByDescendingAsync()
  3449. {
  3450. var rand = new Random(42);
  3451. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3452. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  3453. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3454. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3455. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3456. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3457. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3458. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3459. for (var i = 0; i < 100; i++)
  3460. {
  3461. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3462. }
  3463. }
  3464. [Fact]
  3465. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  3466. {
  3467. var rand = new Random(42);
  3468. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3469. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3470. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3471. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3472. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3473. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3474. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3475. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3476. for (var i = 0; i < 100; i++)
  3477. {
  3478. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3479. }
  3480. }
  3481. [Fact]
  3482. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending_ThenBy()
  3483. {
  3484. var rand = new Random(42);
  3485. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3486. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3487. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3488. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3489. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3490. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3491. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3492. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3493. for (var i = 0; i < 100; i++)
  3494. {
  3495. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3496. }
  3497. }
  3498. [Fact]
  3499. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending_ThenByAsync()
  3500. {
  3501. var rand = new Random(42);
  3502. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3503. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  3504. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3505. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3506. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3507. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3508. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3509. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3510. for (var i = 0; i < 100; i++)
  3511. {
  3512. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3513. }
  3514. }
  3515. [Fact]
  3516. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending_ThenByAsyncWithCancellation()
  3517. {
  3518. var rand = new Random(42);
  3519. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3520. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3521. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3522. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3523. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3524. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3525. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3526. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3527. for (var i = 0; i < 100; i++)
  3528. {
  3529. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3530. }
  3531. }
  3532. [Fact]
  3533. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending_ThenByDescending()
  3534. {
  3535. var rand = new Random(42);
  3536. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3537. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3538. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3539. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3540. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3541. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3542. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3543. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3544. for (var i = 0; i < 100; i++)
  3545. {
  3546. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3547. }
  3548. }
  3549. [Fact]
  3550. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending_ThenByDescendingAsync()
  3551. {
  3552. var rand = new Random(42);
  3553. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3554. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  3555. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3556. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3557. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3558. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3559. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3560. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3561. for (var i = 0; i < 100; i++)
  3562. {
  3563. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3564. }
  3565. }
  3566. [Fact]
  3567. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending_ThenByDescendingAsyncWithCancellation()
  3568. {
  3569. var rand = new Random(42);
  3570. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3571. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3572. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3573. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3574. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3575. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3576. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3577. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3578. for (var i = 0; i < 100; i++)
  3579. {
  3580. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3581. }
  3582. }
  3583. [Fact]
  3584. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync_ThenBy()
  3585. {
  3586. var rand = new Random(42);
  3587. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3588. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  3589. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3590. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3591. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3592. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3593. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3594. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3595. for (var i = 0; i < 100; i++)
  3596. {
  3597. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3598. }
  3599. }
  3600. [Fact]
  3601. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync_ThenByAsync()
  3602. {
  3603. var rand = new Random(42);
  3604. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3605. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  3606. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3607. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3608. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3609. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3610. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3611. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3612. for (var i = 0; i < 100; i++)
  3613. {
  3614. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3615. }
  3616. }
  3617. [Fact]
  3618. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync_ThenByAsyncWithCancellation()
  3619. {
  3620. var rand = new Random(42);
  3621. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3622. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3623. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3624. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3625. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3626. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3627. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3628. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3629. for (var i = 0; i < 100; i++)
  3630. {
  3631. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3632. }
  3633. }
  3634. [Fact]
  3635. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync_ThenByDescending()
  3636. {
  3637. var rand = new Random(42);
  3638. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3639. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  3640. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3641. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3642. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3643. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3644. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3645. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3646. for (var i = 0; i < 100; i++)
  3647. {
  3648. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3649. }
  3650. }
  3651. [Fact]
  3652. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync_ThenByDescendingAsync()
  3653. {
  3654. var rand = new Random(42);
  3655. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3656. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  3657. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3658. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3659. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3660. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3661. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3662. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3663. for (var i = 0; i < 100; i++)
  3664. {
  3665. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3666. }
  3667. }
  3668. [Fact]
  3669. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  3670. {
  3671. var rand = new Random(42);
  3672. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3673. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3674. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3675. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3676. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3677. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3678. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3679. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3680. for (var i = 0; i < 100; i++)
  3681. {
  3682. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3683. }
  3684. }
  3685. [Fact]
  3686. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation_ThenBy()
  3687. {
  3688. var rand = new Random(42);
  3689. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3690. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  3691. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3692. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3693. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3694. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3695. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3696. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3697. for (var i = 0; i < 100; i++)
  3698. {
  3699. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3700. }
  3701. }
  3702. [Fact]
  3703. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation_ThenByAsync()
  3704. {
  3705. var rand = new Random(42);
  3706. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3707. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  3708. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3709. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3710. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3711. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3712. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3713. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3714. for (var i = 0; i < 100; i++)
  3715. {
  3716. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3717. }
  3718. }
  3719. [Fact]
  3720. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  3721. {
  3722. var rand = new Random(42);
  3723. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3724. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3725. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3726. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3727. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3728. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3729. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3730. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3731. for (var i = 0; i < 100; i++)
  3732. {
  3733. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3734. }
  3735. }
  3736. [Fact]
  3737. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation_ThenByDescending()
  3738. {
  3739. var rand = new Random(42);
  3740. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3741. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  3742. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3743. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3744. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3745. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3746. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3747. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3748. for (var i = 0; i < 100; i++)
  3749. {
  3750. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3751. }
  3752. }
  3753. [Fact]
  3754. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  3755. {
  3756. var rand = new Random(42);
  3757. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3758. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  3759. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3760. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3761. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3762. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3763. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3764. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3765. for (var i = 0; i < 100; i++)
  3766. {
  3767. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3768. }
  3769. }
  3770. [Fact]
  3771. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  3772. {
  3773. var rand = new Random(42);
  3774. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3775. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3776. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3777. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3778. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3779. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3780. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3781. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3782. for (var i = 0; i < 100; i++)
  3783. {
  3784. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3785. }
  3786. }
  3787. [Fact]
  3788. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy_ThenBy()
  3789. {
  3790. var rand = new Random(42);
  3791. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3792. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3793. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3794. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3795. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3796. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3797. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3798. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3799. for (var i = 0; i < 100; i++)
  3800. {
  3801. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3802. }
  3803. }
  3804. [Fact]
  3805. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy_ThenByAsync()
  3806. {
  3807. var rand = new Random(42);
  3808. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3809. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  3810. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3811. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3812. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3813. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3814. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3815. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3816. for (var i = 0; i < 100; i++)
  3817. {
  3818. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3819. }
  3820. }
  3821. [Fact]
  3822. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy_ThenByAsyncWithCancellation()
  3823. {
  3824. var rand = new Random(42);
  3825. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3826. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3827. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3828. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3829. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3830. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3831. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3832. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3833. for (var i = 0; i < 100; i++)
  3834. {
  3835. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3836. }
  3837. }
  3838. [Fact]
  3839. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy_ThenByDescending()
  3840. {
  3841. var rand = new Random(42);
  3842. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3843. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3844. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3845. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3846. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3847. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3848. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3849. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3850. for (var i = 0; i < 100; i++)
  3851. {
  3852. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3853. }
  3854. }
  3855. [Fact]
  3856. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy_ThenByDescendingAsync()
  3857. {
  3858. var rand = new Random(42);
  3859. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3860. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  3861. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3862. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3863. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3864. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3865. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3866. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3867. for (var i = 0; i < 100; i++)
  3868. {
  3869. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3870. }
  3871. }
  3872. [Fact]
  3873. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy_ThenByDescendingAsyncWithCancellation()
  3874. {
  3875. var rand = new Random(42);
  3876. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3877. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3878. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3879. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3880. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3881. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3882. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3883. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3884. for (var i = 0; i < 100; i++)
  3885. {
  3886. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3887. }
  3888. }
  3889. [Fact]
  3890. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync_ThenBy()
  3891. {
  3892. var rand = new Random(42);
  3893. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3894. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  3895. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3896. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3897. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3898. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3899. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3900. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3901. for (var i = 0; i < 100; i++)
  3902. {
  3903. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3904. }
  3905. }
  3906. [Fact]
  3907. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync_ThenByAsync()
  3908. {
  3909. var rand = new Random(42);
  3910. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3911. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  3912. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3913. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3914. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3915. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3916. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3917. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3918. for (var i = 0; i < 100; i++)
  3919. {
  3920. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3921. }
  3922. }
  3923. [Fact]
  3924. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync_ThenByAsyncWithCancellation()
  3925. {
  3926. var rand = new Random(42);
  3927. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3928. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3929. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3930. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3931. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3932. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3933. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3934. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3935. for (var i = 0; i < 100; i++)
  3936. {
  3937. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3938. }
  3939. }
  3940. [Fact]
  3941. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync_ThenByDescending()
  3942. {
  3943. var rand = new Random(42);
  3944. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3945. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  3946. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3947. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3948. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3949. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3950. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3951. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3952. for (var i = 0; i < 100; i++)
  3953. {
  3954. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3955. }
  3956. }
  3957. [Fact]
  3958. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync_ThenByDescendingAsync()
  3959. {
  3960. var rand = new Random(42);
  3961. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3962. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  3963. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3964. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3965. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3966. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3967. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3968. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3969. for (var i = 0; i < 100; i++)
  3970. {
  3971. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3972. }
  3973. }
  3974. [Fact]
  3975. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync_ThenByDescendingAsyncWithCancellation()
  3976. {
  3977. var rand = new Random(42);
  3978. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3979. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3980. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3981. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3982. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  3983. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  3984. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  3985. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  3986. for (var i = 0; i < 100; i++)
  3987. {
  3988. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  3989. }
  3990. }
  3991. [Fact]
  3992. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation_ThenBy()
  3993. {
  3994. var rand = new Random(42);
  3995. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3996. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  3997. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3998. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  3999. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4000. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4001. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4002. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4003. for (var i = 0; i < 100; i++)
  4004. {
  4005. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4006. }
  4007. }
  4008. [Fact]
  4009. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByAsync()
  4010. {
  4011. var rand = new Random(42);
  4012. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4013. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  4014. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4015. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4016. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4017. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4018. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4019. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4020. for (var i = 0; i < 100; i++)
  4021. {
  4022. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4023. }
  4024. }
  4025. [Fact]
  4026. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByAsyncWithCancellation()
  4027. {
  4028. var rand = new Random(42);
  4029. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4030. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  4031. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4032. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4033. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4034. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4035. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4036. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4037. for (var i = 0; i < 100; i++)
  4038. {
  4039. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4040. }
  4041. }
  4042. [Fact]
  4043. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByDescending()
  4044. {
  4045. var rand = new Random(42);
  4046. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4047. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  4048. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4049. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4050. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4051. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4052. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4053. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4054. for (var i = 0; i < 100; i++)
  4055. {
  4056. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4057. }
  4058. }
  4059. [Fact]
  4060. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByDescendingAsync()
  4061. {
  4062. var rand = new Random(42);
  4063. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4064. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  4065. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4066. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4067. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4068. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4069. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4070. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4071. for (var i = 0; i < 100; i++)
  4072. {
  4073. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4074. }
  4075. }
  4076. [Fact]
  4077. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  4078. {
  4079. var rand = new Random(42);
  4080. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4081. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  4082. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4083. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4084. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4085. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4086. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4087. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4088. for (var i = 0; i < 100; i++)
  4089. {
  4090. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4091. }
  4092. }
  4093. [Fact]
  4094. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending_ThenBy()
  4095. {
  4096. var rand = new Random(42);
  4097. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4098. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4099. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4100. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4101. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4102. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4103. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4104. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4105. for (var i = 0; i < 100; i++)
  4106. {
  4107. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4108. }
  4109. }
  4110. [Fact]
  4111. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending_ThenByAsync()
  4112. {
  4113. var rand = new Random(42);
  4114. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4115. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  4116. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4117. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4118. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4119. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4120. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4121. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4122. for (var i = 0; i < 100; i++)
  4123. {
  4124. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4125. }
  4126. }
  4127. [Fact]
  4128. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending_ThenByAsyncWithCancellation()
  4129. {
  4130. var rand = new Random(42);
  4131. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4132. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  4133. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4134. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4135. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4136. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4137. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4138. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4139. for (var i = 0; i < 100; i++)
  4140. {
  4141. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4142. }
  4143. }
  4144. [Fact]
  4145. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending_ThenByDescending()
  4146. {
  4147. var rand = new Random(42);
  4148. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4149. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4150. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4151. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4152. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4153. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4154. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4155. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4156. for (var i = 0; i < 100; i++)
  4157. {
  4158. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4159. }
  4160. }
  4161. [Fact]
  4162. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending_ThenByDescendingAsync()
  4163. {
  4164. var rand = new Random(42);
  4165. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4166. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  4167. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4168. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4169. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4170. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4171. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4172. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4173. for (var i = 0; i < 100; i++)
  4174. {
  4175. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4176. }
  4177. }
  4178. [Fact]
  4179. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending_ThenByDescendingAsyncWithCancellation()
  4180. {
  4181. var rand = new Random(42);
  4182. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4183. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  4184. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4185. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4186. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4187. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4188. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4189. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4190. for (var i = 0; i < 100; i++)
  4191. {
  4192. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4193. }
  4194. }
  4195. [Fact]
  4196. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync_ThenBy()
  4197. {
  4198. var rand = new Random(42);
  4199. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4200. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  4201. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4202. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4203. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4204. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4205. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4206. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4207. for (var i = 0; i < 100; i++)
  4208. {
  4209. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4210. }
  4211. }
  4212. [Fact]
  4213. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync_ThenByAsync()
  4214. {
  4215. var rand = new Random(42);
  4216. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4217. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  4218. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4219. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4220. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4221. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4222. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4223. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4224. for (var i = 0; i < 100; i++)
  4225. {
  4226. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4227. }
  4228. }
  4229. [Fact]
  4230. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync_ThenByAsyncWithCancellation()
  4231. {
  4232. var rand = new Random(42);
  4233. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4234. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  4235. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4236. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4237. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4238. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4239. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4240. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4241. for (var i = 0; i < 100; i++)
  4242. {
  4243. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4244. }
  4245. }
  4246. [Fact]
  4247. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync_ThenByDescending()
  4248. {
  4249. var rand = new Random(42);
  4250. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4251. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  4252. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4253. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4254. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4255. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4256. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4257. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4258. for (var i = 0; i < 100; i++)
  4259. {
  4260. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4261. }
  4262. }
  4263. [Fact]
  4264. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync_ThenByDescendingAsync()
  4265. {
  4266. var rand = new Random(42);
  4267. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4268. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  4269. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4270. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4271. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4272. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4273. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4274. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4275. for (var i = 0; i < 100; i++)
  4276. {
  4277. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4278. }
  4279. }
  4280. [Fact]
  4281. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  4282. {
  4283. var rand = new Random(42);
  4284. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4285. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  4286. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4287. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4288. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4289. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4290. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4291. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4292. for (var i = 0; i < 100; i++)
  4293. {
  4294. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4295. }
  4296. }
  4297. [Fact]
  4298. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenBy()
  4299. {
  4300. var rand = new Random(42);
  4301. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4302. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  4303. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4304. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4305. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4306. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4307. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4308. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4309. for (var i = 0; i < 100; i++)
  4310. {
  4311. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4312. }
  4313. }
  4314. [Fact]
  4315. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByAsync()
  4316. {
  4317. var rand = new Random(42);
  4318. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4319. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  4320. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4321. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4322. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4323. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4324. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4325. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4326. for (var i = 0; i < 100; i++)
  4327. {
  4328. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4329. }
  4330. }
  4331. [Fact]
  4332. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  4333. {
  4334. var rand = new Random(42);
  4335. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4336. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  4337. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4338. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4339. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4340. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4341. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4342. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4343. for (var i = 0; i < 100; i++)
  4344. {
  4345. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4346. }
  4347. }
  4348. [Fact]
  4349. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByDescending()
  4350. {
  4351. var rand = new Random(42);
  4352. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4353. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  4354. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4355. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4356. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4357. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4358. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4359. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4360. for (var i = 0; i < 100; i++)
  4361. {
  4362. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4363. }
  4364. }
  4365. [Fact]
  4366. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  4367. {
  4368. var rand = new Random(42);
  4369. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4370. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  4371. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4372. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4373. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4374. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4375. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4376. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4377. for (var i = 0; i < 100; i++)
  4378. {
  4379. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4380. }
  4381. }
  4382. [Fact]
  4383. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  4384. {
  4385. var rand = new Random(42);
  4386. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4387. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  4388. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4389. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  4390. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  4391. Assert.Equal(syncRes.Count(), await asyncRes.CountAsync());
  4392. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  4393. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  4394. for (var i = 0; i < 100; i++)
  4395. {
  4396. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  4397. }
  4398. }
  4399. }
  4400. }