OrderBy.Generated.cs 299 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517
  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. await AssertSorted(asyncRes, syncRes);
  21. var asyncSegment = asyncRes.AsAsyncEnumerable();
  22. var syncSegment = syncRes.AsEnumerable();
  23. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  24. {
  25. asyncSegment = asyncSegment.Skip(skipCount);
  26. syncSegment = syncSegment.Skip(skipCount);
  27. }
  28. foreach (var takeCount in new[] { 31, 29, 23 })
  29. {
  30. asyncSegment = asyncSegment.Take(takeCount);
  31. syncSegment = syncSegment.Take(takeCount);
  32. }
  33. await AssertSorted(asyncSegment, syncSegment);
  34. }
  35. [Fact]
  36. public async Task OrderBy_OrderByAsync()
  37. {
  38. var rand = new Random(42);
  39. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  40. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2));
  41. var syncRes = xs.OrderBy(x => x % 2);
  42. await AssertSorted(asyncRes, syncRes);
  43. var asyncSegment = asyncRes.AsAsyncEnumerable();
  44. var syncSegment = syncRes.AsEnumerable();
  45. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  46. {
  47. asyncSegment = asyncSegment.Skip(skipCount);
  48. syncSegment = syncSegment.Skip(skipCount);
  49. }
  50. foreach (var takeCount in new[] { 31, 29, 23 })
  51. {
  52. asyncSegment = asyncSegment.Take(takeCount);
  53. syncSegment = syncSegment.Take(takeCount);
  54. }
  55. await AssertSorted(asyncSegment, syncSegment);
  56. }
  57. [Fact]
  58. public async Task OrderBy_OrderByAsyncWithCancellation()
  59. {
  60. var rand = new Random(42);
  61. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  62. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2));
  63. var syncRes = xs.OrderBy(x => x % 2);
  64. await AssertSorted(asyncRes, syncRes);
  65. var asyncSegment = asyncRes.AsAsyncEnumerable();
  66. var syncSegment = syncRes.AsEnumerable();
  67. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  68. {
  69. asyncSegment = asyncSegment.Skip(skipCount);
  70. syncSegment = syncSegment.Skip(skipCount);
  71. }
  72. foreach (var takeCount in new[] { 31, 29, 23 })
  73. {
  74. asyncSegment = asyncSegment.Take(takeCount);
  75. syncSegment = syncSegment.Take(takeCount);
  76. }
  77. await AssertSorted(asyncSegment, syncSegment);
  78. }
  79. [Fact]
  80. public async Task OrderBy_OrderByDescending()
  81. {
  82. var rand = new Random(42);
  83. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  84. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2);
  85. var syncRes = xs.OrderByDescending(x => x % 2);
  86. await AssertSorted(asyncRes, syncRes);
  87. var asyncSegment = asyncRes.AsAsyncEnumerable();
  88. var syncSegment = syncRes.AsEnumerable();
  89. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  90. {
  91. asyncSegment = asyncSegment.Skip(skipCount);
  92. syncSegment = syncSegment.Skip(skipCount);
  93. }
  94. foreach (var takeCount in new[] { 31, 29, 23 })
  95. {
  96. asyncSegment = asyncSegment.Take(takeCount);
  97. syncSegment = syncSegment.Take(takeCount);
  98. }
  99. await AssertSorted(asyncSegment, syncSegment);
  100. }
  101. [Fact]
  102. public async Task OrderBy_OrderByDescendingAsync()
  103. {
  104. var rand = new Random(42);
  105. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  106. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2));
  107. var syncRes = xs.OrderByDescending(x => x % 2);
  108. await AssertSorted(asyncRes, syncRes);
  109. var asyncSegment = asyncRes.AsAsyncEnumerable();
  110. var syncSegment = syncRes.AsEnumerable();
  111. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  112. {
  113. asyncSegment = asyncSegment.Skip(skipCount);
  114. syncSegment = syncSegment.Skip(skipCount);
  115. }
  116. foreach (var takeCount in new[] { 31, 29, 23 })
  117. {
  118. asyncSegment = asyncSegment.Take(takeCount);
  119. syncSegment = syncSegment.Take(takeCount);
  120. }
  121. await AssertSorted(asyncSegment, syncSegment);
  122. }
  123. [Fact]
  124. public async Task OrderBy_OrderByDescendingAsyncWithCancellation()
  125. {
  126. var rand = new Random(42);
  127. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  128. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2));
  129. var syncRes = xs.OrderByDescending(x => x % 2);
  130. await AssertSorted(asyncRes, syncRes);
  131. var asyncSegment = asyncRes.AsAsyncEnumerable();
  132. var syncSegment = syncRes.AsEnumerable();
  133. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  134. {
  135. asyncSegment = asyncSegment.Skip(skipCount);
  136. syncSegment = syncSegment.Skip(skipCount);
  137. }
  138. foreach (var takeCount in new[] { 31, 29, 23 })
  139. {
  140. asyncSegment = asyncSegment.Take(takeCount);
  141. syncSegment = syncSegment.Take(takeCount);
  142. }
  143. await AssertSorted(asyncSegment, syncSegment);
  144. }
  145. [Fact]
  146. public async Task OrderBy_OrderBy_ThenBy()
  147. {
  148. var rand = new Random(42);
  149. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  150. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3);
  151. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  152. await AssertSorted(asyncRes, syncRes);
  153. var asyncSegment = asyncRes.AsAsyncEnumerable();
  154. var syncSegment = syncRes.AsEnumerable();
  155. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  156. {
  157. asyncSegment = asyncSegment.Skip(skipCount);
  158. syncSegment = syncSegment.Skip(skipCount);
  159. }
  160. foreach (var takeCount in new[] { 31, 29, 23 })
  161. {
  162. asyncSegment = asyncSegment.Take(takeCount);
  163. syncSegment = syncSegment.Take(takeCount);
  164. }
  165. await AssertSorted(asyncSegment, syncSegment);
  166. }
  167. [Fact]
  168. public async Task OrderBy_OrderBy_ThenByAsync()
  169. {
  170. var rand = new Random(42);
  171. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  172. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3));
  173. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  174. await AssertSorted(asyncRes, syncRes);
  175. var asyncSegment = asyncRes.AsAsyncEnumerable();
  176. var syncSegment = syncRes.AsEnumerable();
  177. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  178. {
  179. asyncSegment = asyncSegment.Skip(skipCount);
  180. syncSegment = syncSegment.Skip(skipCount);
  181. }
  182. foreach (var takeCount in new[] { 31, 29, 23 })
  183. {
  184. asyncSegment = asyncSegment.Take(takeCount);
  185. syncSegment = syncSegment.Take(takeCount);
  186. }
  187. await AssertSorted(asyncSegment, syncSegment);
  188. }
  189. [Fact]
  190. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation()
  191. {
  192. var rand = new Random(42);
  193. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  194. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3));
  195. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  196. await AssertSorted(asyncRes, syncRes);
  197. var asyncSegment = asyncRes.AsAsyncEnumerable();
  198. var syncSegment = syncRes.AsEnumerable();
  199. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  200. {
  201. asyncSegment = asyncSegment.Skip(skipCount);
  202. syncSegment = syncSegment.Skip(skipCount);
  203. }
  204. foreach (var takeCount in new[] { 31, 29, 23 })
  205. {
  206. asyncSegment = asyncSegment.Take(takeCount);
  207. syncSegment = syncSegment.Take(takeCount);
  208. }
  209. await AssertSorted(asyncSegment, syncSegment);
  210. }
  211. [Fact]
  212. public async Task OrderBy_OrderBy_ThenByDescending()
  213. {
  214. var rand = new Random(42);
  215. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  216. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  217. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  218. await AssertSorted(asyncRes, syncRes);
  219. var asyncSegment = asyncRes.AsAsyncEnumerable();
  220. var syncSegment = syncRes.AsEnumerable();
  221. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  222. {
  223. asyncSegment = asyncSegment.Skip(skipCount);
  224. syncSegment = syncSegment.Skip(skipCount);
  225. }
  226. foreach (var takeCount in new[] { 31, 29, 23 })
  227. {
  228. asyncSegment = asyncSegment.Take(takeCount);
  229. syncSegment = syncSegment.Take(takeCount);
  230. }
  231. await AssertSorted(asyncSegment, syncSegment);
  232. }
  233. [Fact]
  234. public async Task OrderBy_OrderBy_ThenByDescendingAsync()
  235. {
  236. var rand = new Random(42);
  237. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  238. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3));
  239. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  240. await AssertSorted(asyncRes, syncRes);
  241. var asyncSegment = asyncRes.AsAsyncEnumerable();
  242. var syncSegment = syncRes.AsEnumerable();
  243. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  244. {
  245. asyncSegment = asyncSegment.Skip(skipCount);
  246. syncSegment = syncSegment.Skip(skipCount);
  247. }
  248. foreach (var takeCount in new[] { 31, 29, 23 })
  249. {
  250. asyncSegment = asyncSegment.Take(takeCount);
  251. syncSegment = syncSegment.Take(takeCount);
  252. }
  253. await AssertSorted(asyncSegment, syncSegment);
  254. }
  255. [Fact]
  256. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation()
  257. {
  258. var rand = new Random(42);
  259. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  260. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3));
  261. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  262. await AssertSorted(asyncRes, syncRes);
  263. var asyncSegment = asyncRes.AsAsyncEnumerable();
  264. var syncSegment = syncRes.AsEnumerable();
  265. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  266. {
  267. asyncSegment = asyncSegment.Skip(skipCount);
  268. syncSegment = syncSegment.Skip(skipCount);
  269. }
  270. foreach (var takeCount in new[] { 31, 29, 23 })
  271. {
  272. asyncSegment = asyncSegment.Take(takeCount);
  273. syncSegment = syncSegment.Take(takeCount);
  274. }
  275. await AssertSorted(asyncSegment, syncSegment);
  276. }
  277. [Fact]
  278. public async Task OrderBy_OrderByAsync_ThenBy()
  279. {
  280. var rand = new Random(42);
  281. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  282. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3);
  283. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  284. await AssertSorted(asyncRes, syncRes);
  285. var asyncSegment = asyncRes.AsAsyncEnumerable();
  286. var syncSegment = syncRes.AsEnumerable();
  287. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  288. {
  289. asyncSegment = asyncSegment.Skip(skipCount);
  290. syncSegment = syncSegment.Skip(skipCount);
  291. }
  292. foreach (var takeCount in new[] { 31, 29, 23 })
  293. {
  294. asyncSegment = asyncSegment.Take(takeCount);
  295. syncSegment = syncSegment.Take(takeCount);
  296. }
  297. await AssertSorted(asyncSegment, syncSegment);
  298. }
  299. [Fact]
  300. public async Task OrderBy_OrderByAsync_ThenByAsync()
  301. {
  302. var rand = new Random(42);
  303. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  304. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3));
  305. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  306. await AssertSorted(asyncRes, syncRes);
  307. var asyncSegment = asyncRes.AsAsyncEnumerable();
  308. var syncSegment = syncRes.AsEnumerable();
  309. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  310. {
  311. asyncSegment = asyncSegment.Skip(skipCount);
  312. syncSegment = syncSegment.Skip(skipCount);
  313. }
  314. foreach (var takeCount in new[] { 31, 29, 23 })
  315. {
  316. asyncSegment = asyncSegment.Take(takeCount);
  317. syncSegment = syncSegment.Take(takeCount);
  318. }
  319. await AssertSorted(asyncSegment, syncSegment);
  320. }
  321. [Fact]
  322. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation()
  323. {
  324. var rand = new Random(42);
  325. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  326. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3));
  327. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  328. await AssertSorted(asyncRes, syncRes);
  329. var asyncSegment = asyncRes.AsAsyncEnumerable();
  330. var syncSegment = syncRes.AsEnumerable();
  331. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  332. {
  333. asyncSegment = asyncSegment.Skip(skipCount);
  334. syncSegment = syncSegment.Skip(skipCount);
  335. }
  336. foreach (var takeCount in new[] { 31, 29, 23 })
  337. {
  338. asyncSegment = asyncSegment.Take(takeCount);
  339. syncSegment = syncSegment.Take(takeCount);
  340. }
  341. await AssertSorted(asyncSegment, syncSegment);
  342. }
  343. [Fact]
  344. public async Task OrderBy_OrderByAsync_ThenByDescending()
  345. {
  346. var rand = new Random(42);
  347. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  348. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3);
  349. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  350. await AssertSorted(asyncRes, syncRes);
  351. var asyncSegment = asyncRes.AsAsyncEnumerable();
  352. var syncSegment = syncRes.AsEnumerable();
  353. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  354. {
  355. asyncSegment = asyncSegment.Skip(skipCount);
  356. syncSegment = syncSegment.Skip(skipCount);
  357. }
  358. foreach (var takeCount in new[] { 31, 29, 23 })
  359. {
  360. asyncSegment = asyncSegment.Take(takeCount);
  361. syncSegment = syncSegment.Take(takeCount);
  362. }
  363. await AssertSorted(asyncSegment, syncSegment);
  364. }
  365. [Fact]
  366. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync()
  367. {
  368. var rand = new Random(42);
  369. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  370. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3));
  371. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  372. await AssertSorted(asyncRes, syncRes);
  373. var asyncSegment = asyncRes.AsAsyncEnumerable();
  374. var syncSegment = syncRes.AsEnumerable();
  375. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  376. {
  377. asyncSegment = asyncSegment.Skip(skipCount);
  378. syncSegment = syncSegment.Skip(skipCount);
  379. }
  380. foreach (var takeCount in new[] { 31, 29, 23 })
  381. {
  382. asyncSegment = asyncSegment.Take(takeCount);
  383. syncSegment = syncSegment.Take(takeCount);
  384. }
  385. await AssertSorted(asyncSegment, syncSegment);
  386. }
  387. [Fact]
  388. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation()
  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 => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3));
  393. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  394. await AssertSorted(asyncRes, syncRes);
  395. var asyncSegment = asyncRes.AsAsyncEnumerable();
  396. var syncSegment = syncRes.AsEnumerable();
  397. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  398. {
  399. asyncSegment = asyncSegment.Skip(skipCount);
  400. syncSegment = syncSegment.Skip(skipCount);
  401. }
  402. foreach (var takeCount in new[] { 31, 29, 23 })
  403. {
  404. asyncSegment = asyncSegment.Take(takeCount);
  405. syncSegment = syncSegment.Take(takeCount);
  406. }
  407. await AssertSorted(asyncSegment, syncSegment);
  408. }
  409. [Fact]
  410. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy()
  411. {
  412. var rand = new Random(42);
  413. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  414. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3);
  415. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  416. await AssertSorted(asyncRes, syncRes);
  417. var asyncSegment = asyncRes.AsAsyncEnumerable();
  418. var syncSegment = syncRes.AsEnumerable();
  419. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  420. {
  421. asyncSegment = asyncSegment.Skip(skipCount);
  422. syncSegment = syncSegment.Skip(skipCount);
  423. }
  424. foreach (var takeCount in new[] { 31, 29, 23 })
  425. {
  426. asyncSegment = asyncSegment.Take(takeCount);
  427. syncSegment = syncSegment.Take(takeCount);
  428. }
  429. await AssertSorted(asyncSegment, syncSegment);
  430. }
  431. [Fact]
  432. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync()
  433. {
  434. var rand = new Random(42);
  435. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  436. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3));
  437. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  438. await AssertSorted(asyncRes, syncRes);
  439. var asyncSegment = asyncRes.AsAsyncEnumerable();
  440. var syncSegment = syncRes.AsEnumerable();
  441. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  442. {
  443. asyncSegment = asyncSegment.Skip(skipCount);
  444. syncSegment = syncSegment.Skip(skipCount);
  445. }
  446. foreach (var takeCount in new[] { 31, 29, 23 })
  447. {
  448. asyncSegment = asyncSegment.Take(takeCount);
  449. syncSegment = syncSegment.Take(takeCount);
  450. }
  451. await AssertSorted(asyncSegment, syncSegment);
  452. }
  453. [Fact]
  454. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation()
  455. {
  456. var rand = new Random(42);
  457. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  458. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3));
  459. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3);
  460. await AssertSorted(asyncRes, syncRes);
  461. var asyncSegment = asyncRes.AsAsyncEnumerable();
  462. var syncSegment = syncRes.AsEnumerable();
  463. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  464. {
  465. asyncSegment = asyncSegment.Skip(skipCount);
  466. syncSegment = syncSegment.Skip(skipCount);
  467. }
  468. foreach (var takeCount in new[] { 31, 29, 23 })
  469. {
  470. asyncSegment = asyncSegment.Take(takeCount);
  471. syncSegment = syncSegment.Take(takeCount);
  472. }
  473. await AssertSorted(asyncSegment, syncSegment);
  474. }
  475. [Fact]
  476. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending()
  477. {
  478. var rand = new Random(42);
  479. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  480. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3);
  481. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  482. await AssertSorted(asyncRes, syncRes);
  483. var asyncSegment = asyncRes.AsAsyncEnumerable();
  484. var syncSegment = syncRes.AsEnumerable();
  485. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  486. {
  487. asyncSegment = asyncSegment.Skip(skipCount);
  488. syncSegment = syncSegment.Skip(skipCount);
  489. }
  490. foreach (var takeCount in new[] { 31, 29, 23 })
  491. {
  492. asyncSegment = asyncSegment.Take(takeCount);
  493. syncSegment = syncSegment.Take(takeCount);
  494. }
  495. await AssertSorted(asyncSegment, syncSegment);
  496. }
  497. [Fact]
  498. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync()
  499. {
  500. var rand = new Random(42);
  501. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  502. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3));
  503. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  504. await AssertSorted(asyncRes, syncRes);
  505. var asyncSegment = asyncRes.AsAsyncEnumerable();
  506. var syncSegment = syncRes.AsEnumerable();
  507. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  508. {
  509. asyncSegment = asyncSegment.Skip(skipCount);
  510. syncSegment = syncSegment.Skip(skipCount);
  511. }
  512. foreach (var takeCount in new[] { 31, 29, 23 })
  513. {
  514. asyncSegment = asyncSegment.Take(takeCount);
  515. syncSegment = syncSegment.Take(takeCount);
  516. }
  517. await AssertSorted(asyncSegment, syncSegment);
  518. }
  519. [Fact]
  520. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  521. {
  522. var rand = new Random(42);
  523. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  524. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3));
  525. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3);
  526. await AssertSorted(asyncRes, syncRes);
  527. var asyncSegment = asyncRes.AsAsyncEnumerable();
  528. var syncSegment = syncRes.AsEnumerable();
  529. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  530. {
  531. asyncSegment = asyncSegment.Skip(skipCount);
  532. syncSegment = syncSegment.Skip(skipCount);
  533. }
  534. foreach (var takeCount in new[] { 31, 29, 23 })
  535. {
  536. asyncSegment = asyncSegment.Take(takeCount);
  537. syncSegment = syncSegment.Take(takeCount);
  538. }
  539. await AssertSorted(asyncSegment, syncSegment);
  540. }
  541. [Fact]
  542. public async Task OrderBy_OrderByDescending_ThenBy()
  543. {
  544. var rand = new Random(42);
  545. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  546. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  547. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  548. await AssertSorted(asyncRes, syncRes);
  549. var asyncSegment = asyncRes.AsAsyncEnumerable();
  550. var syncSegment = syncRes.AsEnumerable();
  551. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  552. {
  553. asyncSegment = asyncSegment.Skip(skipCount);
  554. syncSegment = syncSegment.Skip(skipCount);
  555. }
  556. foreach (var takeCount in new[] { 31, 29, 23 })
  557. {
  558. asyncSegment = asyncSegment.Take(takeCount);
  559. syncSegment = syncSegment.Take(takeCount);
  560. }
  561. await AssertSorted(asyncSegment, syncSegment);
  562. }
  563. [Fact]
  564. public async Task OrderBy_OrderByDescending_ThenByAsync()
  565. {
  566. var rand = new Random(42);
  567. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  568. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3));
  569. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  570. await AssertSorted(asyncRes, syncRes);
  571. var asyncSegment = asyncRes.AsAsyncEnumerable();
  572. var syncSegment = syncRes.AsEnumerable();
  573. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  574. {
  575. asyncSegment = asyncSegment.Skip(skipCount);
  576. syncSegment = syncSegment.Skip(skipCount);
  577. }
  578. foreach (var takeCount in new[] { 31, 29, 23 })
  579. {
  580. asyncSegment = asyncSegment.Take(takeCount);
  581. syncSegment = syncSegment.Take(takeCount);
  582. }
  583. await AssertSorted(asyncSegment, syncSegment);
  584. }
  585. [Fact]
  586. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation()
  587. {
  588. var rand = new Random(42);
  589. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  590. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3));
  591. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  592. await AssertSorted(asyncRes, syncRes);
  593. var asyncSegment = asyncRes.AsAsyncEnumerable();
  594. var syncSegment = syncRes.AsEnumerable();
  595. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  596. {
  597. asyncSegment = asyncSegment.Skip(skipCount);
  598. syncSegment = syncSegment.Skip(skipCount);
  599. }
  600. foreach (var takeCount in new[] { 31, 29, 23 })
  601. {
  602. asyncSegment = asyncSegment.Take(takeCount);
  603. syncSegment = syncSegment.Take(takeCount);
  604. }
  605. await AssertSorted(asyncSegment, syncSegment);
  606. }
  607. [Fact]
  608. public async Task OrderBy_OrderByDescending_ThenByDescending()
  609. {
  610. var rand = new Random(42);
  611. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  612. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  613. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  614. await AssertSorted(asyncRes, syncRes);
  615. var asyncSegment = asyncRes.AsAsyncEnumerable();
  616. var syncSegment = syncRes.AsEnumerable();
  617. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  618. {
  619. asyncSegment = asyncSegment.Skip(skipCount);
  620. syncSegment = syncSegment.Skip(skipCount);
  621. }
  622. foreach (var takeCount in new[] { 31, 29, 23 })
  623. {
  624. asyncSegment = asyncSegment.Take(takeCount);
  625. syncSegment = syncSegment.Take(takeCount);
  626. }
  627. await AssertSorted(asyncSegment, syncSegment);
  628. }
  629. [Fact]
  630. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync()
  631. {
  632. var rand = new Random(42);
  633. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  634. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3));
  635. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  636. await AssertSorted(asyncRes, syncRes);
  637. var asyncSegment = asyncRes.AsAsyncEnumerable();
  638. var syncSegment = syncRes.AsEnumerable();
  639. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  640. {
  641. asyncSegment = asyncSegment.Skip(skipCount);
  642. syncSegment = syncSegment.Skip(skipCount);
  643. }
  644. foreach (var takeCount in new[] { 31, 29, 23 })
  645. {
  646. asyncSegment = asyncSegment.Take(takeCount);
  647. syncSegment = syncSegment.Take(takeCount);
  648. }
  649. await AssertSorted(asyncSegment, syncSegment);
  650. }
  651. [Fact]
  652. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation()
  653. {
  654. var rand = new Random(42);
  655. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  656. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3));
  657. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  658. await AssertSorted(asyncRes, syncRes);
  659. var asyncSegment = asyncRes.AsAsyncEnumerable();
  660. var syncSegment = syncRes.AsEnumerable();
  661. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  662. {
  663. asyncSegment = asyncSegment.Skip(skipCount);
  664. syncSegment = syncSegment.Skip(skipCount);
  665. }
  666. foreach (var takeCount in new[] { 31, 29, 23 })
  667. {
  668. asyncSegment = asyncSegment.Take(takeCount);
  669. syncSegment = syncSegment.Take(takeCount);
  670. }
  671. await AssertSorted(asyncSegment, syncSegment);
  672. }
  673. [Fact]
  674. public async Task OrderBy_OrderByDescendingAsync_ThenBy()
  675. {
  676. var rand = new Random(42);
  677. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  678. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3);
  679. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  680. await AssertSorted(asyncRes, syncRes);
  681. var asyncSegment = asyncRes.AsAsyncEnumerable();
  682. var syncSegment = syncRes.AsEnumerable();
  683. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  684. {
  685. asyncSegment = asyncSegment.Skip(skipCount);
  686. syncSegment = syncSegment.Skip(skipCount);
  687. }
  688. foreach (var takeCount in new[] { 31, 29, 23 })
  689. {
  690. asyncSegment = asyncSegment.Take(takeCount);
  691. syncSegment = syncSegment.Take(takeCount);
  692. }
  693. await AssertSorted(asyncSegment, syncSegment);
  694. }
  695. [Fact]
  696. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync()
  697. {
  698. var rand = new Random(42);
  699. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  700. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3));
  701. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  702. await AssertSorted(asyncRes, syncRes);
  703. var asyncSegment = asyncRes.AsAsyncEnumerable();
  704. var syncSegment = syncRes.AsEnumerable();
  705. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  706. {
  707. asyncSegment = asyncSegment.Skip(skipCount);
  708. syncSegment = syncSegment.Skip(skipCount);
  709. }
  710. foreach (var takeCount in new[] { 31, 29, 23 })
  711. {
  712. asyncSegment = asyncSegment.Take(takeCount);
  713. syncSegment = syncSegment.Take(takeCount);
  714. }
  715. await AssertSorted(asyncSegment, syncSegment);
  716. }
  717. [Fact]
  718. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation()
  719. {
  720. var rand = new Random(42);
  721. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  722. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3));
  723. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  724. await AssertSorted(asyncRes, syncRes);
  725. var asyncSegment = asyncRes.AsAsyncEnumerable();
  726. var syncSegment = syncRes.AsEnumerable();
  727. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  728. {
  729. asyncSegment = asyncSegment.Skip(skipCount);
  730. syncSegment = syncSegment.Skip(skipCount);
  731. }
  732. foreach (var takeCount in new[] { 31, 29, 23 })
  733. {
  734. asyncSegment = asyncSegment.Take(takeCount);
  735. syncSegment = syncSegment.Take(takeCount);
  736. }
  737. await AssertSorted(asyncSegment, syncSegment);
  738. }
  739. [Fact]
  740. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending()
  741. {
  742. var rand = new Random(42);
  743. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  744. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3);
  745. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  746. await AssertSorted(asyncRes, syncRes);
  747. var asyncSegment = asyncRes.AsAsyncEnumerable();
  748. var syncSegment = syncRes.AsEnumerable();
  749. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  750. {
  751. asyncSegment = asyncSegment.Skip(skipCount);
  752. syncSegment = syncSegment.Skip(skipCount);
  753. }
  754. foreach (var takeCount in new[] { 31, 29, 23 })
  755. {
  756. asyncSegment = asyncSegment.Take(takeCount);
  757. syncSegment = syncSegment.Take(takeCount);
  758. }
  759. await AssertSorted(asyncSegment, syncSegment);
  760. }
  761. [Fact]
  762. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync()
  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().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3));
  767. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  768. await AssertSorted(asyncRes, syncRes);
  769. var asyncSegment = asyncRes.AsAsyncEnumerable();
  770. var syncSegment = syncRes.AsEnumerable();
  771. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  772. {
  773. asyncSegment = asyncSegment.Skip(skipCount);
  774. syncSegment = syncSegment.Skip(skipCount);
  775. }
  776. foreach (var takeCount in new[] { 31, 29, 23 })
  777. {
  778. asyncSegment = asyncSegment.Take(takeCount);
  779. syncSegment = syncSegment.Take(takeCount);
  780. }
  781. await AssertSorted(asyncSegment, syncSegment);
  782. }
  783. [Fact]
  784. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  785. {
  786. var rand = new Random(42);
  787. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  788. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3));
  789. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  790. await AssertSorted(asyncRes, syncRes);
  791. var asyncSegment = asyncRes.AsAsyncEnumerable();
  792. var syncSegment = syncRes.AsEnumerable();
  793. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  794. {
  795. asyncSegment = asyncSegment.Skip(skipCount);
  796. syncSegment = syncSegment.Skip(skipCount);
  797. }
  798. foreach (var takeCount in new[] { 31, 29, 23 })
  799. {
  800. asyncSegment = asyncSegment.Take(takeCount);
  801. syncSegment = syncSegment.Take(takeCount);
  802. }
  803. await AssertSorted(asyncSegment, syncSegment);
  804. }
  805. [Fact]
  806. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy()
  807. {
  808. var rand = new Random(42);
  809. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  810. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3);
  811. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  812. await AssertSorted(asyncRes, syncRes);
  813. var asyncSegment = asyncRes.AsAsyncEnumerable();
  814. var syncSegment = syncRes.AsEnumerable();
  815. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  816. {
  817. asyncSegment = asyncSegment.Skip(skipCount);
  818. syncSegment = syncSegment.Skip(skipCount);
  819. }
  820. foreach (var takeCount in new[] { 31, 29, 23 })
  821. {
  822. asyncSegment = asyncSegment.Take(takeCount);
  823. syncSegment = syncSegment.Take(takeCount);
  824. }
  825. await AssertSorted(asyncSegment, syncSegment);
  826. }
  827. [Fact]
  828. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync()
  829. {
  830. var rand = new Random(42);
  831. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  832. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3));
  833. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  834. await AssertSorted(asyncRes, syncRes);
  835. var asyncSegment = asyncRes.AsAsyncEnumerable();
  836. var syncSegment = syncRes.AsEnumerable();
  837. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  838. {
  839. asyncSegment = asyncSegment.Skip(skipCount);
  840. syncSegment = syncSegment.Skip(skipCount);
  841. }
  842. foreach (var takeCount in new[] { 31, 29, 23 })
  843. {
  844. asyncSegment = asyncSegment.Take(takeCount);
  845. syncSegment = syncSegment.Take(takeCount);
  846. }
  847. await AssertSorted(asyncSegment, syncSegment);
  848. }
  849. [Fact]
  850. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  851. {
  852. var rand = new Random(42);
  853. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  854. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3));
  855. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3);
  856. await AssertSorted(asyncRes, syncRes);
  857. var asyncSegment = asyncRes.AsAsyncEnumerable();
  858. var syncSegment = syncRes.AsEnumerable();
  859. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  860. {
  861. asyncSegment = asyncSegment.Skip(skipCount);
  862. syncSegment = syncSegment.Skip(skipCount);
  863. }
  864. foreach (var takeCount in new[] { 31, 29, 23 })
  865. {
  866. asyncSegment = asyncSegment.Take(takeCount);
  867. syncSegment = syncSegment.Take(takeCount);
  868. }
  869. await AssertSorted(asyncSegment, syncSegment);
  870. }
  871. [Fact]
  872. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending()
  873. {
  874. var rand = new Random(42);
  875. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  876. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3);
  877. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  878. await AssertSorted(asyncRes, syncRes);
  879. var asyncSegment = asyncRes.AsAsyncEnumerable();
  880. var syncSegment = syncRes.AsEnumerable();
  881. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  882. {
  883. asyncSegment = asyncSegment.Skip(skipCount);
  884. syncSegment = syncSegment.Skip(skipCount);
  885. }
  886. foreach (var takeCount in new[] { 31, 29, 23 })
  887. {
  888. asyncSegment = asyncSegment.Take(takeCount);
  889. syncSegment = syncSegment.Take(takeCount);
  890. }
  891. await AssertSorted(asyncSegment, syncSegment);
  892. }
  893. [Fact]
  894. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  895. {
  896. var rand = new Random(42);
  897. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  898. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3));
  899. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  900. await AssertSorted(asyncRes, syncRes);
  901. var asyncSegment = asyncRes.AsAsyncEnumerable();
  902. var syncSegment = syncRes.AsEnumerable();
  903. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  904. {
  905. asyncSegment = asyncSegment.Skip(skipCount);
  906. syncSegment = syncSegment.Skip(skipCount);
  907. }
  908. foreach (var takeCount in new[] { 31, 29, 23 })
  909. {
  910. asyncSegment = asyncSegment.Take(takeCount);
  911. syncSegment = syncSegment.Take(takeCount);
  912. }
  913. await AssertSorted(asyncSegment, syncSegment);
  914. }
  915. [Fact]
  916. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  917. {
  918. var rand = new Random(42);
  919. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  920. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3));
  921. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3);
  922. await AssertSorted(asyncRes, syncRes);
  923. var asyncSegment = asyncRes.AsAsyncEnumerable();
  924. var syncSegment = syncRes.AsEnumerable();
  925. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  926. {
  927. asyncSegment = asyncSegment.Skip(skipCount);
  928. syncSegment = syncSegment.Skip(skipCount);
  929. }
  930. foreach (var takeCount in new[] { 31, 29, 23 })
  931. {
  932. asyncSegment = asyncSegment.Take(takeCount);
  933. syncSegment = syncSegment.Take(takeCount);
  934. }
  935. await AssertSorted(asyncSegment, syncSegment);
  936. }
  937. [Fact]
  938. public async Task OrderBy_OrderBy_ThenBy_ThenBy()
  939. {
  940. var rand = new Random(42);
  941. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  942. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  943. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  944. await AssertSorted(asyncRes, syncRes);
  945. var asyncSegment = asyncRes.AsAsyncEnumerable();
  946. var syncSegment = syncRes.AsEnumerable();
  947. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  948. {
  949. asyncSegment = asyncSegment.Skip(skipCount);
  950. syncSegment = syncSegment.Skip(skipCount);
  951. }
  952. foreach (var takeCount in new[] { 31, 29, 23 })
  953. {
  954. asyncSegment = asyncSegment.Take(takeCount);
  955. syncSegment = syncSegment.Take(takeCount);
  956. }
  957. await AssertSorted(asyncSegment, syncSegment);
  958. }
  959. [Fact]
  960. public async Task OrderBy_OrderBy_ThenBy_ThenByAsync()
  961. {
  962. var rand = new Random(42);
  963. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  964. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  965. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  966. await AssertSorted(asyncRes, syncRes);
  967. var asyncSegment = asyncRes.AsAsyncEnumerable();
  968. var syncSegment = syncRes.AsEnumerable();
  969. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  970. {
  971. asyncSegment = asyncSegment.Skip(skipCount);
  972. syncSegment = syncSegment.Skip(skipCount);
  973. }
  974. foreach (var takeCount in new[] { 31, 29, 23 })
  975. {
  976. asyncSegment = asyncSegment.Take(takeCount);
  977. syncSegment = syncSegment.Take(takeCount);
  978. }
  979. await AssertSorted(asyncSegment, syncSegment);
  980. }
  981. [Fact]
  982. public async Task OrderBy_OrderBy_ThenBy_ThenByAsyncWithCancellation()
  983. {
  984. var rand = new Random(42);
  985. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  986. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  987. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  988. await AssertSorted(asyncRes, syncRes);
  989. var asyncSegment = asyncRes.AsAsyncEnumerable();
  990. var syncSegment = syncRes.AsEnumerable();
  991. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  992. {
  993. asyncSegment = asyncSegment.Skip(skipCount);
  994. syncSegment = syncSegment.Skip(skipCount);
  995. }
  996. foreach (var takeCount in new[] { 31, 29, 23 })
  997. {
  998. asyncSegment = asyncSegment.Take(takeCount);
  999. syncSegment = syncSegment.Take(takeCount);
  1000. }
  1001. await AssertSorted(asyncSegment, syncSegment);
  1002. }
  1003. [Fact]
  1004. public async Task OrderBy_OrderBy_ThenBy_ThenByDescending()
  1005. {
  1006. var rand = new Random(42);
  1007. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1008. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1009. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1010. await AssertSorted(asyncRes, syncRes);
  1011. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1012. var syncSegment = syncRes.AsEnumerable();
  1013. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1014. {
  1015. asyncSegment = asyncSegment.Skip(skipCount);
  1016. syncSegment = syncSegment.Skip(skipCount);
  1017. }
  1018. foreach (var takeCount in new[] { 31, 29, 23 })
  1019. {
  1020. asyncSegment = asyncSegment.Take(takeCount);
  1021. syncSegment = syncSegment.Take(takeCount);
  1022. }
  1023. await AssertSorted(asyncSegment, syncSegment);
  1024. }
  1025. [Fact]
  1026. public async Task OrderBy_OrderBy_ThenBy_ThenByDescendingAsync()
  1027. {
  1028. var rand = new Random(42);
  1029. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1030. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  1031. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1032. await AssertSorted(asyncRes, syncRes);
  1033. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1034. var syncSegment = syncRes.AsEnumerable();
  1035. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1036. {
  1037. asyncSegment = asyncSegment.Skip(skipCount);
  1038. syncSegment = syncSegment.Skip(skipCount);
  1039. }
  1040. foreach (var takeCount in new[] { 31, 29, 23 })
  1041. {
  1042. asyncSegment = asyncSegment.Take(takeCount);
  1043. syncSegment = syncSegment.Take(takeCount);
  1044. }
  1045. await AssertSorted(asyncSegment, syncSegment);
  1046. }
  1047. [Fact]
  1048. public async Task OrderBy_OrderBy_ThenBy_ThenByDescendingAsyncWithCancellation()
  1049. {
  1050. var rand = new Random(42);
  1051. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1052. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1053. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1054. await AssertSorted(asyncRes, syncRes);
  1055. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1056. var syncSegment = syncRes.AsEnumerable();
  1057. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1058. {
  1059. asyncSegment = asyncSegment.Skip(skipCount);
  1060. syncSegment = syncSegment.Skip(skipCount);
  1061. }
  1062. foreach (var takeCount in new[] { 31, 29, 23 })
  1063. {
  1064. asyncSegment = asyncSegment.Take(takeCount);
  1065. syncSegment = syncSegment.Take(takeCount);
  1066. }
  1067. await AssertSorted(asyncSegment, syncSegment);
  1068. }
  1069. [Fact]
  1070. public async Task OrderBy_OrderBy_ThenByAsync_ThenBy()
  1071. {
  1072. var rand = new Random(42);
  1073. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1074. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  1075. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1076. await AssertSorted(asyncRes, syncRes);
  1077. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1078. var syncSegment = syncRes.AsEnumerable();
  1079. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1080. {
  1081. asyncSegment = asyncSegment.Skip(skipCount);
  1082. syncSegment = syncSegment.Skip(skipCount);
  1083. }
  1084. foreach (var takeCount in new[] { 31, 29, 23 })
  1085. {
  1086. asyncSegment = asyncSegment.Take(takeCount);
  1087. syncSegment = syncSegment.Take(takeCount);
  1088. }
  1089. await AssertSorted(asyncSegment, syncSegment);
  1090. }
  1091. [Fact]
  1092. public async Task OrderBy_OrderBy_ThenByAsync_ThenByAsync()
  1093. {
  1094. var rand = new Random(42);
  1095. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1096. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  1097. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1098. await AssertSorted(asyncRes, syncRes);
  1099. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1100. var syncSegment = syncRes.AsEnumerable();
  1101. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1102. {
  1103. asyncSegment = asyncSegment.Skip(skipCount);
  1104. syncSegment = syncSegment.Skip(skipCount);
  1105. }
  1106. foreach (var takeCount in new[] { 31, 29, 23 })
  1107. {
  1108. asyncSegment = asyncSegment.Take(takeCount);
  1109. syncSegment = syncSegment.Take(takeCount);
  1110. }
  1111. await AssertSorted(asyncSegment, syncSegment);
  1112. }
  1113. [Fact]
  1114. public async Task OrderBy_OrderBy_ThenByAsync_ThenByAsyncWithCancellation()
  1115. {
  1116. var rand = new Random(42);
  1117. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1118. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1119. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1120. await AssertSorted(asyncRes, syncRes);
  1121. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1122. var syncSegment = syncRes.AsEnumerable();
  1123. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1124. {
  1125. asyncSegment = asyncSegment.Skip(skipCount);
  1126. syncSegment = syncSegment.Skip(skipCount);
  1127. }
  1128. foreach (var takeCount in new[] { 31, 29, 23 })
  1129. {
  1130. asyncSegment = asyncSegment.Take(takeCount);
  1131. syncSegment = syncSegment.Take(takeCount);
  1132. }
  1133. await AssertSorted(asyncSegment, syncSegment);
  1134. }
  1135. [Fact]
  1136. public async Task OrderBy_OrderBy_ThenByAsync_ThenByDescending()
  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).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  1141. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1142. await AssertSorted(asyncRes, syncRes);
  1143. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1144. var syncSegment = syncRes.AsEnumerable();
  1145. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1146. {
  1147. asyncSegment = asyncSegment.Skip(skipCount);
  1148. syncSegment = syncSegment.Skip(skipCount);
  1149. }
  1150. foreach (var takeCount in new[] { 31, 29, 23 })
  1151. {
  1152. asyncSegment = asyncSegment.Take(takeCount);
  1153. syncSegment = syncSegment.Take(takeCount);
  1154. }
  1155. await AssertSorted(asyncSegment, syncSegment);
  1156. }
  1157. [Fact]
  1158. public async Task OrderBy_OrderBy_ThenByAsync_ThenByDescendingAsync()
  1159. {
  1160. var rand = new Random(42);
  1161. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1162. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  1163. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1164. await AssertSorted(asyncRes, syncRes);
  1165. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1166. var syncSegment = syncRes.AsEnumerable();
  1167. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1168. {
  1169. asyncSegment = asyncSegment.Skip(skipCount);
  1170. syncSegment = syncSegment.Skip(skipCount);
  1171. }
  1172. foreach (var takeCount in new[] { 31, 29, 23 })
  1173. {
  1174. asyncSegment = asyncSegment.Take(takeCount);
  1175. syncSegment = syncSegment.Take(takeCount);
  1176. }
  1177. await AssertSorted(asyncSegment, syncSegment);
  1178. }
  1179. [Fact]
  1180. public async Task OrderBy_OrderBy_ThenByAsync_ThenByDescendingAsyncWithCancellation()
  1181. {
  1182. var rand = new Random(42);
  1183. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1184. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1185. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1186. await AssertSorted(asyncRes, syncRes);
  1187. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1188. var syncSegment = syncRes.AsEnumerable();
  1189. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1190. {
  1191. asyncSegment = asyncSegment.Skip(skipCount);
  1192. syncSegment = syncSegment.Skip(skipCount);
  1193. }
  1194. foreach (var takeCount in new[] { 31, 29, 23 })
  1195. {
  1196. asyncSegment = asyncSegment.Take(takeCount);
  1197. syncSegment = syncSegment.Take(takeCount);
  1198. }
  1199. await AssertSorted(asyncSegment, syncSegment);
  1200. }
  1201. [Fact]
  1202. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation_ThenBy()
  1203. {
  1204. var rand = new Random(42);
  1205. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1206. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  1207. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1208. await AssertSorted(asyncRes, syncRes);
  1209. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1210. var syncSegment = syncRes.AsEnumerable();
  1211. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1212. {
  1213. asyncSegment = asyncSegment.Skip(skipCount);
  1214. syncSegment = syncSegment.Skip(skipCount);
  1215. }
  1216. foreach (var takeCount in new[] { 31, 29, 23 })
  1217. {
  1218. asyncSegment = asyncSegment.Take(takeCount);
  1219. syncSegment = syncSegment.Take(takeCount);
  1220. }
  1221. await AssertSorted(asyncSegment, syncSegment);
  1222. }
  1223. [Fact]
  1224. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation_ThenByAsync()
  1225. {
  1226. var rand = new Random(42);
  1227. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1228. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  1229. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1230. await AssertSorted(asyncRes, syncRes);
  1231. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1232. var syncSegment = syncRes.AsEnumerable();
  1233. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1234. {
  1235. asyncSegment = asyncSegment.Skip(skipCount);
  1236. syncSegment = syncSegment.Skip(skipCount);
  1237. }
  1238. foreach (var takeCount in new[] { 31, 29, 23 })
  1239. {
  1240. asyncSegment = asyncSegment.Take(takeCount);
  1241. syncSegment = syncSegment.Take(takeCount);
  1242. }
  1243. await AssertSorted(asyncSegment, syncSegment);
  1244. }
  1245. [Fact]
  1246. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation_ThenByAsyncWithCancellation()
  1247. {
  1248. var rand = new Random(42);
  1249. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1250. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1251. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1252. await AssertSorted(asyncRes, syncRes);
  1253. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1254. var syncSegment = syncRes.AsEnumerable();
  1255. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1256. {
  1257. asyncSegment = asyncSegment.Skip(skipCount);
  1258. syncSegment = syncSegment.Skip(skipCount);
  1259. }
  1260. foreach (var takeCount in new[] { 31, 29, 23 })
  1261. {
  1262. asyncSegment = asyncSegment.Take(takeCount);
  1263. syncSegment = syncSegment.Take(takeCount);
  1264. }
  1265. await AssertSorted(asyncSegment, syncSegment);
  1266. }
  1267. [Fact]
  1268. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation_ThenByDescending()
  1269. {
  1270. var rand = new Random(42);
  1271. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1272. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  1273. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1274. await AssertSorted(asyncRes, syncRes);
  1275. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1276. var syncSegment = syncRes.AsEnumerable();
  1277. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1278. {
  1279. asyncSegment = asyncSegment.Skip(skipCount);
  1280. syncSegment = syncSegment.Skip(skipCount);
  1281. }
  1282. foreach (var takeCount in new[] { 31, 29, 23 })
  1283. {
  1284. asyncSegment = asyncSegment.Take(takeCount);
  1285. syncSegment = syncSegment.Take(takeCount);
  1286. }
  1287. await AssertSorted(asyncSegment, syncSegment);
  1288. }
  1289. [Fact]
  1290. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation_ThenByDescendingAsync()
  1291. {
  1292. var rand = new Random(42);
  1293. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1294. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  1295. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1296. await AssertSorted(asyncRes, syncRes);
  1297. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1298. var syncSegment = syncRes.AsEnumerable();
  1299. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1300. {
  1301. asyncSegment = asyncSegment.Skip(skipCount);
  1302. syncSegment = syncSegment.Skip(skipCount);
  1303. }
  1304. foreach (var takeCount in new[] { 31, 29, 23 })
  1305. {
  1306. asyncSegment = asyncSegment.Take(takeCount);
  1307. syncSegment = syncSegment.Take(takeCount);
  1308. }
  1309. await AssertSorted(asyncSegment, syncSegment);
  1310. }
  1311. [Fact]
  1312. public async Task OrderBy_OrderBy_ThenByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  1313. {
  1314. var rand = new Random(42);
  1315. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1316. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1317. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1318. await AssertSorted(asyncRes, syncRes);
  1319. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1320. var syncSegment = syncRes.AsEnumerable();
  1321. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1322. {
  1323. asyncSegment = asyncSegment.Skip(skipCount);
  1324. syncSegment = syncSegment.Skip(skipCount);
  1325. }
  1326. foreach (var takeCount in new[] { 31, 29, 23 })
  1327. {
  1328. asyncSegment = asyncSegment.Take(takeCount);
  1329. syncSegment = syncSegment.Take(takeCount);
  1330. }
  1331. await AssertSorted(asyncSegment, syncSegment);
  1332. }
  1333. [Fact]
  1334. public async Task OrderBy_OrderBy_ThenByDescending_ThenBy()
  1335. {
  1336. var rand = new Random(42);
  1337. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1338. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1339. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1340. await AssertSorted(asyncRes, syncRes);
  1341. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1342. var syncSegment = syncRes.AsEnumerable();
  1343. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1344. {
  1345. asyncSegment = asyncSegment.Skip(skipCount);
  1346. syncSegment = syncSegment.Skip(skipCount);
  1347. }
  1348. foreach (var takeCount in new[] { 31, 29, 23 })
  1349. {
  1350. asyncSegment = asyncSegment.Take(takeCount);
  1351. syncSegment = syncSegment.Take(takeCount);
  1352. }
  1353. await AssertSorted(asyncSegment, syncSegment);
  1354. }
  1355. [Fact]
  1356. public async Task OrderBy_OrderBy_ThenByDescending_ThenByAsync()
  1357. {
  1358. var rand = new Random(42);
  1359. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1360. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  1361. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1362. await AssertSorted(asyncRes, syncRes);
  1363. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1364. var syncSegment = syncRes.AsEnumerable();
  1365. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1366. {
  1367. asyncSegment = asyncSegment.Skip(skipCount);
  1368. syncSegment = syncSegment.Skip(skipCount);
  1369. }
  1370. foreach (var takeCount in new[] { 31, 29, 23 })
  1371. {
  1372. asyncSegment = asyncSegment.Take(takeCount);
  1373. syncSegment = syncSegment.Take(takeCount);
  1374. }
  1375. await AssertSorted(asyncSegment, syncSegment);
  1376. }
  1377. [Fact]
  1378. public async Task OrderBy_OrderBy_ThenByDescending_ThenByAsyncWithCancellation()
  1379. {
  1380. var rand = new Random(42);
  1381. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1382. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1383. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1384. await AssertSorted(asyncRes, syncRes);
  1385. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1386. var syncSegment = syncRes.AsEnumerable();
  1387. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1388. {
  1389. asyncSegment = asyncSegment.Skip(skipCount);
  1390. syncSegment = syncSegment.Skip(skipCount);
  1391. }
  1392. foreach (var takeCount in new[] { 31, 29, 23 })
  1393. {
  1394. asyncSegment = asyncSegment.Take(takeCount);
  1395. syncSegment = syncSegment.Take(takeCount);
  1396. }
  1397. await AssertSorted(asyncSegment, syncSegment);
  1398. }
  1399. [Fact]
  1400. public async Task OrderBy_OrderBy_ThenByDescending_ThenByDescending()
  1401. {
  1402. var rand = new Random(42);
  1403. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1404. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1405. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1406. await AssertSorted(asyncRes, syncRes);
  1407. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1408. var syncSegment = syncRes.AsEnumerable();
  1409. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1410. {
  1411. asyncSegment = asyncSegment.Skip(skipCount);
  1412. syncSegment = syncSegment.Skip(skipCount);
  1413. }
  1414. foreach (var takeCount in new[] { 31, 29, 23 })
  1415. {
  1416. asyncSegment = asyncSegment.Take(takeCount);
  1417. syncSegment = syncSegment.Take(takeCount);
  1418. }
  1419. await AssertSorted(asyncSegment, syncSegment);
  1420. }
  1421. [Fact]
  1422. public async Task OrderBy_OrderBy_ThenByDescending_ThenByDescendingAsync()
  1423. {
  1424. var rand = new Random(42);
  1425. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1426. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  1427. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1428. await AssertSorted(asyncRes, syncRes);
  1429. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1430. var syncSegment = syncRes.AsEnumerable();
  1431. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1432. {
  1433. asyncSegment = asyncSegment.Skip(skipCount);
  1434. syncSegment = syncSegment.Skip(skipCount);
  1435. }
  1436. foreach (var takeCount in new[] { 31, 29, 23 })
  1437. {
  1438. asyncSegment = asyncSegment.Take(takeCount);
  1439. syncSegment = syncSegment.Take(takeCount);
  1440. }
  1441. await AssertSorted(asyncSegment, syncSegment);
  1442. }
  1443. [Fact]
  1444. public async Task OrderBy_OrderBy_ThenByDescending_ThenByDescendingAsyncWithCancellation()
  1445. {
  1446. var rand = new Random(42);
  1447. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1448. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1449. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1450. await AssertSorted(asyncRes, syncRes);
  1451. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1452. var syncSegment = syncRes.AsEnumerable();
  1453. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1454. {
  1455. asyncSegment = asyncSegment.Skip(skipCount);
  1456. syncSegment = syncSegment.Skip(skipCount);
  1457. }
  1458. foreach (var takeCount in new[] { 31, 29, 23 })
  1459. {
  1460. asyncSegment = asyncSegment.Take(takeCount);
  1461. syncSegment = syncSegment.Take(takeCount);
  1462. }
  1463. await AssertSorted(asyncSegment, syncSegment);
  1464. }
  1465. [Fact]
  1466. public async Task OrderBy_OrderBy_ThenByDescendingAsync_ThenBy()
  1467. {
  1468. var rand = new Random(42);
  1469. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1470. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  1471. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1472. await AssertSorted(asyncRes, syncRes);
  1473. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1474. var syncSegment = syncRes.AsEnumerable();
  1475. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1476. {
  1477. asyncSegment = asyncSegment.Skip(skipCount);
  1478. syncSegment = syncSegment.Skip(skipCount);
  1479. }
  1480. foreach (var takeCount in new[] { 31, 29, 23 })
  1481. {
  1482. asyncSegment = asyncSegment.Take(takeCount);
  1483. syncSegment = syncSegment.Take(takeCount);
  1484. }
  1485. await AssertSorted(asyncSegment, syncSegment);
  1486. }
  1487. [Fact]
  1488. public async Task OrderBy_OrderBy_ThenByDescendingAsync_ThenByAsync()
  1489. {
  1490. var rand = new Random(42);
  1491. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1492. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  1493. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1494. await AssertSorted(asyncRes, syncRes);
  1495. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1496. var syncSegment = syncRes.AsEnumerable();
  1497. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1498. {
  1499. asyncSegment = asyncSegment.Skip(skipCount);
  1500. syncSegment = syncSegment.Skip(skipCount);
  1501. }
  1502. foreach (var takeCount in new[] { 31, 29, 23 })
  1503. {
  1504. asyncSegment = asyncSegment.Take(takeCount);
  1505. syncSegment = syncSegment.Take(takeCount);
  1506. }
  1507. await AssertSorted(asyncSegment, syncSegment);
  1508. }
  1509. [Fact]
  1510. public async Task OrderBy_OrderBy_ThenByDescendingAsync_ThenByAsyncWithCancellation()
  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 => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1515. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1516. await AssertSorted(asyncRes, syncRes);
  1517. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1518. var syncSegment = syncRes.AsEnumerable();
  1519. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1520. {
  1521. asyncSegment = asyncSegment.Skip(skipCount);
  1522. syncSegment = syncSegment.Skip(skipCount);
  1523. }
  1524. foreach (var takeCount in new[] { 31, 29, 23 })
  1525. {
  1526. asyncSegment = asyncSegment.Take(takeCount);
  1527. syncSegment = syncSegment.Take(takeCount);
  1528. }
  1529. await AssertSorted(asyncSegment, syncSegment);
  1530. }
  1531. [Fact]
  1532. public async Task OrderBy_OrderBy_ThenByDescendingAsync_ThenByDescending()
  1533. {
  1534. var rand = new Random(42);
  1535. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1536. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  1537. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1538. await AssertSorted(asyncRes, syncRes);
  1539. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1540. var syncSegment = syncRes.AsEnumerable();
  1541. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1542. {
  1543. asyncSegment = asyncSegment.Skip(skipCount);
  1544. syncSegment = syncSegment.Skip(skipCount);
  1545. }
  1546. foreach (var takeCount in new[] { 31, 29, 23 })
  1547. {
  1548. asyncSegment = asyncSegment.Take(takeCount);
  1549. syncSegment = syncSegment.Take(takeCount);
  1550. }
  1551. await AssertSorted(asyncSegment, syncSegment);
  1552. }
  1553. [Fact]
  1554. public async Task OrderBy_OrderBy_ThenByDescendingAsync_ThenByDescendingAsync()
  1555. {
  1556. var rand = new Random(42);
  1557. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1558. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  1559. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1560. await AssertSorted(asyncRes, syncRes);
  1561. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1562. var syncSegment = syncRes.AsEnumerable();
  1563. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1564. {
  1565. asyncSegment = asyncSegment.Skip(skipCount);
  1566. syncSegment = syncSegment.Skip(skipCount);
  1567. }
  1568. foreach (var takeCount in new[] { 31, 29, 23 })
  1569. {
  1570. asyncSegment = asyncSegment.Take(takeCount);
  1571. syncSegment = syncSegment.Take(takeCount);
  1572. }
  1573. await AssertSorted(asyncSegment, syncSegment);
  1574. }
  1575. [Fact]
  1576. public async Task OrderBy_OrderBy_ThenByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  1577. {
  1578. var rand = new Random(42);
  1579. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1580. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1581. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1582. await AssertSorted(asyncRes, syncRes);
  1583. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1584. var syncSegment = syncRes.AsEnumerable();
  1585. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1586. {
  1587. asyncSegment = asyncSegment.Skip(skipCount);
  1588. syncSegment = syncSegment.Skip(skipCount);
  1589. }
  1590. foreach (var takeCount in new[] { 31, 29, 23 })
  1591. {
  1592. asyncSegment = asyncSegment.Take(takeCount);
  1593. syncSegment = syncSegment.Take(takeCount);
  1594. }
  1595. await AssertSorted(asyncSegment, syncSegment);
  1596. }
  1597. [Fact]
  1598. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation_ThenBy()
  1599. {
  1600. var rand = new Random(42);
  1601. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1602. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  1603. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1604. await AssertSorted(asyncRes, syncRes);
  1605. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1606. var syncSegment = syncRes.AsEnumerable();
  1607. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1608. {
  1609. asyncSegment = asyncSegment.Skip(skipCount);
  1610. syncSegment = syncSegment.Skip(skipCount);
  1611. }
  1612. foreach (var takeCount in new[] { 31, 29, 23 })
  1613. {
  1614. asyncSegment = asyncSegment.Take(takeCount);
  1615. syncSegment = syncSegment.Take(takeCount);
  1616. }
  1617. await AssertSorted(asyncSegment, syncSegment);
  1618. }
  1619. [Fact]
  1620. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation_ThenByAsync()
  1621. {
  1622. var rand = new Random(42);
  1623. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1624. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  1625. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1626. await AssertSorted(asyncRes, syncRes);
  1627. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1628. var syncSegment = syncRes.AsEnumerable();
  1629. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1630. {
  1631. asyncSegment = asyncSegment.Skip(skipCount);
  1632. syncSegment = syncSegment.Skip(skipCount);
  1633. }
  1634. foreach (var takeCount in new[] { 31, 29, 23 })
  1635. {
  1636. asyncSegment = asyncSegment.Take(takeCount);
  1637. syncSegment = syncSegment.Take(takeCount);
  1638. }
  1639. await AssertSorted(asyncSegment, syncSegment);
  1640. }
  1641. [Fact]
  1642. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  1643. {
  1644. var rand = new Random(42);
  1645. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1646. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1647. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  1648. await AssertSorted(asyncRes, syncRes);
  1649. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1650. var syncSegment = syncRes.AsEnumerable();
  1651. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1652. {
  1653. asyncSegment = asyncSegment.Skip(skipCount);
  1654. syncSegment = syncSegment.Skip(skipCount);
  1655. }
  1656. foreach (var takeCount in new[] { 31, 29, 23 })
  1657. {
  1658. asyncSegment = asyncSegment.Take(takeCount);
  1659. syncSegment = syncSegment.Take(takeCount);
  1660. }
  1661. await AssertSorted(asyncSegment, syncSegment);
  1662. }
  1663. [Fact]
  1664. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation_ThenByDescending()
  1665. {
  1666. var rand = new Random(42);
  1667. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1668. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  1669. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1670. await AssertSorted(asyncRes, syncRes);
  1671. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1672. var syncSegment = syncRes.AsEnumerable();
  1673. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1674. {
  1675. asyncSegment = asyncSegment.Skip(skipCount);
  1676. syncSegment = syncSegment.Skip(skipCount);
  1677. }
  1678. foreach (var takeCount in new[] { 31, 29, 23 })
  1679. {
  1680. asyncSegment = asyncSegment.Take(takeCount);
  1681. syncSegment = syncSegment.Take(takeCount);
  1682. }
  1683. await AssertSorted(asyncSegment, syncSegment);
  1684. }
  1685. [Fact]
  1686. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  1687. {
  1688. var rand = new Random(42);
  1689. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1690. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  1691. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1692. await AssertSorted(asyncRes, syncRes);
  1693. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1694. var syncSegment = syncRes.AsEnumerable();
  1695. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1696. {
  1697. asyncSegment = asyncSegment.Skip(skipCount);
  1698. syncSegment = syncSegment.Skip(skipCount);
  1699. }
  1700. foreach (var takeCount in new[] { 31, 29, 23 })
  1701. {
  1702. asyncSegment = asyncSegment.Take(takeCount);
  1703. syncSegment = syncSegment.Take(takeCount);
  1704. }
  1705. await AssertSorted(asyncSegment, syncSegment);
  1706. }
  1707. [Fact]
  1708. public async Task OrderBy_OrderBy_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  1709. {
  1710. var rand = new Random(42);
  1711. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1712. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1713. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  1714. await AssertSorted(asyncRes, syncRes);
  1715. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1716. var syncSegment = syncRes.AsEnumerable();
  1717. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1718. {
  1719. asyncSegment = asyncSegment.Skip(skipCount);
  1720. syncSegment = syncSegment.Skip(skipCount);
  1721. }
  1722. foreach (var takeCount in new[] { 31, 29, 23 })
  1723. {
  1724. asyncSegment = asyncSegment.Take(takeCount);
  1725. syncSegment = syncSegment.Take(takeCount);
  1726. }
  1727. await AssertSorted(asyncSegment, syncSegment);
  1728. }
  1729. [Fact]
  1730. public async Task OrderBy_OrderByAsync_ThenBy_ThenBy()
  1731. {
  1732. var rand = new Random(42);
  1733. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1734. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1735. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1736. await AssertSorted(asyncRes, syncRes);
  1737. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1738. var syncSegment = syncRes.AsEnumerable();
  1739. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1740. {
  1741. asyncSegment = asyncSegment.Skip(skipCount);
  1742. syncSegment = syncSegment.Skip(skipCount);
  1743. }
  1744. foreach (var takeCount in new[] { 31, 29, 23 })
  1745. {
  1746. asyncSegment = asyncSegment.Take(takeCount);
  1747. syncSegment = syncSegment.Take(takeCount);
  1748. }
  1749. await AssertSorted(asyncSegment, syncSegment);
  1750. }
  1751. [Fact]
  1752. public async Task OrderBy_OrderByAsync_ThenBy_ThenByAsync()
  1753. {
  1754. var rand = new Random(42);
  1755. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1756. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  1757. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1758. await AssertSorted(asyncRes, syncRes);
  1759. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1760. var syncSegment = syncRes.AsEnumerable();
  1761. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1762. {
  1763. asyncSegment = asyncSegment.Skip(skipCount);
  1764. syncSegment = syncSegment.Skip(skipCount);
  1765. }
  1766. foreach (var takeCount in new[] { 31, 29, 23 })
  1767. {
  1768. asyncSegment = asyncSegment.Take(takeCount);
  1769. syncSegment = syncSegment.Take(takeCount);
  1770. }
  1771. await AssertSorted(asyncSegment, syncSegment);
  1772. }
  1773. [Fact]
  1774. public async Task OrderBy_OrderByAsync_ThenBy_ThenByAsyncWithCancellation()
  1775. {
  1776. var rand = new Random(42);
  1777. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1778. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  1779. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1780. await AssertSorted(asyncRes, syncRes);
  1781. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1782. var syncSegment = syncRes.AsEnumerable();
  1783. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1784. {
  1785. asyncSegment = asyncSegment.Skip(skipCount);
  1786. syncSegment = syncSegment.Skip(skipCount);
  1787. }
  1788. foreach (var takeCount in new[] { 31, 29, 23 })
  1789. {
  1790. asyncSegment = asyncSegment.Take(takeCount);
  1791. syncSegment = syncSegment.Take(takeCount);
  1792. }
  1793. await AssertSorted(asyncSegment, syncSegment);
  1794. }
  1795. [Fact]
  1796. public async Task OrderBy_OrderByAsync_ThenBy_ThenByDescending()
  1797. {
  1798. var rand = new Random(42);
  1799. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1800. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1801. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1802. await AssertSorted(asyncRes, syncRes);
  1803. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1804. var syncSegment = syncRes.AsEnumerable();
  1805. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1806. {
  1807. asyncSegment = asyncSegment.Skip(skipCount);
  1808. syncSegment = syncSegment.Skip(skipCount);
  1809. }
  1810. foreach (var takeCount in new[] { 31, 29, 23 })
  1811. {
  1812. asyncSegment = asyncSegment.Take(takeCount);
  1813. syncSegment = syncSegment.Take(takeCount);
  1814. }
  1815. await AssertSorted(asyncSegment, syncSegment);
  1816. }
  1817. [Fact]
  1818. public async Task OrderBy_OrderByAsync_ThenBy_ThenByDescendingAsync()
  1819. {
  1820. var rand = new Random(42);
  1821. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1822. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  1823. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1824. await AssertSorted(asyncRes, syncRes);
  1825. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1826. var syncSegment = syncRes.AsEnumerable();
  1827. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1828. {
  1829. asyncSegment = asyncSegment.Skip(skipCount);
  1830. syncSegment = syncSegment.Skip(skipCount);
  1831. }
  1832. foreach (var takeCount in new[] { 31, 29, 23 })
  1833. {
  1834. asyncSegment = asyncSegment.Take(takeCount);
  1835. syncSegment = syncSegment.Take(takeCount);
  1836. }
  1837. await AssertSorted(asyncSegment, syncSegment);
  1838. }
  1839. [Fact]
  1840. public async Task OrderBy_OrderByAsync_ThenBy_ThenByDescendingAsyncWithCancellation()
  1841. {
  1842. var rand = new Random(42);
  1843. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1844. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  1845. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1846. await AssertSorted(asyncRes, syncRes);
  1847. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1848. var syncSegment = syncRes.AsEnumerable();
  1849. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1850. {
  1851. asyncSegment = asyncSegment.Skip(skipCount);
  1852. syncSegment = syncSegment.Skip(skipCount);
  1853. }
  1854. foreach (var takeCount in new[] { 31, 29, 23 })
  1855. {
  1856. asyncSegment = asyncSegment.Take(takeCount);
  1857. syncSegment = syncSegment.Take(takeCount);
  1858. }
  1859. await AssertSorted(asyncSegment, syncSegment);
  1860. }
  1861. [Fact]
  1862. public async Task OrderBy_OrderByAsync_ThenByAsync_ThenBy()
  1863. {
  1864. var rand = new Random(42);
  1865. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1866. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  1867. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1868. await AssertSorted(asyncRes, syncRes);
  1869. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1870. var syncSegment = syncRes.AsEnumerable();
  1871. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1872. {
  1873. asyncSegment = asyncSegment.Skip(skipCount);
  1874. syncSegment = syncSegment.Skip(skipCount);
  1875. }
  1876. foreach (var takeCount in new[] { 31, 29, 23 })
  1877. {
  1878. asyncSegment = asyncSegment.Take(takeCount);
  1879. syncSegment = syncSegment.Take(takeCount);
  1880. }
  1881. await AssertSorted(asyncSegment, syncSegment);
  1882. }
  1883. [Fact]
  1884. public async Task OrderBy_OrderByAsync_ThenByAsync_ThenByAsync()
  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)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  1889. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1890. await AssertSorted(asyncRes, syncRes);
  1891. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1892. var syncSegment = syncRes.AsEnumerable();
  1893. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1894. {
  1895. asyncSegment = asyncSegment.Skip(skipCount);
  1896. syncSegment = syncSegment.Skip(skipCount);
  1897. }
  1898. foreach (var takeCount in new[] { 31, 29, 23 })
  1899. {
  1900. asyncSegment = asyncSegment.Take(takeCount);
  1901. syncSegment = syncSegment.Take(takeCount);
  1902. }
  1903. await AssertSorted(asyncSegment, syncSegment);
  1904. }
  1905. [Fact]
  1906. public async Task OrderBy_OrderByAsync_ThenByAsync_ThenByAsyncWithCancellation()
  1907. {
  1908. var rand = new Random(42);
  1909. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1910. 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));
  1911. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  1912. await AssertSorted(asyncRes, syncRes);
  1913. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1914. var syncSegment = syncRes.AsEnumerable();
  1915. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1916. {
  1917. asyncSegment = asyncSegment.Skip(skipCount);
  1918. syncSegment = syncSegment.Skip(skipCount);
  1919. }
  1920. foreach (var takeCount in new[] { 31, 29, 23 })
  1921. {
  1922. asyncSegment = asyncSegment.Take(takeCount);
  1923. syncSegment = syncSegment.Take(takeCount);
  1924. }
  1925. await AssertSorted(asyncSegment, syncSegment);
  1926. }
  1927. [Fact]
  1928. public async Task OrderBy_OrderByAsync_ThenByAsync_ThenByDescending()
  1929. {
  1930. var rand = new Random(42);
  1931. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1932. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  1933. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1934. await AssertSorted(asyncRes, syncRes);
  1935. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1936. var syncSegment = syncRes.AsEnumerable();
  1937. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1938. {
  1939. asyncSegment = asyncSegment.Skip(skipCount);
  1940. syncSegment = syncSegment.Skip(skipCount);
  1941. }
  1942. foreach (var takeCount in new[] { 31, 29, 23 })
  1943. {
  1944. asyncSegment = asyncSegment.Take(takeCount);
  1945. syncSegment = syncSegment.Take(takeCount);
  1946. }
  1947. await AssertSorted(asyncSegment, syncSegment);
  1948. }
  1949. [Fact]
  1950. public async Task OrderBy_OrderByAsync_ThenByAsync_ThenByDescendingAsync()
  1951. {
  1952. var rand = new Random(42);
  1953. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1954. 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));
  1955. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1956. await AssertSorted(asyncRes, syncRes);
  1957. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1958. var syncSegment = syncRes.AsEnumerable();
  1959. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1960. {
  1961. asyncSegment = asyncSegment.Skip(skipCount);
  1962. syncSegment = syncSegment.Skip(skipCount);
  1963. }
  1964. foreach (var takeCount in new[] { 31, 29, 23 })
  1965. {
  1966. asyncSegment = asyncSegment.Take(takeCount);
  1967. syncSegment = syncSegment.Take(takeCount);
  1968. }
  1969. await AssertSorted(asyncSegment, syncSegment);
  1970. }
  1971. [Fact]
  1972. public async Task OrderBy_OrderByAsync_ThenByAsync_ThenByDescendingAsyncWithCancellation()
  1973. {
  1974. var rand = new Random(42);
  1975. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1976. 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));
  1977. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  1978. await AssertSorted(asyncRes, syncRes);
  1979. var asyncSegment = asyncRes.AsAsyncEnumerable();
  1980. var syncSegment = syncRes.AsEnumerable();
  1981. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  1982. {
  1983. asyncSegment = asyncSegment.Skip(skipCount);
  1984. syncSegment = syncSegment.Skip(skipCount);
  1985. }
  1986. foreach (var takeCount in new[] { 31, 29, 23 })
  1987. {
  1988. asyncSegment = asyncSegment.Take(takeCount);
  1989. syncSegment = syncSegment.Take(takeCount);
  1990. }
  1991. await AssertSorted(asyncSegment, syncSegment);
  1992. }
  1993. [Fact]
  1994. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation_ThenBy()
  1995. {
  1996. var rand = new Random(42);
  1997. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  1998. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  1999. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2000. await AssertSorted(asyncRes, syncRes);
  2001. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2002. var syncSegment = syncRes.AsEnumerable();
  2003. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2004. {
  2005. asyncSegment = asyncSegment.Skip(skipCount);
  2006. syncSegment = syncSegment.Skip(skipCount);
  2007. }
  2008. foreach (var takeCount in new[] { 31, 29, 23 })
  2009. {
  2010. asyncSegment = asyncSegment.Take(takeCount);
  2011. syncSegment = syncSegment.Take(takeCount);
  2012. }
  2013. await AssertSorted(asyncSegment, syncSegment);
  2014. }
  2015. [Fact]
  2016. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation_ThenByAsync()
  2017. {
  2018. var rand = new Random(42);
  2019. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2020. 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));
  2021. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2022. await AssertSorted(asyncRes, syncRes);
  2023. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2024. var syncSegment = syncRes.AsEnumerable();
  2025. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2026. {
  2027. asyncSegment = asyncSegment.Skip(skipCount);
  2028. syncSegment = syncSegment.Skip(skipCount);
  2029. }
  2030. foreach (var takeCount in new[] { 31, 29, 23 })
  2031. {
  2032. asyncSegment = asyncSegment.Take(takeCount);
  2033. syncSegment = syncSegment.Take(takeCount);
  2034. }
  2035. await AssertSorted(asyncSegment, syncSegment);
  2036. }
  2037. [Fact]
  2038. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation_ThenByAsyncWithCancellation()
  2039. {
  2040. var rand = new Random(42);
  2041. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2042. 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));
  2043. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2044. await AssertSorted(asyncRes, syncRes);
  2045. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2046. var syncSegment = syncRes.AsEnumerable();
  2047. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2048. {
  2049. asyncSegment = asyncSegment.Skip(skipCount);
  2050. syncSegment = syncSegment.Skip(skipCount);
  2051. }
  2052. foreach (var takeCount in new[] { 31, 29, 23 })
  2053. {
  2054. asyncSegment = asyncSegment.Take(takeCount);
  2055. syncSegment = syncSegment.Take(takeCount);
  2056. }
  2057. await AssertSorted(asyncSegment, syncSegment);
  2058. }
  2059. [Fact]
  2060. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation_ThenByDescending()
  2061. {
  2062. var rand = new Random(42);
  2063. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2064. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  2065. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2066. await AssertSorted(asyncRes, syncRes);
  2067. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2068. var syncSegment = syncRes.AsEnumerable();
  2069. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2070. {
  2071. asyncSegment = asyncSegment.Skip(skipCount);
  2072. syncSegment = syncSegment.Skip(skipCount);
  2073. }
  2074. foreach (var takeCount in new[] { 31, 29, 23 })
  2075. {
  2076. asyncSegment = asyncSegment.Take(takeCount);
  2077. syncSegment = syncSegment.Take(takeCount);
  2078. }
  2079. await AssertSorted(asyncSegment, syncSegment);
  2080. }
  2081. [Fact]
  2082. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation_ThenByDescendingAsync()
  2083. {
  2084. var rand = new Random(42);
  2085. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2086. 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));
  2087. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2088. await AssertSorted(asyncRes, syncRes);
  2089. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2090. var syncSegment = syncRes.AsEnumerable();
  2091. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2092. {
  2093. asyncSegment = asyncSegment.Skip(skipCount);
  2094. syncSegment = syncSegment.Skip(skipCount);
  2095. }
  2096. foreach (var takeCount in new[] { 31, 29, 23 })
  2097. {
  2098. asyncSegment = asyncSegment.Take(takeCount);
  2099. syncSegment = syncSegment.Take(takeCount);
  2100. }
  2101. await AssertSorted(asyncSegment, syncSegment);
  2102. }
  2103. [Fact]
  2104. public async Task OrderBy_OrderByAsync_ThenByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  2105. {
  2106. var rand = new Random(42);
  2107. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2108. 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));
  2109. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2110. await AssertSorted(asyncRes, syncRes);
  2111. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2112. var syncSegment = syncRes.AsEnumerable();
  2113. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2114. {
  2115. asyncSegment = asyncSegment.Skip(skipCount);
  2116. syncSegment = syncSegment.Skip(skipCount);
  2117. }
  2118. foreach (var takeCount in new[] { 31, 29, 23 })
  2119. {
  2120. asyncSegment = asyncSegment.Take(takeCount);
  2121. syncSegment = syncSegment.Take(takeCount);
  2122. }
  2123. await AssertSorted(asyncSegment, syncSegment);
  2124. }
  2125. [Fact]
  2126. public async Task OrderBy_OrderByAsync_ThenByDescending_ThenBy()
  2127. {
  2128. var rand = new Random(42);
  2129. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2130. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2131. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2132. await AssertSorted(asyncRes, syncRes);
  2133. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2134. var syncSegment = syncRes.AsEnumerable();
  2135. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2136. {
  2137. asyncSegment = asyncSegment.Skip(skipCount);
  2138. syncSegment = syncSegment.Skip(skipCount);
  2139. }
  2140. foreach (var takeCount in new[] { 31, 29, 23 })
  2141. {
  2142. asyncSegment = asyncSegment.Take(takeCount);
  2143. syncSegment = syncSegment.Take(takeCount);
  2144. }
  2145. await AssertSorted(asyncSegment, syncSegment);
  2146. }
  2147. [Fact]
  2148. public async Task OrderBy_OrderByAsync_ThenByDescending_ThenByAsync()
  2149. {
  2150. var rand = new Random(42);
  2151. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2152. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  2153. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2154. await AssertSorted(asyncRes, syncRes);
  2155. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2156. var syncSegment = syncRes.AsEnumerable();
  2157. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2158. {
  2159. asyncSegment = asyncSegment.Skip(skipCount);
  2160. syncSegment = syncSegment.Skip(skipCount);
  2161. }
  2162. foreach (var takeCount in new[] { 31, 29, 23 })
  2163. {
  2164. asyncSegment = asyncSegment.Take(takeCount);
  2165. syncSegment = syncSegment.Take(takeCount);
  2166. }
  2167. await AssertSorted(asyncSegment, syncSegment);
  2168. }
  2169. [Fact]
  2170. public async Task OrderBy_OrderByAsync_ThenByDescending_ThenByAsyncWithCancellation()
  2171. {
  2172. var rand = new Random(42);
  2173. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2174. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  2175. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2176. await AssertSorted(asyncRes, syncRes);
  2177. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2178. var syncSegment = syncRes.AsEnumerable();
  2179. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2180. {
  2181. asyncSegment = asyncSegment.Skip(skipCount);
  2182. syncSegment = syncSegment.Skip(skipCount);
  2183. }
  2184. foreach (var takeCount in new[] { 31, 29, 23 })
  2185. {
  2186. asyncSegment = asyncSegment.Take(takeCount);
  2187. syncSegment = syncSegment.Take(takeCount);
  2188. }
  2189. await AssertSorted(asyncSegment, syncSegment);
  2190. }
  2191. [Fact]
  2192. public async Task OrderBy_OrderByAsync_ThenByDescending_ThenByDescending()
  2193. {
  2194. var rand = new Random(42);
  2195. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2196. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2197. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2198. await AssertSorted(asyncRes, syncRes);
  2199. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2200. var syncSegment = syncRes.AsEnumerable();
  2201. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2202. {
  2203. asyncSegment = asyncSegment.Skip(skipCount);
  2204. syncSegment = syncSegment.Skip(skipCount);
  2205. }
  2206. foreach (var takeCount in new[] { 31, 29, 23 })
  2207. {
  2208. asyncSegment = asyncSegment.Take(takeCount);
  2209. syncSegment = syncSegment.Take(takeCount);
  2210. }
  2211. await AssertSorted(asyncSegment, syncSegment);
  2212. }
  2213. [Fact]
  2214. public async Task OrderBy_OrderByAsync_ThenByDescending_ThenByDescendingAsync()
  2215. {
  2216. var rand = new Random(42);
  2217. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2218. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  2219. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2220. await AssertSorted(asyncRes, syncRes);
  2221. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2222. var syncSegment = syncRes.AsEnumerable();
  2223. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2224. {
  2225. asyncSegment = asyncSegment.Skip(skipCount);
  2226. syncSegment = syncSegment.Skip(skipCount);
  2227. }
  2228. foreach (var takeCount in new[] { 31, 29, 23 })
  2229. {
  2230. asyncSegment = asyncSegment.Take(takeCount);
  2231. syncSegment = syncSegment.Take(takeCount);
  2232. }
  2233. await AssertSorted(asyncSegment, syncSegment);
  2234. }
  2235. [Fact]
  2236. public async Task OrderBy_OrderByAsync_ThenByDescending_ThenByDescendingAsyncWithCancellation()
  2237. {
  2238. var rand = new Random(42);
  2239. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2240. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  2241. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2242. await AssertSorted(asyncRes, syncRes);
  2243. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2244. var syncSegment = syncRes.AsEnumerable();
  2245. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2246. {
  2247. asyncSegment = asyncSegment.Skip(skipCount);
  2248. syncSegment = syncSegment.Skip(skipCount);
  2249. }
  2250. foreach (var takeCount in new[] { 31, 29, 23 })
  2251. {
  2252. asyncSegment = asyncSegment.Take(takeCount);
  2253. syncSegment = syncSegment.Take(takeCount);
  2254. }
  2255. await AssertSorted(asyncSegment, syncSegment);
  2256. }
  2257. [Fact]
  2258. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync_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 => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  2263. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2264. await AssertSorted(asyncRes, syncRes);
  2265. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2266. var syncSegment = syncRes.AsEnumerable();
  2267. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2268. {
  2269. asyncSegment = asyncSegment.Skip(skipCount);
  2270. syncSegment = syncSegment.Skip(skipCount);
  2271. }
  2272. foreach (var takeCount in new[] { 31, 29, 23 })
  2273. {
  2274. asyncSegment = asyncSegment.Take(takeCount);
  2275. syncSegment = syncSegment.Take(takeCount);
  2276. }
  2277. await AssertSorted(asyncSegment, syncSegment);
  2278. }
  2279. [Fact]
  2280. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync_ThenByAsync()
  2281. {
  2282. var rand = new Random(42);
  2283. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2284. 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));
  2285. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2286. await AssertSorted(asyncRes, syncRes);
  2287. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2288. var syncSegment = syncRes.AsEnumerable();
  2289. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2290. {
  2291. asyncSegment = asyncSegment.Skip(skipCount);
  2292. syncSegment = syncSegment.Skip(skipCount);
  2293. }
  2294. foreach (var takeCount in new[] { 31, 29, 23 })
  2295. {
  2296. asyncSegment = asyncSegment.Take(takeCount);
  2297. syncSegment = syncSegment.Take(takeCount);
  2298. }
  2299. await AssertSorted(asyncSegment, syncSegment);
  2300. }
  2301. [Fact]
  2302. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync_ThenByAsyncWithCancellation()
  2303. {
  2304. var rand = new Random(42);
  2305. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2306. 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));
  2307. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2308. await AssertSorted(asyncRes, syncRes);
  2309. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2310. var syncSegment = syncRes.AsEnumerable();
  2311. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2312. {
  2313. asyncSegment = asyncSegment.Skip(skipCount);
  2314. syncSegment = syncSegment.Skip(skipCount);
  2315. }
  2316. foreach (var takeCount in new[] { 31, 29, 23 })
  2317. {
  2318. asyncSegment = asyncSegment.Take(takeCount);
  2319. syncSegment = syncSegment.Take(takeCount);
  2320. }
  2321. await AssertSorted(asyncSegment, syncSegment);
  2322. }
  2323. [Fact]
  2324. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync_ThenByDescending()
  2325. {
  2326. var rand = new Random(42);
  2327. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2328. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  2329. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2330. await AssertSorted(asyncRes, syncRes);
  2331. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2332. var syncSegment = syncRes.AsEnumerable();
  2333. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2334. {
  2335. asyncSegment = asyncSegment.Skip(skipCount);
  2336. syncSegment = syncSegment.Skip(skipCount);
  2337. }
  2338. foreach (var takeCount in new[] { 31, 29, 23 })
  2339. {
  2340. asyncSegment = asyncSegment.Take(takeCount);
  2341. syncSegment = syncSegment.Take(takeCount);
  2342. }
  2343. await AssertSorted(asyncSegment, syncSegment);
  2344. }
  2345. [Fact]
  2346. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync_ThenByDescendingAsync()
  2347. {
  2348. var rand = new Random(42);
  2349. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2350. 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));
  2351. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2352. await AssertSorted(asyncRes, syncRes);
  2353. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2354. var syncSegment = syncRes.AsEnumerable();
  2355. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2356. {
  2357. asyncSegment = asyncSegment.Skip(skipCount);
  2358. syncSegment = syncSegment.Skip(skipCount);
  2359. }
  2360. foreach (var takeCount in new[] { 31, 29, 23 })
  2361. {
  2362. asyncSegment = asyncSegment.Take(takeCount);
  2363. syncSegment = syncSegment.Take(takeCount);
  2364. }
  2365. await AssertSorted(asyncSegment, syncSegment);
  2366. }
  2367. [Fact]
  2368. public async Task OrderBy_OrderByAsync_ThenByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  2369. {
  2370. var rand = new Random(42);
  2371. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2372. 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));
  2373. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2374. await AssertSorted(asyncRes, syncRes);
  2375. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2376. var syncSegment = syncRes.AsEnumerable();
  2377. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2378. {
  2379. asyncSegment = asyncSegment.Skip(skipCount);
  2380. syncSegment = syncSegment.Skip(skipCount);
  2381. }
  2382. foreach (var takeCount in new[] { 31, 29, 23 })
  2383. {
  2384. asyncSegment = asyncSegment.Take(takeCount);
  2385. syncSegment = syncSegment.Take(takeCount);
  2386. }
  2387. await AssertSorted(asyncSegment, syncSegment);
  2388. }
  2389. [Fact]
  2390. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation_ThenBy()
  2391. {
  2392. var rand = new Random(42);
  2393. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2394. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  2395. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2396. await AssertSorted(asyncRes, syncRes);
  2397. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2398. var syncSegment = syncRes.AsEnumerable();
  2399. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2400. {
  2401. asyncSegment = asyncSegment.Skip(skipCount);
  2402. syncSegment = syncSegment.Skip(skipCount);
  2403. }
  2404. foreach (var takeCount in new[] { 31, 29, 23 })
  2405. {
  2406. asyncSegment = asyncSegment.Take(takeCount);
  2407. syncSegment = syncSegment.Take(takeCount);
  2408. }
  2409. await AssertSorted(asyncSegment, syncSegment);
  2410. }
  2411. [Fact]
  2412. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation_ThenByAsync()
  2413. {
  2414. var rand = new Random(42);
  2415. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2416. 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));
  2417. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2418. await AssertSorted(asyncRes, syncRes);
  2419. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2420. var syncSegment = syncRes.AsEnumerable();
  2421. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2422. {
  2423. asyncSegment = asyncSegment.Skip(skipCount);
  2424. syncSegment = syncSegment.Skip(skipCount);
  2425. }
  2426. foreach (var takeCount in new[] { 31, 29, 23 })
  2427. {
  2428. asyncSegment = asyncSegment.Take(takeCount);
  2429. syncSegment = syncSegment.Take(takeCount);
  2430. }
  2431. await AssertSorted(asyncSegment, syncSegment);
  2432. }
  2433. [Fact]
  2434. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  2435. {
  2436. var rand = new Random(42);
  2437. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2438. 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));
  2439. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2440. await AssertSorted(asyncRes, syncRes);
  2441. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2442. var syncSegment = syncRes.AsEnumerable();
  2443. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2444. {
  2445. asyncSegment = asyncSegment.Skip(skipCount);
  2446. syncSegment = syncSegment.Skip(skipCount);
  2447. }
  2448. foreach (var takeCount in new[] { 31, 29, 23 })
  2449. {
  2450. asyncSegment = asyncSegment.Take(takeCount);
  2451. syncSegment = syncSegment.Take(takeCount);
  2452. }
  2453. await AssertSorted(asyncSegment, syncSegment);
  2454. }
  2455. [Fact]
  2456. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation_ThenByDescending()
  2457. {
  2458. var rand = new Random(42);
  2459. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2460. var asyncRes = xs.ToAsyncEnumerable().OrderBy(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  2461. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2462. await AssertSorted(asyncRes, syncRes);
  2463. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2464. var syncSegment = syncRes.AsEnumerable();
  2465. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2466. {
  2467. asyncSegment = asyncSegment.Skip(skipCount);
  2468. syncSegment = syncSegment.Skip(skipCount);
  2469. }
  2470. foreach (var takeCount in new[] { 31, 29, 23 })
  2471. {
  2472. asyncSegment = asyncSegment.Take(takeCount);
  2473. syncSegment = syncSegment.Take(takeCount);
  2474. }
  2475. await AssertSorted(asyncSegment, syncSegment);
  2476. }
  2477. [Fact]
  2478. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  2479. {
  2480. var rand = new Random(42);
  2481. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2482. 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));
  2483. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2484. await AssertSorted(asyncRes, syncRes);
  2485. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2486. var syncSegment = syncRes.AsEnumerable();
  2487. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2488. {
  2489. asyncSegment = asyncSegment.Skip(skipCount);
  2490. syncSegment = syncSegment.Skip(skipCount);
  2491. }
  2492. foreach (var takeCount in new[] { 31, 29, 23 })
  2493. {
  2494. asyncSegment = asyncSegment.Take(takeCount);
  2495. syncSegment = syncSegment.Take(takeCount);
  2496. }
  2497. await AssertSorted(asyncSegment, syncSegment);
  2498. }
  2499. [Fact]
  2500. public async Task OrderBy_OrderByAsync_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  2501. {
  2502. var rand = new Random(42);
  2503. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2504. 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));
  2505. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2506. await AssertSorted(asyncRes, syncRes);
  2507. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2508. var syncSegment = syncRes.AsEnumerable();
  2509. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2510. {
  2511. asyncSegment = asyncSegment.Skip(skipCount);
  2512. syncSegment = syncSegment.Skip(skipCount);
  2513. }
  2514. foreach (var takeCount in new[] { 31, 29, 23 })
  2515. {
  2516. asyncSegment = asyncSegment.Take(takeCount);
  2517. syncSegment = syncSegment.Take(takeCount);
  2518. }
  2519. await AssertSorted(asyncSegment, syncSegment);
  2520. }
  2521. [Fact]
  2522. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy_ThenBy()
  2523. {
  2524. var rand = new Random(42);
  2525. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2526. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2527. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2528. await AssertSorted(asyncRes, syncRes);
  2529. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2530. var syncSegment = syncRes.AsEnumerable();
  2531. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2532. {
  2533. asyncSegment = asyncSegment.Skip(skipCount);
  2534. syncSegment = syncSegment.Skip(skipCount);
  2535. }
  2536. foreach (var takeCount in new[] { 31, 29, 23 })
  2537. {
  2538. asyncSegment = asyncSegment.Take(takeCount);
  2539. syncSegment = syncSegment.Take(takeCount);
  2540. }
  2541. await AssertSorted(asyncSegment, syncSegment);
  2542. }
  2543. [Fact]
  2544. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy_ThenByAsync()
  2545. {
  2546. var rand = new Random(42);
  2547. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2548. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  2549. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2550. await AssertSorted(asyncRes, syncRes);
  2551. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2552. var syncSegment = syncRes.AsEnumerable();
  2553. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2554. {
  2555. asyncSegment = asyncSegment.Skip(skipCount);
  2556. syncSegment = syncSegment.Skip(skipCount);
  2557. }
  2558. foreach (var takeCount in new[] { 31, 29, 23 })
  2559. {
  2560. asyncSegment = asyncSegment.Take(takeCount);
  2561. syncSegment = syncSegment.Take(takeCount);
  2562. }
  2563. await AssertSorted(asyncSegment, syncSegment);
  2564. }
  2565. [Fact]
  2566. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy_ThenByAsyncWithCancellation()
  2567. {
  2568. var rand = new Random(42);
  2569. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2570. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  2571. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2572. await AssertSorted(asyncRes, syncRes);
  2573. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2574. var syncSegment = syncRes.AsEnumerable();
  2575. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2576. {
  2577. asyncSegment = asyncSegment.Skip(skipCount);
  2578. syncSegment = syncSegment.Skip(skipCount);
  2579. }
  2580. foreach (var takeCount in new[] { 31, 29, 23 })
  2581. {
  2582. asyncSegment = asyncSegment.Take(takeCount);
  2583. syncSegment = syncSegment.Take(takeCount);
  2584. }
  2585. await AssertSorted(asyncSegment, syncSegment);
  2586. }
  2587. [Fact]
  2588. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy_ThenByDescending()
  2589. {
  2590. var rand = new Random(42);
  2591. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2592. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2593. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2594. await AssertSorted(asyncRes, syncRes);
  2595. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2596. var syncSegment = syncRes.AsEnumerable();
  2597. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2598. {
  2599. asyncSegment = asyncSegment.Skip(skipCount);
  2600. syncSegment = syncSegment.Skip(skipCount);
  2601. }
  2602. foreach (var takeCount in new[] { 31, 29, 23 })
  2603. {
  2604. asyncSegment = asyncSegment.Take(takeCount);
  2605. syncSegment = syncSegment.Take(takeCount);
  2606. }
  2607. await AssertSorted(asyncSegment, syncSegment);
  2608. }
  2609. [Fact]
  2610. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy_ThenByDescendingAsync()
  2611. {
  2612. var rand = new Random(42);
  2613. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2614. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  2615. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2616. await AssertSorted(asyncRes, syncRes);
  2617. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2618. var syncSegment = syncRes.AsEnumerable();
  2619. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2620. {
  2621. asyncSegment = asyncSegment.Skip(skipCount);
  2622. syncSegment = syncSegment.Skip(skipCount);
  2623. }
  2624. foreach (var takeCount in new[] { 31, 29, 23 })
  2625. {
  2626. asyncSegment = asyncSegment.Take(takeCount);
  2627. syncSegment = syncSegment.Take(takeCount);
  2628. }
  2629. await AssertSorted(asyncSegment, syncSegment);
  2630. }
  2631. [Fact]
  2632. public async Task OrderBy_OrderByAsyncWithCancellation_ThenBy_ThenByDescendingAsyncWithCancellation()
  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().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  2637. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2638. await AssertSorted(asyncRes, syncRes);
  2639. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2640. var syncSegment = syncRes.AsEnumerable();
  2641. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2642. {
  2643. asyncSegment = asyncSegment.Skip(skipCount);
  2644. syncSegment = syncSegment.Skip(skipCount);
  2645. }
  2646. foreach (var takeCount in new[] { 31, 29, 23 })
  2647. {
  2648. asyncSegment = asyncSegment.Take(takeCount);
  2649. syncSegment = syncSegment.Take(takeCount);
  2650. }
  2651. await AssertSorted(asyncSegment, syncSegment);
  2652. }
  2653. [Fact]
  2654. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync_ThenBy()
  2655. {
  2656. var rand = new Random(42);
  2657. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2658. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  2659. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2660. await AssertSorted(asyncRes, syncRes);
  2661. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2662. var syncSegment = syncRes.AsEnumerable();
  2663. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2664. {
  2665. asyncSegment = asyncSegment.Skip(skipCount);
  2666. syncSegment = syncSegment.Skip(skipCount);
  2667. }
  2668. foreach (var takeCount in new[] { 31, 29, 23 })
  2669. {
  2670. asyncSegment = asyncSegment.Take(takeCount);
  2671. syncSegment = syncSegment.Take(takeCount);
  2672. }
  2673. await AssertSorted(asyncSegment, syncSegment);
  2674. }
  2675. [Fact]
  2676. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync_ThenByAsync()
  2677. {
  2678. var rand = new Random(42);
  2679. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2680. 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));
  2681. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2682. await AssertSorted(asyncRes, syncRes);
  2683. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2684. var syncSegment = syncRes.AsEnumerable();
  2685. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2686. {
  2687. asyncSegment = asyncSegment.Skip(skipCount);
  2688. syncSegment = syncSegment.Skip(skipCount);
  2689. }
  2690. foreach (var takeCount in new[] { 31, 29, 23 })
  2691. {
  2692. asyncSegment = asyncSegment.Take(takeCount);
  2693. syncSegment = syncSegment.Take(takeCount);
  2694. }
  2695. await AssertSorted(asyncSegment, syncSegment);
  2696. }
  2697. [Fact]
  2698. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync_ThenByAsyncWithCancellation()
  2699. {
  2700. var rand = new Random(42);
  2701. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2702. 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));
  2703. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2704. await AssertSorted(asyncRes, syncRes);
  2705. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2706. var syncSegment = syncRes.AsEnumerable();
  2707. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2708. {
  2709. asyncSegment = asyncSegment.Skip(skipCount);
  2710. syncSegment = syncSegment.Skip(skipCount);
  2711. }
  2712. foreach (var takeCount in new[] { 31, 29, 23 })
  2713. {
  2714. asyncSegment = asyncSegment.Take(takeCount);
  2715. syncSegment = syncSegment.Take(takeCount);
  2716. }
  2717. await AssertSorted(asyncSegment, syncSegment);
  2718. }
  2719. [Fact]
  2720. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync_ThenByDescending()
  2721. {
  2722. var rand = new Random(42);
  2723. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2724. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  2725. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2726. await AssertSorted(asyncRes, syncRes);
  2727. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2728. var syncSegment = syncRes.AsEnumerable();
  2729. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2730. {
  2731. asyncSegment = asyncSegment.Skip(skipCount);
  2732. syncSegment = syncSegment.Skip(skipCount);
  2733. }
  2734. foreach (var takeCount in new[] { 31, 29, 23 })
  2735. {
  2736. asyncSegment = asyncSegment.Take(takeCount);
  2737. syncSegment = syncSegment.Take(takeCount);
  2738. }
  2739. await AssertSorted(asyncSegment, syncSegment);
  2740. }
  2741. [Fact]
  2742. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync_ThenByDescendingAsync()
  2743. {
  2744. var rand = new Random(42);
  2745. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2746. 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));
  2747. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2748. await AssertSorted(asyncRes, syncRes);
  2749. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2750. var syncSegment = syncRes.AsEnumerable();
  2751. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2752. {
  2753. asyncSegment = asyncSegment.Skip(skipCount);
  2754. syncSegment = syncSegment.Skip(skipCount);
  2755. }
  2756. foreach (var takeCount in new[] { 31, 29, 23 })
  2757. {
  2758. asyncSegment = asyncSegment.Take(takeCount);
  2759. syncSegment = syncSegment.Take(takeCount);
  2760. }
  2761. await AssertSorted(asyncSegment, syncSegment);
  2762. }
  2763. [Fact]
  2764. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsync_ThenByDescendingAsyncWithCancellation()
  2765. {
  2766. var rand = new Random(42);
  2767. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2768. 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));
  2769. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2770. await AssertSorted(asyncRes, syncRes);
  2771. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2772. var syncSegment = syncRes.AsEnumerable();
  2773. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2774. {
  2775. asyncSegment = asyncSegment.Skip(skipCount);
  2776. syncSegment = syncSegment.Skip(skipCount);
  2777. }
  2778. foreach (var takeCount in new[] { 31, 29, 23 })
  2779. {
  2780. asyncSegment = asyncSegment.Take(takeCount);
  2781. syncSegment = syncSegment.Take(takeCount);
  2782. }
  2783. await AssertSorted(asyncSegment, syncSegment);
  2784. }
  2785. [Fact]
  2786. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation_ThenBy()
  2787. {
  2788. var rand = new Random(42);
  2789. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2790. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  2791. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2792. await AssertSorted(asyncRes, syncRes);
  2793. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2794. var syncSegment = syncRes.AsEnumerable();
  2795. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2796. {
  2797. asyncSegment = asyncSegment.Skip(skipCount);
  2798. syncSegment = syncSegment.Skip(skipCount);
  2799. }
  2800. foreach (var takeCount in new[] { 31, 29, 23 })
  2801. {
  2802. asyncSegment = asyncSegment.Take(takeCount);
  2803. syncSegment = syncSegment.Take(takeCount);
  2804. }
  2805. await AssertSorted(asyncSegment, syncSegment);
  2806. }
  2807. [Fact]
  2808. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByAsync()
  2809. {
  2810. var rand = new Random(42);
  2811. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2812. 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));
  2813. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2814. await AssertSorted(asyncRes, syncRes);
  2815. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2816. var syncSegment = syncRes.AsEnumerable();
  2817. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2818. {
  2819. asyncSegment = asyncSegment.Skip(skipCount);
  2820. syncSegment = syncSegment.Skip(skipCount);
  2821. }
  2822. foreach (var takeCount in new[] { 31, 29, 23 })
  2823. {
  2824. asyncSegment = asyncSegment.Take(takeCount);
  2825. syncSegment = syncSegment.Take(takeCount);
  2826. }
  2827. await AssertSorted(asyncSegment, syncSegment);
  2828. }
  2829. [Fact]
  2830. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByAsyncWithCancellation()
  2831. {
  2832. var rand = new Random(42);
  2833. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2834. 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));
  2835. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  2836. await AssertSorted(asyncRes, syncRes);
  2837. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2838. var syncSegment = syncRes.AsEnumerable();
  2839. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2840. {
  2841. asyncSegment = asyncSegment.Skip(skipCount);
  2842. syncSegment = syncSegment.Skip(skipCount);
  2843. }
  2844. foreach (var takeCount in new[] { 31, 29, 23 })
  2845. {
  2846. asyncSegment = asyncSegment.Take(takeCount);
  2847. syncSegment = syncSegment.Take(takeCount);
  2848. }
  2849. await AssertSorted(asyncSegment, syncSegment);
  2850. }
  2851. [Fact]
  2852. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByDescending()
  2853. {
  2854. var rand = new Random(42);
  2855. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2856. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  2857. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2858. await AssertSorted(asyncRes, syncRes);
  2859. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2860. var syncSegment = syncRes.AsEnumerable();
  2861. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2862. {
  2863. asyncSegment = asyncSegment.Skip(skipCount);
  2864. syncSegment = syncSegment.Skip(skipCount);
  2865. }
  2866. foreach (var takeCount in new[] { 31, 29, 23 })
  2867. {
  2868. asyncSegment = asyncSegment.Take(takeCount);
  2869. syncSegment = syncSegment.Take(takeCount);
  2870. }
  2871. await AssertSorted(asyncSegment, syncSegment);
  2872. }
  2873. [Fact]
  2874. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByDescendingAsync()
  2875. {
  2876. var rand = new Random(42);
  2877. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2878. 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));
  2879. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2880. await AssertSorted(asyncRes, syncRes);
  2881. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2882. var syncSegment = syncRes.AsEnumerable();
  2883. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2884. {
  2885. asyncSegment = asyncSegment.Skip(skipCount);
  2886. syncSegment = syncSegment.Skip(skipCount);
  2887. }
  2888. foreach (var takeCount in new[] { 31, 29, 23 })
  2889. {
  2890. asyncSegment = asyncSegment.Take(takeCount);
  2891. syncSegment = syncSegment.Take(takeCount);
  2892. }
  2893. await AssertSorted(asyncSegment, syncSegment);
  2894. }
  2895. [Fact]
  2896. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  2897. {
  2898. var rand = new Random(42);
  2899. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2900. 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));
  2901. var syncRes = xs.OrderBy(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  2902. await AssertSorted(asyncRes, syncRes);
  2903. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2904. var syncSegment = syncRes.AsEnumerable();
  2905. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2906. {
  2907. asyncSegment = asyncSegment.Skip(skipCount);
  2908. syncSegment = syncSegment.Skip(skipCount);
  2909. }
  2910. foreach (var takeCount in new[] { 31, 29, 23 })
  2911. {
  2912. asyncSegment = asyncSegment.Take(takeCount);
  2913. syncSegment = syncSegment.Take(takeCount);
  2914. }
  2915. await AssertSorted(asyncSegment, syncSegment);
  2916. }
  2917. [Fact]
  2918. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending_ThenBy()
  2919. {
  2920. var rand = new Random(42);
  2921. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2922. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2923. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2924. await AssertSorted(asyncRes, syncRes);
  2925. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2926. var syncSegment = syncRes.AsEnumerable();
  2927. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2928. {
  2929. asyncSegment = asyncSegment.Skip(skipCount);
  2930. syncSegment = syncSegment.Skip(skipCount);
  2931. }
  2932. foreach (var takeCount in new[] { 31, 29, 23 })
  2933. {
  2934. asyncSegment = asyncSegment.Take(takeCount);
  2935. syncSegment = syncSegment.Take(takeCount);
  2936. }
  2937. await AssertSorted(asyncSegment, syncSegment);
  2938. }
  2939. [Fact]
  2940. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending_ThenByAsync()
  2941. {
  2942. var rand = new Random(42);
  2943. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2944. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  2945. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2946. await AssertSorted(asyncRes, syncRes);
  2947. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2948. var syncSegment = syncRes.AsEnumerable();
  2949. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2950. {
  2951. asyncSegment = asyncSegment.Skip(skipCount);
  2952. syncSegment = syncSegment.Skip(skipCount);
  2953. }
  2954. foreach (var takeCount in new[] { 31, 29, 23 })
  2955. {
  2956. asyncSegment = asyncSegment.Take(takeCount);
  2957. syncSegment = syncSegment.Take(takeCount);
  2958. }
  2959. await AssertSorted(asyncSegment, syncSegment);
  2960. }
  2961. [Fact]
  2962. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending_ThenByAsyncWithCancellation()
  2963. {
  2964. var rand = new Random(42);
  2965. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2966. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  2967. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  2968. await AssertSorted(asyncRes, syncRes);
  2969. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2970. var syncSegment = syncRes.AsEnumerable();
  2971. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2972. {
  2973. asyncSegment = asyncSegment.Skip(skipCount);
  2974. syncSegment = syncSegment.Skip(skipCount);
  2975. }
  2976. foreach (var takeCount in new[] { 31, 29, 23 })
  2977. {
  2978. asyncSegment = asyncSegment.Take(takeCount);
  2979. syncSegment = syncSegment.Take(takeCount);
  2980. }
  2981. await AssertSorted(asyncSegment, syncSegment);
  2982. }
  2983. [Fact]
  2984. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending_ThenByDescending()
  2985. {
  2986. var rand = new Random(42);
  2987. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  2988. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2989. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  2990. await AssertSorted(asyncRes, syncRes);
  2991. var asyncSegment = asyncRes.AsAsyncEnumerable();
  2992. var syncSegment = syncRes.AsEnumerable();
  2993. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  2994. {
  2995. asyncSegment = asyncSegment.Skip(skipCount);
  2996. syncSegment = syncSegment.Skip(skipCount);
  2997. }
  2998. foreach (var takeCount in new[] { 31, 29, 23 })
  2999. {
  3000. asyncSegment = asyncSegment.Take(takeCount);
  3001. syncSegment = syncSegment.Take(takeCount);
  3002. }
  3003. await AssertSorted(asyncSegment, syncSegment);
  3004. }
  3005. [Fact]
  3006. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending_ThenByDescendingAsync()
  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().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  3011. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3012. await AssertSorted(asyncRes, syncRes);
  3013. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3014. var syncSegment = syncRes.AsEnumerable();
  3015. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3016. {
  3017. asyncSegment = asyncSegment.Skip(skipCount);
  3018. syncSegment = syncSegment.Skip(skipCount);
  3019. }
  3020. foreach (var takeCount in new[] { 31, 29, 23 })
  3021. {
  3022. asyncSegment = asyncSegment.Take(takeCount);
  3023. syncSegment = syncSegment.Take(takeCount);
  3024. }
  3025. await AssertSorted(asyncSegment, syncSegment);
  3026. }
  3027. [Fact]
  3028. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescending_ThenByDescendingAsyncWithCancellation()
  3029. {
  3030. var rand = new Random(42);
  3031. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3032. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3033. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3034. await AssertSorted(asyncRes, syncRes);
  3035. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3036. var syncSegment = syncRes.AsEnumerable();
  3037. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3038. {
  3039. asyncSegment = asyncSegment.Skip(skipCount);
  3040. syncSegment = syncSegment.Skip(skipCount);
  3041. }
  3042. foreach (var takeCount in new[] { 31, 29, 23 })
  3043. {
  3044. asyncSegment = asyncSegment.Take(takeCount);
  3045. syncSegment = syncSegment.Take(takeCount);
  3046. }
  3047. await AssertSorted(asyncSegment, syncSegment);
  3048. }
  3049. [Fact]
  3050. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync_ThenBy()
  3051. {
  3052. var rand = new Random(42);
  3053. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3054. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  3055. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3056. await AssertSorted(asyncRes, syncRes);
  3057. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3058. var syncSegment = syncRes.AsEnumerable();
  3059. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3060. {
  3061. asyncSegment = asyncSegment.Skip(skipCount);
  3062. syncSegment = syncSegment.Skip(skipCount);
  3063. }
  3064. foreach (var takeCount in new[] { 31, 29, 23 })
  3065. {
  3066. asyncSegment = asyncSegment.Take(takeCount);
  3067. syncSegment = syncSegment.Take(takeCount);
  3068. }
  3069. await AssertSorted(asyncSegment, syncSegment);
  3070. }
  3071. [Fact]
  3072. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync_ThenByAsync()
  3073. {
  3074. var rand = new Random(42);
  3075. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3076. 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));
  3077. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3078. await AssertSorted(asyncRes, syncRes);
  3079. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3080. var syncSegment = syncRes.AsEnumerable();
  3081. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3082. {
  3083. asyncSegment = asyncSegment.Skip(skipCount);
  3084. syncSegment = syncSegment.Skip(skipCount);
  3085. }
  3086. foreach (var takeCount in new[] { 31, 29, 23 })
  3087. {
  3088. asyncSegment = asyncSegment.Take(takeCount);
  3089. syncSegment = syncSegment.Take(takeCount);
  3090. }
  3091. await AssertSorted(asyncSegment, syncSegment);
  3092. }
  3093. [Fact]
  3094. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync_ThenByAsyncWithCancellation()
  3095. {
  3096. var rand = new Random(42);
  3097. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3098. 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));
  3099. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3100. await AssertSorted(asyncRes, syncRes);
  3101. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3102. var syncSegment = syncRes.AsEnumerable();
  3103. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3104. {
  3105. asyncSegment = asyncSegment.Skip(skipCount);
  3106. syncSegment = syncSegment.Skip(skipCount);
  3107. }
  3108. foreach (var takeCount in new[] { 31, 29, 23 })
  3109. {
  3110. asyncSegment = asyncSegment.Take(takeCount);
  3111. syncSegment = syncSegment.Take(takeCount);
  3112. }
  3113. await AssertSorted(asyncSegment, syncSegment);
  3114. }
  3115. [Fact]
  3116. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync_ThenByDescending()
  3117. {
  3118. var rand = new Random(42);
  3119. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3120. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  3121. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3122. await AssertSorted(asyncRes, syncRes);
  3123. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3124. var syncSegment = syncRes.AsEnumerable();
  3125. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3126. {
  3127. asyncSegment = asyncSegment.Skip(skipCount);
  3128. syncSegment = syncSegment.Skip(skipCount);
  3129. }
  3130. foreach (var takeCount in new[] { 31, 29, 23 })
  3131. {
  3132. asyncSegment = asyncSegment.Take(takeCount);
  3133. syncSegment = syncSegment.Take(takeCount);
  3134. }
  3135. await AssertSorted(asyncSegment, syncSegment);
  3136. }
  3137. [Fact]
  3138. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync_ThenByDescendingAsync()
  3139. {
  3140. var rand = new Random(42);
  3141. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3142. 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));
  3143. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3144. await AssertSorted(asyncRes, syncRes);
  3145. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3146. var syncSegment = syncRes.AsEnumerable();
  3147. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3148. {
  3149. asyncSegment = asyncSegment.Skip(skipCount);
  3150. syncSegment = syncSegment.Skip(skipCount);
  3151. }
  3152. foreach (var takeCount in new[] { 31, 29, 23 })
  3153. {
  3154. asyncSegment = asyncSegment.Take(takeCount);
  3155. syncSegment = syncSegment.Take(takeCount);
  3156. }
  3157. await AssertSorted(asyncSegment, syncSegment);
  3158. }
  3159. [Fact]
  3160. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  3161. {
  3162. var rand = new Random(42);
  3163. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3164. 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));
  3165. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3166. await AssertSorted(asyncRes, syncRes);
  3167. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3168. var syncSegment = syncRes.AsEnumerable();
  3169. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3170. {
  3171. asyncSegment = asyncSegment.Skip(skipCount);
  3172. syncSegment = syncSegment.Skip(skipCount);
  3173. }
  3174. foreach (var takeCount in new[] { 31, 29, 23 })
  3175. {
  3176. asyncSegment = asyncSegment.Take(takeCount);
  3177. syncSegment = syncSegment.Take(takeCount);
  3178. }
  3179. await AssertSorted(asyncSegment, syncSegment);
  3180. }
  3181. [Fact]
  3182. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenBy()
  3183. {
  3184. var rand = new Random(42);
  3185. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3186. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  3187. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3188. await AssertSorted(asyncRes, syncRes);
  3189. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3190. var syncSegment = syncRes.AsEnumerable();
  3191. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3192. {
  3193. asyncSegment = asyncSegment.Skip(skipCount);
  3194. syncSegment = syncSegment.Skip(skipCount);
  3195. }
  3196. foreach (var takeCount in new[] { 31, 29, 23 })
  3197. {
  3198. asyncSegment = asyncSegment.Take(takeCount);
  3199. syncSegment = syncSegment.Take(takeCount);
  3200. }
  3201. await AssertSorted(asyncSegment, syncSegment);
  3202. }
  3203. [Fact]
  3204. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByAsync()
  3205. {
  3206. var rand = new Random(42);
  3207. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3208. 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));
  3209. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3210. await AssertSorted(asyncRes, syncRes);
  3211. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3212. var syncSegment = syncRes.AsEnumerable();
  3213. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3214. {
  3215. asyncSegment = asyncSegment.Skip(skipCount);
  3216. syncSegment = syncSegment.Skip(skipCount);
  3217. }
  3218. foreach (var takeCount in new[] { 31, 29, 23 })
  3219. {
  3220. asyncSegment = asyncSegment.Take(takeCount);
  3221. syncSegment = syncSegment.Take(takeCount);
  3222. }
  3223. await AssertSorted(asyncSegment, syncSegment);
  3224. }
  3225. [Fact]
  3226. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  3227. {
  3228. var rand = new Random(42);
  3229. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3230. 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));
  3231. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3232. await AssertSorted(asyncRes, syncRes);
  3233. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3234. var syncSegment = syncRes.AsEnumerable();
  3235. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3236. {
  3237. asyncSegment = asyncSegment.Skip(skipCount);
  3238. syncSegment = syncSegment.Skip(skipCount);
  3239. }
  3240. foreach (var takeCount in new[] { 31, 29, 23 })
  3241. {
  3242. asyncSegment = asyncSegment.Take(takeCount);
  3243. syncSegment = syncSegment.Take(takeCount);
  3244. }
  3245. await AssertSorted(asyncSegment, syncSegment);
  3246. }
  3247. [Fact]
  3248. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByDescending()
  3249. {
  3250. var rand = new Random(42);
  3251. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3252. var asyncRes = xs.ToAsyncEnumerable().OrderBy((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  3253. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3254. await AssertSorted(asyncRes, syncRes);
  3255. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3256. var syncSegment = syncRes.AsEnumerable();
  3257. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3258. {
  3259. asyncSegment = asyncSegment.Skip(skipCount);
  3260. syncSegment = syncSegment.Skip(skipCount);
  3261. }
  3262. foreach (var takeCount in new[] { 31, 29, 23 })
  3263. {
  3264. asyncSegment = asyncSegment.Take(takeCount);
  3265. syncSegment = syncSegment.Take(takeCount);
  3266. }
  3267. await AssertSorted(asyncSegment, syncSegment);
  3268. }
  3269. [Fact]
  3270. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  3271. {
  3272. var rand = new Random(42);
  3273. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3274. 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));
  3275. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3276. await AssertSorted(asyncRes, syncRes);
  3277. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3278. var syncSegment = syncRes.AsEnumerable();
  3279. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3280. {
  3281. asyncSegment = asyncSegment.Skip(skipCount);
  3282. syncSegment = syncSegment.Skip(skipCount);
  3283. }
  3284. foreach (var takeCount in new[] { 31, 29, 23 })
  3285. {
  3286. asyncSegment = asyncSegment.Take(takeCount);
  3287. syncSegment = syncSegment.Take(takeCount);
  3288. }
  3289. await AssertSorted(asyncSegment, syncSegment);
  3290. }
  3291. [Fact]
  3292. public async Task OrderBy_OrderByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  3293. {
  3294. var rand = new Random(42);
  3295. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3296. 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));
  3297. var syncRes = xs.OrderBy(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3298. await AssertSorted(asyncRes, syncRes);
  3299. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3300. var syncSegment = syncRes.AsEnumerable();
  3301. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3302. {
  3303. asyncSegment = asyncSegment.Skip(skipCount);
  3304. syncSegment = syncSegment.Skip(skipCount);
  3305. }
  3306. foreach (var takeCount in new[] { 31, 29, 23 })
  3307. {
  3308. asyncSegment = asyncSegment.Take(takeCount);
  3309. syncSegment = syncSegment.Take(takeCount);
  3310. }
  3311. await AssertSorted(asyncSegment, syncSegment);
  3312. }
  3313. [Fact]
  3314. public async Task OrderBy_OrderByDescending_ThenBy_ThenBy()
  3315. {
  3316. var rand = new Random(42);
  3317. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3318. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3319. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3320. await AssertSorted(asyncRes, syncRes);
  3321. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3322. var syncSegment = syncRes.AsEnumerable();
  3323. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3324. {
  3325. asyncSegment = asyncSegment.Skip(skipCount);
  3326. syncSegment = syncSegment.Skip(skipCount);
  3327. }
  3328. foreach (var takeCount in new[] { 31, 29, 23 })
  3329. {
  3330. asyncSegment = asyncSegment.Take(takeCount);
  3331. syncSegment = syncSegment.Take(takeCount);
  3332. }
  3333. await AssertSorted(asyncSegment, syncSegment);
  3334. }
  3335. [Fact]
  3336. public async Task OrderBy_OrderByDescending_ThenBy_ThenByAsync()
  3337. {
  3338. var rand = new Random(42);
  3339. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3340. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  3341. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3342. await AssertSorted(asyncRes, syncRes);
  3343. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3344. var syncSegment = syncRes.AsEnumerable();
  3345. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3346. {
  3347. asyncSegment = asyncSegment.Skip(skipCount);
  3348. syncSegment = syncSegment.Skip(skipCount);
  3349. }
  3350. foreach (var takeCount in new[] { 31, 29, 23 })
  3351. {
  3352. asyncSegment = asyncSegment.Take(takeCount);
  3353. syncSegment = syncSegment.Take(takeCount);
  3354. }
  3355. await AssertSorted(asyncSegment, syncSegment);
  3356. }
  3357. [Fact]
  3358. public async Task OrderBy_OrderByDescending_ThenBy_ThenByAsyncWithCancellation()
  3359. {
  3360. var rand = new Random(42);
  3361. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3362. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3363. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3364. await AssertSorted(asyncRes, syncRes);
  3365. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3366. var syncSegment = syncRes.AsEnumerable();
  3367. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3368. {
  3369. asyncSegment = asyncSegment.Skip(skipCount);
  3370. syncSegment = syncSegment.Skip(skipCount);
  3371. }
  3372. foreach (var takeCount in new[] { 31, 29, 23 })
  3373. {
  3374. asyncSegment = asyncSegment.Take(takeCount);
  3375. syncSegment = syncSegment.Take(takeCount);
  3376. }
  3377. await AssertSorted(asyncSegment, syncSegment);
  3378. }
  3379. [Fact]
  3380. public async Task OrderBy_OrderByDescending_ThenBy_ThenByDescending()
  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 => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3385. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3386. await AssertSorted(asyncRes, syncRes);
  3387. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3388. var syncSegment = syncRes.AsEnumerable();
  3389. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3390. {
  3391. asyncSegment = asyncSegment.Skip(skipCount);
  3392. syncSegment = syncSegment.Skip(skipCount);
  3393. }
  3394. foreach (var takeCount in new[] { 31, 29, 23 })
  3395. {
  3396. asyncSegment = asyncSegment.Take(takeCount);
  3397. syncSegment = syncSegment.Take(takeCount);
  3398. }
  3399. await AssertSorted(asyncSegment, syncSegment);
  3400. }
  3401. [Fact]
  3402. public async Task OrderBy_OrderByDescending_ThenBy_ThenByDescendingAsync()
  3403. {
  3404. var rand = new Random(42);
  3405. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3406. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  3407. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3408. await AssertSorted(asyncRes, syncRes);
  3409. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3410. var syncSegment = syncRes.AsEnumerable();
  3411. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3412. {
  3413. asyncSegment = asyncSegment.Skip(skipCount);
  3414. syncSegment = syncSegment.Skip(skipCount);
  3415. }
  3416. foreach (var takeCount in new[] { 31, 29, 23 })
  3417. {
  3418. asyncSegment = asyncSegment.Take(takeCount);
  3419. syncSegment = syncSegment.Take(takeCount);
  3420. }
  3421. await AssertSorted(asyncSegment, syncSegment);
  3422. }
  3423. [Fact]
  3424. public async Task OrderBy_OrderByDescending_ThenBy_ThenByDescendingAsyncWithCancellation()
  3425. {
  3426. var rand = new Random(42);
  3427. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3428. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3429. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3430. await AssertSorted(asyncRes, syncRes);
  3431. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3432. var syncSegment = syncRes.AsEnumerable();
  3433. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3434. {
  3435. asyncSegment = asyncSegment.Skip(skipCount);
  3436. syncSegment = syncSegment.Skip(skipCount);
  3437. }
  3438. foreach (var takeCount in new[] { 31, 29, 23 })
  3439. {
  3440. asyncSegment = asyncSegment.Take(takeCount);
  3441. syncSegment = syncSegment.Take(takeCount);
  3442. }
  3443. await AssertSorted(asyncSegment, syncSegment);
  3444. }
  3445. [Fact]
  3446. public async Task OrderBy_OrderByDescending_ThenByAsync_ThenBy()
  3447. {
  3448. var rand = new Random(42);
  3449. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3450. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  3451. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3452. await AssertSorted(asyncRes, syncRes);
  3453. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3454. var syncSegment = syncRes.AsEnumerable();
  3455. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3456. {
  3457. asyncSegment = asyncSegment.Skip(skipCount);
  3458. syncSegment = syncSegment.Skip(skipCount);
  3459. }
  3460. foreach (var takeCount in new[] { 31, 29, 23 })
  3461. {
  3462. asyncSegment = asyncSegment.Take(takeCount);
  3463. syncSegment = syncSegment.Take(takeCount);
  3464. }
  3465. await AssertSorted(asyncSegment, syncSegment);
  3466. }
  3467. [Fact]
  3468. public async Task OrderBy_OrderByDescending_ThenByAsync_ThenByAsync()
  3469. {
  3470. var rand = new Random(42);
  3471. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3472. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  3473. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3474. await AssertSorted(asyncRes, syncRes);
  3475. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3476. var syncSegment = syncRes.AsEnumerable();
  3477. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3478. {
  3479. asyncSegment = asyncSegment.Skip(skipCount);
  3480. syncSegment = syncSegment.Skip(skipCount);
  3481. }
  3482. foreach (var takeCount in new[] { 31, 29, 23 })
  3483. {
  3484. asyncSegment = asyncSegment.Take(takeCount);
  3485. syncSegment = syncSegment.Take(takeCount);
  3486. }
  3487. await AssertSorted(asyncSegment, syncSegment);
  3488. }
  3489. [Fact]
  3490. public async Task OrderBy_OrderByDescending_ThenByAsync_ThenByAsyncWithCancellation()
  3491. {
  3492. var rand = new Random(42);
  3493. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3494. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3495. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3496. await AssertSorted(asyncRes, syncRes);
  3497. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3498. var syncSegment = syncRes.AsEnumerable();
  3499. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3500. {
  3501. asyncSegment = asyncSegment.Skip(skipCount);
  3502. syncSegment = syncSegment.Skip(skipCount);
  3503. }
  3504. foreach (var takeCount in new[] { 31, 29, 23 })
  3505. {
  3506. asyncSegment = asyncSegment.Take(takeCount);
  3507. syncSegment = syncSegment.Take(takeCount);
  3508. }
  3509. await AssertSorted(asyncSegment, syncSegment);
  3510. }
  3511. [Fact]
  3512. public async Task OrderBy_OrderByDescending_ThenByAsync_ThenByDescending()
  3513. {
  3514. var rand = new Random(42);
  3515. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3516. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  3517. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3518. await AssertSorted(asyncRes, syncRes);
  3519. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3520. var syncSegment = syncRes.AsEnumerable();
  3521. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3522. {
  3523. asyncSegment = asyncSegment.Skip(skipCount);
  3524. syncSegment = syncSegment.Skip(skipCount);
  3525. }
  3526. foreach (var takeCount in new[] { 31, 29, 23 })
  3527. {
  3528. asyncSegment = asyncSegment.Take(takeCount);
  3529. syncSegment = syncSegment.Take(takeCount);
  3530. }
  3531. await AssertSorted(asyncSegment, syncSegment);
  3532. }
  3533. [Fact]
  3534. public async Task OrderBy_OrderByDescending_ThenByAsync_ThenByDescendingAsync()
  3535. {
  3536. var rand = new Random(42);
  3537. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3538. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  3539. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3540. await AssertSorted(asyncRes, syncRes);
  3541. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3542. var syncSegment = syncRes.AsEnumerable();
  3543. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3544. {
  3545. asyncSegment = asyncSegment.Skip(skipCount);
  3546. syncSegment = syncSegment.Skip(skipCount);
  3547. }
  3548. foreach (var takeCount in new[] { 31, 29, 23 })
  3549. {
  3550. asyncSegment = asyncSegment.Take(takeCount);
  3551. syncSegment = syncSegment.Take(takeCount);
  3552. }
  3553. await AssertSorted(asyncSegment, syncSegment);
  3554. }
  3555. [Fact]
  3556. public async Task OrderBy_OrderByDescending_ThenByAsync_ThenByDescendingAsyncWithCancellation()
  3557. {
  3558. var rand = new Random(42);
  3559. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3560. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3561. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3562. await AssertSorted(asyncRes, syncRes);
  3563. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3564. var syncSegment = syncRes.AsEnumerable();
  3565. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3566. {
  3567. asyncSegment = asyncSegment.Skip(skipCount);
  3568. syncSegment = syncSegment.Skip(skipCount);
  3569. }
  3570. foreach (var takeCount in new[] { 31, 29, 23 })
  3571. {
  3572. asyncSegment = asyncSegment.Take(takeCount);
  3573. syncSegment = syncSegment.Take(takeCount);
  3574. }
  3575. await AssertSorted(asyncSegment, syncSegment);
  3576. }
  3577. [Fact]
  3578. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation_ThenBy()
  3579. {
  3580. var rand = new Random(42);
  3581. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3582. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  3583. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3584. await AssertSorted(asyncRes, syncRes);
  3585. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3586. var syncSegment = syncRes.AsEnumerable();
  3587. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3588. {
  3589. asyncSegment = asyncSegment.Skip(skipCount);
  3590. syncSegment = syncSegment.Skip(skipCount);
  3591. }
  3592. foreach (var takeCount in new[] { 31, 29, 23 })
  3593. {
  3594. asyncSegment = asyncSegment.Take(takeCount);
  3595. syncSegment = syncSegment.Take(takeCount);
  3596. }
  3597. await AssertSorted(asyncSegment, syncSegment);
  3598. }
  3599. [Fact]
  3600. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation_ThenByAsync()
  3601. {
  3602. var rand = new Random(42);
  3603. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3604. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  3605. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3606. await AssertSorted(asyncRes, syncRes);
  3607. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3608. var syncSegment = syncRes.AsEnumerable();
  3609. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3610. {
  3611. asyncSegment = asyncSegment.Skip(skipCount);
  3612. syncSegment = syncSegment.Skip(skipCount);
  3613. }
  3614. foreach (var takeCount in new[] { 31, 29, 23 })
  3615. {
  3616. asyncSegment = asyncSegment.Take(takeCount);
  3617. syncSegment = syncSegment.Take(takeCount);
  3618. }
  3619. await AssertSorted(asyncSegment, syncSegment);
  3620. }
  3621. [Fact]
  3622. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation_ThenByAsyncWithCancellation()
  3623. {
  3624. var rand = new Random(42);
  3625. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3626. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3627. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  3628. await AssertSorted(asyncRes, syncRes);
  3629. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3630. var syncSegment = syncRes.AsEnumerable();
  3631. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3632. {
  3633. asyncSegment = asyncSegment.Skip(skipCount);
  3634. syncSegment = syncSegment.Skip(skipCount);
  3635. }
  3636. foreach (var takeCount in new[] { 31, 29, 23 })
  3637. {
  3638. asyncSegment = asyncSegment.Take(takeCount);
  3639. syncSegment = syncSegment.Take(takeCount);
  3640. }
  3641. await AssertSorted(asyncSegment, syncSegment);
  3642. }
  3643. [Fact]
  3644. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation_ThenByDescending()
  3645. {
  3646. var rand = new Random(42);
  3647. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3648. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  3649. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3650. await AssertSorted(asyncRes, syncRes);
  3651. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3652. var syncSegment = syncRes.AsEnumerable();
  3653. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3654. {
  3655. asyncSegment = asyncSegment.Skip(skipCount);
  3656. syncSegment = syncSegment.Skip(skipCount);
  3657. }
  3658. foreach (var takeCount in new[] { 31, 29, 23 })
  3659. {
  3660. asyncSegment = asyncSegment.Take(takeCount);
  3661. syncSegment = syncSegment.Take(takeCount);
  3662. }
  3663. await AssertSorted(asyncSegment, syncSegment);
  3664. }
  3665. [Fact]
  3666. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation_ThenByDescendingAsync()
  3667. {
  3668. var rand = new Random(42);
  3669. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3670. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  3671. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3672. await AssertSorted(asyncRes, syncRes);
  3673. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3674. var syncSegment = syncRes.AsEnumerable();
  3675. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3676. {
  3677. asyncSegment = asyncSegment.Skip(skipCount);
  3678. syncSegment = syncSegment.Skip(skipCount);
  3679. }
  3680. foreach (var takeCount in new[] { 31, 29, 23 })
  3681. {
  3682. asyncSegment = asyncSegment.Take(takeCount);
  3683. syncSegment = syncSegment.Take(takeCount);
  3684. }
  3685. await AssertSorted(asyncSegment, syncSegment);
  3686. }
  3687. [Fact]
  3688. public async Task OrderBy_OrderByDescending_ThenByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  3689. {
  3690. var rand = new Random(42);
  3691. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3692. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3693. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  3694. await AssertSorted(asyncRes, syncRes);
  3695. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3696. var syncSegment = syncRes.AsEnumerable();
  3697. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3698. {
  3699. asyncSegment = asyncSegment.Skip(skipCount);
  3700. syncSegment = syncSegment.Skip(skipCount);
  3701. }
  3702. foreach (var takeCount in new[] { 31, 29, 23 })
  3703. {
  3704. asyncSegment = asyncSegment.Take(takeCount);
  3705. syncSegment = syncSegment.Take(takeCount);
  3706. }
  3707. await AssertSorted(asyncSegment, syncSegment);
  3708. }
  3709. [Fact]
  3710. public async Task OrderBy_OrderByDescending_ThenByDescending_ThenBy()
  3711. {
  3712. var rand = new Random(42);
  3713. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3714. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3715. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3716. await AssertSorted(asyncRes, syncRes);
  3717. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3718. var syncSegment = syncRes.AsEnumerable();
  3719. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3720. {
  3721. asyncSegment = asyncSegment.Skip(skipCount);
  3722. syncSegment = syncSegment.Skip(skipCount);
  3723. }
  3724. foreach (var takeCount in new[] { 31, 29, 23 })
  3725. {
  3726. asyncSegment = asyncSegment.Take(takeCount);
  3727. syncSegment = syncSegment.Take(takeCount);
  3728. }
  3729. await AssertSorted(asyncSegment, syncSegment);
  3730. }
  3731. [Fact]
  3732. public async Task OrderBy_OrderByDescending_ThenByDescending_ThenByAsync()
  3733. {
  3734. var rand = new Random(42);
  3735. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3736. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  3737. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3738. await AssertSorted(asyncRes, syncRes);
  3739. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3740. var syncSegment = syncRes.AsEnumerable();
  3741. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3742. {
  3743. asyncSegment = asyncSegment.Skip(skipCount);
  3744. syncSegment = syncSegment.Skip(skipCount);
  3745. }
  3746. foreach (var takeCount in new[] { 31, 29, 23 })
  3747. {
  3748. asyncSegment = asyncSegment.Take(takeCount);
  3749. syncSegment = syncSegment.Take(takeCount);
  3750. }
  3751. await AssertSorted(asyncSegment, syncSegment);
  3752. }
  3753. [Fact]
  3754. public async Task OrderBy_OrderByDescending_ThenByDescending_ThenByAsyncWithCancellation()
  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 => x % 2).ThenByDescending(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3759. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3760. await AssertSorted(asyncRes, syncRes);
  3761. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3762. var syncSegment = syncRes.AsEnumerable();
  3763. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3764. {
  3765. asyncSegment = asyncSegment.Skip(skipCount);
  3766. syncSegment = syncSegment.Skip(skipCount);
  3767. }
  3768. foreach (var takeCount in new[] { 31, 29, 23 })
  3769. {
  3770. asyncSegment = asyncSegment.Take(takeCount);
  3771. syncSegment = syncSegment.Take(takeCount);
  3772. }
  3773. await AssertSorted(asyncSegment, syncSegment);
  3774. }
  3775. [Fact]
  3776. public async Task OrderBy_OrderByDescending_ThenByDescending_ThenByDescending()
  3777. {
  3778. var rand = new Random(42);
  3779. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3780. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3781. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3782. await AssertSorted(asyncRes, syncRes);
  3783. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3784. var syncSegment = syncRes.AsEnumerable();
  3785. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3786. {
  3787. asyncSegment = asyncSegment.Skip(skipCount);
  3788. syncSegment = syncSegment.Skip(skipCount);
  3789. }
  3790. foreach (var takeCount in new[] { 31, 29, 23 })
  3791. {
  3792. asyncSegment = asyncSegment.Take(takeCount);
  3793. syncSegment = syncSegment.Take(takeCount);
  3794. }
  3795. await AssertSorted(asyncSegment, syncSegment);
  3796. }
  3797. [Fact]
  3798. public async Task OrderBy_OrderByDescending_ThenByDescending_ThenByDescendingAsync()
  3799. {
  3800. var rand = new Random(42);
  3801. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3802. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  3803. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3804. await AssertSorted(asyncRes, syncRes);
  3805. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3806. var syncSegment = syncRes.AsEnumerable();
  3807. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3808. {
  3809. asyncSegment = asyncSegment.Skip(skipCount);
  3810. syncSegment = syncSegment.Skip(skipCount);
  3811. }
  3812. foreach (var takeCount in new[] { 31, 29, 23 })
  3813. {
  3814. asyncSegment = asyncSegment.Take(takeCount);
  3815. syncSegment = syncSegment.Take(takeCount);
  3816. }
  3817. await AssertSorted(asyncSegment, syncSegment);
  3818. }
  3819. [Fact]
  3820. public async Task OrderBy_OrderByDescending_ThenByDescending_ThenByDescendingAsyncWithCancellation()
  3821. {
  3822. var rand = new Random(42);
  3823. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3824. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3825. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3826. await AssertSorted(asyncRes, syncRes);
  3827. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3828. var syncSegment = syncRes.AsEnumerable();
  3829. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3830. {
  3831. asyncSegment = asyncSegment.Skip(skipCount);
  3832. syncSegment = syncSegment.Skip(skipCount);
  3833. }
  3834. foreach (var takeCount in new[] { 31, 29, 23 })
  3835. {
  3836. asyncSegment = asyncSegment.Take(takeCount);
  3837. syncSegment = syncSegment.Take(takeCount);
  3838. }
  3839. await AssertSorted(asyncSegment, syncSegment);
  3840. }
  3841. [Fact]
  3842. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync_ThenBy()
  3843. {
  3844. var rand = new Random(42);
  3845. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3846. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  3847. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3848. await AssertSorted(asyncRes, syncRes);
  3849. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3850. var syncSegment = syncRes.AsEnumerable();
  3851. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3852. {
  3853. asyncSegment = asyncSegment.Skip(skipCount);
  3854. syncSegment = syncSegment.Skip(skipCount);
  3855. }
  3856. foreach (var takeCount in new[] { 31, 29, 23 })
  3857. {
  3858. asyncSegment = asyncSegment.Take(takeCount);
  3859. syncSegment = syncSegment.Take(takeCount);
  3860. }
  3861. await AssertSorted(asyncSegment, syncSegment);
  3862. }
  3863. [Fact]
  3864. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync_ThenByAsync()
  3865. {
  3866. var rand = new Random(42);
  3867. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3868. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  3869. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3870. await AssertSorted(asyncRes, syncRes);
  3871. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3872. var syncSegment = syncRes.AsEnumerable();
  3873. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3874. {
  3875. asyncSegment = asyncSegment.Skip(skipCount);
  3876. syncSegment = syncSegment.Skip(skipCount);
  3877. }
  3878. foreach (var takeCount in new[] { 31, 29, 23 })
  3879. {
  3880. asyncSegment = asyncSegment.Take(takeCount);
  3881. syncSegment = syncSegment.Take(takeCount);
  3882. }
  3883. await AssertSorted(asyncSegment, syncSegment);
  3884. }
  3885. [Fact]
  3886. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync_ThenByAsyncWithCancellation()
  3887. {
  3888. var rand = new Random(42);
  3889. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3890. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  3891. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3892. await AssertSorted(asyncRes, syncRes);
  3893. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3894. var syncSegment = syncRes.AsEnumerable();
  3895. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3896. {
  3897. asyncSegment = asyncSegment.Skip(skipCount);
  3898. syncSegment = syncSegment.Skip(skipCount);
  3899. }
  3900. foreach (var takeCount in new[] { 31, 29, 23 })
  3901. {
  3902. asyncSegment = asyncSegment.Take(takeCount);
  3903. syncSegment = syncSegment.Take(takeCount);
  3904. }
  3905. await AssertSorted(asyncSegment, syncSegment);
  3906. }
  3907. [Fact]
  3908. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync_ThenByDescending()
  3909. {
  3910. var rand = new Random(42);
  3911. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3912. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  3913. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3914. await AssertSorted(asyncRes, syncRes);
  3915. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3916. var syncSegment = syncRes.AsEnumerable();
  3917. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3918. {
  3919. asyncSegment = asyncSegment.Skip(skipCount);
  3920. syncSegment = syncSegment.Skip(skipCount);
  3921. }
  3922. foreach (var takeCount in new[] { 31, 29, 23 })
  3923. {
  3924. asyncSegment = asyncSegment.Take(takeCount);
  3925. syncSegment = syncSegment.Take(takeCount);
  3926. }
  3927. await AssertSorted(asyncSegment, syncSegment);
  3928. }
  3929. [Fact]
  3930. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync_ThenByDescendingAsync()
  3931. {
  3932. var rand = new Random(42);
  3933. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3934. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  3935. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3936. await AssertSorted(asyncRes, syncRes);
  3937. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3938. var syncSegment = syncRes.AsEnumerable();
  3939. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3940. {
  3941. asyncSegment = asyncSegment.Skip(skipCount);
  3942. syncSegment = syncSegment.Skip(skipCount);
  3943. }
  3944. foreach (var takeCount in new[] { 31, 29, 23 })
  3945. {
  3946. asyncSegment = asyncSegment.Take(takeCount);
  3947. syncSegment = syncSegment.Take(takeCount);
  3948. }
  3949. await AssertSorted(asyncSegment, syncSegment);
  3950. }
  3951. [Fact]
  3952. public async Task OrderBy_OrderByDescending_ThenByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  3953. {
  3954. var rand = new Random(42);
  3955. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3956. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  3957. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  3958. await AssertSorted(asyncRes, syncRes);
  3959. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3960. var syncSegment = syncRes.AsEnumerable();
  3961. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3962. {
  3963. asyncSegment = asyncSegment.Skip(skipCount);
  3964. syncSegment = syncSegment.Skip(skipCount);
  3965. }
  3966. foreach (var takeCount in new[] { 31, 29, 23 })
  3967. {
  3968. asyncSegment = asyncSegment.Take(takeCount);
  3969. syncSegment = syncSegment.Take(takeCount);
  3970. }
  3971. await AssertSorted(asyncSegment, syncSegment);
  3972. }
  3973. [Fact]
  3974. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation_ThenBy()
  3975. {
  3976. var rand = new Random(42);
  3977. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  3978. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  3979. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  3980. await AssertSorted(asyncRes, syncRes);
  3981. var asyncSegment = asyncRes.AsAsyncEnumerable();
  3982. var syncSegment = syncRes.AsEnumerable();
  3983. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  3984. {
  3985. asyncSegment = asyncSegment.Skip(skipCount);
  3986. syncSegment = syncSegment.Skip(skipCount);
  3987. }
  3988. foreach (var takeCount in new[] { 31, 29, 23 })
  3989. {
  3990. asyncSegment = asyncSegment.Take(takeCount);
  3991. syncSegment = syncSegment.Take(takeCount);
  3992. }
  3993. await AssertSorted(asyncSegment, syncSegment);
  3994. }
  3995. [Fact]
  3996. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation_ThenByAsync()
  3997. {
  3998. var rand = new Random(42);
  3999. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4000. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => new ValueTask<int>(x % 4));
  4001. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4002. await AssertSorted(asyncRes, syncRes);
  4003. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4004. var syncSegment = syncRes.AsEnumerable();
  4005. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4006. {
  4007. asyncSegment = asyncSegment.Skip(skipCount);
  4008. syncSegment = syncSegment.Skip(skipCount);
  4009. }
  4010. foreach (var takeCount in new[] { 31, 29, 23 })
  4011. {
  4012. asyncSegment = asyncSegment.Take(takeCount);
  4013. syncSegment = syncSegment.Take(takeCount);
  4014. }
  4015. await AssertSorted(asyncSegment, syncSegment);
  4016. }
  4017. [Fact]
  4018. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  4019. {
  4020. var rand = new Random(42);
  4021. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4022. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  4023. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4024. await AssertSorted(asyncRes, syncRes);
  4025. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4026. var syncSegment = syncRes.AsEnumerable();
  4027. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4028. {
  4029. asyncSegment = asyncSegment.Skip(skipCount);
  4030. syncSegment = syncSegment.Skip(skipCount);
  4031. }
  4032. foreach (var takeCount in new[] { 31, 29, 23 })
  4033. {
  4034. asyncSegment = asyncSegment.Take(takeCount);
  4035. syncSegment = syncSegment.Take(takeCount);
  4036. }
  4037. await AssertSorted(asyncSegment, syncSegment);
  4038. }
  4039. [Fact]
  4040. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation_ThenByDescending()
  4041. {
  4042. var rand = new Random(42);
  4043. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4044. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  4045. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4046. await AssertSorted(asyncRes, syncRes);
  4047. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4048. var syncSegment = syncRes.AsEnumerable();
  4049. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4050. {
  4051. asyncSegment = asyncSegment.Skip(skipCount);
  4052. syncSegment = syncSegment.Skip(skipCount);
  4053. }
  4054. foreach (var takeCount in new[] { 31, 29, 23 })
  4055. {
  4056. asyncSegment = asyncSegment.Take(takeCount);
  4057. syncSegment = syncSegment.Take(takeCount);
  4058. }
  4059. await AssertSorted(asyncSegment, syncSegment);
  4060. }
  4061. [Fact]
  4062. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  4063. {
  4064. var rand = new Random(42);
  4065. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4066. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => new ValueTask<int>(x % 4));
  4067. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4068. await AssertSorted(asyncRes, syncRes);
  4069. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4070. var syncSegment = syncRes.AsEnumerable();
  4071. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4072. {
  4073. asyncSegment = asyncSegment.Skip(skipCount);
  4074. syncSegment = syncSegment.Skip(skipCount);
  4075. }
  4076. foreach (var takeCount in new[] { 31, 29, 23 })
  4077. {
  4078. asyncSegment = asyncSegment.Take(takeCount);
  4079. syncSegment = syncSegment.Take(takeCount);
  4080. }
  4081. await AssertSorted(asyncSegment, syncSegment);
  4082. }
  4083. [Fact]
  4084. public async Task OrderBy_OrderByDescending_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  4085. {
  4086. var rand = new Random(42);
  4087. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4088. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => x % 2).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  4089. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4090. await AssertSorted(asyncRes, syncRes);
  4091. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4092. var syncSegment = syncRes.AsEnumerable();
  4093. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4094. {
  4095. asyncSegment = asyncSegment.Skip(skipCount);
  4096. syncSegment = syncSegment.Skip(skipCount);
  4097. }
  4098. foreach (var takeCount in new[] { 31, 29, 23 })
  4099. {
  4100. asyncSegment = asyncSegment.Take(takeCount);
  4101. syncSegment = syncSegment.Take(takeCount);
  4102. }
  4103. await AssertSorted(asyncSegment, syncSegment);
  4104. }
  4105. [Fact]
  4106. public async Task OrderBy_OrderByDescendingAsync_ThenBy_ThenBy()
  4107. {
  4108. var rand = new Random(42);
  4109. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4110. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4111. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4112. await AssertSorted(asyncRes, syncRes);
  4113. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4114. var syncSegment = syncRes.AsEnumerable();
  4115. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4116. {
  4117. asyncSegment = asyncSegment.Skip(skipCount);
  4118. syncSegment = syncSegment.Skip(skipCount);
  4119. }
  4120. foreach (var takeCount in new[] { 31, 29, 23 })
  4121. {
  4122. asyncSegment = asyncSegment.Take(takeCount);
  4123. syncSegment = syncSegment.Take(takeCount);
  4124. }
  4125. await AssertSorted(asyncSegment, syncSegment);
  4126. }
  4127. [Fact]
  4128. public async Task OrderBy_OrderByDescendingAsync_ThenBy_ThenByAsync()
  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 => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  4133. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4134. await AssertSorted(asyncRes, syncRes);
  4135. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4136. var syncSegment = syncRes.AsEnumerable();
  4137. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4138. {
  4139. asyncSegment = asyncSegment.Skip(skipCount);
  4140. syncSegment = syncSegment.Skip(skipCount);
  4141. }
  4142. foreach (var takeCount in new[] { 31, 29, 23 })
  4143. {
  4144. asyncSegment = asyncSegment.Take(takeCount);
  4145. syncSegment = syncSegment.Take(takeCount);
  4146. }
  4147. await AssertSorted(asyncSegment, syncSegment);
  4148. }
  4149. [Fact]
  4150. public async Task OrderBy_OrderByDescendingAsync_ThenBy_ThenByAsyncWithCancellation()
  4151. {
  4152. var rand = new Random(42);
  4153. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4154. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  4155. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4156. await AssertSorted(asyncRes, syncRes);
  4157. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4158. var syncSegment = syncRes.AsEnumerable();
  4159. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4160. {
  4161. asyncSegment = asyncSegment.Skip(skipCount);
  4162. syncSegment = syncSegment.Skip(skipCount);
  4163. }
  4164. foreach (var takeCount in new[] { 31, 29, 23 })
  4165. {
  4166. asyncSegment = asyncSegment.Take(takeCount);
  4167. syncSegment = syncSegment.Take(takeCount);
  4168. }
  4169. await AssertSorted(asyncSegment, syncSegment);
  4170. }
  4171. [Fact]
  4172. public async Task OrderBy_OrderByDescendingAsync_ThenBy_ThenByDescending()
  4173. {
  4174. var rand = new Random(42);
  4175. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4176. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4177. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4178. await AssertSorted(asyncRes, syncRes);
  4179. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4180. var syncSegment = syncRes.AsEnumerable();
  4181. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4182. {
  4183. asyncSegment = asyncSegment.Skip(skipCount);
  4184. syncSegment = syncSegment.Skip(skipCount);
  4185. }
  4186. foreach (var takeCount in new[] { 31, 29, 23 })
  4187. {
  4188. asyncSegment = asyncSegment.Take(takeCount);
  4189. syncSegment = syncSegment.Take(takeCount);
  4190. }
  4191. await AssertSorted(asyncSegment, syncSegment);
  4192. }
  4193. [Fact]
  4194. public async Task OrderBy_OrderByDescendingAsync_ThenBy_ThenByDescendingAsync()
  4195. {
  4196. var rand = new Random(42);
  4197. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4198. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  4199. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4200. await AssertSorted(asyncRes, syncRes);
  4201. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4202. var syncSegment = syncRes.AsEnumerable();
  4203. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4204. {
  4205. asyncSegment = asyncSegment.Skip(skipCount);
  4206. syncSegment = syncSegment.Skip(skipCount);
  4207. }
  4208. foreach (var takeCount in new[] { 31, 29, 23 })
  4209. {
  4210. asyncSegment = asyncSegment.Take(takeCount);
  4211. syncSegment = syncSegment.Take(takeCount);
  4212. }
  4213. await AssertSorted(asyncSegment, syncSegment);
  4214. }
  4215. [Fact]
  4216. public async Task OrderBy_OrderByDescendingAsync_ThenBy_ThenByDescendingAsyncWithCancellation()
  4217. {
  4218. var rand = new Random(42);
  4219. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4220. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  4221. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4222. await AssertSorted(asyncRes, syncRes);
  4223. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4224. var syncSegment = syncRes.AsEnumerable();
  4225. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4226. {
  4227. asyncSegment = asyncSegment.Skip(skipCount);
  4228. syncSegment = syncSegment.Skip(skipCount);
  4229. }
  4230. foreach (var takeCount in new[] { 31, 29, 23 })
  4231. {
  4232. asyncSegment = asyncSegment.Take(takeCount);
  4233. syncSegment = syncSegment.Take(takeCount);
  4234. }
  4235. await AssertSorted(asyncSegment, syncSegment);
  4236. }
  4237. [Fact]
  4238. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync_ThenBy()
  4239. {
  4240. var rand = new Random(42);
  4241. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4242. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  4243. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4244. await AssertSorted(asyncRes, syncRes);
  4245. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4246. var syncSegment = syncRes.AsEnumerable();
  4247. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4248. {
  4249. asyncSegment = asyncSegment.Skip(skipCount);
  4250. syncSegment = syncSegment.Skip(skipCount);
  4251. }
  4252. foreach (var takeCount in new[] { 31, 29, 23 })
  4253. {
  4254. asyncSegment = asyncSegment.Take(takeCount);
  4255. syncSegment = syncSegment.Take(takeCount);
  4256. }
  4257. await AssertSorted(asyncSegment, syncSegment);
  4258. }
  4259. [Fact]
  4260. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync_ThenByAsync()
  4261. {
  4262. var rand = new Random(42);
  4263. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4264. 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));
  4265. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4266. await AssertSorted(asyncRes, syncRes);
  4267. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4268. var syncSegment = syncRes.AsEnumerable();
  4269. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4270. {
  4271. asyncSegment = asyncSegment.Skip(skipCount);
  4272. syncSegment = syncSegment.Skip(skipCount);
  4273. }
  4274. foreach (var takeCount in new[] { 31, 29, 23 })
  4275. {
  4276. asyncSegment = asyncSegment.Take(takeCount);
  4277. syncSegment = syncSegment.Take(takeCount);
  4278. }
  4279. await AssertSorted(asyncSegment, syncSegment);
  4280. }
  4281. [Fact]
  4282. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync_ThenByAsyncWithCancellation()
  4283. {
  4284. var rand = new Random(42);
  4285. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4286. 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));
  4287. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4288. await AssertSorted(asyncRes, syncRes);
  4289. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4290. var syncSegment = syncRes.AsEnumerable();
  4291. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4292. {
  4293. asyncSegment = asyncSegment.Skip(skipCount);
  4294. syncSegment = syncSegment.Skip(skipCount);
  4295. }
  4296. foreach (var takeCount in new[] { 31, 29, 23 })
  4297. {
  4298. asyncSegment = asyncSegment.Take(takeCount);
  4299. syncSegment = syncSegment.Take(takeCount);
  4300. }
  4301. await AssertSorted(asyncSegment, syncSegment);
  4302. }
  4303. [Fact]
  4304. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync_ThenByDescending()
  4305. {
  4306. var rand = new Random(42);
  4307. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4308. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  4309. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4310. await AssertSorted(asyncRes, syncRes);
  4311. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4312. var syncSegment = syncRes.AsEnumerable();
  4313. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4314. {
  4315. asyncSegment = asyncSegment.Skip(skipCount);
  4316. syncSegment = syncSegment.Skip(skipCount);
  4317. }
  4318. foreach (var takeCount in new[] { 31, 29, 23 })
  4319. {
  4320. asyncSegment = asyncSegment.Take(takeCount);
  4321. syncSegment = syncSegment.Take(takeCount);
  4322. }
  4323. await AssertSorted(asyncSegment, syncSegment);
  4324. }
  4325. [Fact]
  4326. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync_ThenByDescendingAsync()
  4327. {
  4328. var rand = new Random(42);
  4329. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4330. 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));
  4331. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4332. await AssertSorted(asyncRes, syncRes);
  4333. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4334. var syncSegment = syncRes.AsEnumerable();
  4335. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4336. {
  4337. asyncSegment = asyncSegment.Skip(skipCount);
  4338. syncSegment = syncSegment.Skip(skipCount);
  4339. }
  4340. foreach (var takeCount in new[] { 31, 29, 23 })
  4341. {
  4342. asyncSegment = asyncSegment.Take(takeCount);
  4343. syncSegment = syncSegment.Take(takeCount);
  4344. }
  4345. await AssertSorted(asyncSegment, syncSegment);
  4346. }
  4347. [Fact]
  4348. public async Task OrderBy_OrderByDescendingAsync_ThenByAsync_ThenByDescendingAsyncWithCancellation()
  4349. {
  4350. var rand = new Random(42);
  4351. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4352. 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));
  4353. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4354. await AssertSorted(asyncRes, syncRes);
  4355. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4356. var syncSegment = syncRes.AsEnumerable();
  4357. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4358. {
  4359. asyncSegment = asyncSegment.Skip(skipCount);
  4360. syncSegment = syncSegment.Skip(skipCount);
  4361. }
  4362. foreach (var takeCount in new[] { 31, 29, 23 })
  4363. {
  4364. asyncSegment = asyncSegment.Take(takeCount);
  4365. syncSegment = syncSegment.Take(takeCount);
  4366. }
  4367. await AssertSorted(asyncSegment, syncSegment);
  4368. }
  4369. [Fact]
  4370. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation_ThenBy()
  4371. {
  4372. var rand = new Random(42);
  4373. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4374. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  4375. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4376. await AssertSorted(asyncRes, syncRes);
  4377. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4378. var syncSegment = syncRes.AsEnumerable();
  4379. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4380. {
  4381. asyncSegment = asyncSegment.Skip(skipCount);
  4382. syncSegment = syncSegment.Skip(skipCount);
  4383. }
  4384. foreach (var takeCount in new[] { 31, 29, 23 })
  4385. {
  4386. asyncSegment = asyncSegment.Take(takeCount);
  4387. syncSegment = syncSegment.Take(takeCount);
  4388. }
  4389. await AssertSorted(asyncSegment, syncSegment);
  4390. }
  4391. [Fact]
  4392. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation_ThenByAsync()
  4393. {
  4394. var rand = new Random(42);
  4395. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4396. 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));
  4397. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4398. await AssertSorted(asyncRes, syncRes);
  4399. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4400. var syncSegment = syncRes.AsEnumerable();
  4401. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4402. {
  4403. asyncSegment = asyncSegment.Skip(skipCount);
  4404. syncSegment = syncSegment.Skip(skipCount);
  4405. }
  4406. foreach (var takeCount in new[] { 31, 29, 23 })
  4407. {
  4408. asyncSegment = asyncSegment.Take(takeCount);
  4409. syncSegment = syncSegment.Take(takeCount);
  4410. }
  4411. await AssertSorted(asyncSegment, syncSegment);
  4412. }
  4413. [Fact]
  4414. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation_ThenByAsyncWithCancellation()
  4415. {
  4416. var rand = new Random(42);
  4417. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4418. 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));
  4419. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4420. await AssertSorted(asyncRes, syncRes);
  4421. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4422. var syncSegment = syncRes.AsEnumerable();
  4423. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4424. {
  4425. asyncSegment = asyncSegment.Skip(skipCount);
  4426. syncSegment = syncSegment.Skip(skipCount);
  4427. }
  4428. foreach (var takeCount in new[] { 31, 29, 23 })
  4429. {
  4430. asyncSegment = asyncSegment.Take(takeCount);
  4431. syncSegment = syncSegment.Take(takeCount);
  4432. }
  4433. await AssertSorted(asyncSegment, syncSegment);
  4434. }
  4435. [Fact]
  4436. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation_ThenByDescending()
  4437. {
  4438. var rand = new Random(42);
  4439. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4440. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  4441. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4442. await AssertSorted(asyncRes, syncRes);
  4443. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4444. var syncSegment = syncRes.AsEnumerable();
  4445. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4446. {
  4447. asyncSegment = asyncSegment.Skip(skipCount);
  4448. syncSegment = syncSegment.Skip(skipCount);
  4449. }
  4450. foreach (var takeCount in new[] { 31, 29, 23 })
  4451. {
  4452. asyncSegment = asyncSegment.Take(takeCount);
  4453. syncSegment = syncSegment.Take(takeCount);
  4454. }
  4455. await AssertSorted(asyncSegment, syncSegment);
  4456. }
  4457. [Fact]
  4458. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation_ThenByDescendingAsync()
  4459. {
  4460. var rand = new Random(42);
  4461. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4462. 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));
  4463. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4464. await AssertSorted(asyncRes, syncRes);
  4465. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4466. var syncSegment = syncRes.AsEnumerable();
  4467. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4468. {
  4469. asyncSegment = asyncSegment.Skip(skipCount);
  4470. syncSegment = syncSegment.Skip(skipCount);
  4471. }
  4472. foreach (var takeCount in new[] { 31, 29, 23 })
  4473. {
  4474. asyncSegment = asyncSegment.Take(takeCount);
  4475. syncSegment = syncSegment.Take(takeCount);
  4476. }
  4477. await AssertSorted(asyncSegment, syncSegment);
  4478. }
  4479. [Fact]
  4480. public async Task OrderBy_OrderByDescendingAsync_ThenByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  4481. {
  4482. var rand = new Random(42);
  4483. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4484. 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));
  4485. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4486. await AssertSorted(asyncRes, syncRes);
  4487. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4488. var syncSegment = syncRes.AsEnumerable();
  4489. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4490. {
  4491. asyncSegment = asyncSegment.Skip(skipCount);
  4492. syncSegment = syncSegment.Skip(skipCount);
  4493. }
  4494. foreach (var takeCount in new[] { 31, 29, 23 })
  4495. {
  4496. asyncSegment = asyncSegment.Take(takeCount);
  4497. syncSegment = syncSegment.Take(takeCount);
  4498. }
  4499. await AssertSorted(asyncSegment, syncSegment);
  4500. }
  4501. [Fact]
  4502. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending_ThenBy()
  4503. {
  4504. var rand = new Random(42);
  4505. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4506. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4507. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4508. await AssertSorted(asyncRes, syncRes);
  4509. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4510. var syncSegment = syncRes.AsEnumerable();
  4511. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4512. {
  4513. asyncSegment = asyncSegment.Skip(skipCount);
  4514. syncSegment = syncSegment.Skip(skipCount);
  4515. }
  4516. foreach (var takeCount in new[] { 31, 29, 23 })
  4517. {
  4518. asyncSegment = asyncSegment.Take(takeCount);
  4519. syncSegment = syncSegment.Take(takeCount);
  4520. }
  4521. await AssertSorted(asyncSegment, syncSegment);
  4522. }
  4523. [Fact]
  4524. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending_ThenByAsync()
  4525. {
  4526. var rand = new Random(42);
  4527. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4528. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  4529. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4530. await AssertSorted(asyncRes, syncRes);
  4531. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4532. var syncSegment = syncRes.AsEnumerable();
  4533. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4534. {
  4535. asyncSegment = asyncSegment.Skip(skipCount);
  4536. syncSegment = syncSegment.Skip(skipCount);
  4537. }
  4538. foreach (var takeCount in new[] { 31, 29, 23 })
  4539. {
  4540. asyncSegment = asyncSegment.Take(takeCount);
  4541. syncSegment = syncSegment.Take(takeCount);
  4542. }
  4543. await AssertSorted(asyncSegment, syncSegment);
  4544. }
  4545. [Fact]
  4546. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending_ThenByAsyncWithCancellation()
  4547. {
  4548. var rand = new Random(42);
  4549. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4550. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  4551. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4552. await AssertSorted(asyncRes, syncRes);
  4553. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4554. var syncSegment = syncRes.AsEnumerable();
  4555. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4556. {
  4557. asyncSegment = asyncSegment.Skip(skipCount);
  4558. syncSegment = syncSegment.Skip(skipCount);
  4559. }
  4560. foreach (var takeCount in new[] { 31, 29, 23 })
  4561. {
  4562. asyncSegment = asyncSegment.Take(takeCount);
  4563. syncSegment = syncSegment.Take(takeCount);
  4564. }
  4565. await AssertSorted(asyncSegment, syncSegment);
  4566. }
  4567. [Fact]
  4568. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending_ThenByDescending()
  4569. {
  4570. var rand = new Random(42);
  4571. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4572. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4573. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4574. await AssertSorted(asyncRes, syncRes);
  4575. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4576. var syncSegment = syncRes.AsEnumerable();
  4577. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4578. {
  4579. asyncSegment = asyncSegment.Skip(skipCount);
  4580. syncSegment = syncSegment.Skip(skipCount);
  4581. }
  4582. foreach (var takeCount in new[] { 31, 29, 23 })
  4583. {
  4584. asyncSegment = asyncSegment.Take(takeCount);
  4585. syncSegment = syncSegment.Take(takeCount);
  4586. }
  4587. await AssertSorted(asyncSegment, syncSegment);
  4588. }
  4589. [Fact]
  4590. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending_ThenByDescendingAsync()
  4591. {
  4592. var rand = new Random(42);
  4593. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4594. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  4595. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4596. await AssertSorted(asyncRes, syncRes);
  4597. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4598. var syncSegment = syncRes.AsEnumerable();
  4599. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4600. {
  4601. asyncSegment = asyncSegment.Skip(skipCount);
  4602. syncSegment = syncSegment.Skip(skipCount);
  4603. }
  4604. foreach (var takeCount in new[] { 31, 29, 23 })
  4605. {
  4606. asyncSegment = asyncSegment.Take(takeCount);
  4607. syncSegment = syncSegment.Take(takeCount);
  4608. }
  4609. await AssertSorted(asyncSegment, syncSegment);
  4610. }
  4611. [Fact]
  4612. public async Task OrderBy_OrderByDescendingAsync_ThenByDescending_ThenByDescendingAsyncWithCancellation()
  4613. {
  4614. var rand = new Random(42);
  4615. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4616. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  4617. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4618. await AssertSorted(asyncRes, syncRes);
  4619. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4620. var syncSegment = syncRes.AsEnumerable();
  4621. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4622. {
  4623. asyncSegment = asyncSegment.Skip(skipCount);
  4624. syncSegment = syncSegment.Skip(skipCount);
  4625. }
  4626. foreach (var takeCount in new[] { 31, 29, 23 })
  4627. {
  4628. asyncSegment = asyncSegment.Take(takeCount);
  4629. syncSegment = syncSegment.Take(takeCount);
  4630. }
  4631. await AssertSorted(asyncSegment, syncSegment);
  4632. }
  4633. [Fact]
  4634. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync_ThenBy()
  4635. {
  4636. var rand = new Random(42);
  4637. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4638. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  4639. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4640. await AssertSorted(asyncRes, syncRes);
  4641. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4642. var syncSegment = syncRes.AsEnumerable();
  4643. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4644. {
  4645. asyncSegment = asyncSegment.Skip(skipCount);
  4646. syncSegment = syncSegment.Skip(skipCount);
  4647. }
  4648. foreach (var takeCount in new[] { 31, 29, 23 })
  4649. {
  4650. asyncSegment = asyncSegment.Take(takeCount);
  4651. syncSegment = syncSegment.Take(takeCount);
  4652. }
  4653. await AssertSorted(asyncSegment, syncSegment);
  4654. }
  4655. [Fact]
  4656. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync_ThenByAsync()
  4657. {
  4658. var rand = new Random(42);
  4659. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4660. 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));
  4661. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4662. await AssertSorted(asyncRes, syncRes);
  4663. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4664. var syncSegment = syncRes.AsEnumerable();
  4665. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4666. {
  4667. asyncSegment = asyncSegment.Skip(skipCount);
  4668. syncSegment = syncSegment.Skip(skipCount);
  4669. }
  4670. foreach (var takeCount in new[] { 31, 29, 23 })
  4671. {
  4672. asyncSegment = asyncSegment.Take(takeCount);
  4673. syncSegment = syncSegment.Take(takeCount);
  4674. }
  4675. await AssertSorted(asyncSegment, syncSegment);
  4676. }
  4677. [Fact]
  4678. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync_ThenByAsyncWithCancellation()
  4679. {
  4680. var rand = new Random(42);
  4681. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4682. 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));
  4683. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4684. await AssertSorted(asyncRes, syncRes);
  4685. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4686. var syncSegment = syncRes.AsEnumerable();
  4687. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4688. {
  4689. asyncSegment = asyncSegment.Skip(skipCount);
  4690. syncSegment = syncSegment.Skip(skipCount);
  4691. }
  4692. foreach (var takeCount in new[] { 31, 29, 23 })
  4693. {
  4694. asyncSegment = asyncSegment.Take(takeCount);
  4695. syncSegment = syncSegment.Take(takeCount);
  4696. }
  4697. await AssertSorted(asyncSegment, syncSegment);
  4698. }
  4699. [Fact]
  4700. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync_ThenByDescending()
  4701. {
  4702. var rand = new Random(42);
  4703. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4704. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  4705. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4706. await AssertSorted(asyncRes, syncRes);
  4707. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4708. var syncSegment = syncRes.AsEnumerable();
  4709. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4710. {
  4711. asyncSegment = asyncSegment.Skip(skipCount);
  4712. syncSegment = syncSegment.Skip(skipCount);
  4713. }
  4714. foreach (var takeCount in new[] { 31, 29, 23 })
  4715. {
  4716. asyncSegment = asyncSegment.Take(takeCount);
  4717. syncSegment = syncSegment.Take(takeCount);
  4718. }
  4719. await AssertSorted(asyncSegment, syncSegment);
  4720. }
  4721. [Fact]
  4722. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync_ThenByDescendingAsync()
  4723. {
  4724. var rand = new Random(42);
  4725. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4726. 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));
  4727. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4728. await AssertSorted(asyncRes, syncRes);
  4729. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4730. var syncSegment = syncRes.AsEnumerable();
  4731. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4732. {
  4733. asyncSegment = asyncSegment.Skip(skipCount);
  4734. syncSegment = syncSegment.Skip(skipCount);
  4735. }
  4736. foreach (var takeCount in new[] { 31, 29, 23 })
  4737. {
  4738. asyncSegment = asyncSegment.Take(takeCount);
  4739. syncSegment = syncSegment.Take(takeCount);
  4740. }
  4741. await AssertSorted(asyncSegment, syncSegment);
  4742. }
  4743. [Fact]
  4744. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  4745. {
  4746. var rand = new Random(42);
  4747. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4748. 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));
  4749. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4750. await AssertSorted(asyncRes, syncRes);
  4751. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4752. var syncSegment = syncRes.AsEnumerable();
  4753. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4754. {
  4755. asyncSegment = asyncSegment.Skip(skipCount);
  4756. syncSegment = syncSegment.Skip(skipCount);
  4757. }
  4758. foreach (var takeCount in new[] { 31, 29, 23 })
  4759. {
  4760. asyncSegment = asyncSegment.Take(takeCount);
  4761. syncSegment = syncSegment.Take(takeCount);
  4762. }
  4763. await AssertSorted(asyncSegment, syncSegment);
  4764. }
  4765. [Fact]
  4766. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation_ThenBy()
  4767. {
  4768. var rand = new Random(42);
  4769. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4770. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  4771. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4772. await AssertSorted(asyncRes, syncRes);
  4773. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4774. var syncSegment = syncRes.AsEnumerable();
  4775. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4776. {
  4777. asyncSegment = asyncSegment.Skip(skipCount);
  4778. syncSegment = syncSegment.Skip(skipCount);
  4779. }
  4780. foreach (var takeCount in new[] { 31, 29, 23 })
  4781. {
  4782. asyncSegment = asyncSegment.Take(takeCount);
  4783. syncSegment = syncSegment.Take(takeCount);
  4784. }
  4785. await AssertSorted(asyncSegment, syncSegment);
  4786. }
  4787. [Fact]
  4788. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation_ThenByAsync()
  4789. {
  4790. var rand = new Random(42);
  4791. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4792. 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));
  4793. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4794. await AssertSorted(asyncRes, syncRes);
  4795. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4796. var syncSegment = syncRes.AsEnumerable();
  4797. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4798. {
  4799. asyncSegment = asyncSegment.Skip(skipCount);
  4800. syncSegment = syncSegment.Skip(skipCount);
  4801. }
  4802. foreach (var takeCount in new[] { 31, 29, 23 })
  4803. {
  4804. asyncSegment = asyncSegment.Take(takeCount);
  4805. syncSegment = syncSegment.Take(takeCount);
  4806. }
  4807. await AssertSorted(asyncSegment, syncSegment);
  4808. }
  4809. [Fact]
  4810. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  4811. {
  4812. var rand = new Random(42);
  4813. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4814. 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));
  4815. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  4816. await AssertSorted(asyncRes, syncRes);
  4817. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4818. var syncSegment = syncRes.AsEnumerable();
  4819. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4820. {
  4821. asyncSegment = asyncSegment.Skip(skipCount);
  4822. syncSegment = syncSegment.Skip(skipCount);
  4823. }
  4824. foreach (var takeCount in new[] { 31, 29, 23 })
  4825. {
  4826. asyncSegment = asyncSegment.Take(takeCount);
  4827. syncSegment = syncSegment.Take(takeCount);
  4828. }
  4829. await AssertSorted(asyncSegment, syncSegment);
  4830. }
  4831. [Fact]
  4832. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation_ThenByDescending()
  4833. {
  4834. var rand = new Random(42);
  4835. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4836. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending(x => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  4837. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4838. await AssertSorted(asyncRes, syncRes);
  4839. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4840. var syncSegment = syncRes.AsEnumerable();
  4841. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4842. {
  4843. asyncSegment = asyncSegment.Skip(skipCount);
  4844. syncSegment = syncSegment.Skip(skipCount);
  4845. }
  4846. foreach (var takeCount in new[] { 31, 29, 23 })
  4847. {
  4848. asyncSegment = asyncSegment.Take(takeCount);
  4849. syncSegment = syncSegment.Take(takeCount);
  4850. }
  4851. await AssertSorted(asyncSegment, syncSegment);
  4852. }
  4853. [Fact]
  4854. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  4855. {
  4856. var rand = new Random(42);
  4857. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4858. 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));
  4859. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4860. await AssertSorted(asyncRes, syncRes);
  4861. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4862. var syncSegment = syncRes.AsEnumerable();
  4863. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4864. {
  4865. asyncSegment = asyncSegment.Skip(skipCount);
  4866. syncSegment = syncSegment.Skip(skipCount);
  4867. }
  4868. foreach (var takeCount in new[] { 31, 29, 23 })
  4869. {
  4870. asyncSegment = asyncSegment.Take(takeCount);
  4871. syncSegment = syncSegment.Take(takeCount);
  4872. }
  4873. await AssertSorted(asyncSegment, syncSegment);
  4874. }
  4875. [Fact]
  4876. public async Task OrderBy_OrderByDescendingAsync_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  4877. {
  4878. var rand = new Random(42);
  4879. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4880. 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));
  4881. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  4882. await AssertSorted(asyncRes, syncRes);
  4883. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4884. var syncSegment = syncRes.AsEnumerable();
  4885. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4886. {
  4887. asyncSegment = asyncSegment.Skip(skipCount);
  4888. syncSegment = syncSegment.Skip(skipCount);
  4889. }
  4890. foreach (var takeCount in new[] { 31, 29, 23 })
  4891. {
  4892. asyncSegment = asyncSegment.Take(takeCount);
  4893. syncSegment = syncSegment.Take(takeCount);
  4894. }
  4895. await AssertSorted(asyncSegment, syncSegment);
  4896. }
  4897. [Fact]
  4898. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy_ThenBy()
  4899. {
  4900. var rand = new Random(42);
  4901. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4902. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4903. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4904. await AssertSorted(asyncRes, syncRes);
  4905. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4906. var syncSegment = syncRes.AsEnumerable();
  4907. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4908. {
  4909. asyncSegment = asyncSegment.Skip(skipCount);
  4910. syncSegment = syncSegment.Skip(skipCount);
  4911. }
  4912. foreach (var takeCount in new[] { 31, 29, 23 })
  4913. {
  4914. asyncSegment = asyncSegment.Take(takeCount);
  4915. syncSegment = syncSegment.Take(takeCount);
  4916. }
  4917. await AssertSorted(asyncSegment, syncSegment);
  4918. }
  4919. [Fact]
  4920. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy_ThenByAsync()
  4921. {
  4922. var rand = new Random(42);
  4923. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4924. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  4925. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4926. await AssertSorted(asyncRes, syncRes);
  4927. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4928. var syncSegment = syncRes.AsEnumerable();
  4929. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4930. {
  4931. asyncSegment = asyncSegment.Skip(skipCount);
  4932. syncSegment = syncSegment.Skip(skipCount);
  4933. }
  4934. foreach (var takeCount in new[] { 31, 29, 23 })
  4935. {
  4936. asyncSegment = asyncSegment.Take(takeCount);
  4937. syncSegment = syncSegment.Take(takeCount);
  4938. }
  4939. await AssertSorted(asyncSegment, syncSegment);
  4940. }
  4941. [Fact]
  4942. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy_ThenByAsyncWithCancellation()
  4943. {
  4944. var rand = new Random(42);
  4945. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4946. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  4947. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  4948. await AssertSorted(asyncRes, syncRes);
  4949. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4950. var syncSegment = syncRes.AsEnumerable();
  4951. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4952. {
  4953. asyncSegment = asyncSegment.Skip(skipCount);
  4954. syncSegment = syncSegment.Skip(skipCount);
  4955. }
  4956. foreach (var takeCount in new[] { 31, 29, 23 })
  4957. {
  4958. asyncSegment = asyncSegment.Take(takeCount);
  4959. syncSegment = syncSegment.Take(takeCount);
  4960. }
  4961. await AssertSorted(asyncSegment, syncSegment);
  4962. }
  4963. [Fact]
  4964. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy_ThenByDescending()
  4965. {
  4966. var rand = new Random(42);
  4967. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4968. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4969. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4970. await AssertSorted(asyncRes, syncRes);
  4971. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4972. var syncSegment = syncRes.AsEnumerable();
  4973. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4974. {
  4975. asyncSegment = asyncSegment.Skip(skipCount);
  4976. syncSegment = syncSegment.Skip(skipCount);
  4977. }
  4978. foreach (var takeCount in new[] { 31, 29, 23 })
  4979. {
  4980. asyncSegment = asyncSegment.Take(takeCount);
  4981. syncSegment = syncSegment.Take(takeCount);
  4982. }
  4983. await AssertSorted(asyncSegment, syncSegment);
  4984. }
  4985. [Fact]
  4986. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy_ThenByDescendingAsync()
  4987. {
  4988. var rand = new Random(42);
  4989. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  4990. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  4991. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  4992. await AssertSorted(asyncRes, syncRes);
  4993. var asyncSegment = asyncRes.AsAsyncEnumerable();
  4994. var syncSegment = syncRes.AsEnumerable();
  4995. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  4996. {
  4997. asyncSegment = asyncSegment.Skip(skipCount);
  4998. syncSegment = syncSegment.Skip(skipCount);
  4999. }
  5000. foreach (var takeCount in new[] { 31, 29, 23 })
  5001. {
  5002. asyncSegment = asyncSegment.Take(takeCount);
  5003. syncSegment = syncSegment.Take(takeCount);
  5004. }
  5005. await AssertSorted(asyncSegment, syncSegment);
  5006. }
  5007. [Fact]
  5008. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenBy_ThenByDescendingAsyncWithCancellation()
  5009. {
  5010. var rand = new Random(42);
  5011. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5012. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  5013. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  5014. await AssertSorted(asyncRes, syncRes);
  5015. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5016. var syncSegment = syncRes.AsEnumerable();
  5017. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5018. {
  5019. asyncSegment = asyncSegment.Skip(skipCount);
  5020. syncSegment = syncSegment.Skip(skipCount);
  5021. }
  5022. foreach (var takeCount in new[] { 31, 29, 23 })
  5023. {
  5024. asyncSegment = asyncSegment.Take(takeCount);
  5025. syncSegment = syncSegment.Take(takeCount);
  5026. }
  5027. await AssertSorted(asyncSegment, syncSegment);
  5028. }
  5029. [Fact]
  5030. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync_ThenBy()
  5031. {
  5032. var rand = new Random(42);
  5033. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5034. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  5035. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  5036. await AssertSorted(asyncRes, syncRes);
  5037. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5038. var syncSegment = syncRes.AsEnumerable();
  5039. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5040. {
  5041. asyncSegment = asyncSegment.Skip(skipCount);
  5042. syncSegment = syncSegment.Skip(skipCount);
  5043. }
  5044. foreach (var takeCount in new[] { 31, 29, 23 })
  5045. {
  5046. asyncSegment = asyncSegment.Take(takeCount);
  5047. syncSegment = syncSegment.Take(takeCount);
  5048. }
  5049. await AssertSorted(asyncSegment, syncSegment);
  5050. }
  5051. [Fact]
  5052. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync_ThenByAsync()
  5053. {
  5054. var rand = new Random(42);
  5055. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5056. 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));
  5057. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  5058. await AssertSorted(asyncRes, syncRes);
  5059. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5060. var syncSegment = syncRes.AsEnumerable();
  5061. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5062. {
  5063. asyncSegment = asyncSegment.Skip(skipCount);
  5064. syncSegment = syncSegment.Skip(skipCount);
  5065. }
  5066. foreach (var takeCount in new[] { 31, 29, 23 })
  5067. {
  5068. asyncSegment = asyncSegment.Take(takeCount);
  5069. syncSegment = syncSegment.Take(takeCount);
  5070. }
  5071. await AssertSorted(asyncSegment, syncSegment);
  5072. }
  5073. [Fact]
  5074. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync_ThenByAsyncWithCancellation()
  5075. {
  5076. var rand = new Random(42);
  5077. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5078. 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));
  5079. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  5080. await AssertSorted(asyncRes, syncRes);
  5081. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5082. var syncSegment = syncRes.AsEnumerable();
  5083. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5084. {
  5085. asyncSegment = asyncSegment.Skip(skipCount);
  5086. syncSegment = syncSegment.Skip(skipCount);
  5087. }
  5088. foreach (var takeCount in new[] { 31, 29, 23 })
  5089. {
  5090. asyncSegment = asyncSegment.Take(takeCount);
  5091. syncSegment = syncSegment.Take(takeCount);
  5092. }
  5093. await AssertSorted(asyncSegment, syncSegment);
  5094. }
  5095. [Fact]
  5096. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync_ThenByDescending()
  5097. {
  5098. var rand = new Random(42);
  5099. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5100. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  5101. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  5102. await AssertSorted(asyncRes, syncRes);
  5103. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5104. var syncSegment = syncRes.AsEnumerable();
  5105. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5106. {
  5107. asyncSegment = asyncSegment.Skip(skipCount);
  5108. syncSegment = syncSegment.Skip(skipCount);
  5109. }
  5110. foreach (var takeCount in new[] { 31, 29, 23 })
  5111. {
  5112. asyncSegment = asyncSegment.Take(takeCount);
  5113. syncSegment = syncSegment.Take(takeCount);
  5114. }
  5115. await AssertSorted(asyncSegment, syncSegment);
  5116. }
  5117. [Fact]
  5118. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync_ThenByDescendingAsync()
  5119. {
  5120. var rand = new Random(42);
  5121. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5122. 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));
  5123. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  5124. await AssertSorted(asyncRes, syncRes);
  5125. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5126. var syncSegment = syncRes.AsEnumerable();
  5127. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5128. {
  5129. asyncSegment = asyncSegment.Skip(skipCount);
  5130. syncSegment = syncSegment.Skip(skipCount);
  5131. }
  5132. foreach (var takeCount in new[] { 31, 29, 23 })
  5133. {
  5134. asyncSegment = asyncSegment.Take(takeCount);
  5135. syncSegment = syncSegment.Take(takeCount);
  5136. }
  5137. await AssertSorted(asyncSegment, syncSegment);
  5138. }
  5139. [Fact]
  5140. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsync_ThenByDescendingAsyncWithCancellation()
  5141. {
  5142. var rand = new Random(42);
  5143. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5144. 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));
  5145. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  5146. await AssertSorted(asyncRes, syncRes);
  5147. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5148. var syncSegment = syncRes.AsEnumerable();
  5149. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5150. {
  5151. asyncSegment = asyncSegment.Skip(skipCount);
  5152. syncSegment = syncSegment.Skip(skipCount);
  5153. }
  5154. foreach (var takeCount in new[] { 31, 29, 23 })
  5155. {
  5156. asyncSegment = asyncSegment.Take(takeCount);
  5157. syncSegment = syncSegment.Take(takeCount);
  5158. }
  5159. await AssertSorted(asyncSegment, syncSegment);
  5160. }
  5161. [Fact]
  5162. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation_ThenBy()
  5163. {
  5164. var rand = new Random(42);
  5165. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5166. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  5167. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  5168. await AssertSorted(asyncRes, syncRes);
  5169. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5170. var syncSegment = syncRes.AsEnumerable();
  5171. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5172. {
  5173. asyncSegment = asyncSegment.Skip(skipCount);
  5174. syncSegment = syncSegment.Skip(skipCount);
  5175. }
  5176. foreach (var takeCount in new[] { 31, 29, 23 })
  5177. {
  5178. asyncSegment = asyncSegment.Take(takeCount);
  5179. syncSegment = syncSegment.Take(takeCount);
  5180. }
  5181. await AssertSorted(asyncSegment, syncSegment);
  5182. }
  5183. [Fact]
  5184. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByAsync()
  5185. {
  5186. var rand = new Random(42);
  5187. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5188. 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));
  5189. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  5190. await AssertSorted(asyncRes, syncRes);
  5191. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5192. var syncSegment = syncRes.AsEnumerable();
  5193. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5194. {
  5195. asyncSegment = asyncSegment.Skip(skipCount);
  5196. syncSegment = syncSegment.Skip(skipCount);
  5197. }
  5198. foreach (var takeCount in new[] { 31, 29, 23 })
  5199. {
  5200. asyncSegment = asyncSegment.Take(takeCount);
  5201. syncSegment = syncSegment.Take(takeCount);
  5202. }
  5203. await AssertSorted(asyncSegment, syncSegment);
  5204. }
  5205. [Fact]
  5206. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByAsyncWithCancellation()
  5207. {
  5208. var rand = new Random(42);
  5209. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5210. 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));
  5211. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenBy(x => x % 4);
  5212. await AssertSorted(asyncRes, syncRes);
  5213. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5214. var syncSegment = syncRes.AsEnumerable();
  5215. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5216. {
  5217. asyncSegment = asyncSegment.Skip(skipCount);
  5218. syncSegment = syncSegment.Skip(skipCount);
  5219. }
  5220. foreach (var takeCount in new[] { 31, 29, 23 })
  5221. {
  5222. asyncSegment = asyncSegment.Take(takeCount);
  5223. syncSegment = syncSegment.Take(takeCount);
  5224. }
  5225. await AssertSorted(asyncSegment, syncSegment);
  5226. }
  5227. [Fact]
  5228. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByDescending()
  5229. {
  5230. var rand = new Random(42);
  5231. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5232. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenBy((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  5233. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  5234. await AssertSorted(asyncRes, syncRes);
  5235. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5236. var syncSegment = syncRes.AsEnumerable();
  5237. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5238. {
  5239. asyncSegment = asyncSegment.Skip(skipCount);
  5240. syncSegment = syncSegment.Skip(skipCount);
  5241. }
  5242. foreach (var takeCount in new[] { 31, 29, 23 })
  5243. {
  5244. asyncSegment = asyncSegment.Take(takeCount);
  5245. syncSegment = syncSegment.Take(takeCount);
  5246. }
  5247. await AssertSorted(asyncSegment, syncSegment);
  5248. }
  5249. [Fact]
  5250. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByDescendingAsync()
  5251. {
  5252. var rand = new Random(42);
  5253. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5254. 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));
  5255. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  5256. await AssertSorted(asyncRes, syncRes);
  5257. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5258. var syncSegment = syncRes.AsEnumerable();
  5259. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5260. {
  5261. asyncSegment = asyncSegment.Skip(skipCount);
  5262. syncSegment = syncSegment.Skip(skipCount);
  5263. }
  5264. foreach (var takeCount in new[] { 31, 29, 23 })
  5265. {
  5266. asyncSegment = asyncSegment.Take(takeCount);
  5267. syncSegment = syncSegment.Take(takeCount);
  5268. }
  5269. await AssertSorted(asyncSegment, syncSegment);
  5270. }
  5271. [Fact]
  5272. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  5273. {
  5274. var rand = new Random(42);
  5275. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5276. 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));
  5277. var syncRes = xs.OrderByDescending(x => x % 2).ThenBy(x => x % 3).ThenByDescending(x => x % 4);
  5278. await AssertSorted(asyncRes, syncRes);
  5279. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5280. var syncSegment = syncRes.AsEnumerable();
  5281. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5282. {
  5283. asyncSegment = asyncSegment.Skip(skipCount);
  5284. syncSegment = syncSegment.Skip(skipCount);
  5285. }
  5286. foreach (var takeCount in new[] { 31, 29, 23 })
  5287. {
  5288. asyncSegment = asyncSegment.Take(takeCount);
  5289. syncSegment = syncSegment.Take(takeCount);
  5290. }
  5291. await AssertSorted(asyncSegment, syncSegment);
  5292. }
  5293. [Fact]
  5294. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending_ThenBy()
  5295. {
  5296. var rand = new Random(42);
  5297. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5298. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  5299. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  5300. await AssertSorted(asyncRes, syncRes);
  5301. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5302. var syncSegment = syncRes.AsEnumerable();
  5303. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5304. {
  5305. asyncSegment = asyncSegment.Skip(skipCount);
  5306. syncSegment = syncSegment.Skip(skipCount);
  5307. }
  5308. foreach (var takeCount in new[] { 31, 29, 23 })
  5309. {
  5310. asyncSegment = asyncSegment.Take(takeCount);
  5311. syncSegment = syncSegment.Take(takeCount);
  5312. }
  5313. await AssertSorted(asyncSegment, syncSegment);
  5314. }
  5315. [Fact]
  5316. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending_ThenByAsync()
  5317. {
  5318. var rand = new Random(42);
  5319. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5320. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy(x => new ValueTask<int>(x % 4));
  5321. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  5322. await AssertSorted(asyncRes, syncRes);
  5323. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5324. var syncSegment = syncRes.AsEnumerable();
  5325. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5326. {
  5327. asyncSegment = asyncSegment.Skip(skipCount);
  5328. syncSegment = syncSegment.Skip(skipCount);
  5329. }
  5330. foreach (var takeCount in new[] { 31, 29, 23 })
  5331. {
  5332. asyncSegment = asyncSegment.Take(takeCount);
  5333. syncSegment = syncSegment.Take(takeCount);
  5334. }
  5335. await AssertSorted(asyncSegment, syncSegment);
  5336. }
  5337. [Fact]
  5338. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending_ThenByAsyncWithCancellation()
  5339. {
  5340. var rand = new Random(42);
  5341. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5342. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenBy((x, ct) => new ValueTask<int>(x % 4));
  5343. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  5344. await AssertSorted(asyncRes, syncRes);
  5345. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5346. var syncSegment = syncRes.AsEnumerable();
  5347. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5348. {
  5349. asyncSegment = asyncSegment.Skip(skipCount);
  5350. syncSegment = syncSegment.Skip(skipCount);
  5351. }
  5352. foreach (var takeCount in new[] { 31, 29, 23 })
  5353. {
  5354. asyncSegment = asyncSegment.Take(takeCount);
  5355. syncSegment = syncSegment.Take(takeCount);
  5356. }
  5357. await AssertSorted(asyncSegment, syncSegment);
  5358. }
  5359. [Fact]
  5360. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending_ThenByDescending()
  5361. {
  5362. var rand = new Random(42);
  5363. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5364. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  5365. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  5366. await AssertSorted(asyncRes, syncRes);
  5367. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5368. var syncSegment = syncRes.AsEnumerable();
  5369. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5370. {
  5371. asyncSegment = asyncSegment.Skip(skipCount);
  5372. syncSegment = syncSegment.Skip(skipCount);
  5373. }
  5374. foreach (var takeCount in new[] { 31, 29, 23 })
  5375. {
  5376. asyncSegment = asyncSegment.Take(takeCount);
  5377. syncSegment = syncSegment.Take(takeCount);
  5378. }
  5379. await AssertSorted(asyncSegment, syncSegment);
  5380. }
  5381. [Fact]
  5382. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending_ThenByDescendingAsync()
  5383. {
  5384. var rand = new Random(42);
  5385. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5386. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending(x => new ValueTask<int>(x % 4));
  5387. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  5388. await AssertSorted(asyncRes, syncRes);
  5389. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5390. var syncSegment = syncRes.AsEnumerable();
  5391. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5392. {
  5393. asyncSegment = asyncSegment.Skip(skipCount);
  5394. syncSegment = syncSegment.Skip(skipCount);
  5395. }
  5396. foreach (var takeCount in new[] { 31, 29, 23 })
  5397. {
  5398. asyncSegment = asyncSegment.Take(takeCount);
  5399. syncSegment = syncSegment.Take(takeCount);
  5400. }
  5401. await AssertSorted(asyncSegment, syncSegment);
  5402. }
  5403. [Fact]
  5404. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescending_ThenByDescendingAsyncWithCancellation()
  5405. {
  5406. var rand = new Random(42);
  5407. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5408. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescending((x, ct) => new ValueTask<int>(x % 4));
  5409. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  5410. await AssertSorted(asyncRes, syncRes);
  5411. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5412. var syncSegment = syncRes.AsEnumerable();
  5413. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5414. {
  5415. asyncSegment = asyncSegment.Skip(skipCount);
  5416. syncSegment = syncSegment.Skip(skipCount);
  5417. }
  5418. foreach (var takeCount in new[] { 31, 29, 23 })
  5419. {
  5420. asyncSegment = asyncSegment.Take(takeCount);
  5421. syncSegment = syncSegment.Take(takeCount);
  5422. }
  5423. await AssertSorted(asyncSegment, syncSegment);
  5424. }
  5425. [Fact]
  5426. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync_ThenBy()
  5427. {
  5428. var rand = new Random(42);
  5429. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5430. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  5431. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  5432. await AssertSorted(asyncRes, syncRes);
  5433. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5434. var syncSegment = syncRes.AsEnumerable();
  5435. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5436. {
  5437. asyncSegment = asyncSegment.Skip(skipCount);
  5438. syncSegment = syncSegment.Skip(skipCount);
  5439. }
  5440. foreach (var takeCount in new[] { 31, 29, 23 })
  5441. {
  5442. asyncSegment = asyncSegment.Take(takeCount);
  5443. syncSegment = syncSegment.Take(takeCount);
  5444. }
  5445. await AssertSorted(asyncSegment, syncSegment);
  5446. }
  5447. [Fact]
  5448. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync_ThenByAsync()
  5449. {
  5450. var rand = new Random(42);
  5451. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5452. 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));
  5453. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  5454. await AssertSorted(asyncRes, syncRes);
  5455. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5456. var syncSegment = syncRes.AsEnumerable();
  5457. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5458. {
  5459. asyncSegment = asyncSegment.Skip(skipCount);
  5460. syncSegment = syncSegment.Skip(skipCount);
  5461. }
  5462. foreach (var takeCount in new[] { 31, 29, 23 })
  5463. {
  5464. asyncSegment = asyncSegment.Take(takeCount);
  5465. syncSegment = syncSegment.Take(takeCount);
  5466. }
  5467. await AssertSorted(asyncSegment, syncSegment);
  5468. }
  5469. [Fact]
  5470. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync_ThenByAsyncWithCancellation()
  5471. {
  5472. var rand = new Random(42);
  5473. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5474. 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));
  5475. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  5476. await AssertSorted(asyncRes, syncRes);
  5477. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5478. var syncSegment = syncRes.AsEnumerable();
  5479. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5480. {
  5481. asyncSegment = asyncSegment.Skip(skipCount);
  5482. syncSegment = syncSegment.Skip(skipCount);
  5483. }
  5484. foreach (var takeCount in new[] { 31, 29, 23 })
  5485. {
  5486. asyncSegment = asyncSegment.Take(takeCount);
  5487. syncSegment = syncSegment.Take(takeCount);
  5488. }
  5489. await AssertSorted(asyncSegment, syncSegment);
  5490. }
  5491. [Fact]
  5492. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync_ThenByDescending()
  5493. {
  5494. var rand = new Random(42);
  5495. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5496. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  5497. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  5498. await AssertSorted(asyncRes, syncRes);
  5499. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5500. var syncSegment = syncRes.AsEnumerable();
  5501. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5502. {
  5503. asyncSegment = asyncSegment.Skip(skipCount);
  5504. syncSegment = syncSegment.Skip(skipCount);
  5505. }
  5506. foreach (var takeCount in new[] { 31, 29, 23 })
  5507. {
  5508. asyncSegment = asyncSegment.Take(takeCount);
  5509. syncSegment = syncSegment.Take(takeCount);
  5510. }
  5511. await AssertSorted(asyncSegment, syncSegment);
  5512. }
  5513. [Fact]
  5514. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync_ThenByDescendingAsync()
  5515. {
  5516. var rand = new Random(42);
  5517. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5518. 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));
  5519. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  5520. await AssertSorted(asyncRes, syncRes);
  5521. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5522. var syncSegment = syncRes.AsEnumerable();
  5523. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5524. {
  5525. asyncSegment = asyncSegment.Skip(skipCount);
  5526. syncSegment = syncSegment.Skip(skipCount);
  5527. }
  5528. foreach (var takeCount in new[] { 31, 29, 23 })
  5529. {
  5530. asyncSegment = asyncSegment.Take(takeCount);
  5531. syncSegment = syncSegment.Take(takeCount);
  5532. }
  5533. await AssertSorted(asyncSegment, syncSegment);
  5534. }
  5535. [Fact]
  5536. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsync_ThenByDescendingAsyncWithCancellation()
  5537. {
  5538. var rand = new Random(42);
  5539. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5540. 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));
  5541. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  5542. await AssertSorted(asyncRes, syncRes);
  5543. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5544. var syncSegment = syncRes.AsEnumerable();
  5545. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5546. {
  5547. asyncSegment = asyncSegment.Skip(skipCount);
  5548. syncSegment = syncSegment.Skip(skipCount);
  5549. }
  5550. foreach (var takeCount in new[] { 31, 29, 23 })
  5551. {
  5552. asyncSegment = asyncSegment.Take(takeCount);
  5553. syncSegment = syncSegment.Take(takeCount);
  5554. }
  5555. await AssertSorted(asyncSegment, syncSegment);
  5556. }
  5557. [Fact]
  5558. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenBy()
  5559. {
  5560. var rand = new Random(42);
  5561. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5562. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenBy(x => x % 4);
  5563. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  5564. await AssertSorted(asyncRes, syncRes);
  5565. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5566. var syncSegment = syncRes.AsEnumerable();
  5567. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5568. {
  5569. asyncSegment = asyncSegment.Skip(skipCount);
  5570. syncSegment = syncSegment.Skip(skipCount);
  5571. }
  5572. foreach (var takeCount in new[] { 31, 29, 23 })
  5573. {
  5574. asyncSegment = asyncSegment.Take(takeCount);
  5575. syncSegment = syncSegment.Take(takeCount);
  5576. }
  5577. await AssertSorted(asyncSegment, syncSegment);
  5578. }
  5579. [Fact]
  5580. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByAsync()
  5581. {
  5582. var rand = new Random(42);
  5583. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5584. 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));
  5585. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  5586. await AssertSorted(asyncRes, syncRes);
  5587. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5588. var syncSegment = syncRes.AsEnumerable();
  5589. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5590. {
  5591. asyncSegment = asyncSegment.Skip(skipCount);
  5592. syncSegment = syncSegment.Skip(skipCount);
  5593. }
  5594. foreach (var takeCount in new[] { 31, 29, 23 })
  5595. {
  5596. asyncSegment = asyncSegment.Take(takeCount);
  5597. syncSegment = syncSegment.Take(takeCount);
  5598. }
  5599. await AssertSorted(asyncSegment, syncSegment);
  5600. }
  5601. [Fact]
  5602. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByAsyncWithCancellation()
  5603. {
  5604. var rand = new Random(42);
  5605. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5606. 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));
  5607. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenBy(x => x % 4);
  5608. await AssertSorted(asyncRes, syncRes);
  5609. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5610. var syncSegment = syncRes.AsEnumerable();
  5611. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5612. {
  5613. asyncSegment = asyncSegment.Skip(skipCount);
  5614. syncSegment = syncSegment.Skip(skipCount);
  5615. }
  5616. foreach (var takeCount in new[] { 31, 29, 23 })
  5617. {
  5618. asyncSegment = asyncSegment.Take(takeCount);
  5619. syncSegment = syncSegment.Take(takeCount);
  5620. }
  5621. await AssertSorted(asyncSegment, syncSegment);
  5622. }
  5623. [Fact]
  5624. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByDescending()
  5625. {
  5626. var rand = new Random(42);
  5627. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5628. var asyncRes = xs.ToAsyncEnumerable().OrderByDescending((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending((x, ct) => new ValueTask<int>(x % 3)).ThenByDescending(x => x % 4);
  5629. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  5630. await AssertSorted(asyncRes, syncRes);
  5631. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5632. var syncSegment = syncRes.AsEnumerable();
  5633. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5634. {
  5635. asyncSegment = asyncSegment.Skip(skipCount);
  5636. syncSegment = syncSegment.Skip(skipCount);
  5637. }
  5638. foreach (var takeCount in new[] { 31, 29, 23 })
  5639. {
  5640. asyncSegment = asyncSegment.Take(takeCount);
  5641. syncSegment = syncSegment.Take(takeCount);
  5642. }
  5643. await AssertSorted(asyncSegment, syncSegment);
  5644. }
  5645. [Fact]
  5646. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsync()
  5647. {
  5648. var rand = new Random(42);
  5649. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5650. 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));
  5651. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  5652. await AssertSorted(asyncRes, syncRes);
  5653. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5654. var syncSegment = syncRes.AsEnumerable();
  5655. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5656. {
  5657. asyncSegment = asyncSegment.Skip(skipCount);
  5658. syncSegment = syncSegment.Skip(skipCount);
  5659. }
  5660. foreach (var takeCount in new[] { 31, 29, 23 })
  5661. {
  5662. asyncSegment = asyncSegment.Take(takeCount);
  5663. syncSegment = syncSegment.Take(takeCount);
  5664. }
  5665. await AssertSorted(asyncSegment, syncSegment);
  5666. }
  5667. [Fact]
  5668. public async Task OrderBy_OrderByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation_ThenByDescendingAsyncWithCancellation()
  5669. {
  5670. var rand = new Random(42);
  5671. var xs = Enumerable.Range(0, 100).Select(x => rand.Next(0, 100)).ToArray().Select(x => x);
  5672. 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));
  5673. var syncRes = xs.OrderByDescending(x => x % 2).ThenByDescending(x => x % 3).ThenByDescending(x => x % 4);
  5674. await AssertSorted(asyncRes, syncRes);
  5675. var asyncSegment = asyncRes.AsAsyncEnumerable();
  5676. var syncSegment = syncRes.AsEnumerable();
  5677. foreach (var skipCount in new[] { 3, 7, 2, 5 })
  5678. {
  5679. asyncSegment = asyncSegment.Skip(skipCount);
  5680. syncSegment = syncSegment.Skip(skipCount);
  5681. }
  5682. foreach (var takeCount in new[] { 31, 29, 23 })
  5683. {
  5684. asyncSegment = asyncSegment.Take(takeCount);
  5685. syncSegment = syncSegment.Take(takeCount);
  5686. }
  5687. await AssertSorted(asyncSegment, syncSegment);
  5688. }
  5689. private async Task AssertSorted<T>(IAsyncEnumerable<T> asyncRes, IEnumerable<T> syncRes)
  5690. {
  5691. Assert.True(await syncRes.ToAsyncEnumerable().SequenceEqualAsync(asyncRes));
  5692. Assert.True(syncRes.ToList().SequenceEqual(await asyncRes.ToListAsync()));
  5693. Assert.True(syncRes.ToArray().SequenceEqual(await asyncRes.ToArrayAsync()));
  5694. int syncCount = syncRes.Count();
  5695. int asyncCount = await asyncRes.CountAsync();
  5696. Assert.Equal(syncCount, asyncCount);
  5697. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  5698. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  5699. for (var i = 0; i < syncCount; i++)
  5700. {
  5701. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  5702. }
  5703. }
  5704. }
  5705. }