
    ;e8                     j   d Z ddlZddlmZmZmZ ddlmZ d Zd Z	d Z
d Z G d	 d
e          Z G d dej                  Z G d d          Zd Z G d de          Z G d dee          Z G d de          Z G d de          Ze                    e            G d dej                  Z G d de          Z G d de          Z G d d e          Z G d! d"e          Z G d# d$e          Z G d% d&          Z G d' d(ej                  Zed)k    r ej                      dS dS )*z9Tests for binary operators on subtypes of built-in types.    N)eqlene)ABCMetac                     | r	|| z  | }} | 	|S )z1Greatest common divisor using Euclid's algorithm. )abs     &/usr/lib/python3.11/test/test_binop.pygcdr      s&    
 sA1  H    c                 ,    t          | t                    S )z-Test whether an object is an instance of int.)
isinstanceintxs    r   isintr          ar   c                 Z    t           t          t          fD ]}t          | |          r dS dS )zATest whether an object is an instance of a built-in numeric type.   r   )r   floatcomplexr   )r   Ts     r   isnumr      s;    %   a 	11	1r   c                 ,    t          | t                    S )z7Test whether an object is an instance of the Rat class.)r   Ratr   s    r   isRatr      r   r   c                       e Zd ZdZddgZddZd Z eed          Zd	 Z	 ee	d          Z
d
 Zd Zd Zd Zd ZeZd Zd Zd ZeZd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )r   z9Rational number implemented as a normalized pair of ints.	_Rat__num	_Rat__denr   r   c                 0   t          |          st          d|z            t          |          st          d|z            |dk    rt          d          t          ||          }t	          ||z            | _        t	          ||z            | _        dS )z[Constructor: Rat([num[, den]]).

        The arguments must be ints, and default to (0, 1).zRat numerator must be int (%r)z Rat denominator must be int (%r)r   zzero denominatorN)r   	TypeErrorZeroDivisionErrorr   r   r   r    )selfnumdengs       r   __init__zRat.__init__"   s     Szz 	D<sBCCCSzz 	F>DEEE!88#$6777SMMa[[
a[[


r   c                     | j         S )z7Accessor function for read-only 'num' attribute of Rat.)r   r$   s    r   _get_numzRat._get_num1   
    zr   Nc                     | j         S )z7Accessor function for read-only 'den' attribute of Rat.)r    r*   s    r   _get_denzRat._get_den6   r,   r   c                 $    d| j         | j        fz  S )z<Convert a Rat to a string resembling a Rat constructor call.zRat(%d, %d)r   r    r*   s    r   __repr__zRat.__repr__;   s    
DJ777r   c                 :    t          t          |                     S )z=Convert a Rat to a string resembling a decimal numeric value.)strr   r*   s    r   __str__zRat.__str__?   s    5;;r   c                 &    | j         dz  | j        z  S )zConvert a Rat to a float.      ?r0   r*   s    r   	__float__zRat.__float__C   s    z#~dj((r   c                     | j         dk    rB	 t          | j                  S # t          $ r  t          dt	          |           z            w xY wt          dt	          |           z            )z,Convert a Rat to an int; self.den must be 1.r   z%s too large to convert to intzcan't convert %s to int)r    r   r   OverflowErrorrepr
ValueErrorr*   s    r   __int__zRat.__int__G   sz    :??24:&  2 2 2#$D&*4jj%1 2 2 22 2T$ZZ?@@@s	   ! *Ac                     t          |          rt          |          }t          |          r:t          | j        |j        z  |j        | j        z  z   | j        |j        z            S t          |          rt          |           |z   S t          S )z$Add two Rats, or a Rat and a number.r   r   r   r   r    r   r   NotImplementedr$   others     r   __add__zRat.__add__Q       << 	JJE<< 	/tz%+-DJ0FFz%+-/ / /<< 	';;&&r   c                     t          |          rt          |          }t          |          r:t          | j        |j        z  |j        | j        z  z
  | j        |j        z            S t          |          rt          |           |z
  S t          S )z)Subtract two Rats, or a Rat and a number.r>   r@   s     r   __sub__zRat.__sub__^   rC   r   c                     t          |          rt          |          }t          |          r:t          |j        | j        z  | j        |j        z  z
  | j        |j        z            S t          |          r|t          |           z
  S t          S )z9Subtract two Rats, or a Rat and a number (reversed args).r>   r@   s     r   __rsub__zRat.__rsub__i   s    << 	JJE<< 	/u{4:-
5;0FFz%+-/ / /<< 	'5;;&&r   c                    t          |          r*t          | j        |j        z  | j        |j        z            S t	          |          rt          | j        |z  | j                  S t          |          rt          |           |z  S t          S )z)Multiply two Rats, or a Rat and a number.r   r   r   r    r   r   r   r?   r@   s     r   __mul__zRat.__mul__t   s    << 	Gtz%+-tz%+/EFFF<< 	5tz%'444<< 	%;;u$$r   c                    t          |          r*t          | j        |j        z  | j        |j        z            S t	          |          rt          | j        | j        |z            S t          |          rt          |           |z  S t          S )z'Divide two Rats, or a Rat and a number.rI   r@   s     r   __truediv__zRat.__truediv__   s    << 	Gtz%+-tz%+/EFFF<< 	5tz4:e#3444<< 	';;&&r   c                    t          |          r*t          |j        | j        z  |j        | j        z            S t	          |          rt          || j        z  | j                  S t          |          r|t          |           z  S t          S )z7Divide two Rats, or a Rat and a number (reversed args).rI   r@   s     r   __rtruediv__zRat.__rtruediv__   s    << 	Gu{4:-u{4:/EFFF<< 	5uTZ'444<< 	'5;;&&r   c                     t          |          rt          |          }nt          |          st          S | |z  }|j        |j        z  S )z.Divide two Rats, returning the floored result.)r   r   r   r?   r   r    r$   rA   r   s      r   __floordiv__zRat.__floordiv__   sI    << 	"JJEEu 	"!!Jw!'!!r   c                 *    || z  }|j         |j        z  S )z>Divide two Rats, returning the floored result (reversed args).r0   rP   s      r   __rfloordiv__zRat.__rfloordiv__   s    $Jw!'!!r   c                     t          |          rt          |          }nt          |          st          S | |z  }|| ||z  z
  fS )z2Divide two Rats, returning quotient and remainder.)r   r   r   r?   rP   s      r   
__divmod__zRat.__divmod__   sN    << 	"JJEEu 	"!!%K4%!)#$$r   c                     t          |          rt          |          }nt          |          st          S t	          ||           S )zBDivide two Rats, returning quotient and remainder (reversed args).)r   r   r   r?   divmodr@   s     r   __rdivmod__zRat.__rdivmod__   sA    << 	"JJEEu 	"!!eT"""r   c                 .    t          | |          d         S )zTake one Rat modulo another.r   rW   r@   s     r   __mod__zRat.__mod__   s    dE""1%%r   c                 .    t          ||           d         S )z,Take one Rat modulo another (reversed args).r   rZ   r@   s     r   __rmod__zRat.__rmod__   s    eT""1%%r   c                     t          |          r| j        dk    o
| j        |k    S t          |          r | j        |j        k    o| j        |j        k    S t	          |          rt          |           |k    S t          S )zCompare two Rats for equality.r   )r   r    r   r   r   r   r?   r@   s     r   __eq__z
Rat.__eq__   sy    << 	;:?:tzU'::<< 	K:,Ju{1JJ<< 	(;;%''r   )r   r   )__name__
__module____qualname____doc__	__slots__r(   r+   propertyr%   r.   r&   r1   r4   r7   r<   rB   __radd__rE   rG   rJ   __rmul__rL   rN   rQ   rS   rU   rX   r[   r]   r_   r   r   r   r   r      s       CCk*I! ! ! !   (8T
"
"C   (8T
"
"C8 8 8     ) ) )A A A	 	 	 H	 	 		 	 	   H    " " "" " "
% % %# # #& & && & &    r   r   c                   H    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZdS )RatTestCasez3Unit tests for Rat class and its support utilities.c                    |                      t          dd          d           |                      t          dd          d           |                      t          dd          d           |                      t          dd          d           |                      t          d	d          d
           |                      t          dd
          d           |                      t          d	d
          d
           t          dd          D ]}t          dd          D ]}|                     t          ||          dk               |                     t          | |          dk                |                     t          ||           dk               |                     t          | |           dk                d S )N
                  r   d      r   )assertEqualr   range
assertTrue)r$   ijs      r   test_gcdzRatTestCase.test_gcd   s   Ra(((Ra(((Ra(((S"q)))S!b)))Ra(((S"r***q" 	1 	1A1b\\ 1 1Aq		A...QB

Q///Ar

Q///QBa0000	1	1 	1r   c           	      B   t          dd          }|                     |j        d           |                     |j        d           t          dd          }|                     |j        d           |                     |j        d           t          dd          }|                     |j        d           |                     |j        d           t          dd          }|                     |j        d           |                     |j        d           t          d          }|                     |j        d           |                     |j        d	           	 t          d	d
          }|                     d           n# t
          $ r Y nw xY wddddg i d t           t          f	D ]t}	 t          |          }|                     d|z             n# t          $ r Y nw xY w	 t          d	|          }|                     d|z             e# t          $ r Y qw xY wd S )Nrk   rn   rm      irs   rr      r   r   z(Rat(1, 0) didn't raise ZeroDivisionError0g        y                r   zRat(%r) didn't raise TypeErrorz!Rat(1, %r) didn't raise TypeError)r   ru   r%   r&   failr#   unittestr"   )r$   r	   bads      r   test_constructorzRatTestCase.test_constructor   sR   BKK""""""CLL###"""RLL###"""SMM""""""FF""""""	BAq		A II@AAAA ! 	 	 	D	 RRT3@ 	E 	ECBHH 		:S@AAAA    E3KK 		=CDDDD    	E 	Es6   F 
FF-G
G"!G"&H
HHc                    |                      t          dd          t          dd          z   d           |                      t          dd          dz   t          dd                     |                      dt          dd          z   t          dd                     |                      dt          dd          z   d           |                      t          dd          dz   d           d S )Nrm   r|   r   ro   r6         ?ru   r   r*   s    r   test_addzRatTestCase.test_add   s    QSAYY.222QQAq		222SAYYAq		222s1ayy#...QS#.....r   c                    |                      t          dd          t          dd          z
  t          dd                     |                      t          dd          dz
  t          dd                     |                      dt          dd          z
  t          dd                     |                      t          dd          dz
  d	           |                      dt          dd          z
  d	           d S )
Nr}   rm   ro      rk   r   r|   r6         ?r   r*   s    r   test_subzRatTestCase.test_sub  s    QSAYY.B<<<QQAq		222SAYYAq		222QS#...s1ayy#.....r   c                    |                      t          dd          t          dd          z  t          dd                     |                      t          dd          dz  d           |                      dt          dd          z  d           |                      t          dd          dz  d           |                      dt          dd          z  d           d S )	Nrm   r|   ro   r}   rk   r   r   r6   r   r*   s    r   test_mulzRatTestCase.test_mul	  s    QSAYY.B<<<Ra,,,SQZZ,,,Rc)3///s2qzz)3/////r   c                    |                      t          dd          t          dd          z  t          dd                     |                      t          dd          dz  t          dd                     |                      dt          d          z  t          dd                     |                      dt          d	d          z  d
           |                      t          d	d          dz  d
           d S )Nrk   r|   ro   r}      	   rm         @r   r   r   r*   s    r   test_divzRatTestCase.test_div  s    Rc!Qii/R<<<RaR444SVVSAYY///s1ayy#...QS#.....r   c                 n   |                      t          d          t          d          z  d           |                      t          dd          t          dd          z  d           |                      t          d          dz  d           |                      dt          d          z  d           d S )Nrk      rm   r|   r   r*   s    r   test_floordivzRatTestCase.test_floordiv  s    RCFF*A...Rs1ayy0!444RAq)))s1vvq)))))r   c                    |                      t          d          t          dd                     |                      t          d          d           |                      dt          d                     |                      t          d          d           |                      dt          d                     d S )Nrk   rt   rm   g      $@r   r*   s    r   test_eqzRatTestCase.test_eq  s    R#b!**---R"%%%SWW%%%R$'''s2ww'''''r   c                 @   |                      t          dd          t          dd          z  t          dd                     |                      t          dd          dz  t          dd                     |                      dt          d          z  t          dd                     |                      dt          d	d          z  d
           |                      t          d	d          dz  d
           |                      t          d          d           d S )Nrk   r|   ro   r}   r   r   rm   r   r   r   z1/2r   )ru   r   evalr*   s    r   test_true_divzRatTestCase.test_true_div$  s    Rc!Qii/R<<<RaR444SVVSAYY///s1ayy#...QS#...ec*****r   N)r`   ra   rb   rc   rz   r   r   r   r   r   r   r   r   r   r   r   ri   ri      s        ==1 1 1"E "E "EH/ / // / /0 0 0/ / /* * *( ( (+ + + + +r   ri   c                       e Zd ZdZd Zd ZdS )OperationLoggerz.Base class for classes with operation logging.c                     || _         d S Nlogger)r$   r   s     r   r(   zOperationLogger.__init__1  s    r   c                      | j         |  d S r   r   )r$   argss     r   log_operationzOperationLogger.log_operation3  s    Tr   N)r`   ra   rb   rc   r(   r   r   r   r   r   r   /  s8        88      r   r   c                     g }g }|D ]%}|                      ||j                              &	  | |  n# t          $ r Y nw xY w|S )zvReturn the sequence of operations that results from applying
    the operation `op` to instances of the given classes.)appendr"   )opclasseslog	instancescs        r   op_sequencer   6  su     CI ( (3:''''
I   Js   4 
A Ac                        e Zd Zd Zd Zd ZdS )Ac                 :    |                      d           t          S )NA.__eq__r   r?   r@   s     r   r_   zA.__eq__E      :&&&r   c                 :    |                      d           t          S )NA.__le__r   r@   s     r   __le__zA.__le__H  r   r   c                 :    |                      d           t          S )NA.__ge__r   r@   s     r   __ge__zA.__ge__K  r   r   Nr`   ra   rb   r_   r   r   r   r   r   r   r   D  A                r   r   c                        e Zd Zd Zd Zd ZdS )Bc                 :    |                      d           t          S )NB.__eq__r   r@   s     r   r_   zB.__eq__P  r   r   c                 :    |                      d           t          S )NB.__le__r   r@   s     r   r   zB.__le__S  r   r   c                 :    |                      d           t          S )NB.__ge__r   r@   s     r   r   zB.__ge__V  r   r   Nr   r   r   r   r   r   O  r   r   r   )	metaclassc                        e Zd Zd Zd Zd ZdS )Cc                 :    |                      d           t          S )NC.__eq__r   r@   s     r   r_   zC.__eq__[  r   r   c                 :    |                      d           t          S )NC.__le__r   r@   s     r   r   zC.__le__^  r   r   c                 :    |                      d           t          S )NC.__ge__r   r@   s     r   r   zC.__ge__a  r   r   Nr   r   r   r   r   r   Z  r   r   r   c                   $    e Zd ZdZd Zd Zd ZdS )VzVirtual subclass of Bc                 :    |                      d           t          S )NV.__eq__r   r@   s     r   r_   zV.__eq__g  r   r   c                 :    |                      d           t          S )NzV.__le__r   r@   s     r   r   zV.__le__j  r   r   c                 :    |                      d           t          S )NV.__ge__r   r@   s     r   r   zV.__ge__m  r   r   N)r`   ra   rb   rc   r_   r   r   r   r   r   r   r   e  sG                r   r   c                       e Zd Zd ZdS )OperationOrderTestsc                 p   |                      t          t          t          t                    ddg           |                      t          t          t          t                    ddg           |                      t          t          t          t                    ddg           |                      t          t          t          t
                    ddg           |                      t          t          t
          t                    ddg           |                      t          t          t          t                    ddg           |                      t          t          t          t                    ddg           |                      t          t          t          t                    ddg           |                      t          t          t          t
                    ddg           |                      t          t          t
          t                    d	dg           |                     t          t          t                               |                      t          t          t          t                    dd
g           |                      t          t          t          t                    ddg           d S )Nr   r   r   r   r   r   r   r   r   r   r   )
ru   r   r   r   r   r   r   rw   
issubclassr   r*   s    r   test_comparison_ordersz*OperationOrderTests.test_comparison_orderst  s   RA..Z0HIIIRA..Z0HIIIRA..Z0HIIIRA..Z0HIIIRA..Z0HIIIRA..Z0HIIIRA..Z0HIIIRA..Z0HIIIRA..Z0HIIIRA..Z0HIII
1a(()))RA..Z0HIIIRA..Z0HIIIIIr   N)r`   ra   rb   r   r   r   r   r   r   s  s(        J J J J Jr   r   c                       e Zd ZdZd ZdS )SupEqzClass that can test equalityc                     dS NTr   r@   s     r   r_   zSupEq.__eq__      tr   Nr`   ra   rb   rc   r_   r   r   r   r   r     s)        &&    r   r   c                       e Zd ZdZdZdS )Sz"Subclass of SupEq that should failNr   r   r   r   r   r             ,,FFFr   r   c                       e Zd ZdZdS )Fz'Independent class that should fall backN)r`   ra   rb   rc   r   r   r   r   r     s        1111r   r   c                       e Zd ZdZdZdS )Xz"Independent class that should failNr   r   r   r   r   r     r   r   r   c                       e Zd ZdZdZdS )SNz>Subclass of SupEq that can test equality, but not non-equalityN)r`   ra   rb   rc   __ne__r   r   r   r   r     s        HHFFFr   r   c                       e Zd ZdZd ZdZdS )XNz>Independent class that can test equality, but not non-equalityc                     dS r   r   r@   s     r   r_   z	XN.__eq__  r   r   N)r`   ra   rb   rc   r_   r   r   r   r   r   r     s(        HH  FFFr   r   c                       e Zd ZdZd Zd ZdS )FallbackBlockingTestsz#Unit tests for None method blockingc                    t                      t                      t                      t                      f\  }}}}|                     ||           |                     ||           |                     ||           |                     ||           |                     t          t          ||           |                     t          t          ||           |                     t          t          ||           d S r   )r   r   r   r   ru   assertRaisesr"   r   )r$   efsr   s        r   test_fallback_rmethod_blockingz4FallbackBlockingTests.test_fallback_rmethod_blocking  s    WWacc133+
1aAAAA)RA...)RA...)RA.....r   c                    t                      t                      t                      }}}|                     ||k               |                     t
          t          ||           |                     t
          t          ||           |                     ||k               |                     t
          t          ||           d S r   )r   r   r   assertFalser   r"   r   )r$   r   snxns       r   test_fallback_ne_blockingz/FallbackBlockingTests.test_fallback_ne_blocking  s    GGRTT244r2a   )RB///)RQ///b!!!)RQ/////r   N)r`   ra   rb   rc   r   r   r   r   r   r   r     s8        --
/ 
/ 
/0 0 0 0 0r   r   __main__)!rc   r   operatorr   r   r   abcr   r   r   r   r   objectr   TestCaseri   r   r   r   r   r   r   registerr   r   r   r   r   r   r   r   r`   mainr   r   r   <module>r      sb   ? ?                         g g g g g& g g gRe+ e+ e+ e+ e+(# e+ e+ e+T         	 	 	 	 	 	 	 		 	 	 	 	7 	 	 	 		 	 	 	 	 	 	 	
 
 
 
 
 
 
 
 

1J J J J J(+ J J J&    F   
       2 2 2 2 2 2 2 2                     0 0 0 0 0H- 0 0 0. zHMOOOOO r   