开发者

CPPUnit access defines in c files

开发者 https://www.devze.com 2023-02-15 13:42 出处:网络
I am doing some unit test on c code using CPPUnit. In the code to test there are some defines in c files and not in the h files. So they are not accessable from other places in the code. Many of the

I am doing some unit test on c code using CPPUnit.

In the code to test there are some defines in c files and not in the h files. So they are not accessable from other places in the code. Many of the defines are used to set the size of arrays.

When I do th开发者_Python百科e unit tests I would like to make them so dynamical as possible meaning that they will still work when the defines are changed before the final release. I've made some loops and stuff to go through the functions using the arrays, so that I test as many times as there are entries in an array etc. But until now I use a copy of the define in my unit test code. So if the defines are changed in the code to test, then they are not changed in the unit test code.

So my main question is, if there is some trick to use just for the unit testing purpose to somehow making the defines global or anything without changing the original source code just to fit the use of unit testing. Maybe a compiler setting? (I am using visual studio 2008).


No, you cannot make a #define in a .c file "global" so that it applies to other C files, any more than you can make a static function global without changing the source. The way to do this is to change the source to put those #defines where they belong, in header files that define interfaces.

=== edit ===

credit to Radek S for the idea of #includeing the .c file. This has limited applicability, but should work for unit tests of functions in that one file, provided that the the original file is not compiled when creating the test executable.

0

精彩评论

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

关注公众号