Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write test for C++ templates?

Suppose I am writing a template library consisting of a function template

template<T> void f(T);

with the requirement that it works with a predefined set of classes A, B, C, and D, e.g., the following must compile:

template<> void f(A);
template<> void f(B);
template<> void f(C);
template<> void f(D);

Which test framework can I use to write test cases that captures this requirement at runtime instead of failing at compilation of the test code? In another word, I would like the framework to instantiate the templates at runtime and produce a nicely formatted error report if a subset of them fails.

I know I can forego test frameworks altogether and simply write a simple cc file containing the 4 lines above. But I was hoping I could incorporate this requirement into regular, standard test cases for generation of test status reports. For example,

test f works with A: passed.
test f works with B: passed.
test f works with C: failed!  Cannot cast type C!
test f works with D: passed.

3 of 4 tests passed.
1 of 4 tests failed.
like image 440
kirakun Avatar asked Feb 03 '11 03:02

kirakun


People also ask

How do you write a test case in Objective C?

In Objective-C also, we can use the XCTest framework to validate the test cases. We can use a combination of the OCMock library & XCTest. Using the OCMock library saves our code for mocking the class. Also, we can stub only those methods which we want to invoke which saves time too.

What is test template?

An ATF test template is a reusable set of test steps that are commonly used together. Test Templates can include any combination of test steps. A template's test steps may require additional configuration once added to a test. ATF includes a Default New Form Test Template as an example.


2 Answers

Write a test case that spawns the compiler... that's how e.g. autoconf tests for existence of features.

like image 64
Ben Voigt Avatar answered Sep 30 '22 11:09

Ben Voigt


I don't understand why failing at runtime is preferable to failing at compile time. The earlier you fail in the unit testing process the better. It is preferable to have your unit tests not compile than fail. Its even easier to fix, In fact it probably won't even be committed to source control. Your unit test should just include those four lines and assert true at the end. Note this isn't the way I would go about doing it myself.

like image 21
Steve Avatar answered Sep 30 '22 11:09

Steve