B
    u9a8                 @   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   x| r||  |  } }qW |S )z1Greatest common divisor using Euclid's algorithm. )abr   r    /usr/lib/python3.7/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&   x t ttfD ]}t| |rdS qW 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   Rat)r   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.g      ?)r   r   )r   r   r   r	   	__float__C   s    zRat.__float__c             C   sN   | j dkr:y
t| jS  tk
r8   tdt|  Y nX 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
ValueError)r   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   r   r   r)   )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   r   r   r)   )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   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.)r   r   r   r   r   r   r   r)   )r   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).)r   r   r   r   r   r   r   r)   )r   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   r*   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   r   )r   r*   r   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)   )r   r*   r   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)   divmod)r   r*   r   r   r	   __rdivmod__   s
    
zRat.__rdivmod__c             C   s   t | |d S )zTake one Rat modulo another.r   )r4   )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   )r4   )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   r	   __eq__   s    z
Rat.__eq__)r   r   )__name__
__module____qualname____doc__	__slots__r   r   propertyr   r    r   r!   r#   r$   r(   r+   __radd__r,   r-   r.   __rmul__r/   r0   r1   r2   r3   r5   r6   r7   r8   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
 x~tddD ]p}xjtddD ]\}| t||dk | t| |dk  | t|| dk | t| | dk  qW qW d S )N
                  r   d   i   r   )assertEqualr
   range
assertTrue)r   ijr   r   r	   test_gcd   s    zRatTestCase.test_gcdc          	   C   sv  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	 yt d	d
}W n tk
r   Y nX | d xddddg i d t tf	D ]l}yt |}W n tk
r(   Y nX | d|  yt d	|}W n tk
r\   Y nX | d|  qW d S )NrB   rE   rD      irI   i   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   rK   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 )NrD   rQ   r   rF   g      ?g      ?)rK   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 )
NrR   rD   rF      rB   r   rQ   g      ?g      ?)rK   r   )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 )	NrD   rQ   rF   rR   rB   rW   g      ?g      ?)rK   r   )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 )NrB   rQ   rF   rR      	   rD   g      @r   g      ?)rK   r   )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 )NrB      rD   rQ   )rK   r   )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 )NrB   rJ   rD   g      $@)rK   r   )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 )NrB   rQ   rF   rR   rZ   r[   rD   g      @r   g      ?z1/2g      ?)rK   r   eval)r   r   r   r	   test_true_div$  s    "zRatTestCase.test_true_divN)r9   r:   r;   r<   rP   rU   rV   rX   rY   r\   r^   r_   ra   r   r   r   r	   rA      s   $rA   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   rc   r   r   r	   r   1  s    zOperationLogger.__init__c             G   s   | j |  d S )N)rc   )r   argsr   r   r	   log_operation3  s    zOperationLogger.log_operationN)r9   r:   r;   r<   r   re   r   r   r   r	   rb   /  s   rb   c             G   sL   g }g }x|D ]}| ||j  qW y| |  W n tk
rF   Y nX |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    
rk   c               @   s$   e Zd Zdd Zdd Zdd ZdS )Ac             C   s   |  d tS )NzA.__eq__)re   r)   )r   r*   r   r   r	   r8   E  s    
zA.__eq__c             C   s   |  d tS )NzA.__le__)re   r)   )r   r*   r   r   r	   __le__H  s    
zA.__le__c             C   s   |  d tS )NzA.__ge__)re   r)   )r   r*   r   r   r	   __ge__K  s    
zA.__ge__N)r9   r:   r;   r8   rm   rn   r   r   r   r	   rl   D  s   rl   c               @   s$   e Zd Zdd Zdd Zdd ZdS )Bc             C   s   |  d tS )NzB.__eq__)re   r)   )r   r*   r   r   r	   r8   P  s    
zB.__eq__c             C   s   |  d tS )NzB.__le__)re   r)   )r   r*   r   r   r	   rm   S  s    
zB.__le__c             C   s   |  d tS )NzB.__ge__)re   r)   )r   r*   r   r   r	   rn   V  s    
zB.__ge__N)r9   r:   r;   r8   rm   rn   r   r   r   r	   ro   O  s   ro   )	metaclassc               @   s$   e Zd Zdd Zdd Zdd ZdS )Cc             C   s   |  d tS )NzC.__eq__)re   r)   )r   r*   r   r   r	   r8   [  s    
zC.__eq__c             C   s   |  d tS )NzC.__le__)re   r)   )r   r*   r   r   r	   rm   ^  s    
zC.__le__c             C   s   |  d tS )NzC.__ge__)re   r)   )r   r*   r   r   r	   rn   a  s    
zC.__ge__N)r9   r:   r;   r8   rm   rn   r   r   r   r	   rq   Z  s   rq   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 )NzV.__eq__)re   r)   )r   r*   r   r   r	   r8   g  s    
zV.__eq__c             C   s   |  d tS )NzV.__le__)re   r)   )r   r*   r   r   r	   rm   j  s    
zV.__le__c             C   s   |  d tS )NzV.__ge__)re   r)   )r   r*   r   r   r	   rn   m  s    
zV.__ge__N)r9   r:   r;   r<   r8   rm   rn   r   r   r   r	   rr   e  s   rr   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 )NzA.__eq__zB.__eq__zC.__eq__zA.__le__zA.__ge__zB.__ge__zB.__le__zC.__ge__zC.__le__zV.__eq__zV.__ge__)
rK   rk   r   rl   ro   rq   r   rM   
issubclassrr   )r   r   r   r	   test_comparison_orderst  s    z*OperationOrderTests.test_comparison_ordersN)r9   r:   r;   ru   r   r   r   r	   rs   s  s   rs   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   r	   r8     s    zSupEq.__eq__N)r9   r:   r;   r<   r8   r   r   r   r	   rv     s   rv   c               @   s   e Zd ZdZdZdS )Sz"Subclass of SupEq that should failN)r9   r:   r;   r<   r8   r   r   r   r	   rw     s   rw   c               @   s   e Zd ZdZdS )Fz'Independent class that should fall backN)r9   r:   r;   r<   r   r   r   r	   rx     s   rx   c               @   s   e Zd ZdZdZdS )Xz"Independent class that should failN)r9   r:   r;   r<   r8   r   r   r   r	   ry     s   ry   c               @   s   e Zd ZdZdZdS )SNz>Subclass of SupEq that can test equality, but not non-equalityN)r9   r:   r;   r<   __ne__r   r   r   r	   rz     s   rz   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 )NTr   )r   r*   r   r   r	   r8     s    z	XN.__eq__N)r9   r:   r;   r<   r8   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 )N)rv   rx   rw   ry   rK   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 )N)rv   rz   r|   ZassertFalser~   r   r   )r   r   ZsnZxnr   r   r	   test_fallback_ne_blocking  s    z/FallbackBlockingTests.test_fallback_ne_blockingN)r9   r:   r;   r<   r   r   r   r   r   r	   r}     s   r}   __main__)!r<   rT   operatorr   r   r   abcr   r
   r   r   r   objectr   ZTestCaserA   rb   rk   rl   ro   rq   rr   registerrs   rv   rw   rx   ry   rz   r|   r}   r9   mainr   r   r   r	   <module>   s6    *j

