a zet@sddlZddlZddlZddlZddlZddlZddlZddlZzddl m Z Wne yfdZ Yn0ddZ Gdddej ZGdddZGd d d eZGd d d ZGd ddeZGdddeZee dudGdddej ZedkredS)Nhamtcstfdd}|S)z*Needed to make reftracking test mode work.cs t}|jg|Ri|SN contextvarsContextrun)argskwargsctxfuncE/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_context.pywrappersz!isolated_context..wrapper) functoolswraps)r rrr risolated_contextsrc@seZdZddZeddZddZddZd d Zd d Z d dZ ddZ ddZ eddZ ddZddZddZeddZeddZedd Zed!d"Zed#d$Zd%d&Zed'd(Zd)S)* ContextTestcCs|tdtWdn1s*0Y|tdtdWdn1s`0Ytd}||jd|td|_Wdn1s0Y|t |t ddS)Nztakes exactly 1z must be a straaabbb) assertRaisesRegex TypeErrorr ContextVar assertEqualname assertRaisesAttributeErrorassertNotEqualhash)selfcrrrtest_context_var_new_1s&(  $z"ContextTest.test_context_var_new_1cCstd}|dt|tjddd}|dt|g}tjd|d}|||dt||dt||d}|t|t||dt||||dt|dS)Na{defaultZ123z...rz used )rrassertInreprappendset assertNotInreset)r!r"Zlsttrrrtest_context_var_repr_1)s    z#ContextTest.test_context_var_repr_1cCs|td"Gdddtj}Wdn1s40Y|td"Gdddtj}Wdn1sr0Y|td"Gdddtj}Wdn1s0YdS)Nznot an acceptable base typec@s eZdZdS)z.MyContextVarN__name__ __module__ __qualname__rrrr MyContextVar?sr4c@s eZdZdS)z9ContextTest.test_context_subclassing_1..MyContextNr0rrrr MyContextDsr5c@s eZdZdS)z7ContextTest.test_context_subclassing_1..MyTokenNr0rrrrMyTokenHsr6)rrrrrToken)r!r4r5r6rrrtest_context_subclassing_1=s 00z&ContextTest.test_context_subclassing_1cCs|tdtdWdn1s,0Y|tdtjdddWdn1sf0Y|tdtjddWdn1s0YtjfiidS)Nz any argumentsrr$)rrrrr!rrrtest_context_new_1Ks(,*zContextTest.test_context_new_1cCst}|td|dWdn1s20Y|tdd|vWdn1sf0Y|td|dWdn1s0YdS)NzContextVar key was expectedr)rrrrgetr!r rrrtest_context_typerrors_1Ts&&z$ContextTest.test_context_typerrors_1cCst}||tjdSr)r copy_contextZassertIsInstancerr=rrrtest_context_get_context_1^sz&ContextTest.test_context_get_context_1cCs@t}|td|Wdn1s20YdS)Nzmissing 1 required)rrrrrr=rrrtest_context_run_1bszContextTest.test_context_run_1cCst}dd}|t|fD]}|||dddif|||ddddif||j|dd dddd f||j|d dd d ddd fi}||j|d fi|d ddif||iqdS) Nc_sd|d<|d7}||fS)Nfoospambarrr r rrrr ksz,ContextTest.test_context_run_2..funcrDrCrBr)rrEr9)r$rC )rHrE)rrrpartialrr)r!r r fr$rrrtest_context_run_2hs&    zContextTest.test_context_run_2cCst}dd}|t||Wdn1s:0Y|t||ddWdn1sr0Y|t"|j|ddddWdn1s0YdS)Nc_s dddS)NrrrrFrrrr sz,ContextTest.test_context_run_3..funcrrGr%r9)rrrZeroDivisionErrorrr!r r rrrtest_context_run_3s ( , zContextTest.test_context_run_3cspt}ttdfddfdd}||}|||d|dS)NvarcsddSr) assertIsNoner<rr!rOrrfunc2sz-ContextTest.test_context_run_4..func2csbddddt}t|d|d|S)NrCr)rPr<r+rrrr?len)Zcurctx2rRr!rOrrfunc1s  z-ContextTest.test_context_run_4..func1rC)rrrrrr()r!ctx1rVZ returned_ctxrrTrtest_context_run_4s   zContextTest.test_context_run_4csht}tdfdd}t||Wdn1sJ0YddS)NrOcs&dddddS)NrCrr)rPr<r+rrQrrr s z,ContextTest.test_context_run_5..func)rrrrrLrrPr<rMrrQrtest_context_run_5s   (zContextTest.test_context_run_5cs4ttjdddfdd}|dS)Nr$rr&csPdddddS)Nr*)rr<rPr+rr"r r!rrfuns  z+ContextTest.test_context_run_6..funrrrrr!r\rr[rtest_context_run_6szContextTest.test_context_run_6cs&tfdddS)Ncs:tdWdn1s,0YdS)Nzis already entered)r RuntimeErrorrrr r\r!rrr\sz+ContextTest.test_context_run_7..funrr:rrartest_context_run_7szContextTest.test_context_run_7cCstd}|t|Wdn1s20Y||d|d}||d||dd||j |j ||j tj j ||j ||d}||d||dd||j d| |||d||dd|d|td| |Wdn1sF0Y||dt}|||| ||td| |Wdn1s0Y||d||||||d|||dd|t|d|t||dfg|t|dg|t||g|t||gt}||||t||Wdn1s0Y|||dd|t|d|t|gdS) Nr"rZrCZspam2zhas already been usedaarr)rrr LookupErrorr<rPr+rassertIs old_valueMISSINGr7rOr-rr`r?r(rSlistitemsvalueskeysr,KeyError)r!r"t0r.rWrUrrrtest_context_getset_1sT  &    *  *   (z!ContextTest.test_context_getset_1cCsXtd}td}|d}|td||Wdn1sJ0YdS)Nv1v2rZzby a different)rrr+r ValueErrorr-)r!rorpt1rrrtest_context_getset_2s    z!ContextTest.test_context_getset_2cs4tjdddtfdd}|dS)Nr"rZr&cs dtWdn1s80Yddtgd}tgd |tgtWdn1s0YdS)NrZrCr) rr<rrlrPr,rhrkr+r-)r.r[rrr\s &    z.ContextTest.test_context_getset_3..fun)rrrrr^rr[rtest_context_getset_3sz!ContextTest.test_context_getset_3cCs^tjddd}t}||jd}|td||Wdn1sP0YdS)Nr"rZr&rzdifferent Context)rrrrr+rrqr-)r!r"r tokrrrtest_context_getset_4's z!ContextTest.test_context_getset_4csHtjdddgfdd}t|gdS)Nr"rZr&cs.gddgdS)NrZ)r+r<r*rrr"r!rrr\6s z.ContextTest.test_context_getset_5..fun)rrr+r?rrr<r^rrwrtest_context_getset_51s  z!ContextTest.test_context_getset_5csDttjdddfdd}fdd|dS)Nr"rZr&cszd}|ddd|d|d|ddS)N )r+copyrr)rUr"rWctx2_funr!rrctx1_funBs   z1ContextTest.test_context_copy_1..ctx1_funcs.ddddS)Nryr{)rr<r+rrwrrr~Ps z1ContextTest.test_context_copy_1..ctx2_funr])r!rrr}rtest_context_copy_1>s zContextTest.test_context_copy_1csjtdfdd}tjjdd}z t||td}W|n |0 |ttddS)NcvarcsFtdD]8}||ttdd||q|S)NrygMbP?g?)ranger+timesleeprandomuniformrr<)numirr!rrsub[s  z/ContextTest.test_context_threads_1..subry) max_workers) rr concurrentfuturesThreadPoolExecutorrhmaprshutdownr)r!rtpresultsrrrtest_context_threads_1Ws z"ContextTest.test_context_threads_1N)r1r2r3r#rr/r8r;r>r@rArKrNrXrYr_rbrnrsrtrvrxrrrrrrrs8       5    rc@s6eZdZdZddddZddZddZd d ZdS) HashKeyNerror_on_eq_tocCs"|dks J||_||_||_dS)N)rr r)r!r rrrrr__init__ps zHashKey.__init__cCsd|jd|jdS)Nz )rr r:rrr__repr__vszHashKey.__repr__cCs|jdur|jjrt|jSr)_crasher error_on_hash HashingErrorr r:rrr__hash__yszHashKey.__hash__cCst|tstS|jdur$|jjr$t|jdurL|j|urLtd|d||jdurt|j|urttd|d||j|j f|j|j fkS)Nzcannot compare z to ) isinstancerNotImplementedr error_on_eqEqErrorrrqrr r!otherrrr__eq__s zHashKey.__eq__)r1r2r3rrrrrrrrrrms rcs(eZdZfddZfddZZS)KeyStrcs tjdurtjjrttSr)rrrrsuperrr: __class__rrrszKeyStr.__hash__cs"tjdurtjjrtt|Sr)rrrrrrrrrrrsz KeyStr.__eq__)r1r2r3rr __classcell__rrrrrs rc@s,eZdZdddddZddZddZd S) HaskKeyCrasherFrrcCs||_||_dSrr)r!rrrrrrszHaskKeyCrasher.__init__cCstjdurtd|t_dS)Nzcannot nest crashers)rrr`r:rrr __enter__s zHaskKeyCrasher.__enter__cGs dt_dSr)rr)r!excrrr__exit__szHaskKeyCrasher.__exit__N)r1r2r3rrrrrrrrsrc@s eZdZdS)rNr0rrrrrsrc@s eZdZdS)rNr0rrrrrsrz!_testcapi lacks "hamt()" functionc@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-S).HamtTestcCsntdd}tdd}||||t|t|t}d||<d||<|||d|||ddS)Nryrrr$b)rrrr dict)r!k1k2drrrtest_hashkey_helper_1s   zHamtTest.test_hashkey_helper_1cCst}d}dSrrr!hrrrtest_hamt_basics_1szHamtTest.test_hamt_basics_1cCsBt}|t|d|dd}||||t|d|t|d||d||ddd||dd|dd}||||t|d|t|d|t|d||dd||dd||d||d||d||ddd}}}dS)Nrr$rrrZryrG)rrrSr+ assertIsNotrPr<)r!rh2h3rrrtest_hamt_basics_2s*    zHamtTest.test_hamt_basics_2cCs4t}t}|d|}|d|}|||dS)N1)robjectr+re)r!roh1rrrrtest_hamt_basics_3s   zHamtTest.test_hamt_basics_3cCsft}|dg}|dg}||||t|d|t|d||d|ddS)Nkeyr)rr+rrrSr<)r!rrrrrrtest_hamt_basics_4s   zHamtTest.test_hamt_basics_4c Cstdd}tdd}tdd}t}||d}||d}|||d|||d|||d|||d|||d|||d||d}||d}|||d|||d|||d|||d|||d|||d|||d|||d|||d|t|d |t|d |t|d |t|d |t|d dS) Nryrrcccr$rZccrcrrrG)rrr+rr<rS) r!rrZk3rrrZh4Zh5rrrtest_hamt_collision_1s8       zHamtTest.test_hamt_collision_1c Csd}d}d}d}d}t|D]p}t}t}t|D]J} t| } | |stddF|t|| | Wdn1s0YWdn1s0Y|| | }| |s&tddH|t| t| Wdn1s0YWdn1s0Y| || <| t |t || |s6| t| t| | t | t | q6| t ||t|D]} | | t| d | qt t|} t| t| D]\} } t| } | |sXtddF|t|| Wdn1s.0YWdn1sN0Y| |stddJ|t|t| Wdn1s0YWdn1s0Y|| }| | | d d || =| t |t || |d kr|} |}| |s| t|t|| t |t |q| t |d | t |d |D] } | | t| || q| t |t | t| D]\} } | t| } | | t| d d |t| d| t |t || |s| t|t|| t |t |q| t |d | t |d | t | gqdS) NiXiarHrTrrz not foundrGr)rrrrrrrr+rr<rrSrirhrshuffle enumeratedeleter|rkstrpoprj)r!ZCOLLECTION_SIZEZTEST_ITERS_EVERYZCRASH_HASH_EVERYZCRASH_EQ_EVERYZ RUN_XTIMES_rrrrZkeys_to_deleteZiter_iZhmZdmrrrtest_hamt_stresss  H    L      J   N     zHamtTest.test_hamt_stressc Csvtdd}tdd}tdd}tdd}td d }td d }tdd |d}t}||d}||d}||d}||d}||d}t|} ||}|t|| d|td||Wdn1s0Y||}|t|| d||} || |||}|t|| d|| |dd|| |d|| |ddS)NdAeBfCgDhEZErrr$rr"rercannot comparerGrrZ) rrr+rSrrrrqrer<) r!rrrrrrrrorig_lenrrrrtest_hamt_delete_1qs6            (    zHamtTest.test_hamt_delete_1c Cstdd}tdd}tdd}tdd}td d }td d }tdd |d}t}||d}||d}||d}||d}||d}t|} |td||Wdn1s0Y||}|t|| ||}|t|| d||}|t|| d||}|t|| d|||d|||d||}||}||}||}|t|ddS)Nrr)r鉊rrrrrrrrrr$rr"rrrrrGrr) rrr+rSrrqrrr<) r!rrrrrrrrrrrrtest_hamt_delete_2s>            (        zHamtTest.test_hamt_delete_2cCstdd}tdd}tdd}tdd}tdd }t}||d }||d }||d }||d }||d}t|}||}|t||d||}|t||d|||d |||d dS)Nrrrrrrrrr$rr"rrrrG)rrr+rSrrr<r!rrrrrrrrrrtest_hamt_delete_3s$            zHamtTest.test_hamt_delete_3cCstdd}tdd}tdd}tdd}tdd}t}||d }||d }||d }||d }||d }t|}||}|t||d||}|t||d||}|t||d||}|t||d||}|t|ddS)Nrrrrrrrrr$rr"rrrrGrr)rrr+rSrrrrrrtest_hamt_delete_4s,               zHamtTest.test_hamt_delete_4cCs"t}g}tdD].}t|t|}||||d|}qtdd}||d}|t|d||d}|t|d||}|t|d||d}|t|d||d }|t|d ||d }|t|d |D]}||}q|t|d dS) Nzval-Z18Z collisionrGrr) rrrrr*r+rrSr)r!rrkrrZcollision_key16rrrtest_hamt_delete_5 s,      zHamtTest.test_hamt_delete_5c Cstdd}tdd}tdd}tdd}td d }td d }t}||d }||d}||d}||d}||d}||d}|}|tt||d f|df|df|df|df|dfhdS)NrrrrrrrrrrnFr$rr"rrrJrrr+rirrh r!rrrrrrritrrrtest_hamt_items_1Qs$             &zHamtTest.test_hamt_items_1c Cstdd}tdd}tdd}tdd}tdd}td d }t}||d }||d }||d }||d}||d}||d}|}|tt||d f|d f|d f|df|df|dfhdSNrrrrrrrrrrr$rr"rrrJrrrrrtest_hamt_items_2fs$             &zHamtTest.test_hamt_items_2c Cstdd}tdd}tdd}tdd}tdd}td d }t}||d }||d }||d }||d}||d}||d}|tt|||||||h|tt|||||||hdSr)rrr+rrhrk)r!rrrrrrrrrrtest_hamt_keys_1{s            $zHamtTest.test_hamt_keys_1cCs2t}|t|d|t|gdS)Nr)rrrSrirhrrrrtest_hamt_items_3szHamtTest.test_hamt_items_3cCstdd}tdd}tdd}tdd}tdd }t}||d }||d }||d }||d }t}||d }|||k|||k||d }|||k|||k||d }|||k|||k||d}|||k|||k||d }|||k|||k||d}|||k|||k||}|||k|||k||d }|||k|||kdS)Nrrrrrrrxrr$rr"rZd2r)rrr+ assertFalse assertTruer)r!rrrrrrrrrrtest_hamt_eq_1sF                 zHamtTest.test_hamt_eq_1cCstdd}tdd|d}t}||d}t}||d}|td||kWdn1sf0Y|td||kWdn1s0YdS)Nrrrrr$r)rrr+rrq)r!rrrrrrrtest_hamt_eq_2s   &zHamtTest.test_hamt_eq_2cCstdd}t}|dd}t|}g}||||g}|||||||}~~~ttt||dS)Nrrr) rrr+weakrefrefr*gccollectrP)r!rrrr$rrrrtest_hamt_gc_1s         zHamtTest.test_hamt_gc_1cCsxtdd}tdd}t}||d}|||}t|}|}t|~~ttt| |dS)Nrrrrr$) rrr+rrrinextrrrP)r!rrrrhirrrtest_hamt_gc_2s     zHamtTest.test_hamt_gc_2c Cstdd}tdd}tdd}t}||d}|||v|||v|tBtdd||vWdn1s0YWdn1s0Y|tBtdd||vWdn1s0YWdn1s0YdS NrrrrrTrr) rrr+rrrrrrr!rZAArrrrrtest_hamt_in_1s      D  zHamtTest.test_hamt_in_1c CsBtdd}tdd}tdd}t}||d}|||d|||d|t||Wdn1sx0Y|tBtdd||Wdn1s0YWdn1s0Y|tDtdd||Wdn1s0YWdn1s40YdSr) rrr+rrrlrrrr rrrtest_hamt_getitem_1s     &  D  zHamtTest.test_hamt_getitem_1N)r1r2r3rrrrrrrrrrrrrrrrrrrrr r rrrrrs, %Y-4$*1/r__main__)concurrent.futuresrrrrrrZunittestrZ _testcapir ImportErrorrZTestCaserrrrr ExceptionrrZskipIfrr1mainrrrrs8   V! |