Re: A critique of test-first...
From: Laurent Bossavit (laurent_at_dontspambossavit.com)
Date: 11/15/04
- Previous message: Laurent Bossavit: "Re: A critique of test-first..."
- In reply to: CTips: "Re: A critique of test-first..."
- Next in thread: CTips: "Re: A critique of test-first..."
- Reply: CTips: "Re: A critique of test-first..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 15 Nov 2004 23:23:53 +0100
> Even if we break it down to the minimal amount of analysis per transform kind,
> it will still amount to 1kloc+. (BTW: this is based on recent experience,
> not hypothetical).
I suspect we differ on what we call a "unit" test, or a "real" test.
Here is a thought experiment. Suppose you suspect that, somewhere in
these 1000 lines of optimizer code for a given transformation, there is
one nasty bug.
I would suppose that you will instrument the code for debugging - put a
printf in there, or activate one that you've already put there for that
purpose.
The next step might be running the compiler on a test source file that
is carefully designed to exercise, among other paths, one that causes
the printf statement to be activated.
Now we have a string that has been output to stdout. If the debug
instrumentation is at all helpful, we have a precise idea what that
string should be. If the string actually output differs from that, we
have a "smoking gun" that the bug is indeed, in the main, what and where
we suspected.
We could make this more efficient by writing a small function that
examines stdout for us, and compares the particular line in the output
that we're interested in with the value we expect. Couldn't we ?
Maybe we don't need to exercise the whole compiler, either. The analysis
is, I suppose, running over part of the partially compiled code - mostly
assembly with unresolved jump locations, or somesuch. Maybe we can store
the binary representation of that instead of the source file. And do we
really need more than a dozen or a few dozen bytes of it in order to
expose the bug ? I may be wrong (I've only written one compiler in my
life, with fairly simple peephole optimizations), but I'll assume that's
about right.
Here's the one part I'm not 100% sure of. Is the 1000 lines of analysis
code one big function, or is it broken down further into functions ? In
the latter case, presumably we can massage the input data further still,
until we know exactly what *would* be passed into the function that has
the debug printf, if we were encountering the bug in real use. Instead
of a printf, perhaps we can devise other ways to get the output back -
say in a return value, or a parameter passed by reference.
So we can write a test function that:
- constructs the input data we need
- passes it into the relevant function of the analysis module
- receives an encoded "status" string as one result of the function
- compares the string it received with an "expected" string
Well, surprise - that test function is an XP-style unit test. Ex
hypothesi (unless I've made a mistake in the above) it tests something
that is considerably less than 1000 lines of code. (I haven't put a
number on that size, but it's "whatever the function length you consider
reasonable"). Is it a "real" test ? Well, again ex hypothesi, I wrote it
because of an actual bug, so presumably it tests something relevant.
Instead of writing these tests when a bug surfaces, when arguably it is
too late, XP suggests that they should be written before the code. This
helps developers focus on testability in their design (they can't get
away with a 1000-line function), avoids confirmation bias, and helps
with debugging in much the same way that instrumentation does, when a
rare bug does surface. Though actually the beginning of this paragraph
should read "In addition to", not "Instead of" - in XP it's considered a
best practice to write a new test for any bug found.
Laurent
- Previous message: Laurent Bossavit: "Re: A critique of test-first..."
- In reply to: CTips: "Re: A critique of test-first..."
- Next in thread: CTips: "Re: A critique of test-first..."
- Reply: CTips: "Re: A critique of test-first..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|