I have a .c file that basically manages its own little database of people. There are add, remove, edit and search functions among some others. It has a static array which it uses to store the data.What is a good way to go about unit testing a file like this? Because the array is static I can't put in my own data.Should I employ some trickery and make a function that passes out a pointer to the array that gets compiled out in the production version? Should I just test the functions as is and make the data they way I want it via a series of adds and removes? What is the best way to test a file like this?
Testing a file that relies heavily on a static array
c;testing;unit testing
The best thing you can do is to refactor the code to make it more testable. Not only does this yield immediate benefits in testablity, it has long-term benefits like improved code readability. If for some reason you can't refactor this code, you add some test-specific code that improves testablity. Namely, something that lets you set the content of the array. You can use #define in C to filter this code out of non-test mode execution.