For Programmers: Free Programming Magazines  


Home > Archive > Extreme Programming > February 2006 > Test driven develop question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Test driven develop question
linq936@hotmail.com

2006-02-09, 7:00 pm

Hi,
I am adapting the Test driven development, TDD, into my daily work. I
know I need to systematically study the methodology, but here I have a
quetion.

I am working on a code which read some data in, form up a link list
and then do some complex calculation on the list. The code is C++ and
it runs stable for quite a while. Note that when I first wrote the
code, I had no idea what TDD stand for.

Now I need to add some new functionality, and to do that I must erase
some data from the link list and put it on the head of the list. And I
want to apply TDD.

But I am wondering how to apply it.

The data is on the fly. What I can imagine is I have some sample data
pre-cooked, and then in the code after I do the erase-and-insert, I
dump out the contents of the link list, then in the test code, I
compare the dump out with the pre-cooked data.

Is this what I can do?

My concern is how I can make sure that the order of the link list
does not change later on? Then I have to add many list dump out
function along the road and do the test all the time?

In my mind, TDD is a great way to prevent re-gression. Suppose time
later I do some code change on some other place and break the list
order, what is the efficient way to catch it?

Thanks a lot.

Phlip

2006-02-09, 9:56 pm

linq936 wrote:

> I am adapting the Test driven development, TDD, into my daily work. I
> know I need to systematically study the methodology, but here I have a
> quetion.
>
> I am working on a code which read some data in, form up a link list
> and then do some complex calculation on the list. The code is C++


Use std::vector<>. After replacing your list with that, add the _next_
feature via TDD.

--
Phlip
[url]http://www.greencheese.org/ZLand[/url] <-- NOT a blog!!!


Randy A. Ynchausti

2006-02-10, 3:57 am

linq936,

> My concern is how I can make sure that the order of the link list
> does not change later on? Then I have to add many list dump out
> function along the road and do the test all the time?


Implement a method that checks to see if some object is in the linked list.
You might even go so far as to check if it is in the right part of the
linked list. However, the exact position in the linked list may not be that
important to you. Call that method in your unit-tests to ensure that
everything is working correctly.

Note that it is ok to put code in the production system that makes it easier
to test these things.

Regards,

Randy


Laurent Bossavit

2006-02-10, 3:57 am

Linq,

> But I am wondering how to apply it.


You might want to practice TDD first on code that has little
entanglement with pre-TDD code, or even entirely new programs. Once you
get good at it, it's easier to adapt it to more difficult situations.

> The data is on the fly. What I can imagine is I have some sample data
> pre-cooked, and then in the code after I do the erase-and-insert, I
> dump out the contents of the link list, then in the test code, I
> compare the dump out with the pre-cooked data.
> Is this what I can do?


Yes. For this to work, you may have to do some refactoring. Suppose your
code is to look like this after you write the new feature:

int someFunction(int blah, someStruct* bar) {
// init linked list
list* aList = makeList();
// read data from bar
...code here...
// now we have our linked list, reorder it
if (reorder) {
...the code which moves things around...
}
...more code...
// done
return result;
}

In the context of our discussion, this code has no tests. What you want
to do is not necessarily to write a test for "someFunction" - that could
get hairy. You'd have to instrument that function with facilities to
store the list solely for the purpose of testing.

Rather, write a test that looks like the following. The function
"moveLastNtoHead" doesn't exist yet. It is what we intend to write in
the above, at the place marked "code which moves things around".

void testReorder() {
aList = makeList("a", "b", "c");
expected = makeList("c", "a", "b");
TS_ASSERT_EQUALS( moveLastNtoHead(aList, 1), expected);
}

Since the function doesn't exist, the code won't compile. Make it
compile by creating a "stub" version of moveLastNtoHead that does...
simply nothing. Now the test will fail.

Make it pass by writing the code for "moveLastNtoHead". Then refactor
the original code to this:

int someFunction(int blah, someStruct* bar) {
// init linked list
list* aList = makeList();
// read data from bar
...code here...
// now we have our linked list, reorder it
if (reorder) {
moveLastNtoHead(aList, 4);
}
...more code...
// done
return result;
}

> My concern is how I can make sure that the order of the link list
> does not change later on? Then I have to add many list dump out
> function along the road and do the test all the time?


I don't quite understand what you mean here. Perhaps this will help: if
you follow the steps I outlined above, then obviously you'll still have
many things that are not tested.

It can be useful to add "after the fact" tests. This is not TDD, but it
can make it easier to use TDD, by allowing you to refactor code without
worrying about breaking much. For instance, you could write a test to
check that the linked list is generated with the correct order in the
first place.

Laurent
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com