I have a macross that accepts function like an argument, and function that takes it from macross
template <typename Function>
void SomeFunc(const Function func, const string func_str) {
}
#define SomeMacross(func) SomeFunc((func), #func)
void SomeTest() {
}
int main()
{
SomeMacross(SomeTest);
}
So how to do this unit test right? How can I check that this void function working fine?
CodePudding user response:
Assuming SomeTest is the function being tested here, it is probably going to read and modify global variables or do system calls in some way, otherway you'd be testing a function doing nothing since it doesn't have any parameters nor return value.
You're looking for a test tailored specifically to the side effects SomeTest is supposed to have. If SomeTest is supposed to create a file "sometest.dat" upon successfull completion your test has to make sure the file doesn't exist before SomeTest is called, and check that the file exists after SomeTest completes.
You're basically missing a function like this:
void SomeTestTest() {
// prepare execution (i.e. delete previous test file)
SomeTest()
assert( postconditions ); // i.e.
}
You might want to look into Unit Test Frameworks for that, especially if you don't want to work with plain asserts.
CodePudding user response:
Well, every routine can be studied by its syncs and sources; or outputs and inputs respectively. Sources are in this case, the parameters. The function must send info or actuate something, as its sync; or else it would be a trivial routine. You check those results, however they may come out.
NOTES: You might reasonably have such a routine with no syncs, but it will behave identically to a different one with no syncs (unless it is like sleep(); but then it's actuation is to cause execution to wait). An example of such a routine might be a dummy routine you pass a reference to as an argument, which takes the place of another routine which might do something. OTOH, you can have a routine with no sources, but syncs. The routine might be to print a dozen lines of text; but not care take any parameters, or what else is in memory at the time. This kind certainly can be unit tested.
I was trained by a guy who was trained to write FORTRAN for Boeing Computer Support Services at NASA. My first task was to write a program that analyzed syncs and sources for FORTRAN source files, across multiple files. You don't see that much these days; but there are a few such utilities out there. it is very useful in QA and code walkthroughs.
