Your IP : 216.73.216.85


Current Path : /opt/alt/python39/lib64/python3.9/test/__pycache__/
Upload File :
Current File : //opt/alt/python39/lib64/python3.9/test/__pycache__/test_fractions.cpython-39.pyc

a

XC?h]v�@s�dZddlmZddlmZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlmZm
Z
ddlmZmZejZGdd�de�ZGd	d
�d
e�ZGdd�dej�Zd
d�ZGdd�dej�Zedkr�e��dS)zTests for Lib/fractions.py.�)�Decimal)�requires_IEEE_754N)�copy�deepcopy)�dumps�loadsc@s\eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�ZeZ
dS)�
DummyFloatz8Dummy float class for testing comparisons with FractionscCst|t�std��||_dS)Nz-DummyFloat can only be initialized from float)�
isinstance�float�	TypeError�value��selfr�r�8/opt/alt/python39/lib64/python3.9/test/test_fractions.py�__init__s
zDummyFloat.__init__cCs>t|tj�r|t�|j�|�St|t�r6||j|j�StSdS�N)r	�numbers�Rational�F�
from_floatrr�NotImplemented)r�other�oprrr�_richcmps

zDummyFloat._richcmpcCs|�|tj�Sr)r�operator�eq�rrrrr�__eq__!�zDummyFloat.__eq__cCs|�|tj�Sr)rr�lerrrr�__le__"rzDummyFloat.__le__cCs|�|tj�Sr)rr�ltrrrr�__lt__#rzDummyFloat.__lt__cCs|�|tj�Sr)rr�gerrrr�__ge__$rzDummyFloat.__ge__cCs|�|tj�Sr)rr�gtrrrr�__gt__%rzDummyFloat.__gt__cCsdsJd��dS)NFz/__float__ should not be invoked for comparisonsr�rrrr�	__float__(szDummyFloat.__float__cCsdsJd��dS)NFz-__sub__ should not be invoked for comparisonsrrrrr�__sub__,szDummyFloat.__sub__N)�__name__�
__module__�__qualname__�__doc__rrrr!r#r%r'r)r*�__rsub__rrrrrsrc@sHeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dS)�
DummyRationalzATest comparison of Fraction with a naive rational implementation.cCs$t�||�}|||_|||_dSr)�mathZgcd�num�den)rr2r3�grrrr4s
zDummyRational.__init__cCs,t|tj�r$|j|jko"|j|jkStSdSr)r	�	fractions�Fractionr2�
_numeratorr3�_denominatorrrrrrr9s

�zDummyRational.__eq__cCs|j|j|j|jkSr�r2r8r3r7rrrrr#@szDummyRational.__lt__cCs|j|j|j|jkSrr9rrrrr'CszDummyRational.__gt__cCs|j|j|j|jkSrr9rrrrr!FszDummyRational.__le__cCs|j|j|j|jkSrr9rrrrr%IszDummyRational.__ge__cCsdsJd��dS)NFz__float__ should not be invokedrr(rrrr)NszDummyRational.__float__N)r+r,r-r.rrr#r'r!r%r)rrrrr01sr0c@seZdZdZdS)�
DummyFractionz6Dummy Fraction subclass for copy and deepcopy testing.N)r+r,r-r.rrrrr:Qsr:cCs|j|jfSr)�	numerator�denominator)�rrrr�_componentsUsr>c@s eZdZdd�Zdd�Zdd�Zdd�Zed	d
��Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zd9d:�Z d;d<�Z!d=d>�Z"d?d@�Z#dAdB�Z$dCdD�Z%dES)F�FractionTestcCs$|�t|�t|��|�||�dS)z4Asserts that both the types and values are the same.N)�assertEqual�type�rZexpectedZactualrrr�assertTypedEquals[szFractionTest.assertTypedEqualscCs0|�||�|�ttt|��ttt|���dS)zBAsserts that both the types and values in the tuples are the same.N)ZassertTupleEqualZassertListEqual�list�maprArBrrr�assertTypedTupleEquals`sz#FractionTest.assertTypedTupleEqualsc
Os\z||i|��Wn4|yF}z|�|t|��WYd}~nd}~00|�d|j�dS)z@Asserts that callable(*args, **kwargs) raises exc_type(message).Nz
%s not raised)r@�strZfailr+)r�exc_type�message�callable�args�kwargs�errr�assertRaisesMessagees
&z FractionTest.assertRaisesMessagec	Cs�|�dtt���|�dttd���|�dtttdd����|�dttdd���|�dttdd���|�d	ttd
d
���|�dttdd
���|�dttdd���|�dttd���|�dtttdd�d���|�dttdtdd����|�dtttdd�tdd����|�tdtdd�|�ttd�|�ttd d�|�ttdd!�|�ttdd"�|�ttddd�dS)#N�r�)�rPrQ�rQ�rS)���rPrTrP)rPrP���)rP���
)rQ�rY)�m+?�RrPrZ)rS�M�)���rWrV����	)i�	i�	��c�qzFraction(12, 0)�ry�?@�3/2yy�?)r@r>rrN�ZeroDivisionError�assertRaisesrr(rrr�testInitos("�zFractionTest.testInitcCsv|�dttd���|�dttd���|�dttd���|�tttd��|�tttd��|�tttd	��dS)
N)rWrV�@rO��)��L�33fl皙�����?�nan�inf�-inf)r@r>rrf�
ValueErrorr
�
OverflowErrorr(rrr�testInitFromFloat�s
�zFractionTest.testInitFromFloatcCs�|�dtttd����|�dtttd����|�dtttd����|�tttd��|�tttd��|�tttd	��|�tttd
��dS)N)r\rXz1.1)rQ��z3.5e-2rOz.000e20rl�snanrmrn)r@r>rrrfrorpr(rrr�testInitFromDecimal�s���z FractionTest.testInitFromDecimalcCs�|�dttd���|�dttd���|�dttd���|�dttd���|�dttd	���|�d
ttd���|�dttd
���|�dttd���|�dttd���|�dttd���|�dttd���|�dttd���|�dttd���|�dttd���|�tdtd�|�tdtd�|�tdtd �|�td!td"�|�td#td$�|�td%td&�|�td'td(�|�td)td*�|�td+td,�|�td-td.�dS)/N)rWrP�5)rSrVrdz 
  +3/2)���rVz-3/2  )�
rVz    013/02 
  )�rWz 3.2 )i�rWz -3.2 )rvrPz -3. )rSrWz .6 )rPi5z32.e-5)i@BrPz1E+06)i���rPz-1.23e4rOz .0e+0	z-0.000e0zFraction(3, 0)z3/0z"Invalid literal for Fraction: '3/'z3/z"Invalid literal for Fraction: '/2'z/2z$Invalid literal for Fraction: '3 /2'z3 /2z$Invalid literal for Fraction: '3/+2'z3/+2z%Invalid literal for Fraction: '+ 3/2'z+ 3/2z#Invalid literal for Fraction: '3a2'Z3a2z%Invalid literal for Fraction: '3/7.2'z3/7.2z%Invalid literal for Fraction: '3.2/7'z3.2/7z!Invalid literal for Fraction: '.'�.)r@r>rrNreror(rrr�testFromString�sl����������zFractionTest.testFromStringcCs�tdd�}|�dd�|�dt|��|�tt|dd�|�tt|dd	�|�dt|��d
|_d|_|�dt|��|�	td
d�|�dS)NrQrSrVrYrRr;rcr<��)r|rV)
rrr@r>rf�AttributeError�setattrr7r8�assertNotEqual�rr=rrr�
testImmutable�s
zFractionTest.testImmutablecCs*|�ttjd�|�dtt�d���d}|�|dftt�|���|�dtt�d���|�dtt�d���|�d	tt�d
���|�dtt�d���|�td
�tt�d���|�dtt�d���|�dtt�d���d}||}|�tdtj|�|�tdtj|�|�t	dtj|�dS)N�@@)rXrPrXl�{�C	rPrOri�$@)���rVg�)ll+?�RrP�J��-�DrZ)rjl g������	@g��(cannot convert Infinity to integer ratio�#cannot convert NaN to integer ratio)
rfrrrr@r>r
rNrpro)rZbigintrmrlrrr�
testFromFloat�s:�����zFractionTest.testFromFloatcCs|�ttjd�|�tdd�t�d��|�td�t�td���|�tdd�t�td���|�tdd�t�td	���|�td
�t�td���|�dtdd�t�td
���|�tdtjtd��|�tdtjtd��|�tdtjtd��|�tdtjtd��dS)Nr�rXrPrz-0rWz0.5i�z5e-3i�Z5e3l�7�3M�e'z 0.999999999999999999999999999999r�rmrnr�rlrs)	rfrrZfrom_decimalr@rrNrpror(rrr�testFromDecimals2�
�
�
�
�zFractionTest.testFromDecimalcCs\|�tdd���d�|�tdd���d�|�tdd���d�|�tdd���d�dS)	Nr|r{)rVrS���)rUrSi����rrO)r@r�as_integer_ratior(rrr�test_as_integer_ratiosz"FractionTest.test_as_integer_ratiocCs�td�}|�|�d�tdd��|�|�d�tdd��|�|�d�tdd��|�|�d�tdd��|�td	d
��d�td��|�td	d
��d
�tdd
��|�td��d�td��dD]}|�tdtd�j|�q�dS)Nz3.1415926535897932i'rarbi�����piM�j��rr�drP�e�fr)rrTz$max_denominator should be at least 1)rr@Zlimit_denominatorrNro)rZrpi�irrr�testLimitDenominators
�z!FractionTest.testLimitDenominatorcCsr|�dt�tdd���|�dt�tdd���|�dt�tdd���|�dt�tdd���|�dt�tdd���|�dttdd���|�dttdd���|�dttd	d���|�dttd
d���|�dttdd���|�dt	tdd���|�d
t	tdd���|�dt
tdd���|�tt
td��|�
dt
ttd�td����|�dttdd���dS)NrTi����rXrPr\rUr^rr������FTrSrVrkA�22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222227gUUUUUU�?A�33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333331��������?)rCr1�truncrZfloorZceil�int�roundr@�boolr
rfrp�assertAlmostEqual�complexr(rrr�testConversions-s$�zFractionTest.testConversionscCsrtjGdd�d��}tj�|�|d�}t|�}|�|j|�|�t|�d�|d�}t|�}|�t|�d�dS)Nc@s8eZdZdZdd�Zdd�Zedd��Zdd	�ZeZ	d
S)z=FractionTest.testBoolGuarateesBoolReturn.<locals>.CustomValuerPcSs
||_dSr)rr
rrrrLszFFractionTest.testBoolGuarateesBoolReturn.<locals>.CustomValue.__init__cSs
t|j�Sr)r�rr(rrr�__bool__OszFFractionTest.testBoolGuarateesBoolReturn.<locals>.CustomValue.__bool__cSs|Srrr(rrrr;RszGFractionTest.testBoolGuarateesBoolReturn.<locals>.CustomValue.numeratorcSstd��dS)Nz&Avoid comparisons in Fraction.__bool__)�AssertionErrorrrrrrWszDFractionTest.testBoolGuarateesBoolReturn.<locals>.CustomValue.__eq__N)
r+r,r-r<rr��propertyr;rr#rrrr�CustomValueHs
r�rPTrF)	�	functools�total_orderingrr�registerrZassertIsr;r�)rr�r;r=rrr�testBoolGuarateesBoolReturnEsz(FractionTest.testBoolGuarateesBoolReturncCs�|�td�ttd�d��|�td�ttd�d��|�td�ttd�d��|�tdd�ttd	d
�d��|�tdd�ttdd
�d��dS)
Ni8���ij���rUi�����rTrXr�r�rPi��)rCrr�r(rrr�	testRoundis
zFractionTest.testRoundcCs�|�tdd�tdd�tdd��|�tdd�tdd�tdd��|�tdd�tdd�tdd��|�tdd�tdd�tdd��|�dtdd�tdd��|�d	td	d�td��|�tdd
�tdd�tdd��|�tdd�td
d�tdd��|�td�tdd
�fttdd�tdd���|�td�tdd�fttd
d�tdd���|�tdd�tdd�td��|�tdd�tdd�td��|�dtd�tdd��|�tdd�tdd�
�ttd�tdd��}|�|jd�|�|jd�tdd�d}|�|tdd��|�|jd�|�|j	d�tdd�d}|�|tdd��|�|jd�|�|j	d�tdd�d}|�|tdd��|�|jd�|�|j	d�dS)NrPrVrXrWrv�r|r_rZr{rQrSr�rU���@rTr)
r@rrC�divmod�powr��real�imagr;r<)r�z�prrr�testArithmeticps:"""""",,  zFractionTest.testArithmeticc	Cs^|�tdd�tdddd�tdddd��|�tdd�td	d
d�tdd	d
��|�d
tdd�fttdddd�tdddd���|�d	ddtd	d
d�tdd	d
��|�dtdd	d
�tdd	d
��|�dtd	d	d
�fttdd	d
�tdd	d
���|�d	ddtdd�fttd	d
d�tdd	d
���dS)Nl�b;e�f�Op�Umn�P|�}�l�~J�w�5�PHyT#/jei�4�T�rX�#rPlP��~�cegrQlrVr�rSrWl��_�rrrY)rCrrFr�r(rrr�testLargeArithmetic�sH�"����$������z FractionTest.testLargeArithmeticcCs�|�tdd�tdd�d�|�dtdd�d�|�dtdd�d�|�tdd�dtdd��|�ddtdd��|�ddtdd��|�tdd�tdd�d�|�d	tdd�d�|�d
tdd�d�|�tdd�dtdd��|�ddtdd��|�d
dtdd��|�tdd�tdd�d�|�dtdd�d�|�dtdd�d�|�tdd�dtdd��|�ddtdd��|�ddtdd��|�tdd�tdd�d�|�dtdd�d�|�dtdd�d�|�tdd�dtdd��|�ddtdd��|�ddtdd��|�dtdd�d�|�dtdd�d�|�ddtdd��|�ddtdd��|�ddtdd��|�tdd�tdd�d�|�dtdd�d�|�tdd�dtdd��|�ddtdd��|�dtdd�td��|�td�tdd�td��|�td�tdd�td��|�dtdd�td��|�dtdd�fttdd�d��|�tdd�ttdd�d��|�dtd�ftdtdd���|�tdd�tdtdd���|�tdtd��ttdd�td���|�tdtd��ttdd�td���|�tdtd��ttdd�td���|�tdtd��ttdd�td���|�tdd�tdd�d�|�tdd�tdd�d�|�dtdd�d�|�dtdd�d�|�ddtdd��tdtdd��}|�d|j�|�d|j	�|�tdd�dtdd��|�d dtdd��|�d!d tdd��|�ddtdd��|�
ttjtdd�d�dS)"Nr\rXrPg�������?g�?y�������?y�?r]g�������y�������r_g�������?y�������?rkr�r�y$@r�rZl�d�&O�!g"@g�������?rmrnrTg��������r�rUrVr|r�g�?)
rCrr
rFr�r�r�r�r@r�rfrer)rr�rrr�testMixedArithmetic�sx" &&&&

�z FractionTest.testMixedArithmeticcCs<|�ttjtdd�td��|�ttjtd�tdd��dS)NrSr\z	3.1415926)rfrr�addrrr(rrr�testMixingWithDecimal�s
�
�z"FractionTest.testMixingWithDecimalcCs�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�dS)NrPrVrS)�
assertTruer�assertFalser(rrr�testComparisons�szFractionTest.testComparisonscCs�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�|�tdd�tdd�k�dS)NrPrVrSr|rQ)r�rr0r�r(rrr�testComparisonsDummyRational	s8z)FractionTest.testComparisonsDummyRationalcCs�td�}tdd�}|�||k�|�||kp2||k�|�||k�|�||koV||k�|�||k�|�||kpz||k�|�||k�|�||ko�||k�dS)NgUUUUUU�?rPrS)rrr�r�)r�x�yrrr�testComparisonsDummyFloat)s
z&FractionTest.testComparisonsDummyFloatcCs4|�dtdd�k�|�dtdd�k�|�tdd�dk�|�tdd�dk�|�tdd�dk�|�tdd�dk�|�dtdd�k�|�dtdd�k�|�td	�tdd�k�|�td
�tdd�k�|�td
�tdd�k�|�tdd�td	�k�|�tdd�td
�k�|�tdd�td
�k�dS)NrVrWr|rSrP�333333�?皙�����?��?rmrnrrXrlrvrQ�rc�����r�rr�r
r(rrr�
testMixedLess5szFractionTest.testMixedLesscCs4|�dtdd�k�|�dtdd�k�|�tdd�dk�|�tdd�dk�|�dtdd�k�|�dtdd�k�|�tdd�dk�|�tdd�dk�|�td	�tdd�k�|�td
�tdd�k�|�td
�tdd�k�|�tdd�td	�k�|�tdd�td
�k�|�tdd�td
�k�dS)Nr�rPrVr�r�r|rSrWrmrnrrXrlrvrQr�rcr�r�r�r(rrr�testMixedLessEqualGszFractionTest.testMixedLessEqualcCs�|�td�td�k�|�dttt�d�d��k�|�dtt�d�d�k�|�dtt�d�d�k�|�dtt�d�d�k�|�dtt�d�d�k�dS)NrZr�rP)r�rr
r1r�r�r(rrr�testBigFloatComparisonsXs z$FractionTest.testBigFloatComparisonscCs�|�td�td�k�|�ttjtd�td��|�ttjtd�td��tdd�}tdd�}tdd�}|�||k�|�||k�|�||k�|�||k�tj	tjtjtj
fD]D}|�t|||�|�t|||�|�t|||�|�t|||�q�dS)NrZrSr�g�?r�g�������?)r�rr�rfrrr&r r�r"r$)rr�r��wrrrr�testBigComplexComparisonsbs


z&FractionTest.testBigComplexComparisonscCs|�dtdd�k�|�dtdd�k�|�tdd�dk�|�tdd�dk�|�dtdd�k�|�dtdd�k�|�tdd�dk�|�tdd�dk�|�tdd�td	�k�|�td	�tdd
�k�|�tdd�td�k�|�td�tdd�k�dS)
Nr�rPrVr�r�r|rSrWrlrQrmrnr�r(rrr�testMixedEqualtszFractionTest.testMixedEqualcCsp|�dttdd���|�dttd���|�dttdd���|�d	ttdd���|�d
ttdd���dS)NzFraction(7, 3)rQrSz Fraction(6283185307, 2000000000)z3.1415926535z#Fraction(-1, 100000000000000000000)rPl���� F�x:^Vz7/3�7)r@�reprrrGr(rrr�testStringification�s
��z FractionTest.testStringificationcCs�tjj}tjj}|�td�ttdd���|�tdd�ttdd���|�ttd��ttd���|�|ttd|���|�ttd��td��	��dS)	NrhrWrVrX�2rZrPrT)
�sys�	hash_info�modulusrmr@�hashrrr
�__hash__)rZhmodZhinfrrr�testHash�s zFractionTest.testHashc	Cs�td�}d|dddddf\}}}}}}}t||�tdd�kr||}|||d}}|||d}}|||}||7}q(|�tj|�dS)NrSrrP��ʚ;r�� )r�absr�r1Zpi)	rZthree�lasts�t�s�nZna�dZdarrr�testApproximatePi�s 
zFractionTest.testApproximatePicCs�td�}ddtd�dddf\}}}}}}t||�tdd�kr�|}|d7}|||d9}|||9}|d9}||||7}q(|�t�d�|�dS)NrPrr�rVrT)rr�r�r1Zcos)rr�r�r�r�Zfactr2Zsignrrr�testApproximateCos1�s z FractionTest.testApproximateCos1cCs�tdd�}tdd�}|�|tt|���|�t|�tt|���|�t|�tt|���|�t|�tt|���|�t|�tt|���|�	|t|��|�	|t|��dS)NrwrQ)
rr:r@rr�idrrrrC)rr=Zdrrrr�test_copy_deepcopy_pickle�s

z&FractionTest.test_copy_deepcopy_picklecCs tdd�}|�tt|dd�dS)NrwrQ�arX)rrfr}r~r�rrr�
test_slots�s
zFractionTest.test_slotscCshGdd�dt�}t�|d�|d��}|�|jd�|�|jd�|�t|j�|�|�t|j�|�dS)Nc@s<eZdZdd�Zdd�Zdd�Zedd��Zed	d
��ZdS)z-FractionTest.test_int_subclass.<locals>.myintcSst|�t|�t|��Sr�rAr�rrrr�__mul__�sz5FractionTest.test_int_subclass.<locals>.myint.__mul__cSst|�t|�t|��Srr�rrrr�__floordiv__�sz:FractionTest.test_int_subclass.<locals>.myint.__floordiv__cSst|�t|�t|��}|Srr�)rrr�rrr�__mod__�sz5FractionTest.test_int_subclass.<locals>.myint.__mod__cSst|�t|��Srr�r(rrrr;�sz7FractionTest.test_int_subclass.<locals>.myint.numeratorcSst|�d�S)NrP)rAr(rrrr<�sz9FractionTest.test_int_subclass.<locals>.myint.denominatorN)	r+r,r-r�r�r�r�r;r<rrrr�myint�s
r�rSr{rPrV)r�r5r6r@r;r<rA)rr��frrr�test_int_subclass�szFractionTest.test_int_subclassN)&r+r,r-rCrFrNrgrrqrtrzr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr?YsF



5$ "E 
	
r?�__main__)r.�decimalrZtest.supportrr1rrr5r�r�Zunittestrr�picklerrr6r�objectrr0r:r>ZTestCaser?r+�mainrrrr�<module>s0