a ze8@sjdZddlZddlmZmZmZddlmZddZddZ d d Z d d Z Gd dde Z GdddejZGdddZddZGdddeZGdddeedZGdddeZGdddeZeeGdddejZGd d!d!e ZGd"d#d#eZGd$d%d%e ZGd&d'd'e ZGd(d)d)eZGd*d+d+ZGd,d-d-ejZed.krfe dS)/z9Tests for binary operators on subtypes of built-in types.N)eqlene)ABCMetacCs|r|||}}q|S)z1Greatest common divisor using Euclid's algorithm.)abrrC/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_binop.pygcdsr cCs t|tS)z-Test whether an object is an instance of int.) isinstanceintxrrr isint srcCs$tttfD]}t||r dSq dS)zATest whether an object is an instance of a built-in numeric type.r)r floatcomplexr )rTrrr isnums rcCs t|tS)z7Test whether an object is an instance of the Rat class.)r Ratr rrr isRatsrc@seZdZdZddgZd/ddZdd Zeed Zd d Z ee d Z d dZ ddZ ddZ ddZddZeZddZddZddZeZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd S)0rz9Rational number implemented as a normalized pair of ints. _Rat__num _Rat__denrrcCsbt|std|t|s(td||dkr8tdt||}t|||_t|||_dS)z[Constructor: Rat([num[, den]]). The arguments must be ints, and default to (0, 1).zRat numerator must be int (%r)z Rat denominator must be int (%r)rzzero denominatorN)r TypeErrorZeroDivisionErrorr r rr)selfnumdengrrr __init__"s   z Rat.__init__cCs|jS)z7Accessor function for read-only 'num' attribute of Rat.)rrrrr _get_num1sz Rat._get_numNcCs|jS)z7Accessor function for read-only 'den' attribute of Rat.)rr rrr _get_den6sz Rat._get_dencCsd|j|jfS)zt|j|j|j|j|j|jSt|rRt||StS)z$Add two Rats, or a Rat and a number.rrrrrrrNotImplementedrotherrrr __add__Qs  z Rat.__add__cCsVt|rt|}t|r>t|j|j|j|j|j|jSt|rRt||StS)z)Subtract two Rats, or a Rat and a number.r-r/rrr __sub__^s  z Rat.__sub__cCsVt|rt|}t|r>t|j|j|j|j|j|jSt|rR|t|StS)z9Subtract two Rats, or a Rat and a number (reversed args).r-r/rrr __rsub__is  z Rat.__rsub__cCsTt|r"t|j|j|j|jSt|rDivide two Rats, returning the floored result (reversed args).r#r8rrr __rfloordiv__szRat.__rfloordiv__cCs6t|rt|}n t|stS||}||||fS)z2Divide two Rats, returning quotient and remainder.)rrrr.r8rrr __divmod__s  zRat.__divmod__cCs(t|rt|}n t|stSt||S)zBDivide two Rats, returning quotient and remainder (reversed args).)rrrr.divmodr/rrr __rdivmod__s  zRat.__rdivmod__cCst||dS)zTake one Rat modulo another.rr<r/rrr __mod__sz Rat.__mod__cCst||dS)z,Take one Rat modulo another (reversed args).rr>r/rrr __rmod__sz Rat.__rmod__cCsTt|r|jdko|j|kSt|r<|j|jko:|j|jkSt|rPt||kStS)zCompare two Rats for equality.r)rrrrrrr.r/rrr __eq__s z Rat.__eq__)rr)__name__ __module__ __qualname____doc__ __slots__rr!propertyrr"rr$r&r(r,r1__radd__r2r3r5__rmul__r6r7r9r:r;r=r?r@rArrrr rs4            rc@sXeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dS) RatTestCasez3Unit tests for Rat class and its support utilities.cCs|tddd|tddd|tddd|tddd|td dd |tdd d|td d d tdd D]l}tdd D]\}|t||d k|t| |d k|t|| d k|t| | d kqqdS) N  rdr) assertEqualr range assertTrue)rijrrr test_gcdszRatTestCase.test_gcdc Csjtdd}||jd||jdtdd}||jd||jdtdd}||jd||jdtdd}||jd||jdtd}||jd||jd ztd d }WntyYn 0|d d d ddgidttf D]f}z t|}Wnty"Yn0|d|ztd |}WntyTYq0|d|qdS)NrKrNrMirSrRrrz(Rat(1, 0) didn't raise ZeroDivisionError0gyrzRat(%r) didn't raise TypeErrorz!Rat(1, %r) didn't raise TypeError)rrUrrrZfailunittestr)rrZbadrrr test_constructors>       zRatTestCase.test_constructorcCs|tddtddd|tdddtdd|dtddtdd|dtddd|tdddddS)NrMr[rrOr'?rUrr rrr test_adds zRatTestCase.test_addcCs|tddtddtdd|tdddtdd|dtddtdd|tdddd |dtddd dS) Nr\rMrOrKrr[r'?rar rrr test_subs "zRatTestCase.test_subcCs~|tddtddtdd|tdddd|dtddd|tdddd|dtddddS) NrMr[rOr\rKrcrdr'rar rrr test_mul s "zRatTestCase.test_mulcCs|tddtddtdd|tdddtdd|dtdtdd|dtd dd |td ddd dS) NrKr[rOr\ rM@rr`rar rrr test_divs "zRatTestCase.test_divcCs`|tdtdd|tddtddd|tddd|dtdddS)NrKrMr[rar rrr test_floordivszRatTestCase.test_floordivcCsZ|tdtdd|tdd|dtd|tdd|dtddS)NrKrTrMg$@rar rrr test_eqs zRatTestCase.test_eqcCs|tddtddtdd|tdddtdd|dtdtdd|dtd dd |td ddd |td d dS) NrKr[rOr\rgrhrMrirr`z1/2rd)rUrevalr rrr test_true_div$s "zRatTestCase.test_true_divN) rBrCrDrErZr_rbrerfrjrlrmrorrrr rJs$rJc@s eZdZdZddZddZdS)OperationLoggerz.Base class for classes with operation logging.cCs ||_dSNlogger)rrsrrr r1szOperationLogger.__init__cGs|j|dSrqrr)rargsrrr log_operation3szOperationLogger.log_operationN)rBrCrDrErrurrrr rp/srpcGsFg}g}|D]}|||jq z ||Wnty@Yn0|S)zvReturn the sequence of operations that results from applying the operation `op` to instances of the given classes.)appendr)opclasseslogZ instancescrrr op_sequence6s  r{c@s$eZdZddZddZddZdS)AcCs|dtS)NA.__eq__rur.r/rrr rAEs r}cCs|dtS)NA.__le__r~r/rrr __le__Hs rcCs|dtS)NA.__ge__r~r/rrr __ge__Ks rNrBrCrDrArrrrrr r|Dsr|c@s$eZdZddZddZddZdS)BcCs|dtS)NB.__eq__r~r/rrr rAPs rcCs|dtS)NB.__le__r~r/rrr rSs rcCs|dtS)NB.__ge__r~r/rrr rVs rNrrrrr rOsr) metaclassc@s$eZdZddZddZddZdS)CcCs|dtS)NC.__eq__r~r/rrr rA[s rcCs|dtS)NC.__le__r~r/rrr r^s rcCs|dtS)NC.__ge__r~r/rrr ras rNrrrrr rZsrc@s(eZdZdZddZddZddZdS) VzVirtual subclass of BcCs|dtS)NV.__eq__r~r/rrr rAgs rcCs|dtS)NV.__le__r~r/rrr rjs rcCs|dtS)NV.__ge__r~r/rrr rms rN)rBrCrDrErArrrrrr resrc@seZdZddZdS)OperationOrderTestscCs4|ttttddg|ttttddg|ttttddg|ttttddg|ttttddg|ttttddg|ttttddg|ttttddg|ttttddg|ttttd dg|tt t|tttt dd g|tttt dd gdS) Nr}rrrrrrrrrr) rUr{rr|rrrrW issubclassrr rrr test_comparison_orderstsz*OperationOrderTests.test_comparison_ordersN)rBrCrDrrrrr rssrc@seZdZdZddZdS)SupEqzClass that can test equalitycCsdSNTrr/rrr rAsz SupEq.__eq__NrBrCrDrErArrrr rsrc@seZdZdZdZdS)Sz"Subclass of SupEq that should failNrrrrr rsrc@seZdZdZdS)Fz'Independent class that should fall backN)rBrCrDrErrrr rsrc@seZdZdZdZdS)Xz"Independent class that should failNrrrrr rsrc@seZdZdZdZdS)SNz>Subclass of SupEq that can test equality, but not non-equalityN)rBrCrDrE__ne__rrrr rsrc@seZdZdZddZdZdS)XNz>Independent class that can test equality, but not non-equalitycCsdSrrr/rrr rAsz XN.__eq__N)rBrCrDrErArrrrr rsrc@s eZdZdZddZddZdS)FallbackBlockingTestsz#Unit tests for None method blockingcCsttttf\}}}}|||||||||||||tt|||tt|||tt||dSrq)rrrrrU assertRaisesrr)refsrrrr test_fallback_rmethod_blockings    z4FallbackBlockingTests.test_fallback_rmethod_blockingcCsfttt}}}|||k|tt|||tt|||||k|tt||dSrq)rrrZ assertFalserrr)rrZsnZxnrrr test_fallback_ne_blockings z/FallbackBlockingTests.test_fallback_ne_blockingN)rBrCrDrErrrrrr rs r__main__)!rEr^operatorrrrabcrr rrrobjectrZTestCaserJrpr{r|rrrregisterrrrrrrrrrBmainrrrr s6 *j