OrderBy.Generated.cs 305 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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().OrderByAwait(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().OrderByAwaitWithCancellation((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().OrderByDescendingAwait(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().OrderByDescendingAwaitWithCancellation((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).ThenByAwait(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).ThenByAwaitWithCancellation((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).ThenByDescendingAwait(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).ThenByDescendingAwaitWithCancellation((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().OrderByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((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().OrderByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((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().OrderByAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((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().OrderByAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((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).ThenByAwait(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).ThenByAwaitWithCancellation((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).ThenByDescendingAwait(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).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((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().OrderByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((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).ThenByAwait(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).ThenByAwaitWithCancellation((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).ThenByDescendingAwait(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).ThenByDescendingAwaitWithCancellation((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).ThenByAwait(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).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByAwait(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).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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).ThenByAwait(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).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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).ThenByAwaitWithCancellation((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).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwait(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).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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).ThenByAwaitWithCancellation((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).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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).ThenByAwait(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).ThenByAwaitWithCancellation((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).ThenByDescendingAwait(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).ThenByDescendingAwaitWithCancellation((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).ThenByDescendingAwait(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).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByAwait(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).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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).ThenByDescendingAwait(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).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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).ThenByDescendingAwaitWithCancellation((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).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwait(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).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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).ThenByDescendingAwaitWithCancellation((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).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByAwaitWithCancellation((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().OrderByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescendingAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescendingAwaitWithCancellation((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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByAwaitWithCancellation((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().OrderByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescendingAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescendingAwaitWithCancellation((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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByAwaitWithCancellation((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().OrderByAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescendingAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescendingAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByAwaitWithCancellation((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().OrderByAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescendingAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescendingAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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).ThenByAwait(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).ThenByAwaitWithCancellation((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).ThenByDescendingAwait(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).ThenByDescendingAwaitWithCancellation((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).ThenByAwait(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).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByAwait(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).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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).ThenByAwait(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).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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).ThenByAwaitWithCancellation((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).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwait(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).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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).ThenByAwaitWithCancellation((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).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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).ThenByAwait(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).ThenByAwaitWithCancellation((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).ThenByDescendingAwait(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).ThenByDescendingAwaitWithCancellation((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).ThenByDescendingAwait(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).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByAwait(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).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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).ThenByDescendingAwait(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).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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).ThenByDescendingAwaitWithCancellation((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).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwait(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).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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).ThenByDescendingAwaitWithCancellation((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).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByAwaitWithCancellation((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().OrderByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByAwaitWithCancellation((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().OrderByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByDescendingAwait(x => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescendingAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenBy(x => x % 3).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescendingAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescending(x => x % 3).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwait(x => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwait(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().OrderByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 2)).ThenByDescendingAwaitWithCancellation((x, ct) => new ValueTask<int>(x % 3)).ThenByDescendingAwaitWithCancellation((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. var toListResult = await asyncRes.ToListAsync();
  5693. Assert.True(syncRes.ToList().SequenceEqual(toListResult));
  5694. var toArrayResult = await asyncRes.ToArrayAsync();
  5695. Assert.True(syncRes.ToArray().SequenceEqual(toArrayResult));
  5696. int syncCount = syncRes.Count();
  5697. int asyncCount = await asyncRes.CountAsync();
  5698. Assert.Equal(syncCount, asyncCount);
  5699. Assert.Equal(syncRes.First(), await asyncRes.FirstAsync());
  5700. Assert.Equal(syncRes.Last(), await asyncRes.LastAsync());
  5701. for (var i = 0; i < syncCount; i++)
  5702. {
  5703. Assert.Equal(syncRes.ElementAt(i), await asyncRes.ElementAtAsync(i));
  5704. }
  5705. }
  5706. }
  5707. }