o
    GXh8                     @   sl  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re   dS dS )/z9Tests for binary operators on subtypes of built-in types.    N)eqlene)ABCMetac                 C   s   | r||  | } }| s|S )z1Greatest common divisor using Euclid's algorithm. )abr   r   9/opt/python-3.10.19/usr/lib/python3.10/test/test_binop.pygcd   s   r
   c                 C   
   t | tS )z-Test whether an object is an instance of int.)
isinstanceintxr   r   r	   isint      
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   r   )z7Test whether an object is an instance of the Rat class.)r   Ratr   r   r   r	   isRat   r   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rt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      | j S )z7Accessor function for read-only 'num' attribute of Rat.)r   r   r   r   r	   _get_num1      zRat._get_numNc                 C   r"   )z7Accessor function for read-only 'den' attribute of Rat.)r   r#   r   r   r	   _get_den6   r%   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__;      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   r)   zRat.__float__c                 C   sF   | j dkrzt| jW S  ty   tdt|  w tdt|  )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#   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|r)t| | 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      
zRat.__add__c                 C   sV   t |rt|}t|rt| j|j |j| j  | j|j S t|r)t| | S tS )z)Subtract two Rats, or a Rat and a number.r2   r4   r   r   r	   __sub__^   r7   zRat.__sub__c                 C   sV   t |rt|}t|rt|j| j | j|j  | j|j S t|r)|t|  S tS )z9Subtract two Rats, or a Rat and a number (reversed args).r2   r4   r   r   r	   __rsub__i   r7   zRat.__rsub__c                 C   sT   t |rt| j|j | j|j S t|rt| j| | jS t|r(t| | S tS )z)Multiply two Rats, or a Rat and a number.r   r   r   r   r   r   r   r3   r4   r   r   r	   __mul__t      zRat.__mul__c                 C   sT   t |rt| j|j | j|j S t|rt| j| j| S t|r(t| | S tS )z'Divide two Rats, or a Rat and a number.r:   r4   r   r   r	   __truediv__   r<   zRat.__truediv__c                 C   sT   t |rt|j| j |j| j S t|rt|| j | jS t|r(|t|  S tS )z7Divide two Rats, or a Rat and a number (reversed args).r:   r4   r   r   r	   __rtruediv__   r<   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   r3   r   r   r   r5   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	   __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   r3   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   r3   divmodr4   r   r   r	   __rdivmod__   s
   

zRat.__rdivmod__c                 C   s   t | |d S )zTake one Rat modulo another.r   rC   r4   r   r   r	   __mod__      zRat.__mod__c                 C   s   t || d S )z,Take one Rat modulo another (reversed args).r   rE   r4   r   r   r	   __rmod__   rG   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|r(t| |kS tS )zCompare two Rats for equality.r   )r   r   r   r   r   r   r3   r4   r   r   r	   __eq__   s   z
Rat.__eq__)r   r   )__name__
__module____qualname____doc__	__slots__r!   r$   propertyr   r&   r   r(   r+   r-   r1   r6   __radd__r8   r9   r;   __rmul__r=   r>   r@   rA   rB   rD   rF   rH   rI   r   r   r   r	   r      s6    






		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 ]6}tddD ].}| t||dk | t| |dk  | t|| dk | t| | dk  qKqDd S )N
                  r   d      r   )assertEqualr
   range
assertTrue)r   ijr   r   r	   test_gcd   s   zRatTestCase.test_gcdc              	   C   sf  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yn   Y nw | d ddddg i d t tf	D ]1}zt |}W n	 ty   Y nw | d|  zt d	|}W n	 ty   Y qw | d|  qd S )NrS   rV   rU      ir[   rZ      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   r]   r   r   r   Zfailunittestr   )r   r   Zbadr   r   r	   test_constructor   sF   




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 )NrU   rc   r   rW   r,         ?r]   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 )
Nrd   rU   rW      rS   r   rc   r,         ?ri   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 )	NrU   rc   rW   rd   rS   rk   rl   r,   ri   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 )NrS   rc   rW   rd      	   rU         @r   rh   ri   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 )NrS      rU   rc   ri   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 )NrS   r\   rU   g      $@ri   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 )NrS   rc   rW   rd   ro   rp   rU   rq   r   rh   z1/2rl   )r]   r   evalr#   r   r   r	   test_true_div$  s   "zRatTestCase.test_true_divN)rJ   rK   rL   rM   rb   rg   rj   rm   rn   rr   rt   ru   rw   r   r   r   r	   rR      s    $rR   c                   @       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   r|   r   r   r	   r!   1  s   
zOperationLogger.__init__c                 G   s   | j |  d S rz   r{   )r   argsr   r   r	   log_operation3  s   zOperationLogger.log_operationN)rJ   rK   rL   rM   r!   r~   r   r   r   r	   ry   /  s    ry   c                 G   sF   g }g }|D ]
}| ||j  qz| |  W |S  ty"   Y |S w )zvReturn the sequence of operations that results from applying
    the operation `op` to instances of the given classes.)appendr   )opZclasseslogZ	instancescr   r   r	   op_sequence6  s   
r   c                   @   $   e Zd Zdd Zdd Zdd ZdS )Ac                 C      |  d tS )NA.__eq__r~   r3   r4   r   r   r	   rI   E     
r   c                 C   r   )NA.__le__r   r4   r   r   r	   __le__H  r   r   c                 C   r   )NA.__ge__r   r4   r   r   r	   __ge__K  r   r   NrJ   rK   rL   rI   r   r   r   r   r   r	   r   D      r   c                   @   r   )Bc                 C   r   )NB.__eq__r   r4   r   r   r	   rI   P  r   r   c                 C   r   )NB.__le__r   r4   r   r   r	   r   S  r   r   c                 C   r   )NB.__ge__r   r4   r   r   r	   r   V  r   r   Nr   r   r   r   r	   r   O  r   r   )	metaclassc                   @   r   )Cc                 C   r   )NC.__eq__r   r4   r   r   r	   rI   [  r   r   c                 C   r   )NC.__le__r   r4   r   r   r	   r   ^  r   r   c                 C   r   )NC.__ge__r   r4   r   r   r	   r   a  r   r   Nr   r   r   r   r	   r   Z  r   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   r   )NV.__eq__r   r4   r   r   r	   rI   g  r   r   c                 C   r   )NV.__le__r   r4   r   r   r	   r   j  r   r   c                 C   r   )NV.__ge__r   r4   r   r   r	   r   m  r   r   N)rJ   rK   rL   rM   rI   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   )
r]   r   r   r   r   r   r   r_   
issubclassr   r#   r   r   r	   test_comparison_orderst  s   z*OperationOrderTests.test_comparison_ordersN)rJ   rK   rL   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      dS NTr   r4   r   r   r	   rI        zSupEq.__eq__NrJ   rK   rL   rM   rI   r   r   r   r	   r     s    r   c                   @      e Zd ZdZdZdS )Sz"Subclass of SupEq that should failNr   r   r   r   r	   r         r   c                   @   s   e Zd ZdZdS )Fz'Independent class that should fall backN)rJ   rK   rL   rM   r   r   r   r	   r     s    r   c                   @   r   )Xz"Independent class that should failNr   r   r   r   r	   r     r   r   c                   @   r   )SNz>Subclass of SupEq that can test equality, but not non-equalityN)rJ   rK   rL   rM   __ne__r   r   r   r	   r     r   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   r   r   r   r4   r   r   r	   rI     r   z	XN.__eq__N)rJ   rK   rL   rM   rI   r   r   r   r   r	   r     s    r   c                   @   rx   )FallbackBlockingTestsz#Unit tests for None method blockingc                 C   s   t  t t t f\}}}}| || | || | || | || | tt|| | tt|| | tt|| d S rz   )r   r   r   r   r]   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 rz   )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)rJ   rK   rL   rM   r   r   r   r   r   r	   r     s    r   __main__)!rM   rf   operatorr   r   r   abcr   r
   r   r   r   objectr   ZTestCaserR   ry   r   r   r   r   r   registerr   r   r   r   r   r   r   r   rJ   mainr   r   r   r	   <module>   s:     *j
