diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:49:52 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:49:52 +0000 |
commit | 55944e5e40b1be2afc4855d8d2baf4b73d1876b5 (patch) | |
tree | 33f869f55a1b149e9b7c2b7e201867ca5dd52992 /src/test/test-math-util.c | |
parent | Initial commit. (diff) | |
download | systemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.tar.xz systemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.zip |
Adding upstream version 255.4.upstream/255.4
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/test/test-math-util.c | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/test/test-math-util.c b/src/test/test-math-util.c new file mode 100644 index 0000000..9771576 --- /dev/null +++ b/src/test/test-math-util.c @@ -0,0 +1,110 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include <float.h> + +#include "math-util.h" +#include "tests.h" + +TEST(iszero_safe) { + /* zeros */ + assert_se(iszero_safe(0.0)); + assert_se(iszero_safe(-0.0)); + assert_se(iszero_safe(0e0)); + assert_se(iszero_safe(-0e0)); + assert_se(iszero_safe(0e+0)); + assert_se(iszero_safe(0e-0)); + assert_se(iszero_safe(-0e-0)); + assert_se(iszero_safe(-0e000)); + assert_se(iszero_safe(0e000)); + + /* non-zero normal values */ + assert_se(!iszero_safe(42.0)); + assert_se(!iszero_safe(M_PI)); + assert_se(!iszero_safe(DBL_MAX)); + assert_se(!iszero_safe(-DBL_MAX)); + assert_se(!iszero_safe(DBL_MIN)); + assert_se(!iszero_safe(-DBL_MIN)); + assert_se(!iszero_safe(1 / DBL_MAX)); + + /* subnormal values */ + assert_se(!iszero_safe(DBL_MIN / 2)); + assert_se(!iszero_safe(-DBL_MIN / 42)); + assert_se(!iszero_safe(1 / DBL_MAX / 2)); + + /* too small values which cannot be in subnormal form */ + assert_se( iszero_safe(DBL_MIN / DBL_MAX)); + assert_se( iszero_safe(DBL_MIN / -DBL_MAX)); + assert_se( iszero_safe(-DBL_MIN / DBL_MAX)); + assert_se( iszero_safe(-DBL_MIN / -DBL_MAX)); + + /* NaN or infinity */ + assert_se(!iszero_safe(NAN)); + assert_se(!iszero_safe(INFINITY)); + assert_se(!iszero_safe(-INFINITY)); + assert_se(!iszero_safe(1 / NAN)); + + /* inverse of infinity */ + assert_se( iszero_safe(1 / INFINITY)); + assert_se( iszero_safe(1 / -INFINITY)); + assert_se( iszero_safe(-1 / INFINITY)); + assert_se( iszero_safe(-1 / -INFINITY)); + assert_se( iszero_safe(42 / -INFINITY)); + assert_se( iszero_safe(-42 / -INFINITY)); + assert_se( iszero_safe(DBL_MIN / INFINITY)); + assert_se( iszero_safe(DBL_MIN / -INFINITY)); + assert_se( iszero_safe(DBL_MAX / INFINITY / 2)); + assert_se( iszero_safe(DBL_MAX / -INFINITY * DBL_MAX)); + + /* infinity / infinity is NaN */ + assert_se(!iszero_safe(INFINITY / INFINITY)); + assert_se(!iszero_safe(INFINITY * 2 / INFINITY)); + assert_se(!iszero_safe(INFINITY / DBL_MAX / INFINITY)); +} + +TEST(fp_equal) { + /* normal values */ + assert_se( fp_equal(0.0, -0e0)); + assert_se( fp_equal(3.0, 3)); + assert_se(!fp_equal(3.000001, 3)); + assert_se( fp_equal(M_PI, M_PI)); + assert_se(!fp_equal(M_PI, -M_PI)); + assert_se( fp_equal(DBL_MAX, DBL_MAX)); + assert_se(!fp_equal(DBL_MAX, -DBL_MAX)); + assert_se(!fp_equal(-DBL_MAX, DBL_MAX)); + assert_se( fp_equal(-DBL_MAX, -DBL_MAX)); + assert_se( fp_equal(DBL_MIN, DBL_MIN)); + assert_se(!fp_equal(DBL_MIN, -DBL_MIN)); + assert_se(!fp_equal(-DBL_MIN, DBL_MIN)); + assert_se( fp_equal(-DBL_MIN, -DBL_MIN)); + + /* subnormal values */ + assert_se( fp_equal(DBL_MIN / 10, DBL_MIN / 10)); + assert_se(!fp_equal(DBL_MIN / 10, -DBL_MIN / 10)); + assert_se(!fp_equal(-DBL_MIN / 10, DBL_MIN / 10)); + assert_se( fp_equal(-DBL_MIN / 10, -DBL_MIN / 10)); + assert_se(!fp_equal(DBL_MIN / 10, DBL_MIN / 15)); + assert_se(!fp_equal(DBL_MIN / 10, DBL_MIN / 15)); + + /* subnormal difference */ + assert_se(!fp_equal(DBL_MIN / 10, DBL_MIN + DBL_MIN / 10)); + assert_se( fp_equal(3.0, 3.0 + DBL_MIN / 2)); /* 3.0 + DBL_MIN / 2 is truncated to 3.0 */ + + /* too small values */ + assert_se( fp_equal(DBL_MIN / DBL_MAX, -DBL_MIN / DBL_MAX)); + + /* NaN or infinity */ + assert_se(!fp_equal(NAN, NAN)); + assert_se(!fp_equal(NAN, 0)); + assert_se(!fp_equal(NAN, INFINITY)); + assert_se(!fp_equal(INFINITY, INFINITY)); + assert_se(!fp_equal(INFINITY, -INFINITY)); + assert_se(!fp_equal(-INFINITY, INFINITY)); + assert_se(!fp_equal(-INFINITY, -INFINITY)); + + /* inverse of infinity */ + assert_se( fp_equal(0, 1 / INFINITY)); + assert_se( fp_equal(42 / INFINITY, 1 / -INFINITY)); + assert_se(!fp_equal(42 / INFINITY, INFINITY / INFINITY)); +} + +DEFINE_TEST_MAIN(LOG_DEBUG); |