Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Division by 11 and property based testing with Hypothesis

Division by 11 and property based testing with Hypothesis

A demo of the Python framework for property based tests: https://hypothesis.readthedocs.org/en/latest/

You can find an html version of these slides as well as the scripts I ran in it here: http://vknight.org/Talks/

Vince Knight

March 22, 2016
Tweet

More Decks by Vince Knight

Other Decks in Research

Transcript

  1. > > > f o r k i n r

    a n g e ( 1 0 ) : . . . p r i n t ( 1 1 * k ) 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
  2. A number is divisible by 11 if and only if

    the alternating (in sign) sum of the number’s digits is 0. f (11) = 1 − 1 f (22) = 2 − 2 f (121) = 1 − 2 + 1
  3. `main.py` d e f d i v i s i

    b l e _ b y _ 1 1 ( n u m b e r ) : " " " U s e s a b o v e c r i t e r i o n t o c h e c k i f n u m b e r i s d i v i s i b l e b y 1 1 " " " s t r i n g _ n u m b e r = s t r ( n u m b e r ) a l t e r n a t i n g _ s u m = s u m ( [ ( - 1 ) * * i * i n t ( d ) f o r i , d i n e n u m e r a t e ( s t r i n g _ n u m b e r ) ] ) r e t u r n a l t e r n a t i n g _ s u m = = 0
  4. > > > i m p o r t m

    a i n > > > f o r k i n r a n g e ( 1 0 ) : . . . p r i n t ( m a i n . d i v i s i b l e _ b y _ 1 1 ( 1 1 * k ) ) T r u e T r u e T r u e T r u e T r u e T r u e T r u e T r u e T r u e T r u e
  5. `test_main.py` i m p o r t u n i

    t t e s t i m p o r t m a i n c l a s s T e s t D i v i s i b l e ( u n i t t e s t . T e s t C a s e ) : d e f t e s t _ d i v i s i b l e _ b y _ 1 1 ( s e l f ) : f o r k i n r a n g e ( 1 0 ) : s e l f . a s s e r t T r u e ( m a i n . d i v i s i b l e _ b y _ 1 1 ( 1 1 * k ) ) s e l f . a s s e r t F a l s e ( m a i n . d i v i s i b l e _ b y _ 1 1 ( 1 1 * k + 1 ) ) # S o m e m o r e e x a m p l e s s e l f . a s s e r t T r u e ( m a i n . d i v i s i b l e _ b y _ 1 1 ( 1 2 1 ) ) s e l f . a s s e r t F a l s e ( m a i n . d i v i s i b l e _ b y _ 1 1 ( 1 2 3 ) )
  6. $ p y t h o n 3 - m

    u n i t t e s t t e s t _ m a i n . - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - R a n 1 t e s t i n 0 . 0 0 0 s
  7. `test_property_main.py` i m p o r t u n i

    t t e s t i m p o r t m a i n f r o m h y p o t h e s i s i m p o r t g i v e n f r o m h y p o t h e s i s . s t r a t e g i e s i m p o r t i n t e g e r s c l a s s T e s t D i v i s i b l e ( u n i t t e s t . T e s t C a s e ) : @ g i v e n ( k = i n t e g e r s ( m i n _ v a l u e = 1 ) ) d e f t e s t _ d i v i s i b l e _ b y _ 1 1 ( s e l f , k ) : s e l f . a s s e r t T r u e ( m a i n . d i v i s i b l e _ b y _ 1 1 ( 1 1 * k ) )
  8. $ p y t h o n 3 - m

    u n i t t e s t t e s t _ p r o p e r t y _ m a i n F a l s i f y i n g e x a m p l e : t e s t _ d i v i s i b l e _ b y _ 1 1 ( s e l f = < t e s t _ p r o p e r t y _ m a i n . t e s t d i v i s i b l e t F = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = F A I L : t e s t _ d i v i s i b l e _ b y _ 1 1 ( t e s t _ p r o p e r t y _ m a i n . T e s t D i v i s i b l e ) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - T r a c e b a c k ( m o s t r e c e n t c a l l l a s t ) : F i l e " t e s t _ p r o p e r t y _ m a i n . p y " , l i n e 1 0 , i n t e s t _ d i v i s i b l e _ b y _ 1 1 d e f t e s t _ d i v i s i b l e _ b y _ 1 1 ( s e l f , k ) : F i l e " / u s r / l o c a l / l i b / p y t h o n 2 . 7 / s i t e - p a c k a g e s / h y p o t h e s i s / c o r e . p y " , l i n e 5 0 2 p r i n t _ e x a m p l e = T r u e , i s _ f i n a l = T r u e F i l e " / u s r / l o c a l / l i b / p y t h o n 2 . 7 / s i t e - p a c k a g e s / h y p o t h e s i s / e x e c u t o r s . p y " , l i n e r e t u r n f u n c t i o n ( d a t a ) F i l e " / u s r / l o c a l / l i b / p y t h o n 2 . 7 / s i t e - p a c k a g e s / h y p o t h e s i s / c o r e . p y " , l i n e 1 0 3 r e t u r n t e s t ( * a r g s , * * k w a r g s ) F i l e " t e s t _ p r o p e r t y _ m a i n . p y " , l i n e 1 1 , i n t e s t _ d i v i s i b l e _ b y _ 1 1 s e l f . a s s e r t T r u e ( m a i n . d i v i s i b l e _ b y _ 1 1 ( 1 1 * k ) )
  9. `main.py` d e f d i v i s i

    b l e _ b y _ 1 1 ( n u m b e r ) : " " " U s e s * * c o r r e c t * * c r i t e r i o n t o c h e c k i f n u m b e r i s d i v i s i b l e b y 1 1 " " " s t r i n g _ n u m b e r = s t r ( n u m b e r ) a l t e r n a t i n g _ s u m = s u m ( [ ( - 1 ) * * i * i n t ( d ) f o r i , d i n e n u m e r a t e ( s t r i n g _ n u m b e r ) ] ) r e t u r n a l t e r n a t i n g _ s u m % 1 1 = = 0