Testing Compile-time Constructs in a Runtime Unit Testing Framework – Igor Bogoslavskyi – CppCon 21

  • Lobby
  • Science & Technology
  • Testing Compile-time Constructs in a Runtime Unit Testing Framework – Igor Bogoslavskyi – CppCon 21

https://cppcon.org/
https://github.com/CppCon/CppCon2020
---
C++ is used in many different areas, including in safety-critical applications. Coming from the automotive industry, we must conform to stringent safety standards (such as ISO 26262) which nudge developers away from dynamic dispatch and towards a safer and more static flavor of C++. However, following coding guidelines is not enough to write safe code; any program is only as safe as it’s testing suite is robust, so it is critical to have a solid testing framework and a watertight testing strategy.

There are multiple well-known frameworks for testing the code at runtime, such as GoogleTest, CPPUnit or Catch2, but as more and more work is done at compile-time, we believe that there is a need for a framework that also supports testing compile-time constructs. We believe that with the increasing use of static assertions, SFINAE and, soon, contracts, these compile-time constraints become part of the user-facing API and must be rigorously tested as well. As an example, consider a function which is part of the user-facing API with a static assertion in it to ensure that it can only be called with a certain set of types. With the existing testing frameworks there is no way to test that this function would _not_ accept certain types and only the correct behavior with the expected types can be tested. Therefore it is possible that during the development of the library the static assertion gets removed or modified by mistake, allowing the function to accept more types than expected. This kind of bug can lie undetected until it leads to a failure at some later point in time. We therefore believe that testing the compile-time aspects of the code, and especially being able to test what the code should _not_ be able to do, is just as important as testing the nominal runtime behavior. However, it is challenging to test the behavior of the code at compile time as for that the code must be, well, compiled.

To address this, we present a new library that integrates seamlessly with GoogleTest to allow writing special test cases, that we call static tests, within which certain statements can be wrapped in a SHOULD_NOT_COMPILE macro. When such a test is run, the library attempts to compile the statements within these macros . If they compile without an error, the test will fail and the developer will be alerted with an appropriate failure message. In this talk, we will expand on why we believe such a library is useful with real-life examples, cover how this library can be used, and provide some details on its current implementation.

---
Igor Bogoslavskyi

Igor is a senior software engineer at Apex.AI. He has a PhD in Robotics and is working on safe and certifiable algorithms for autonomous and automated driving. Igor is passionate about C++ and, apart from working with C++ for over 10 years professionally and writing and maintaining multiple open source C++ applications, he also maintains EasyClangComplete - a C++ code completion tool for Sublime Text.

---
Videos Streamed & Edited by Digital Medium: http://online.digital-medium.co.uk

The CppCon YouTube Channel Is Sponsored By:
JetBrains : http://jb.gg/cpptools
SonarSource: https://www.sonarsource.com/

Filed under: Science & Technology

No comment yet, add your voice below!


Add a Comment