Skip to content

Instantly share code, notes, and snippets.

@jbclements
Created July 3, 2013 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbclements/5921746 to your computer and use it in GitHub Desktop.
Save jbclements/5921746 to your computer and use it in GitHub Desktop.
slides for a short talk on mutation and testing. Won't make much sense without the scripted talk. Also, you need to have light-bulb-changer.png.
#lang slideshow
(define machine-bitmap
(scale (bitmap "./light-bulb-changer.png")
0.75))
;; yikes! this code could use some serious cleanup. Sorry.
(define middle-rect-width 350)
(define middle-rect-height 350)
(define insetted
(inset/clip machine-bitmap
(/ (- middle-rect-width (pict-width machine-bitmap)) 2)
(/ (- middle-rect-height (pict-height machine-bitmap)) 2)))
(slide
(text "Mutation and Testing" null 60))
(slide
(text "So, you wrote a program." null 30)
(text "does it work?" null 30))
;; put an arrow from the rc of l to the lc of r in this pict
(define (my-arrower pict l r)
(pin-arrow-line
30.0
pict l rc-find r lc-find
#:line-width 10))
;; same, only gray...
(define (my-gray-arrower pict l r)
(pin-arrow-line
30.0
pict l rc-find r lc-find
#:line-width 10
#:color "gray"))
(define (make-placeholder)
(blank 1 1))
(define input-space 150)
(define input-end-space 40)
;; blanks to be used as the beginnings of arrows
(define i1 (make-placeholder))
(define i2 (make-placeholder))
(define i3 (make-placeholder))
(define i-stack
(vc-append i1
(blank 0 input-space)
i2
(blank 0 input-space)
i3))
;; blanks to be used as the ends of arrows
(define ie1 (make-placeholder))
(define ie2 (make-placeholder))
(define ie3 (make-placeholder))
(define ie-stack
(vc-append ie1
(blank 0 input-end-space)
ie2
(blank 0 input-end-space)
ie3))
(define output-space 30)
(define output-begin-space 8)
(define outputs (for/list ([i 9])
(make-placeholder)))
(define o-stack
(apply vc-append
(add-between outputs
(blank 0 output-space))))
;; same as before, only this time abstract over it...
(define output-begins (for/list ([i 9]) (make-placeholder)))
(define ob-stack
(apply vc-append
(add-between output-begins
(blank 0 output-begin-space))))
(define middle (linewidth
5
(rectangle 350 350)))
(define arrow-pairs
(list (list i1 ie1)
(list i2 ie2)
(list i3 ie3)))
(define output-arrow-pairs
(map list output-begins outputs))
(define (add-input-arrow new-pair pict)
(my-arrower
pict
(first new-pair)
(second new-pair)))
(define (add-state-output-arrow new-pair pict)
(my-gray-arrower
pict
(first new-pair)
(second new-pair)))
(define (functional-fun middle-pict)
(my-arrower
(foldr
add-input-arrow
(hc-append
i-stack
(blank 170 0)
ie-stack
middle-pict
ob-stack
(blank 170 0)
o-stack)
arrow-pairs)
middle
(list-ref outputs 4)))
(define (functional-fun/outputs middle-pict out-indexes)
(foldr
add-state-output-arrow
(my-arrower
(foldr
add-input-arrow
(hc-append
i-stack
(blank 170 0)
ie-stack
middle-pict
ob-stack
(blank 170 0)
o-stack)
arrow-pairs)
middle
(list-ref outputs 4))
(for/list ([i out-indexes]) (list-ref output-arrow-pairs i))))
(slide (functional-fun middle))
(slide (functional-fun
(cc-superimpose middle (colorize (text "X" null 200) "red"))))
(slide (functional-fun
(cc-superimpose
insetted
middle)))
(define little-funs-placement
`(( 0.4 10 20)
( 0.3 100 221)
( 0.15 242 13)
(0.05 227 97)
(0.05 132 128)
(0.05 26 98)
(0.05 95 64)
(0.05 290 192)
(0.05 170 27)
(0.05 99 105)
(0.05 142 99)
(0.05 281 143)
(0.05 309 312)
(0.05 54 186)
(0.05 47 210)
(0.05 1 2)))
(define (add-little-fun coords pict)
(pin-over pict (second coords) (third coords)
(scale (functional-fun middle) (first coords))))
(slide (foldr
add-little-fun
middle
little-funs-placement))(slide (foldr
add-little-fun
middle
little-funs-placement))
(slide (scale (functional-fun middle)
0.05))
(slide (functional-fun (cc-superimpose (text "f" null 30) middle))
(blank 0 10)
(hb-append
(text "check that " null 30)
(text "f(13,14,\"Horse\")" 'modern 30)
(text "returns 15" null 30))
(ghost (text "XXX" null 30)))
(slide (functional-fun/outputs (cc-superimpose (text "f" null 30) middle)
'(0))
(blank 0 10)
(hb-append
(text "check that " null 30)
(text "f(13,14,\"Horse\")" 'modern 30)
(text "returns 15" null 30)
(colorize (text " and sets counter to 34." null 30)
"red"))
(ghost (text "XXX" null 30)))
(slide (functional-fun/outputs (cc-superimpose (text "f" null 30) middle)
(for/list ([i 9]) i))
(blank 0 10)
(hb-append
(text "check that " null 30)
(text "f(13,14,\"Horse\")" 'modern 30)
(text "returns ...void?" null 30)
(colorize (text " and updates " null 30)
"red"))
(colorize (text "internal state." null 30)
"red"))
(slide (item "Possibly many things to test")
(item "Probably no way to observe them")
(item "Much worse with global state"))
(define java-text
'("int a = o.g(13);"
"int b = o.h(9);"
"int c = o.i(4);"
"// if b is wrong, where to look?"))
(slide (item "a basic example")
(subitem (apply vl-append
(for/list([l java-text])
(text l 'modern 30)))))
(define c-text
'("int a = g(13);"
"int b = h(9);"
"int c = i(4);"
"// if b is wrong, where to look?"))
(slide (item "an extreme example")
(subitem (apply vl-append
(for/list([l c-text])
(text l 'modern 30)))))
(slide (item "problem solved!")
(subitem (apply vl-append
(for/list([l c-text])
(text l 'modern 30)))))
(slide (item "Mutation is great! Avoid testing.")
(item "Testing is great! Avoid mutation."))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment