0

I am trying to write a C++ unit test class. What I want to do is to run a given set of input test files on another class (simulator class as in code below).

Assume, runTest function as shown below calls my required class on each test file.

void runTest(vector<string> testFiles)
{
        for(auto file : testFiles)
        {
                cout <<" Running test file:"<<file<<std::endl;
                simulator simulatorObj(file);
                simulatorObj.run();
        }
}

Now there are some input test files where the simulator class can generate a Seg fault (due to a bug in simulator class). Now I want to catch this seg-fault in my test class. Is there any way in which I can convert seg-fault signal to a C++ exception or something similar.

I tried using try catch block with generic exception but it didn't work. Also I am not able to think of any solution.

Please help.

Mayank Jain
  • 2,504
  • 9
  • 33
  • 52
  • *I tried using try catch block with generic exception but it didn't work* -- Not surprising, since seg faults are not C++ exceptions. – PaulMcKenzie Nov 11 '16 at 05:30
  • Yes. But is there any way I can detect them in C++ code? May be a callback function in case Seg fault is generated. – Mayank Jain Nov 11 '16 at 05:31
  • Unfortunately there's no C++ standard for handling segfaults. What platform are you using? – MrEricSir Nov 11 '16 at 05:32
  • Unix. GCC compiler. – Mayank Jain Nov 11 '16 at 05:33
  • You need to see if your compiler has any sort of extension(s) to support reporting of segmentation faults. Otherwise, C++ has no concept of catching exceptions that are not explicitly thrown using C++ syntax, i.e. the `throw` keyword. – PaulMcKenzie Nov 11 '16 at 05:33
  • @MayankJain Also, not all bugs produce seg faults. So I don't know what real benefit you get out of catching a bug that (by chance) produces a segmentation fault, other than indicating you need to fix your program. – PaulMcKenzie Nov 11 '16 at 05:38
  • @PaulMcKenzie - I can have 1000 test files and let's say first test file fails due to some error in code. Even in this case I have to show to user saying test 1 failed and continue to next test file. – Mayank Jain Nov 11 '16 at 05:41
  • 4
    @MayankJain -- How do you know that the corruption caused by the failed test won't corrupt the further tests? Maybe you should write a batch script to run the tests, and not code the whole test suite in one program? Your program is compromised, IMO if you get a seg fault. – PaulMcKenzie Nov 11 '16 at 05:43
  • @PaulMcKenzie - I do agree with you and think what you are suggesting is better option. Thanks for suggestion. Although, I will keep the question open If any one has any solution for this. – Mayank Jain Nov 11 '16 at 05:47

0 Answers0