Rewind!
Colin Putney
cputney at wiresong.ca
Tue Mar 9 17:24:18 UTC 2004
On Mar 8, 2004, at 10:45 AM, Markus Gaelli wrote:
> One little thing which could have great impact on the quality of the
> code, would
> be to use the little Object>>assert: mechanism, which is there since
> long time,
> waiting to be used in all kinds of pre- and postconditions and
> invariants.
About 6 months ago I posted a message about why I think unit tests are
better than assertions. I won't repeat it, but just provide the link:
http://lists.squeakfoundation.org/pipermail/squeak-dev/2003-October/
067876.html
> An example where one might want to move an already abstract assertion
> from the test in the
> code could be
This example is a bit tricky because randomness is generally difficult
to test. The whole point of tests is to be repeatable, which is hard if
you've got randomness involved.
> Random >> next
> "Answer a random Float in the interval [0 to 1)."
> ^ (seed _ self nextValue) / m
>
> One could replace the comment with some active code from
>
> RandomTest >> testNext
> 10000 timesRepeat: [
> | next |
> next := gen next.
> self assert: (next >= 0).
> self assert: (next < 1)].
>
> and end up with:
I find the above version much easier to understand than the version
below. By removing the assertions from the test, we make it unclear
what, if anything, it tests. Is it testing the range of the returned
values? That the period of the PRNG is greater than 10000? That 10000
random numbers can be generated within a given time? The only way to
understand the test is to step through the code in the debugger and
take note of the assertions that get executed.
I also find the inline assertions confusing, although admittedly less
so. They imply that #nextValue might return an invalid value at
runtime. With the assertions in the test, it's clear that the assertion
is pathological and it may well be the case that #next will never
return an invalid number.
> Random >> next
> |aResult|
> aResult:= (seed _ self nextValue) / m.
> self assert: [aResult >= 0].
> self assert: [aResult < 1].
> ^aResult
>
> and
>
> RandomTest >> testNext
> 10000 timesRepeat: [gen next]
>
> Cheers,
>
> Markus
>
More information about the Squeak-dev
mailing list
|