AK+LibM: Make EXPECT_CLOSE more useful during debugging

This commit is contained in:
Ben Wiederhake 2021-03-02 20:33:44 +01:00 committed by Andreas Kling
parent 9b27b0cd1a
commit 5df014b8ff
Notes: sideshowbarker 2024-07-18 21:45:10 +09:00
2 changed files with 27 additions and 19 deletions

View file

@ -324,3 +324,16 @@ using AK::TestSuite;
current_test_case_did_fail(); \
} \
} while (false)
#define EXPECT_CLOSE(a, b) \
do { \
auto expect_close_lhs = a; \
auto expect_close_rhs = b; \
auto expect_close_diff = expect_close_lhs - expect_close_rhs; \
if (fabs(expect_close_diff) >= 0.000001) { \
warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_CLOSE({}, {})" \
" failed with lhs={}, rhs={}, (lhs-rhs)={}", \
__FILE__, __LINE__, #a, #b, expect_close_lhs, expect_close_rhs, expect_close_diff); \
current_test_case_did_fail(); \
} \
} while (false)

View file

@ -28,17 +28,12 @@
#include <math.h>
#define EXPECT_CLOSE(a, b) \
{ \
EXPECT(fabs(a - b) < 0.000001); \
}
TEST_CASE(trig)
{
EXPECT_CLOSE(sin(1234), 0.601927);
EXPECT_CLOSE(cos(1234), -0.798550);
EXPECT_CLOSE(tan(1234), -0.753775);
EXPECT_CLOSE(sqrt(1234), 35.128336)
EXPECT_CLOSE(sqrt(1234), 35.128336);
EXPECT_CLOSE(sin(-1), -0.8414709848078965);
EXPECT_CLOSE(cos(-1), 0.5403023058681398);
EXPECT_CLOSE(tan(-1), -1.5574077246549023);
@ -55,12 +50,12 @@ TEST_CASE(trig)
EXPECT_CLOSE(asin(0.9), 1.119770);
EXPECT_CLOSE(asin(0.99), 1.429245);
EXPECT_CLOSE(asin(1.0), 1.570750);
EXPECT_CLOSE(atan(0), 0.0)
EXPECT_CLOSE(atan(0.5), 0.463648)
EXPECT_CLOSE(atan(-0.5), -0.463648)
EXPECT_CLOSE(atan(5.5), 1.390943)
EXPECT_CLOSE(atan(-5.5), -1.390943)
EXPECT_CLOSE(atan(555.5), 1.568996)
EXPECT_CLOSE(atan(0), 0.0);
EXPECT_CLOSE(atan(0.5), 0.463648);
EXPECT_CLOSE(atan(-0.5), -0.463648);
EXPECT_CLOSE(atan(5.5), 1.390943);
EXPECT_CLOSE(atan(-5.5), -1.390943);
EXPECT_CLOSE(atan(555.5), 1.568996);
}
TEST_CASE(other)
@ -102,13 +97,13 @@ TEST_CASE(logarithms)
{
EXPECT(isnan(log(-1)));
EXPECT(log(0) < -1000000);
EXPECT_CLOSE(log(0.5), -0.693233)
EXPECT_CLOSE(log(1.1), 0.095310)
EXPECT_CLOSE(log(5), 1.609480)
EXPECT_CLOSE(log(5.5), 1.704842)
EXPECT_CLOSE(log(500), 6.214104)
EXPECT_CLOSE(log2(5), 2.321989)
EXPECT_CLOSE(log10(5), 0.698988)
EXPECT_CLOSE(log(0.5), -0.693233);
EXPECT_CLOSE(log(1.1), 0.095310);
EXPECT_CLOSE(log(5), 1.609480);
EXPECT_CLOSE(log(5.5), 1.704842);
EXPECT_CLOSE(log(500), 6.214104);
EXPECT_CLOSE(log2(5), 2.321989);
EXPECT_CLOSE(log10(5), 0.698988);
}
union Extractor {