Unit testing an
expression template
library in C++20
Braden Ganetsky
C++Now 2024

"constexpr ALL the things!"
https://www.youtube.com/watch?v=HMB9oXFobJc

Unit testing

Unit testing

Unit testing

Compile-time versus run-time


						[] {
							Checker<1> chk;
							static_assert(chk.check(1));
							assert(chk.check(0));
						}();
					
`tok3n` library

Basic usage

Basic usage

Basic usage

Basic usage

Basic usage

Basic usage

Basic usage

Basic usage

Basic usage

Basic usage

Modifiers

Modifiers

Modifiers

Modifiers

Modifiers

Modifiers

Non-char spans

Non-char spans

Recursion with JSON

Unit testing frameworks

Test setup

  • Test parse result
  • Test combinator operations
  • Test parser equality
  • Test parser properties

Run-time testing with GoogleTest

Run-time testing with GoogleTest

Run-time testing with GoogleTest

Run-time testing with GoogleTest

Run-time testing with GoogleTest

Run-time testing with GoogleTest

Run-time testing with GoogleTest

Run-time testing with GoogleTest

Run-time testing with GoogleTest

✔️

Run-time testing with Boost.Test

Run-time testing with Boost.Test

Run-time testing with Boost.Test

Run-time testing with Boost.Test

Run-time testing with Boost.Test

Run-time testing with Boost.Test

✔️

Run-time testing with Catch2

Run-time testing with Catch2

Run-time testing with Catch2

Run-time testing with Catch2

Run-time testing with Catch2

✔️

Run-time testing with doctest

Run-time testing with doctest

Run-time testing with doctest

Run-time testing with doctest

Run-time testing with doctest

✔️

Run-time testing with snitch

Run-time testing with snitch

Run-time testing with snitch

Run-time testing with snitch

Run-time testing with snitch

✔️

Run-time testing with UT

Run-time testing with UT

Run-time testing with UT

Run-time testing with UT

Run-time testing with UT

✔️

Compile-time testing

Compile-time testing

Compile-time testing with GoogleTest

Compile-time testing with GoogleTest

Compile-time testing with GoogleTest

Compile-time testing with GoogleTest

Compile-time testing with GoogleTest

Compile-time testing with GoogleTest

Compile-time testing with GoogleTest

Compile-time testing with GoogleTest?

Compile-time testing with Boost.Test

Compile-time testing with Boost.Test

Compile-time testing with Boost.Test

Compile-time testing with Catch2

Compile-time testing with Catch2

Compile-time testing with Catch2

Compile-time testing with Catch2

Compile-time testing with doctest

Compile-time testing with snitch

Compile-time testing with snitch

Compile-time testing with snitch

Compile-time testing with snitch

Compile-time testing with snitch

Compile-time testing with snitch

Compile-time testing with snitch

Compile-time testing with Catch2

Compile-time testing with snitch

Compile-time testing with snitch

Compile-time testing with snitch

Compile-time testing with snitch

✔️

Compile-time testing with UT

Compile-time testing with UT

Compile-time testing with UT

Compile-time testing with UT

Compile-time testing with UT

Compile-time testing with UT

Compile-time testing with UT

Compile-time testing with UT

Compile-time testing with UT

✔️

Parametrized compile-time testing with snitch

Parametrized compile-time testing with snitch

Parametrized compile-time testing with snitch

My solution

static_assert messages in C++20

[dcl.pre]

In a static_assert-declaration, the constant-expression shall be a contextually converted constant expression of type bool [...] the resulting diagnostic message (4.1) shall include the text of the string-literal, if one is supplied, [...]

Mini testing library

Mini testing library

Mini testing library

Mini testing library

Simple test

Simple test

Simple test

Simple test


Infeasable to write many of these

Infeasable to write many of these

DRY!

Parametrize the testing

Parametrize the testing

Parametrize the testing

Parametrize the testing

Parametrize the testing

Trouble with string literals

Trouble with string literals

Trouble with string literals

Trouble with string literals

??

Parametrize the testing - more macros

Parametrize the testing - more macros

Parametrize the testing - more macros

Parametrize the testing - more macros

Parametrize the testing - more macros

Upcoming C++ features

static_assert(false)

static_assert(false)

Pre C++23
C++23

static_assert(false)

User-generated static_assert messages

User-generated static_assert messages

User-generated static_assert messages

Conclusion
Thank you!
Braden Ganetsky

braden@ganets.ky
GitHub @k3DW