test first

Discussion in 'C Programming' started by deanhiller, Jan 9, 2006.

  1. deanhiller

    deanhiller Guest

    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
    deanhiller, Jan 9, 2006
    #1
    1. Advertising

  2. deanhiller

    Mike Wahler Guest

    "deanhiller" <> wrote in message
    news:...
    > 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
    Mike Wahler, Jan 10, 2006
    #2
    1. Advertising

  3. deanhiller

    deanhiller Guest

    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
    deanhiller, Jan 10, 2006
    #3
  4. deanhiller

    Mike Wahler Guest

    Re: [OT] test first

    "deanhiller" <> wrote in message
    news:...
    > 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
    Mike Wahler, Jan 10, 2006
    #4
  5. deanhiller

    deanhiller Guest

    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
    deanhiller, Jan 10, 2006
    #5
  6. deanhiller

    Mike Wahler Guest

    Re: [still OT] test first

    "deanhiller" <> wrote in message
    news:...
    > 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
    Mike Wahler, Jan 10, 2006
    #6
  7. Re: [still OT] test first

    In article <zhRwf.1178$>, "Mike Wahler" <> writes:
    > "deanhiller" <> wrote in message
    > news:...
    > > 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.


    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

    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
    Michael Wojcik, Jan 12, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. matt
    Replies:
    9
    Views:
    405
    Andrew Thompson
    Oct 27, 2004
  2. Guest
    Replies:
    7
    Views:
    665
    Walter Wang [MSFT]
    Jun 28, 2006
  3. arotem
    Replies:
    4
    Views:
    9,005
    arotem
    Oct 17, 2005
  4. tconkling

    First statement always evaluated first?

    tconkling, Jul 9, 2005, in forum: C Programming
    Replies:
    3
    Views:
    316
    Keith Thompson
    Jul 9, 2005
  5. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    683
    Skybuck Flying
    Oct 4, 2009
Loading...

Share This Page