Rackunit and pict

I've been introducing students to racket using slideshow (based roughly
on the Quick tutorial), and we just started talking about
rackunit. Naturally the question of how to test slideshow programs came
up. I'm not sure of the best way to do it, but one way that works is

#lang slideshow
(require rackunit)
(require racket/serialize)

(define pict1 (hc-append 4 (circle 5) (circle 10) (circle 20)))

(define pict2
  (apply hc-append 4 (map circle '(5 10 20))))

(check-equal? (serialize pict1) (serialize pict2))

Has anyone got a suggestion for a better / more idiomatic / more robust
way of doing this?

1 Like

I think that depends on what you actually want to test.
For me your example looks more like a test that picts are equivalent values and that apply map and serialize seem to be working.
If for example circle would produce a triangle that test would still not fail, so maybe a test that actually compares with a bitmap image would be more reliable?

The problem with things like that is, that there often can be slight differences in how images are rendered, so that then creates the question: how do you actually compare the generated with the reference image and is there some threshold of allowed difference?

(For example font rendering is often different on different platforms)
Sorry, that I only have more questions instead of some nice solution... :sweat_smile:

I think at some point you just have to consider it from a cost/benefit standpoint and think about what kinds of tests could help you find bugs and whether that is likely to help you more, than it costs you to create the tests. While I like testing, I don't think everything is easily testable and always worth the effort.

Another thing to consider are these unit tests or integration tests?
But this seems to "circle" :drum: back around to what is the intent of the test?


I didn't really realize that this was about creating tests for student provided solutions.
Similar to coding practice websites and (I guess class room exercises).
I was more thinking along the lines, how would you detect errors in the slide implementation itself.

Have you considered using the teaching languages and 2htdp/image for your students instead of Racket and Slideshow?

#lang htdp/isl+

(require 2htdp/image)

(define img1 (beside (circle 5 'outline 'red) (circle 10 'outline 'red) (circle 20 'outline 'red)))
(define img2 (apply beside (map (λ (r) (circle r 'outline 'red)) '[5 10 20])))

(check-expect img1 img2)

(Program intentionally altered a bit.)

EmEf via Racket Discussions notifications@racket.discoursemail.com
writes:

Have you considered using the teaching languages and 2htdp/image for your students instead of Racket and Slideshow?

#lang htdp/isl+

(require 2htdp/image)

(define img1 (beside (circle 5 'outline 'red) (circle 10 'outline 'red) (circle 20 'outline 'red)))
(define img2 (apply beside (map (λ (r) (circle r 'outline 'red)) '[5 10 20])))

(check-expect img1 img2)

(Program intentionally altered a bit.)

At least for this year switching is not practical. In general I don't
think teaching languages are a good fit for this particular course
(survey of programming languaes), but that's a topic for a different
thread.

d

#lang racket

(require 2htdp/image)
(require test-engine/racket-tests)

(define img1 (beside (circle 5 'outline 'red) (circle 10 'outline 'red) (circle 20 'outline 'red)))
(define img2 (apply beside (map (λ (r) (circle r 'outline 'red)) '[5 10 20])))

(check-expect img1 img2)
(check-expect img1 3)

(test)