
[XMc           @   s   d  d l  Z  d  d l Z d  d l m Z d  d l m Z d  d l m Z d  d l m	 Z	 m
 Z
 d e j f d     YZ d e j f d	     YZ d
 e j f d     YZ d S(   iN(   t   unittest(   t   banana(   t   failure(   t   protocolt   maint   MathTestCasec           B   s   e  Z d    Z RS(   c         C   s   t  d d  t  d d  t  d d  d
 g } xg | D]_ } t j   } t j | | j  | j   } t j |  } | | k s7 t d	 | | f  q7 Wd  S(   Ni    id   i  iL  i@B iB i   l   
 s   y = %s; i = %sl                (   t   ranget   StringIOR   t   int2b128t   writet   getvaluet   b1282intt   AssertionError(   t   selft	   funkylistt   it   xt   vt   y(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   testInt2b128   s    0(   t   __name__t
   __module__R   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyR      s   t   BananaTestCasec           B   s   e  Z e j Z d    Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   c         C   s]   t  j    |  _ |  j   |  _ |  j j t j |  j   |  j j d  |  j |  j _	 d  S(   Nt   none(
   R   t   iot   encClasst   enct   makeConnectionR   t   FileWrappert   _selectDialectt	   putResultt   expressionReceived(   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   setUp   s
    c         C   s   | |  _  d  S(   N(   t   result(   R   R!   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyR   #   s    c         C   s&   |  j  j t j t j   |  `  d  S(   N(   R   t   connectionLostR   t   FailureR   t   CONNECTION_DONE(   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   tearDown&   s    c         C   sH   |  j  j d  g  } |  j  j |  j j    |  j d k sD t  d  S(   Nt   hello(   R   t   sendEncodedt   dataReceivedR   R
   R!   R   (   R   t   l(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt
   testString*   s    c         C   sd   x] d D]U } |  j  j |  |  j  j |  j j    |  j |  j d  |  j |  j t  q Wd S(   s   
        A positive integer less than 2 ** 32 should round-trip through
        banana without changing value and should come out represented
        as an C{int} (regardless of the type which was encoded).
        i'  l   'N(   i'  l   '(	   R   R'   R(   R   R
   t   assertEqualsR!   t   assertIsInstancet   int(   R   t   value(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   test_int0   s
    c         C   s   x d	 D] } x d
 D] } d | | } x | | d f D] } |  j  j d  |  j j |  |  j j |  j  j    |  j |  j |  | t j	 k s | t j	 d k  r |  j
 |  j t  q: |  j
 |  j t  q: Wq Wq Wd S(   s  
        Integers greater than 2 ** 32 and less than -2 ** 32 should
        round-trip through banana without changing value and should
        come out represented as C{int} instances if the value fits
        into that type on the receiving platform.
        i    i@   i   i   i    i   i   N(   i    i@   i   i   (   i    i   (   R   t   truncateR   R'   R(   R
   R+   R!   t   syst   maxintR,   t   longR-   (   R   t   expt   addt   mt   n(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   test_largeLong=   s    #c         C   s2   |  j  j } | d } d | d } | d } | S(   Ni   i   i   (   R   t   prefixLimit(   R   t   bytest   bitst   largestt   smallest(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   _getSmallestR   s
    

c         C   s,   |  j    } |  j t j |  j j |  d S(   sz   
        Test that a long above the implementation-specific limit is rejected
        as too large to be encoded.
        N(   R>   t   assertRaisesR   t   BananaErrorR   R'   (   R   R=   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   test_encodeTooLargeLong_   s    c         C   s   |  j    } |  j j |  j j d  |  j j |  |  j j   } |  j j d  |  j j |  j j d  |  j t	 j
 |  j j |  d S(   sz   
        Test that a long above the implementation specific limit is rejected
        as too large to be decoded.
        i   i    N(   R>   R   t   setPrefixLimitR9   R'   R   R
   R0   R?   R   R@   R(   (   R   R=   t   encoded(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   test_decodeTooLargeLongh   s    c         C   s   |  j    S(   N(   R>   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   _getLargestw   s    c         C   s,   |  j    } |  j t j |  j j |  d S(   s   
        Test that a negative long below the implementation-specific limit is
        rejected as too small to be encoded.
        N(   RE   R?   R   R@   R   R'   (   R   R<   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   test_encodeTooSmallLong{   s    c         C   s   |  j    } |  j j |  j j d  |  j j |  |  j j   } |  j j d  |  j j |  j j d  |  j t	 j
 |  j j |  d S(   s   
        Test that a negative long below the implementation specific limit is
        rejected as too small to be decoded.
        i   i    N(   RE   R   RB   R9   R'   R   R
   R0   R?   R   R@   R(   (   R   R<   RC   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   test_decodeTooSmallLong   s    c         C   sL   |  j  j d  |  j  j |  j j    |  j d k sH t d |  j  d  S(   Nls   should be -1015l, got %s(   R   R'   R(   R   R
   R!   R   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   testNegativeLong   s    c         C   sL   |  j  j d  |  j  j |  j j    |  j d k sH t d |  j  d  S(   Ni  s   should be 1015, got %s(   R   R'   R(   R   R
   R!   R   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   testInteger   s    c         C   sL   |  j  j d  |  j  j |  j j    |  j d k sH t d |  j  d  S(   Ni	s   should be -1015, got %s(   R   R'   R(   R   R
   R!   R   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   testNegative   s    c         C   sB   |  j  j d  |  j  j |  j j    |  j d k s> t  d  S(   Ng     @(   R   R'   R(   R   R
   R!   R   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt	   testFloat   s    c      	   C   s   d d d d g d d g d d d	 d
 d g g d g g  g } |  j  j |  |  j  j |  j j    |  j | k s t d t |  j  t |  j  f  d  S(   Ni   i   i   i   g     >@gD@i   t   sixt   sevent   eighti	   i
   s   %s!=%s(   R   R'   R(   R   R
   R!   R   t   repr(   R   t   foo(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   testList   s    <c         C   s   d d d d g d d g d d d	 d
 d g g d g t  j d t  j d t  j d g
 } |  j j |  x' |  j j   D] } |  j j |  qw W|  j | k s t d t	 |  j  t	 |  f  d  S(   Ni   i   i   i   g     >@gD@i   RL   RM   RN   i	   i
   l    l    l s   %s!=%s(
   R1   R2   R   R'   R   R
   R(   R!   R   RO   (   R   RP   t   byte(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   testPartial   s    $c         C   s%   x | D] } |  j  j |  q Wd  S(   N(   R   R(   (   R   t   dataRR   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   feed   s    c         C   s#   d } |  j  t j |  j |  d  S(   Ns   (   t   failUnlessRaisesR   R@   RU   (   R   RT   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   testOversizedList   s    c         C   s#   d } |  j  t j |  j |  d  S(   Ns   (   RV   R   R@   RU   (   R   RT   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   testOversizedString   s    c         C   s5   d } y |  j  j |  Wn t j k
 r0 n Xd  S(   Nt       (   R   R(   R   R@   (   R   t   crashString(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   testCrashString   s
    c         C   sL   |  j  j d  |  j  j |  j j    |  j d k sH t d |  j  d  S(   Ni   s   should be -2147483648, got %s(   R   R'   R(   R   R
   R!   R   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   testCrashNegativeLong   s    c            s    f d   } d 
} d } d }   j  | | d  d |    j  | | d  d |    j  | | d	  d
 |  d }   j  | | d  d |    j  | | d  d |    j  | | d  d |  d }   j  | | d  d |    j  | | d  d
 |    j  | | d	  d  d }   j  | | d  d |    j  | | d  d |    j  | | d  d |  d S(   s   
        Test that integers below the maximum C{INT} token size cutoff are
        serialized as C{INT} or C{NEG} and that larger integers are
        serialized as C{LONGINT} or C{LONGNEG}.
        c            s:     j  j d    j  j     j j |     j  j   S(   Ni    (   R   t   seekR0   R   R'   R
   (   R7   (   R   (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyRC      s    ii   s   i   s   }i   s   ~i    s   t      t    s   i   s   s   t       t      s   N(   t   assertEqual(   R   RC   t	   baseIntInt	   baseNegInt
   baseIntOutt   baseLongIntOutt
   baseNegOutt   baseLongNegOut(    (   R   s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   test_sizedIntegerTypes   s&    (   R   R   R   t   BananaR   R    R   R%   R*   R/   R8   R>   RA   RD   RE   RF   RG   RH   RI   RJ   RK   RQ   RS   RU   RW   RX   R[   R\   Ri   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyR      s2   																					
					t   GlobalCoderTestsc           B   s   e  Z d  Z d   Z RS(   sM   
    Tests for the free functions L{banana.encode} and L{banana.decode}.
    c         C   sJ   d d d } |  j  t j t j |  d } |  j t j |  d  d S(   sW   
        Test that state doesn't carry over between calls to L{banana.decode}.
        s   iA   s   s   i   N(   R?   R   R@   t   decodeRb   (   R   t   undecodablet	   decodable(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   test_statelessDecode  s    (   R   R   t   __doc__Ro   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyRk     s   (   R   R1   t   twisted.trialR    t   twisted.spreadR   t   twisted.pythonR   t   twisted.internetR   R   t   TestCaseR   R   Rk   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_banana.pyt   <module>   s   
