how to purposely fail "fopen" for debuggin
I need to purposely fail the "fopen"-ing of a file to test my code. How do I do that. my code so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void MeshTrian::init (std::string filename)
{
if (!sofa::helper::system::DataRepository.findFile(filename))
{
msg_error() << "File '" << filename << "' not found." ;
return;
}
FILE *f = fopen(filename.c_str(), "r");
bool status;
msg_error_when(!(status=f))<<sofa::helper::message::UnableToOpenFile(filename.c_str());
if (status)
{
readTrian (f);
fclose(f);
}
}
| |
Can you post the code for msg_error_when?
In test mode, you could have TEST_MODE defined. Either in the code, or as an argument passed to the compiler.
Then something like this in the right place:
1 2 3
|
#ifdef TEST_MODE
#define fopen(x, y) nullptr;
#endif
| |
which would turn your fopen call into a straight nullptr value.
I've not tested this and I rarely use this kind of preprocessor macro, so it might be bad syntax; the idea should be solid.
Even something as awkward as
1 2 3 4 5
|
#ifdef TEST_MODE_FAIL_FILE_OPEN
FILE *f = nullptr;
#else
FILE *f = fopen(filename.c_str(), "r");
#endif
| |
at line 8 would do it, although it's possible to end up getting silly with excessive preprocessor magic.
Last edited on
Another option is to set filename to some non-existing file:
1 2 3 4 5
|
#ifdef TEST_MODE
filename = "I don't exist";
#else
filename = "Existing filename";
#endif
| |
Change the permissions on the file so it can't be read by the program.
Topic archived. No new replies allowed.