如何编写 C++ 测试模板?

发布于 2024-10-15 11:13:11 字数 654 浏览 3 评论 0原文

假设我正在编写一个由函数模板组成的模板库,

template<T> void f(T);

要求它与一组预定义的类 A、B、C 和 D 配合使用,例如,必须编译以下内容:

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

哪个测试框架我可以用来编写在运行时捕获此需求的测试用例,而不是在编译测试代码时失败吗?换句话说,我希望框架在运行时实例化模板,并在其中一部分失败时生成格式良好的错误报告。

我知道我可以完全放弃测试框架,只需编写一个包含上述 4 行的简单 cc 文件。但我希望能够将此要求合并到常规的标准测试用例中,以生成测试状态报告。例如,

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只怪假的太真实 2024-10-22 11:13:11

编写一个生成编译器的测试用例...这就是 autoconf 测试功能是否存在的方式。

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

妄司 2024-10-22 11:13:11

我不明白为什么运行时失败比编译时失败更好。单元测试过程越早失败越好。单元测试不编译比失败要好。它更容易修复,事实上它甚至可能不会被提交到源代码控制。您的单元测试应该只包含这四行并在最后断言 true。请注意,这不是我自己做这件事的方式。

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.

跨年 2024-10-22 11:13:11

C++ 模板是编译时功能。在许多情况下,它们会根据设计在编译时失败。你根本无法解决这个不做一些真正疯狂的事情

但是,您还想知道您的模板专业化是否正确,因为专业化会覆盖您从模板中获得的行为。所以要考专业。但要意识到你永远无法绕过模板的编译时间方面。

C++ templates are a compile time feature. In many cases they will fail at compile time, by design. You simply can't get around this without doing something really crazy.

However, you're also going to want to know that your template specializations are correct, because the specializations override the behavior you would otherwise get from the template. So test the specializations. But realize you will never get around the compile time aspects of templates.

感悟人生的甜 2024-10-22 11:13:11

根据您在这里尝试测试的内容,检查是否可以编译是您可以执行的唯一明智的测试。

测试不应该是为了测试,而是为了保证功能的正确性。如果您想对您的类进行适当的测试,您应该编写测试来验证模板的功能以及可以编译模板的所有 4 个不同的类。

Based on what you're trying to test here, checking if the thing can compile is the only sensible test you can perform.

Testing should not be for the sake of testing, but to ensure functional correctness. If you want to have proper tests around your class, you should write tests that verify the functionality of your template with all of the 4 different classes it can be compiled with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文