开发者

Automatic code testing in c upon execution

开发者 https://www.devze.com 2023-01-31 17:43 出处:网络
each source file has a function: unsigned char test(){ //return true if tests succceed false otherwise }

each source file has a function:

unsigned char test(){
    //return true if tests succceed false otherwise
}

Everytime the sourcefile gets recompiled I want the test function of that file to be run on execution of the executable and if the tests return false to halt. However, I want only code whose test code hasnt been run since build.

I was thinking that I could use the makefile system to help with this problem since makefiles will only recompile changed code which is exactly what i want. Therefore, I was thinking of adding something to the command line arguments. I saw an example in which they used echo and date to add a time last modified. The code would write the time last modified to a save file on a successful execution of the test code开发者_运维技巧 with which it will compare to the next time.

Is there a better way? One that doesnt rely on make? At the least is there a way without relying on spefic unix commands? Lastly, even if that isnt possible, what would the command line to do the addition to the file look like.


Yes, you could make a file.t target dependant on the file.c.
file.t would get generated based on the successful completion of the test.

It'll be easier if you call your test function "main" and wrap it in #ifdef's like this:

#ifdef TEST
int main() {
    if (test_something() == 1)
        return 0;
    else
        return 1;
}
#endif

Of course, you can do other trickier things if you want.

An example Makefile (using GNU Make syntax)

all: foo.t

%.t: %.c
    gcc -DTEST $< -o $@.tmp
    if ./$@.tmp ; then echo "1" > $@; rm $@.tmp ; fi

However! You'll probably be better off adopting something like CUtest (http://cutest.sourceforge.net/).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号