U
    ‰dô/  ã                   @   s@   d dl mZmZmZmZ d dlZe e¡ZG dd„ dej	ƒZ
dS )é    )ÚcontrolÚcoreÚ	test_utilÚ	workspaceNc                       st  e Zd Z‡ f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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'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Z d=d>„ Z!d?d@„ Z"dAdB„ Z#dCdD„ Z$dEdF„ Z%dGdH„ Z&dIdJ„ Z'dKdL„ Z(dMdN„ Z)dOdP„ Z*dQdR„ Z+dSdT„ Z,dUdV„ Z-dWdX„ Z.‡  Z/S )YÚTestControlc           	         sØ  t t| ƒ ¡  d| _t d¡| _| jjg dd}| jjg g | jtj	j
d}| jjg g dtj	j
d}t d¡| _| j |g¡ | j |g¡}| jjg |gg dtj	j
d | j |¡ t d¡| _| j |g¡ | j |g¡ | j |g¡}| jjg |gg dtj	j
d | j |¡ t d¡| _| j ||g¡}| j |¡ t d	¡| _| j ||g¡}| j |¡ t d
¡| _| j ||g¡}| j |¡ t d¡| _| j ||g¡}| j |¡ t d¡| _| jjg g dtj	j
d d S )Né
   zinit-netr   )Z
init_count)ÚshapeÚvalueZdtypezcnt-netz	cnt-2-netzcond-netznot-cond-netztrue-cond-netzfalse-cond-netzidle-net)Úsuperr   ÚsetUpÚN_r   ZNetÚ	init_net_ZCreateCounterZConstantFillZDataTypeZINT64Úcnt_net_ZCountUpZRetrieveCountZAddExternalOutputÚ
cnt_2_net_Ú	cond_net_ÚLTÚnot_cond_net_ZGEÚtrue_cond_net_Úfalse_cond_net_ÚGTÚ	idle_net_)	ÚselfZcntZconst_nZconst_0Zcurr_cntZ
curr_cnt_2Z	cond_blobZ	true_blobZ
false_blob©Ú	__class__© ú>/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/control_test.pyr      st       ÿ   ÿ    ÿ    ÿ   ÿzTestControl.setUpc                 C   s2   |D ](\}}t  | ¡ jd ¡}|  ||¡ qdS )zm
        Check the net output is expected
        nets_and_expects is a list of tuples (net, expect)
        éÿÿÿÿN)r   Ú	FetchBlobÚProtoÚexternal_outputÚassertEqual)r   Znets_and_expectsÚnetÚexpectÚoutputr   r   r   ÚCheckNetOutput;   s
    ÿzTestControl.CheckNetOutputc                 C   sT   |   t| ¡ jƒt|ƒ¡ tt|ƒƒD ](}t | ¡ j| ¡}|   ||| ¡ q&dS )zV
        Check the net output is expected
        expects is a list of bools.
        N)r    Úlenr   r   Úranger   r   )r   r!   ZexpectsÚir#   r   r   r   ÚCheckNetAllOutputE   s    ÿzTestControl.CheckNetAllOutputc                 C   s>   t  d¡}| t d| j¡¡ | |¡ |  t |¡d¡ d S )NÚtestÚinitT)	r   ZPlanZAddStepr   ÚDor   r    r   ZRunPlan)r   ÚstepZplanr   r   r   ÚBuildAndRunPlanP   s    

zTestControl.BuildAndRunPlanc                 C   s2   t  d|| j¡}|  |¡ |  | j| jfg¡ d S )NZmyFor)r   ZForr   r-   r$   r   ©r   Znets_or_stepsr,   r   r   r   ÚForLoopTestV   s    
zTestControl.ForLoopTestc                 C   s"   |   | j¡ |   | j| jg¡ d S ©N)r/   r   r   ©r   r   r   r   ÚtestForLoopWithNets[   s    zTestControl.testForLoopWithNetsc                 C   s,   t  d| j¡}|  |¡ |  || jg¡ d S ©NÚcount)r   r+   r   r/   r   ©r   r,   r   r   r   ÚtestForLoopWithStep_   s    
zTestControl.testForLoopWithStepc                 C   s2   t  d| j|¡}|  |¡ |  | j| jfg¡ d S )NZmyWhile)r   ZWhiler   r-   r$   r   r   r.   r   r   r   ÚWhileLoopTestd   s    
zTestControl.WhileLoopTestc                 C   s"   |   | j¡ |   | j| jg¡ d S r0   )r7   r   r   r1   r   r   r   ÚtestWhileLoopWithNeti   s    z TestControl.testWhileLoopWithNetc                 C   s,   t  d| j¡}|  |¡ |  || jg¡ d S r3   )r   r+   r   r7   r   r5   r   r   r   ÚtestWhileLoopWithStepm   s    
z!TestControl.testWhileLoopWithStepc                 C   s2   t  d| j|¡}|  |¡ |  | j| jfg¡ d S )NZmyUntil)r   ZUntilr   r-   r$   r   r   r.   r   r   r   ÚUntilLoopTestr   s    
zTestControl.UntilLoopTestc                 C   s"   |   | j¡ |   | j| jg¡ d S r0   )r:   r   r   r1   r   r   r   ÚtestUntilLoopWithNetw   s    z TestControl.testUntilLoopWithNetc                 C   s,   t  d| j¡}|  |¡ |  || jg¡ d S r3   )r   r+   r   r:   r   r5   r   r   r   ÚtestUntilLoopWithStep{   s    
z!TestControl.testUntilLoopWithStepc                 C   s2   t  d| j|¡}|  |¡ |  | j| jfg¡ d S )NZ	myDoWhile)r   ZDoWhiler   r-   r$   r   r   r.   r   r   r   ÚDoWhileLoopTest€   s    
zTestControl.DoWhileLoopTestc                 C   s"   |   | j¡ |   | j| jg¡ d S r0   )r=   r   r   r1   r   r   r   ÚtestDoWhileLoopWithNet…   s    z"TestControl.testDoWhileLoopWithNetc                 C   s,   t  d| j¡}|  |¡ |  | j|g¡ d S r3   )r   r+   r   r=   r   r5   r   r   r   ÚtestDoWhileLoopWithStep‰   s    
z#TestControl.testDoWhileLoopWithStepc                 C   s2   t  d| j|¡}|  |¡ |  | j| jfg¡ d S )NZ	myDoUntil)r   ZDoUntilr   r-   r$   r   r   r.   r   r   r   ÚDoUntilLoopTestŽ   s    
zTestControl.DoUntilLoopTestc                 C   s"   |   | j¡ |   | j| jg¡ d S r0   )r@   r   r   r1   r   r   r   ÚtestDoUntilLoopWithNet“   s    z"TestControl.testDoUntilLoopWithNetc                 C   s,   t  d| j¡}|  |¡ |  | j|g¡ d S r3   )r   r+   r   r@   r   r5   r   r   r   ÚtestDoUntilLoopWithStep—   s    
z#TestControl.testDoUntilLoopWithStepc              	   C   sb   |r2t  dt  d|¡t  d| ¡ jd | j¡¡}nt  d|| j¡}|  |¡ |  | j|fg¡ d S )Nzif-allr4   ZmyIfr   )r   r+   ÚIfr   r   r   r-   r$   ©r   Úcond_netr"   Úcond_on_blobr,   r   r   r   Ú
IfCondTestœ   s    
ÿý
zTestControl.IfCondTestc                 C   s   |   | jdd¡ d S ©Né   F©rG   r   r1   r   r   r   ÚtestIfCondTrueOnNet¨   s    zTestControl.testIfCondTrueOnNetc                 C   s   |   | jdd¡ d S ©NrI   TrJ   r1   r   r   r   ÚtestIfCondTrueOnBlob«   s    z TestControl.testIfCondTrueOnBlobc                 C   s   |   | jdd¡ d S ©Nr   F©rG   r   r1   r   r   r   ÚtestIfCondFalseOnNet®   s    z TestControl.testIfCondFalseOnNetc                 C   s   |   | jdd¡ d S ©Nr   TrO   r1   r   r   r   ÚtestIfCondFalseOnBlob±   s    z!TestControl.testIfCondFalseOnBlobc              
   C   sz   |r| j }n| j}|rHt dt d|¡t d| ¡ jd | j | j¡¡}nt d|| j | j¡}|  |¡ |  ||fg¡ d S )Nzif-else-allr4   ZmyIfElser   )	r   r   r   r+   rC   r   r   r-   r$   ©r   rE   Z
cond_valuer"   rF   Zrun_netr,   r   r   r   ÚIfElseCondTest´   s$    
 ÿý ÿ
zTestControl.IfElseCondTestc                 C   s   |   | jddd¡ d S )NTrI   F©rT   r   r1   r   r   r   ÚtestIfElseCondTrueOnNetÅ   s    z#TestControl.testIfElseCondTrueOnNetc                 C   s   |   | jddd¡ d S )NTrI   rU   r1   r   r   r   ÚtestIfElseCondTrueOnBlobÈ   s    z$TestControl.testIfElseCondTrueOnBlobc                 C   s   |   | jddd¡ d S )NFé   ©rT   r   r1   r   r   r   ÚtestIfElseCondFalseOnNetË   s    z$TestControl.testIfElseCondFalseOnNetc                 C   s   |   | jddd¡ d S )NFrX   TrY   r1   r   r   r   ÚtestIfElseCondFalseOnBlobÎ   s    z%TestControl.testIfElseCondFalseOnBlobc              	   C   sb   |r2t  dt  d|¡t  d| ¡ jd | j¡¡}nt  d|| j¡}|  |¡ |  | j|fg¡ d S )Nzif-notr4   ZmyIfNotr   )r   r+   ÚIfNotr   r   r   r-   r$   rD   r   r   r   ÚIfNotCondTestÑ   s    
ÿý
zTestControl.IfNotCondTestc                 C   s   |   | jdd¡ d S rN   ©r]   r   r1   r   r   r   ÚtestIfNotCondTrueOnNetÝ   s    z"TestControl.testIfNotCondTrueOnNetc                 C   s   |   | jdd¡ d S rQ   r^   r1   r   r   r   ÚtestIfNotCondTrueOnBlobà   s    z#TestControl.testIfNotCondTrueOnBlobc                 C   s   |   | jdd¡ d S rH   ©r]   r   r1   r   r   r   ÚtestIfNotCondFalseOnNetã   s    z#TestControl.testIfNotCondFalseOnNetc                 C   s   |   | jdd¡ d S rL   ra   r1   r   r   r   ÚtestIfNotCondFalseOnBlobæ   s    z$TestControl.testIfNotCondFalseOnBlobc              
   C   sz   |r| j }n| j}|rHt dt d|¡t d| ¡ jd | j| j ¡¡}nt d|| j| j ¡}|  |¡ |  ||fg¡ d S )Nzif-not-elser4   ZmyIfNotElser   )	r   r   r   r+   r\   r   r   r-   r$   rS   r   r   r   ÚIfNotElseCondTesté   s&    
 þý ÿ
zTestControl.IfNotElseCondTestc                 C   s   |   | jddd¡ d S )NTrX   F©rd   r   r1   r   r   r   ÚtestIfNotElseCondTrueOnNetû   s    z&TestControl.testIfNotElseCondTrueOnNetc                 C   s   |   | jddd¡ d S )NTrX   re   r1   r   r   r   ÚtestIfNotElseCondTrueOnBlobþ   s    z'TestControl.testIfNotElseCondTrueOnBlobc                 C   s   |   | jddd¡ d S )NFrI   ©rd   r   r1   r   r   r   ÚtestIfNotElseCondFalseOnNet  s    z'TestControl.testIfNotElseCondFalseOnNetc                 C   s   |   | jddd¡ d S )NFrI   Trh   r1   r   r   r   ÚtestIfNotElseCondFalseOnBlob  s    z(TestControl.testIfNotElseCondFalseOnBlobc                 C   sF   t  d| j| jf| j| jf¡}|  |¡ |  | jdf| jdfg¡ d S )NZmySwitchr   rX   )r   ZSwitchr   r   r   r   r-   r$   r5   r   r   r   Ú
testSwitch  s    

ý
zTestControl.testSwitchc                 C   sF   t  d| j| jf| j| jf¡}|  |¡ |  | jdf| jdfg¡ d S )NZmySwitchNotrI   r   )r   Z	SwitchNotr   r   r   r   r-   r$   r5   r   r   r   ÚtestSwitchNot  s    

ý
zTestControl.testSwitchNotc                 C   s˜   t  d¡}t  d|¡}|  |¡ |  |dg¡ t  dd¡}t  d|¡}|  |¡ |  |ddg¡ t  ddg¡}t  d|¡}|  |¡ |  |ddg¡ d S )N)ÚaTÚboolT)ÚbFF)r   ZBoolNetr+   r-   r(   )r   Zbool_netr,   r   r   r   ÚtestBoolNet  s    



zTestControl.testBoolNetc                 C   sŒ   t  d| j| jgd¡}t  d| j| j|¡}|  |¡ |  |dfg¡ t  d| j| jgd¡}t  d| j| j|¡}|  |¡ |  |dfg¡ d S )Nr)   ÚOrÚcombineTÚAndF)r   ZCombineConditionsr   r   r+   r-   r$   )r   Zcombine_netr,   r   r   r   ÚtestCombineConditions)  s0     
 ÿý
 
 ÿý
z!TestControl.testCombineConditionsc                 C   s|   t  d| j| jgd¡}t  d|¡}|  |¡ |  |dfg¡ t  d| j| jgd¡}t  d|¡}|  |¡ |  |dfg¡ d S )Nr)   rq   ÚmergeTrs   F)r   ZMergeConditionNetsr   r   r+   r-   r$   )r   Z	merge_netr,   r   r   r   ÚtestMergeConditionNets>  s      
 ÿ
 
 ÿ
z"TestControl.testMergeConditionNets)0Ú__name__Ú
__module__Ú__qualname__r   r$   r(   r-   r/   r2   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rG   rK   rM   rP   rR   rT   rV   rW   rZ   r[   r]   r_   r`   rb   rc   rd   rf   rg   ri   rj   rk   rl   rp   rt   rv   Ú__classcell__r   r   r   r   r      sX   .
		r   )Zcaffe2.pythonr   r   r   r   ÚloggingÚ	getLoggerrw   ÚloggerZTestCaser   r   r   r   r   Ú<module>   s   
