diff options
| -rw-r--r-- | meson.build | 10 | ||||
| -rw-r--r-- | subprojects/rapidcheck.wrap | 4 | ||||
| -rw-r--r-- | tests/test_fixed_point.cpp | 26 |
3 files changed, 39 insertions, 1 deletions
diff --git a/meson.build b/meson.build index bff85ce..9f860b9 100644 --- a/meson.build +++ b/meson.build @@ -3,6 +3,14 @@ project('fg-engine', 'cpp', cpp = meson.get_compiler('cpp') +cmake = import('cmake') + +rc_options = cmake.subproject_options() +rc_options.add_cmake_defines({'RC_ENABLE_CATCH': 'ON'}) +rapidcheck_proj = cmake.subproject('rapidcheck', options: rc_options) +rapidcheck_dep = rapidcheck_proj.dependency('rapidcheck') +rapidcheck_catch_dep = rapidcheck_proj.dependency('rapidcheck_catch') + if cpp.get_id() in ['msvc', 'clang-cl'] add_project_arguments('/std:c++latest', language: 'cpp') else @@ -25,7 +33,7 @@ catch2_dep = dependency('catch2-with-main') test_exe = executable('fg-tests', sources: files('tests/test_fixed_point.cpp'), - dependencies: [sim_core_dep, catch2_dep], + dependencies: [sim_core_dep, catch2_dep, rapidcheck_dep, rapidcheck_catch_dep], ) test('unit tests', test_exe, args: ['-r', 'automake']) diff --git a/subprojects/rapidcheck.wrap b/subprojects/rapidcheck.wrap new file mode 100644 index 0000000..bdd4274 --- /dev/null +++ b/subprojects/rapidcheck.wrap @@ -0,0 +1,4 @@ +[wrap-git] +url = https://github.com/emil-e/rapidcheck.git +revision = 6e8dadfdafa3a74eabb52ead87f8787f72eccd0b +clone-recursive = true diff --git a/tests/test_fixed_point.cpp b/tests/test_fixed_point.cpp index 8c5c269..87a6ef2 100644 --- a/tests/test_fixed_point.cpp +++ b/tests/test_fixed_point.cpp @@ -1,4 +1,5 @@ #include <catch2/catch_test_macros.hpp> +#include <rapidcheck/catch.h> #include "fixed_point.h" TEST_CASE("fp_t addition is commutative") { @@ -96,3 +97,28 @@ TEST_CASE("fp_t division truncates precision below 1 ULP") { fp_t c = fp_t::from_raw(0); CHECK(a / b == c); } + +namespace rc { + template<> + struct Arbitrary<fp_t> { + static Gen<fp_t> arbitrary() { + return gen::map(gen::arbitrary<std::int32_t>(), + [](std::int32_t raw) { return fp_t::from_raw(raw); }); + } + }; +} + +TEST_CASE("fp_t addition is commutative for any bit pattern (fuzzed)") { + rc::prop("a + b == b + a", + [](const fp_t& a, const fp_t& b) { + RC_ASSERT(a + b == b + a); + }); +} + +TEST_CASE("fp_t division by itself is identity for any nonzero value (fuzzed)") { + rc::prop("a / a == from_int(1) when a.raw != 0", + [](const fp_t& a) { + RC_PRE(a.raw != 0); + RC_ASSERT(a / a == fp_t::from_int(1)); + }); +} |
