snitch-org / snitch

Lightweight C++20 testing framework.
Boost Software License 1.0
257 stars 7 forks source link

Add `CONSTEXPR_CHECK` and `CONSTEXPR_REQUIRE` #76

Closed cschreib closed 1 year ago

cschreib commented 1 year ago

This PR introduces full support for compile-time testing. This includes compile-time evaluation and expression decomposition. For this purpose, two new sets of macros are included:

Although we already know at compile-time if a compile-time test passes or fail, the test failure is nonetheless only reported at run-time. This is done to follow the same reporting mechanism as the one set up for regular run-time tests (custom reporter, etc.).

To achieve this, I needed to implement constexpr integer and float serialization functions, since the C++20 STL does not provide anything usable at compile time (in C++23 we would be able to use std::to_chars, but only for integers and not for floats). Although integer serialization is pretty easy, float is not. The algorithm I used is based on fixed point arithmetic with 64 bit worth of digits in base 10. It is a simpler form of common state-of-the-art algorithms (e.g. Grisu). It is probably nowhere near as efficient, but on the plus side doesn't require storing large tables or complicated arbitrary precision types. To keep it simple, I only implemented exponential format with fixed precision (6 digits after the decimal point for float, and 15 for double). This achieves a perfect match to std::printf() for floats and 6 digits precision in 99.995% of the cases (when it fails, it's just the very last digit that is off by one). I don't have the stats for doubles.

I also needed to refactor the expression decomposer a little bit, to be constexpr-friendly. This had the nice side effect of improving compilation times slightly overall.

Closes #77. Closes #60.

codecov[bot] commented 1 year ago

Codecov Report

Merging #76 (d6cb28d) into main (793baf8) will increase coverage by 0.04%. The diff coverage is 94.64%.

Additional details and impacted files [![Impacted file tree graph](https://codecov.io/gh/cschreib/snitch/pull/76/graphs/tree.svg?width=650&height=150&src=pr&token=X422DE81PN&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Corentin+Schreiber)](https://codecov.io/gh/cschreib/snitch/pull/76?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Corentin+Schreiber) ```diff @@ Coverage Diff @@ ## main #76 +/- ## ========================================== + Coverage 92.56% 92.61% +0.04% ========================================== Files 2 2 Lines 1049 1272 +223 ========================================== + Hits 971 1178 +207 - Misses 78 94 +16 ``` | [Impacted Files](https://codecov.io/gh/cschreib/snitch/pull/76?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Corentin+Schreiber) | Coverage Δ | | |---|---|---| | [include/snitch/snitch.hpp](https://codecov.io/gh/cschreib/snitch/pull/76?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Corentin+Schreiber#diff-aW5jbHVkZS9zbml0Y2gvc25pdGNoLmhwcA==) | `95.03% <94.38%> (-1.03%)` | :arrow_down: | | [src/snitch.cpp](https://codecov.io/gh/cschreib/snitch/pull/76?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Corentin+Schreiber#diff-c3JjL3NuaXRjaC5jcHA=) | `90.67% <100.00%> (-0.29%)` | :arrow_down: | ------ [Continue to review full report in Codecov by Sentry](https://codecov.io/gh/cschreib/snitch/pull/76?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Corentin+Schreiber). > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Corentin+Schreiber) > `Δ = absolute (impact)`, `ø = not affected`, `? = missing data` > Powered by [Codecov](https://codecov.io/gh/cschreib/snitch/pull/76?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Corentin+Schreiber). Last update [69d813b...d6cb28d](https://codecov.io/gh/cschreib/snitch/pull/76?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Corentin+Schreiber). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Corentin+Schreiber).