ThrowTheSwitch / Unity

Simple Unit Testing for C
ThrowTheSwitch.org
MIT License
4.02k stars 969 forks source link

static variable inside of target function is reset everytime when it is invoked #702

Closed joe-ju closed 10 months ago

joe-ju commented 11 months ago

Hi, I implemented a test function in unity like: / tests / void test_helloworld(void) { target_fun(); target_fun(); }

And the target function implementation in my source is the following: void target_func(void) { static in i = 0; if (++i >=2) printf("hello world\n"); }

The print never happened and after some investigation, when target_fun() is invoked for the second time the "i" was reset to 0 again, but I also tested target_func(void) in the same way without unity and it worked. Do you have any idea about this test in unity? Thanks!

mvandervoord commented 10 months ago

I'm confident you've misdiagnosed what is happening here. Likely you've just missed a detail somewhere. Is it actually this example or is this a simplified version of the problem? If the latter, some things to double check:

Unity does a lot of tricky stuff in the background, but it can't change the rules of C for you. If you've marked a variable as static in your release function, it's still static and Unity can't touch it.

joe-ju commented 10 months ago

Hi, thanks. My bad. I added the compilation macro "-Dstatic=" in my makefile, which means all static would be replaced with null.

Thanks a lot again!!

mvandervoord commented 10 months ago

:) Awesome. That makes sense!