a
    ze8                     @   sj  d Z ddlZddlmZmZmZ ddlmZ dd Zdd Z	d	d
 Z
dd ZG dd deZG dd dejZG dd dZdd ZG dd deZG dd deedZG dd deZG dd deZee G dd dejZG d d! d!eZG d"d# d#eZG d$d% d%eZG d&d' d'eZG d(d) d)eZG d*d+ d+ZG d,d- d-ejZed.krfe   dS )/z9Tests for binary operators on subtypes of built-in types.    N)eqlene)ABCMetac                 C   s   | r||  |  } }q |S )z1Greatest common divisor using Euclid's algorithm. )abr   r   %/usr/lib/python3.9/test/test_binop.pygcd   s    r
   c                 C   s
   t | tS )z-Test whether an object is an instance of int.)
isinstanceintxr   r   r	   isint   s    r   c                 C   s$   t ttfD ]}t| |r
 dS q
dS )zATest whether an object is an instance of a built-in numeric type.   r   )r   floatcomplexr   )r   Tr   r   r	   isnum   s    
r   c                 C   s
   t | tS )z7Test whether an object is an instance of the Rat class.)r   Ratr   r   r   r	   isRat   s    r   c                   @   s   e Zd ZdZddgZd/ddZdd	 Zeed
Zdd Z	ee	d
Z
dd Zdd Zdd Zdd Zdd ZeZdd Zdd Zdd ZeZdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd
S )0r   z9Rational number implemented as a normalized pair of ints.	_Rat__num	_Rat__denr   r   c                 C   sb   t |std| t |s(td| |dkr8tdt||}t|| | _t|| | _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gr   r   r	   __init__"   s    
zRat.__init__c                 C   s   | j S )z7Accessor function for read-only 'num' attribute of Rat.)r   r   r   r   r	   _get_num1   s    zRat._get_numNc                 C   s   | j S )z7Accessor function for read-only 'den' attribute of Rat.)r   r    r   r   r	   _get_den6   s    zRat._get_denc                 C   s   d| j | jf S )z<Convert a Rat to a string resembling a Rat constructor call.zRat(%d, %d)r   r   r    r   r   r	   __repr__;   s    zRat.__repr__c                 C   s   t t| S )z=Convert a Rat to a string resembling a decimal numeric value.)strr   r    r   r   r	   __str__?   s    zRat.__str__c                 C   s   | j d | j S )zConvert a Rat to a float.      ?r#   r    r   r   r	   	__float__C   s    zRat.__float__c                 C   sN   | j dkr:zt| jW S  ty8   tdt|  Y n0 tdt|  dS )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 intN)r   r   r   OverflowErrorrepr
ValueErrorr    r   r   r	   __int__G   s    
zRat.__int__c                 C   sV   t |rt|}t|r>t| j|j |j| j  | j|j S t|rRt| | S tS )z$Add two Rats, or a Rat and a number.r   r   r   r   r   r   r   NotImplementedr   otherr   r   r	   __add__Q   s    
zRat.__add__c                 C   sV   t |rt|}t|r>t| j|j |j| j  | j|j S t|rRt| | S tS )z)Subtract two Rats, or a Rat and a number.r-   r/   r   r   r	   __sub__^   s    
zRat.__sub__c                 C   sV   t |rt|}t|r>t|j| j | j|j  | j|j S t|rR|t|  S tS )z9Subtract two Rats, or a Rat and a number (reversed args).r-   r/   r   r   r	   __rsub__i   s    
zRat.__rsub__c                 C   sT   t |r"t| j|j | j|j S t|r<t| j| | jS t|rPt| | S tS )z)Multiply two Rats, or a Rat and a number.r   r   r   r   r   r   r   r.   r/   r   r   r	   __mul__t   s    zRat.__mul__c                 C   sT   t |r"t| j|j | j|j S t|r<t| j| j| S t|rPt| | S tS )z'Divide two Rats, or a Rat and a number.r4   r/   r   r   r	   __truediv__   s    zRat.__truediv__c                 C   sT   t |r"t|j| j |j| j S t|r<t|| j | jS t|rP|t|  S tS )z7Divide two Rats, or a Rat and a number (reversed args).r4   r/   r   r   r	   __rtruediv__   s    zRat.__rtruediv__c                 C   s2   t |rt|}nt|stS | | }|j|j S )z.Divide two Rats, returning the floored result.)r   r   r   r.   r   r   r   r0   r   r   r   r	   __floordiv__   s    
zRat.__floordiv__c                 C   s   ||  }|j |j S )z>Divide two Rats, returning the floored result (reversed args).r#   r8   r   r   r	   __rfloordiv__   s    zRat.__rfloordiv__c                 C   s6   t |rt|}nt|stS | | }|| ||  fS )z2Divide two Rats, returning quotient and remainder.)r   r   r   r.   r8   r   r   r	   
__divmod__   s    
zRat.__divmod__c                 C   s(   t |rt|}nt|stS t|| S )zBDivide two Rats, returning quotient and remainder (reversed args).)r   r   r   r.   divmodr/   r   r   r	   __rdivmod__   s
    
zRat.__rdivmod__c                 C   s   t | |d S )zTake one Rat modulo another.r   r<   r/   r   r   r	   __mod__   s    zRat.__mod__c                 C   s   t || d S )z,Take one Rat modulo another (reversed args).r   r>   r/   r   r   r	   __rmod__   s    zRat.__rmod__c                 C   sT   t |r| jdko| j|kS t|r<| j|jko:| j|jkS t|rPt| |kS tS )zCompare two Rats for equality.r   )r   r   r   r   r   r   r.   r/   r   r   r	   __eq__   s    z
Rat.__eq__)r   r   )__name__
__module____qualname____doc__	__slots__r   r!   propertyr   r"   r   r$   r&   r(   r,   r1   __radd__r2   r3   r5   __rmul__r6   r7   r9   r:   r;   r=   r?   r@   rA   r   r   r   r	   r      s4   






		r   c                   @   sX   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd ZdS )RatTestCasez3Unit tests for Rat class and its support utilities.c                 C   s   |  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 ]l}tddD ]\}| t||dk | t| |dk  | t|| dk | t| | dk  qqd S )N
                  r   d      r   )assertEqualr
   range
assertTrue)r   ijr   r   r	   test_gcd   s    zRatTestCase.test_gcdc              	   C   sj  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	 zt d	d
}W n ty   Y n0 | d ddddg i d t tf	D ]f}zt |}W n ty"   Y n0 | d|  zt d	|}W n tyT   Y q0 | d|  qd 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   r   Zfailunittestr   )r   r   Zbadr   r   r	   test_constructor   s>    




zRatTestCase.test_constructorc                 C   s   |  tddtdd d |  tddd tdd |  dtdd tdd |  dtdd d |  tddd d d S )NrM   r[   r   rO   r'         ?rU   r   r    r   r   r	   test_add   s
    zRatTestCase.test_addc                 C   s   |  tddtdd tdd |  tddd tdd |  dtdd tdd |  tddd d	 |  dtdd d	 d S )
Nr\   rM   rO      rK   r   r[   r'         ?ra   r    r   r   r	   test_sub  s
    "zRatTestCase.test_subc                 C   s~   |  tddtdd tdd |  tddd d |  dtdd d |  tddd d |  dtdd d d S )	NrM   r[   rO   r\   rK   rc   rd   r'   ra   r    r   r   r	   test_mul	  s
    "zRatTestCase.test_mulc                 C   s   |  tddtdd tdd |  tddd tdd |  dtd tdd |  dtd	d d
 |  td	dd d
 d S )NrK   r[   rO   r\      	   rM         @r   r`   ra   r    r   r   r	   test_div  s
    "zRatTestCase.test_divc                 C   s`   |  tdtd d |  tddtdd d |  tdd d |  dtd d d S )NrK      rM   r[   ra   r    r   r   r	   test_floordiv  s    zRatTestCase.test_floordivc                 C   sZ   |  tdtdd |  tdd |  dtd |  tdd |  dtd d S )NrK   rT   rM   g      $@ra   r    r   r   r	   test_eq  s
    zRatTestCase.test_eqc                 C   s   |  tddtdd tdd |  tddd tdd |  dtd tdd |  dtd	d d
 |  td	dd d
 |  tdd d S )NrK   r[   rO   r\   rg   rh   rM   ri   r   r`   z1/2rd   )rU   r   evalr    r   r   r	   test_true_div$  s    "zRatTestCase.test_true_divN)rB   rC   rD   rE   rZ   r_   rb   re   rf   rj   rl   rm   ro   r   r   r   r	   rJ      s   $rJ   c                   @   s    e Zd ZdZdd Zdd ZdS )OperationLoggerz.Base class for classes with operation logging.c                 C   s
   || _ d S Nlogger)r   rs   r   r   r	   r   1  s    zOperationLogger.__init__c                 G   s   | j |  d S rq   rr   )r   argsr   r   r	   log_operation3  s    zOperationLogger.log_operationN)rB   rC   rD   rE   r   ru   r   r   r   r	   rp   /  s   rp   c                 G   sF   g }g }|D ]}| ||j  qz| |  W n ty@   Y n0 |S )zvReturn the sequence of operations that results from applying
    the operation `op` to instances of the given classes.)appendr   )opclasseslogZ	instancescr   r   r	   op_sequence6  s    r{   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )Ac                 C   s   |  d tS )NA.__eq__ru   r.   r/   r   r   r	   rA   E  s    
r}   c                 C   s   |  d tS )NA.__le__r~   r/   r   r   r	   __le__H  s    
r   c                 C   s   |  d tS )NA.__ge__r~   r/   r   r   r	   __ge__K  s    
r   NrB   rC   rD   rA   r   r   r   r   r   r	   r|   D  s   r|   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )Bc                 C   s   |  d tS )NB.__eq__r~   r/   r   r   r	   rA   P  s    
r   c                 C   s   |  d tS )NB.__le__r~   r/   r   r   r	   r   S  s    
r   c                 C   s   |  d tS )NB.__ge__r~   r/   r   r   r	   r   V  s    
r   Nr   r   r   r   r	   r   O  s   r   )	metaclassc                   @   s$   e Zd Zdd Zdd Zdd ZdS )Cc                 C   s   |  d tS )NC.__eq__r~   r/   r   r   r	   rA   [  s    
r   c                 C   s   |  d tS )NC.__le__r~   r/   r   r   r	   r   ^  s    
r   c                 C   s   |  d tS )NC.__ge__r~   r/   r   r   r	   r   a  s    
r   Nr   r   r   r   r	   r   Z  s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	VzVirtual subclass of Bc                 C   s   |  d tS )NV.__eq__r~   r/   r   r   r	   rA   g  s    
r   c                 C   s   |  d tS )NV.__le__r~   r/   r   r   r	   r   j  s    
r   c                 C   s   |  d tS )NV.__ge__r~   r/   r   r   r	   r   m  s    
r   N)rB   rC   rD   rE   rA   r   r   r   r   r   r	   r   e  s   r   c                   @   s   e Zd Zdd ZdS )OperationOrderTestsc                 C   s4  |  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    r   r   r	   test_comparison_orderst  s    z*OperationOrderTests.test_comparison_ordersN)rB   rC   rD   r   r   r   r   r	   r   s  s   r   c                   @   s   e Zd ZdZdd ZdS )SupEqzClass that can test equalityc                 C   s   dS NTr   r/   r   r   r	   rA     s    zSupEq.__eq__NrB   rC   rD   rE   rA   r   r   r   r	   r     s   r   c                   @   s   e Zd ZdZdZdS )Sz"Subclass of SupEq that should failNr   r   r   r   r	   r     s   r   c                   @   s   e Zd ZdZdS )Fz'Independent class that should fall backN)rB   rC   rD   rE   r   r   r   r	   r     s   r   c                   @   s   e Zd ZdZdZdS )Xz"Independent class that should failNr   r   r   r   r	   r     s   r   c                   @   s   e Zd ZdZdZdS )SNz>Subclass of SupEq that can test equality, but not non-equalityN)rB   rC   rD   rE   __ne__r   r   r   r	   r     s   r   c                   @   s   e Zd ZdZdd ZdZdS )XNz>Independent class that can test equality, but not non-equalityc                 C   s   dS r   r   r/   r   r   r	   rA     s    z	XN.__eq__N)rB   rC   rD   rE   rA   r   r   r   r   r	   r     s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )FallbackBlockingTestsz#Unit tests for None method blockingc                 C   s   t  t t t f\}}}}| || | || | || | || | tt|| | tt|| | tt|| d S rq   )r   r   r   r   rU   assertRaisesr   r   )r   efsr   r   r   r	   test_fallback_rmethod_blocking  s    z4FallbackBlockingTests.test_fallback_rmethod_blockingc                 C   sf   t  t t   }}}| ||k | tt|| | tt|| | ||k | tt|| d S rq   )r   r   r   ZassertFalser   r   r   )r   r   ZsnZxnr   r   r	   test_fallback_ne_blocking  s    z/FallbackBlockingTests.test_fallback_ne_blockingN)rB   rC   rD   rE   r   r   r   r   r   r	   r     s   r   __main__)!rE   r^   operatorr   r   r   abcr   r
   r   r   r   objectr   ZTestCaserJ   rp   r{   r|   r   r   r   registerr   r   r   r   r   r   r   r   rB   mainr   r   r   r	   <module>   s6    *j

