Unit-testing for compiling errors with C ++

I wrote the Matrix class and verified that it forbade multiplication of matrices of incompatible sizes using C ++ exceptions. I wrote unit tests to test this behavior, and they expect an exception.

Now I resize the matrix as a run-time variable in the template parameter. If I can do it right, code that tries to multiply matrices of the wrong size will not even compile.

It would seem that now these unit tests are redundant. However, since I don’t know how I will change my code in the future, and what will break, I still want to run tests for this. If I had previously expected my tests to throw specific exceptions in specific places, now I want my test to throw specific compilation errors in specific places.

What is the best way to do this? I would suggest some kind of mechanism based on Makefile and shell scripts that will wait for specific error codes - or should I try something else? Is this idea a common practice or complete insanity?

Change Of course, “Unit tests” are not suitable for such a mechanism, I know this, but so far I just can’t think of a better way. There are already three commentators who have spent their valuable time and effort explaining to me what unit tests are and which are not. Unfortunately, although this is technically true, it does not help solve the real problem.

Change 2 . This is a BDD script that I want to test for:

  • Given two matrices of sizes 2x2 and 3x3
  • When a user tries to propagate them
  • Then it receives an error message

Previously, this error was a run-time error, and testing for it was trivial. But now I have become a compile-time error, and I don’t see how I can automatically check this script and confirm, with every commit (I have unit tests in my git hooks) that still give me an error.

+6
source share
2 answers

It is harmless to maintain your “single” test, even if the new template code style makes matrix mismatch impossible at run time. You may have an error that goes through runtime, and as you say, the code may change again.

If you use gcc , gcc uses DejaGnu to test itself. This should be robust enough to detect gcc compilation errors.

+1
source

I don't have enough Rep to comment on @Paul Evans answer yet, so please tell me if I should just not respond.

@Paul Evans recommends DejaGnu, and I support this choice. However, if you want to create more comprehensible compilation errors (template compilation errors tend to get messy), you can use static_asserts .

They are standard since C ++ 11 (although you should check if your compiler can use them) and are similar to traditional statements, only they are checked at compile time and can display their own error message.

Here's the documentation if you want to check them out: http://en.cppreference.com/w/cpp/language/static_assert

0
source

Source: https://habr.com/ru/post/982912/


All Articles