<div dir="ltr">Ouch. I just spent a better part of Friday trying to convince my colleague to provide unit tests in one of his library. I have been under the self-deception/illusion that I tend to test my codes. But I am willing to accept that my test coverage was not enough in this case. I would be most curious in hearing your professional opinion as to whether I have "enough" tests for the given task here (the full code pasted below with the bug fix).<div>
<br></div><div>Also, it's not clear testing would have helped me in my earlier problem since it did not occur to me that passing list of 1 vector to a plot procedure was a NO-NO. (My bad I did not read the doc carefully of courese. I tend to shoot-aim-shoot-aim... But was I that reckless to assume that plot procedure might have put a dot on the screen given one data point instead of throwing an error?) At the end of the day, I am willing to concede that I should have tested rendering the initial world. Then the question is how does one go about testing rendering a scene in a unit test?<div>
<br></div><div><div>#lang racket</div><div><br></div><div>(require (prefix-in p: plot)</div><div> 2htdp/image</div><div> 2htdp/universe</div><div> test-engine/racket-tests)</div><div><br></div><div>
;; World is a list of current price and</div><div>;; a list of all past prices in chronological order</div><div>(define initial-world</div><div> (list 1 (list 1)))</div><div><br></div><div>;; World -> Number</div><div>
(define (world-price world)</div><div> (first world))</div><div><br></div><div>;; World -> [Number]</div><div>(define (world-prices world)</div><div> (second world))</div><div><br></div><div>(check-expect (world-price initial-world) 1)</div>
<div>(check-expect (world-prices initial-world) (list 1))</div><div><br></div><div>;; -> Number in [-0.5, 0.5]</div><div>(define (random-btw-neg-half-and-pos-half)</div><div> (- (random) 1/2))</div><div><br></div><div>
(define (damper)</div><div> 1/100)</div><div><br></div><div>;; -> Number in [-0.5/100, 0.5/100] if damper returns 1/100</div><div>(define (step)</div><div> (* (damper)</div><div> (random-btw-neg-half-and-pos-half)))</div>
<div><br></div><div>;; World -> World</div><div>;; advances world by one more trading session</div><div>(define (random-walk world)</div><div> (let* ((price (first world))</div><div> (new-price (+ price (step))))</div>
<div> (list new-price</div><div> (append (second world)</div><div> (list new-price)))))</div><div><br></div><div>;; (Number n) =>'(1 (1)) -> (n (1 n))</div><div>(check-expect (length (world-prices (random-walk initial-world)))</div>
<div> 2)</div><div><br></div><div>;; World Decimal -> World</div><div>(define (market-shock world shock)</div><div> (let* ((price (first world))</div><div> (new-price (* price shock)))</div><div> (list new-price</div>
<div> (append (second world)</div><div> (list new-price)))))</div><div><br></div><div>(check-expect (world-price (market-shock initial-world 1.5))</div><div> 1.5)</div><div><br></div>
<div>;; world key-event? -> world</div><div>(define (shock world a-key)</div><div> (cond</div><div> [(key=? a-key "up")</div><div> (market-shock world 1.25)]</div><div> [(key=? a-key "down") </div>
<div> (market-shock world 0.75)]</div><div> [else world]))</div><div><br></div><div>;; World -> [#(Int Number)]</div><div>(define (world->list-of-vectors world)</div><div> (for/list ((price (world-prices world))</div>
<div> (i (in-naturals)))</div><div> (vector i price)))</div><div><br></div><div>;; need to have at least 2 data points otherwise you are greeted by</div><div>;; plot: could not determine sensible plot bounds; got x ∈ [0,0], y ∈ [1,1]</div>
<div>(define (plot-render world)</div><div> (if (< (length (world-prices world))</div><div> 2)</div><div> (square 600 'solid 'black)</div><div> (overlay</div><div> (p:plot </div><div> (p:lines</div>
<div> (world->list-of-vectors world)))</div><div> (square 600 'solid 'black))))</div><div><br></div><div>(define (sim init)</div><div> (big-bang init</div><div> (on-tick random-walk 1/2)</div>
<div> (on-key shock)</div><div> (to-draw plot-render)</div><div> ))</div><div><br></div><div>(sim initial-world)</div></div><div><br></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Sun, Aug 11, 2013 at 3:58 PM, Matthias Felleisen <span dir="ltr"><<a href="mailto:matthias@ccs.neu.edu" target="_blank">matthias@ccs.neu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word"><div><br></div><div>Tests your functions. Use Rackunit Luke. </div><div><div class="h5"><br><div><div>On Aug 11, 2013, at 3:40 PM, J G Cho wrote:</div><br><blockquote type="cite"><div dir="ltr">
Thanks for the assurance. It helped me to focus on my world->list-of-vectors It turns out it did not return enough vectors (at least 2, I think) for initial-world.<div><br></div><div><br></div></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Sun, Aug 11, 2013 at 12:34 PM, Matthias Felleisen <span dir="ltr"><<a href="mailto:matthias@ccs.neu.edu" target="_blank">matthias@ccs.neu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word"><div><div><br><div><div>On Aug 10, 2013, at 7:04 PM, J G Cho wrote:</div><br><blockquote type="cite"><span style="border-collapse:separate;font-family:'Lucida Grande';font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;font-size:medium">I get an error (somewhat expectedly) when I try<div>
<br></div><div><div> (big-bang init</div><div> (on-tick random-walk 1)</div><div> ;(on-key door-actions)</div><div> (to-draw plot-render)</div><div> ))</div></div><div><br></div>
<div>where plot-render is defined as</div><div><br></div><div><div>(define (plot-render world)</div><div> (p:plot (p:lines</div><div> (world->list-of-vectors world)</div><div> #:color 6 #:label "Random walk")))</div>
</div><div><br></div><div>with the following at the top</div><div><br></div><div>(require (prefix-in p: plot))<br></div><div><br></div><div>The error msg is</div><div><br></div><div>plot: could not determine sensible plot bounds; got x ∈ [0,0], y ∈ [1,1]<br>
</div><div><br></div><div>Is there way to let plot know of the context? Or pass a context to plot and extract the pixels and hand it over to via some proc in 2htdp/image? Or maybe there is a simple way?</div></span></blockquote>
</div><br><div><br></div></div></div><div>This works like a charm for me: </div><div><br></div><div><div>#lang racket </div><div><br></div><div>(require (prefix-in p: plot) 2htdp/universe)</div><div><div><br></div>
<div>(define (plot-render world)</div><div> (p:plot (p:lines (world->list-of-vectors world)</div><div> #:color 6 #:label "Random walk")))</div><div><br></div></div><div>(define (world->list-of-vectors world)</div>
<div> `(#(1 3) #(2 -2) #(4 3)))</div><div><br></div><div>(define (random-walk world)</div><div> 'more-dummy)</div><div><br></div><div>(big-bang 'dummy ; init </div><div> (on-tick random-walk 1)</div><div>
(to-draw plot-render))</div></div><div><br></div><div>;; --- </div><div><br></div><div>When I had a student implement the first plot version, I intentionally made sure that plot returned something I could use in big-bang. </div>
<div><br></div><div>And btw, this works too: </div><div><br></div><div><div>#lang racket </div><div><br></div><div>(require (prefix-in p: plot) 2htdp/universe 2htdp/image)</div><div><br></div><div>(define (plot-render world)</div>
<div> (overlay </div><div><div> (p:plot (p:lines (world->list-of-vectors world)</div><div> #:color 6 #:label "Random walk"))</div></div><div> (square 800 'solid 'blue)))</div>
<div><br></div><div>(define (world->list-of-vectors world)</div><div> `(#(1 3) #(2 -2) #(4 3)))</div><div><br></div><div>(define (random-walk world)</div><div> 'more-dummy)</div><div><br></div><div>(big-bang 'dummy ; init </div>
<div> (on-tick random-walk 1)</div><div> (to-draw plot-render))</div></div><div><br></div></div></blockquote></div><br></div>
</blockquote></div><br></div></div></div></blockquote></div><br></div>