test first

D

deanhiller

In C++ or Java, or whatever OO, I am used to using the Dependency
Injection Pattern(or I believe formerly called Inversion of Control
Pattern) to accomplish test first in a large system. ie. I would have
5 architecture components, and I would simply create a mock object of
one of those systems like
Station s = MockObjectFactory.createMock(Station.class);

I would then pass the station into the system I was going to test and I
could poke and prod the system from both ends(bottom and top where
bottom is the Station api and top is the "system under test"s api).
How can I do this in C?
thanks,
dean
 
M

Mike Wahler

deanhiller said:
In C++ or Java, or whatever OO, I am used to using the Dependency
Injection Pattern(or I believe formerly called Inversion of Control
Pattern) to accomplish test first in a large system. ie. I would have
5 architecture components, and I would simply create a mock object of
one of those systems like
Station s = MockObjectFactory.createMock(Station.class);

I would then pass the station into the system I was going to test and I
could poke and prod the system from both ends(bottom and top where
bottom is the Station api and top is the "system under test"s api).
How can I do this in C?

As I understand it, the concept of 'test first' is language
neutral, not dependent upon a particular syntax. It means:
"Envision that certain needed or desired functions already exist.
Write code to use those functions. Of course you'll get errors
from the translator. Incrementally add code until no errors
remain. Then check that the results are the intended ones.
If not, modify the code until they are."

First we write:

#include <stdio.h>

int main()
{
int x = 25;
int y = 17;
int r = sum(x, y); /* compute sum of two values */
printf("%d + %d == %d\n", x, y, r); /* display values and their sum */
return 0;
}

We compile ... get error: no 'sum()' function.
So we write:

#include <stdio.h>

int sum(int a, int b)
{
return a * b;
}

int main()
{
int x = 25;
int y = 17;
int r = sum(x, y); /* compute sum of two values */
printf("%d + %d == %d\n", x, y, r); /* display values and their sum */
return 0;
}
No compiler errors. Great. Now we run it, and get output:

25 + 17 == 425

Oops.

So we continue to edit:

#include <stdio.h>

int sum(int a, int b)
{
return a + b;
}

int main()
{
int x = 25;
int y = 17;
int r = sum(x, y); /* compute sum of two values */
printf("%d + %d == %d\n", x, y, r); /* display values and their sum */
return 0;
}

Compile, run, get output:

25 + 17 == 42

Voila! The Answer.

This is of course very simplified, real life testing would
be much more thorough. But this is the essential idea.

As well as helping to manage bugs, this is also a good way
to determine what functions are needed. IOW when writing
the 'top level' (test) code, when needed, simply imagine
and make up (meaningful) names for functions that implement it.

'Stylized' example:

int main()
{
initialize();
compute();
cleanup();
return 0;
}

I have not (yet) developed a habit of always using 'test first',
but when I have used it, it has indeed helped with both bug
control and organizing my code.

-Mike
 
D

deanhiller

uhhhhmm....sorry, I should have been more clear. I meant automated
unit tests where you need to mock part of the system. ie. I want to
mock out the interface to the dsp chip so I the automated builds can
run all the unit tests without even being on the embedded hardware and
can run on any linux box with gcc.

thanks,
dean
 
M

Mike Wahler

deanhiller said:
uhhhhmm....sorry, I should have been more clear. I meant automated
unit tests where you need to mock part of the system. ie. I want to
mock out the interface to the dsp chip so I the automated builds can
run all the unit tests without even being on the embedded hardware and
can run on any linux box with gcc.

Um, sorry, I probably should not have replied at
all, since your query isn't about the C langauge,
the only topic here. I showed how one can implement
'test first' in C, which might make my reply marginally
topical, but discussions of automated testing, etc. do not
belong here.

Try a newsgroup about software engineering.
Visit www.groups.google.com to find out which
groups discuss what.

Purpose of comp.lang.c:
http://www.ungerhu.com/jxh/clc.welcome.txt

-Mike
 
D

deanhiller

My question is to how to do automated testing in C. Above I meant "Can
I mock i C". It sounds like on a very large system, the only way is to
test all the way through which your example does. So there is no way
to mock out a subsystem or interface to a DSP or something like that?
I am just trying to find out if we have to switch to C++ to do this
type of work.
thanks,
dean
 
M

Mike Wahler

deanhiller said:
My question is to how to do automated testing in C. Above I meant "Can
I mock i C". It sounds like on a very large system, the only way is to
test all the way through which your example does. So there is no way
to mock out a subsystem or interface to a DSP or something like that?

There might be, I don't know. But the point of my last
reply was that this newsgroup is not the place to ask.
Did you read the 'welcome message' whose link I cited?
I am just trying to find out if we have to switch to C++ to do this
type of work.

www.google.com
www.groups.google.com

-Mike
 
M

Michael Wojcik

There might be, I don't know. But the point of my last
reply was that this newsgroup is not the place to ask.
Did you read the 'welcome message' whose link I cited?

I'm not convinced that this *is* an off-topic question. It appears
to be something along the lines of "what facilities, if any, does
the C language provide to generate a 'mock' implementation of an
interface"? That's a question about what the C language contains,
and as such is topical.

Unfortunately for the OP, the answer is "none in particular". It
is certainly possible to create a dummy implementation in C, but
the language makes no special provisions for it.

The relevance of the comparison to C++ is not clear to me, but I
assume the OP is referring, eliptically, to some feature of C++
which is useful in this task. Nothing immediately springs to my
mind, but unless the same, or an equivalent, feature exists in C,
that *would* be off-topic here.

--
Michael Wojcik (e-mail address removed)

There are some, who do not understand true enjoyment, will tell you that
rules spoil convivial meetings, and that a merry company becomes a dull
committee as soon as it is called a club. Do not believe them: the
precedents are all against them. -- Arthur Ransome
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top