Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit-testing for compile errors with C++

I've written a Matrix class, and checked that it disallowed multiplication of matrices of incompatible sizes using C++ exceptions. I wrote unit tests to check that behavior, and they are expecting exception to be thrown.

Now I'm changing the Matrix size from being a runtime variable to a template parameter. If I'll be able to do that correctly, the code that will try to multiply matrices of wrong sizes won't even compile.

It would seem that now these unit tests are redundant. However, since I don't know how I'll change my code in the future, and what will break, I still want to implement tests for this. If before I expected my tests to throw specific exceptions at specific places, now I want my test to throw specific compile errors at specific places.

What's the best way to do that? I would imagine some sort of mechanism based on Makefile and shell scripting that would wait for a specific error codes — or should I try something else? Is this idea a common practice or complete madness?

Edit: Of course, "Unit tests" isn't a fitting name for this kind of mechanism, I know that, but for now, I just can't think of a better way. There are already three comment authors who spent their valuable time and effort explaining to me what unit tests are and what they aren't. Unfortunately, while being technically true, this doesn't help to solve an actual problem at hand here.

Edit 2: This is BDD scenario that I want to test for:

  • Given two matrices of sizes 2x2 and 3x3
  • When the user tries to multiply them
  • Then he gets an error

Before, that error was runtime error, and it testing for it was trivial. But now I it became a compile time error, and I don't see how I can keep automatically testing this scenario and confirming, on every commit (I have unit tests in my git hooks) that it still gives me an error.

like image 331
Max Yankov Avatar asked Feb 23 '15 21:02

Max Yankov


People also ask

Does unit testing test compile errors?

Unit testing exercises all the parts of the code. It is useful to make sure you really instantiated every template you wrote, the compiler accepts usage as intended, and that the functions do what they are supposed to. However, sometimes you want to make sure something does not compile.

Does C have unit testing?

A Unit Testing Framework for CCUnit is a lightweight system for writing, administering, and running unit tests in C. It provides C programmers a basic testing functionality with a flexible variety of user interfaces.

What can be used for unit testing in C?

The most scalable way to write unit tests in C is using a unit testing framework, such as: CppUTest. Unity. Google Test.

What is a compile error in C?

Syntax errors: Errors that occur when you violate the rules of writing C/C++ syntax are known as syntax errors. This compiler error indicates something that must be fixed before the code can be compiled. All these errors are detected by compiler and thus are known as compile-time errors.


1 Answers

It's harmless to keep your "unit" test, even if the new template style of code makes it "impossible" to mismatch matrix's at run time. You may still have a bug that gets through to run time and, as you say, the code could change again.

If you're using gcc, gcc uses DejaGnu to test itself. That should be robust enough to detect gcc compilation errors.

like image 103
Paul Evans Avatar answered Oct 05 '22 23:10

Paul Evans