Aussie AI
Testing C++ Code Portability
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Testing C++ Code Portability
How can you assess whether your C++ code is portable? The short answer is: test it!
But you can't test portability on your own box. Instead, you should try to compile and run your code on all of your target platforms, as often as possible. The main points of this plan are:
- Compile on all platforms.
- Resolve compiler errors (e.g. add an extra type cast, or wrap non-portability with
#if
and macros). - Check the compiler warnings, not just the errors. Aim for “warning-free compilation” on all platforms.
- Run unit tests and regression test harnesses.
- Run the memory debug tools for that platform.
- Run any static code analysis tools available on that platform.
Virtually Portable. If you don't have access to a big lab full of boxes with random operating systems, then do it virtually. Spin up a new VM on your cloud provider, install the C++ compiler and build tools, upload your C++ source code, compile it, run the tests, shut it down again. Oops, start again, this time save the output results before shutting it down. Using a VM is a powerful way to try lots of platforms and it's not very expensive to do, unless you forget about an instance and accidentally leave it idling for a month.
Source Code Portability Assessment: If you want to try to assess code portability without actually running it on those boxes, try these suggestions:
- Review compiler warnings, which often warn of usage of undefined things, such as pointer casting.
- Run static analysis code checkers.
- Turn on the “strict” or “compliance” modes of your C++ compiler (if you enjoy pain).
- Add more unit tests: ensure that the unit tests run through paths that will be found on other platforms.
And one final suggestion on prioritizing testing of portability: test more on your current platform, instead. There'll be more bugs in your code on every platform. I'd bet V-bucks on it.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |