How to write own code coverage?

Hi!

Take a look at following simple example:

if(...)
{
/*some code*/
if(...)
{
/*some code2*/
}
/*some code*/
}

How can I write a code that would be able to perform some statistic
about what lines of code in my example have acctually been executed?
I have an IDE, Ubicom ip2k, that is hard to integrate with other tools to perform dynamic analysis, so I want to write my own, a most simple one.

What would you recommed me to do? Have somebody made such a code that can perform some kind of dynamic test?

Please provide me with a example or tip on how I can go on?

Comments

  • Why not just the way below?

    [code]
    int count1 = 0;
    int count2 = 0;
    int count3 = 0;

    if (std::rand()%2 == 0)
    {
    ++count1;
    if (std::rand()%2 == 0)
    {
    ++count2;
    }
    }
    else
    {
    ++count3;
    }
    [/code]

    See ya,
    bilderbikkel

  • Sorry for the SHITTY code!!! It is NOT a good solution.

    To count which functions are accessed, you need some way to store counters. If you are working in a class, just use members variables. You could use globals, but also keep a tally in the form of a Singleton: see www.codepedia.com/1/CppSingleton where I copied the example code below from:

    [code]
    #include
    class Singleton
    {
    public:
    static Singleton* instance()
    {
    if (mpInstance==0) mpInstance = new Singleton();
    return mpInstance;
    }
    protected:
    Singleton() {}
    private:
    static Singleton* mpInstance;
    };

    Singleton* Singleton::mpInstance=0;

    Singleton* myGlobal1 = Singleton::instance();
    Singleton* myGlobal2 = Singleton::instance();
    assert(myGlobal1==myGlobal2);
    [/code]

    This sounds like a better solution. Good luck,
    bilderbikkel

  • HI!

    I do not use any classes. I only use C code, and the enviroment I work on, Ubicom ip2k, does not support assertions or gcc gcov, code coverage.

    This is the reason I need to do this in pure C. What should I do then?



    : Sorry for the SHITTY code!!! It is NOT a good solution.
    :
    : To count which functions are accessed, you need some way to store counters. If you are working in a class, just use members variables. You could use globals, but also keep a tally in the form of a Singleton: see www.codepedia.com/1/CppSingleton where I copied the example code below from:
    :
    : [code]
    : #include
    : class Singleton
    : {
    : public:
    : static Singleton* instance()
    : {
    : if (mpInstance==0) mpInstance = new Singleton();
    : return mpInstance;
    : }
    : protected:
    : Singleton() {}
    : private:
    : static Singleton* mpInstance;
    : };
    :
    : Singleton* Singleton::mpInstance=0;
    :
    : Singleton* myGlobal1 = Singleton::instance();
    : Singleton* myGlobal2 = Singleton::instance();
    : assert(myGlobal1==myGlobal2);
    : [/code]
    :
    : This sounds like a better solution. Good luck,
    : bilderbikkel
    :
    :

  • : Sorry for the SHITTY code!!! It is NOT a good solution.
    :
    : To count which functions are accessed, you need some way to store counters. If you are working in a class, just use members variables. You could use globals, but also keep a tally in the form of a Singleton: see www.codepedia.com/1/CppSingleton where I copied the example code below from:
    :
    : [code]
    : #include
    : class Singleton
    : {
    : public:
    : static Singleton* instance()
    : {
    : if (mpInstance==0) mpInstance = new Singleton();
    : return mpInstance;
    : }
    : protected:
    : Singleton() {}
    : private:
    : static Singleton* mpInstance;
    : };
    :
    : Singleton* Singleton::mpInstance=0;
    :
    : Singleton* myGlobal1 = Singleton::instance();
    : Singleton* myGlobal2 = Singleton::instance();
    : assert(myGlobal1==myGlobal2);
    : [/code]
    :
    : This sounds like a better solution. Good luck,
    : bilderbikkel
    :
    :


    Hmm? Singleton pattern is used to make sure only one instance of the class is created. I don't see how it would help to determine how many times a certain code is executed...?

    I also believe there is a bug in that code, you must deallocate the contents pointed at by the static pointer, which causes trouble sence it is private and you can't do it from the desctructor.

    Something like this perhaps?
    (with reservations for my rusty C++)

    [code]
    #include
    #include
    using namespace std;

    class Singleton
    {
    public:
    static Singleton* instance()
    {
    if (mpInstance.get()==NULL)
    {
    auto_ptr tmp (new Singleton);
    mpInstance = tmp;
    }
    return mpInstance.get();
    }
    protected:
    Singleton() {}
    private:
    static auto_ptr mpInstance;
    };

    auto_ptr Singleton::mpInstance(NULL);

    Singleton* myGlobal1 = Singleton::instance();
    Singleton* myGlobal2 = Singleton::instance();
    [/code]

  • [b][red]This message was edited by bilderbikkel at 2006-1-19 3:4:1[/red][/b][hr]
    : Hmm? Singleton pattern is used to make sure only one instance of the class is created. I don't see how it would help to determine how many times a certain code is executed...?
    Just by putting (public) counters in the Singleton.

    : I also believe there is a bug in that code, you must deallocate the contents pointed at by the static pointer, which causes trouble sence it is private and you can't do it from the desctructor.
    :
    : Something like this perhaps?
    : (with reservations for my rusty C++)
    :
    : [code]
    : #include
    : #include
    : using namespace std;
    :
    : class Singleton
    : {
    : public:
    : static Singleton* instance()
    : {
    : if (mpInstance.get()==NULL)
    : {
    : [red]
    //auto_ptr tmp (new Singleton);
    //mpInstance = tmp;
    mpInstance.reset(new Singleton); //A shorter notation
    [/red]
    : }
    : return mpInstance.get();
    : }
    : protected:
    : Singleton() {}
    : private:
    : static auto_ptr mpInstance;
    : };
    :
    : auto_ptr Singleton::mpInstance(NULL);
    :
    : Singleton* myGlobal1 = Singleton::instance();
    : Singleton* myGlobal2 = Singleton::instance();
    : [/code]
    Cool you mention this ([b]edit: I wrote 'Stober' here first, sorry Lundin...[/b]) Lundin! I also have bad dreams on this. So in a short while, I will check this out.

    See ya,
    bilderbikkel



  • : HI!
    :
    : I do not use any classes. I only use C code, and the enviroment I work on, Ubicom ip2k, does not support assertions or gcc gcov, code coverage.
    :
    : This is the reason I need to do this in pure C. What should I do then?
    Sounds like you are stuck with global counters then. I believe there is no alternative. To not lose overview on your globals, you might want to put them in only one header file called 'globals.h', instead of scattering them around in all headers.

    Good luck,
    bilderbikkel

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories