Merge remote-tracking branch 'origin/master' into blender2.8
[blender.git] / tests / gtests / blenlib / BLI_math_base_test.cc
1 /* Apache License, Version 2.0 */
2
3 #include "testing/testing.h"
4
5 #include "BLI_math.h"
6
7 /* In tests below, when we are using -1.0f as max_diff value, we actually turn the function into a pure-ULP one. */
8
9 /* Put this here, since we cannot use BLI_assert() in inline math files it seems... */
10 TEST(math_base, CompareFFRelativeValid)
11 {
12         EXPECT_TRUE(sizeof(float) == sizeof(int));
13 }
14
15 TEST(math_base, CompareFFRelativeNormal)
16 {
17         float f1 = 1.99999988f;  /* *(float *)&(*(int *)&f2 - 1) */
18         float f2 = 2.00000000f;
19         float f3 = 2.00000048f;  /* *(float *)&(*(int *)&f2 + 2) */
20         float f4 = 2.10000000f;  /* *(float *)&(*(int *)&f2 + 419430) */
21
22         const float max_diff = FLT_EPSILON * 0.1f;
23
24         EXPECT_TRUE(compare_ff_relative(f1, f2, max_diff, 1));
25         EXPECT_TRUE(compare_ff_relative(f2, f1, max_diff, 1));
26
27         EXPECT_TRUE(compare_ff_relative(f3, f2, max_diff, 2));
28         EXPECT_TRUE(compare_ff_relative(f2, f3, max_diff, 2));
29
30         EXPECT_FALSE(compare_ff_relative(f3, f2, max_diff, 1));
31         EXPECT_FALSE(compare_ff_relative(f2, f3, max_diff, 1));
32
33
34         EXPECT_FALSE(compare_ff_relative(f3, f2, -1.0f, 1));
35         EXPECT_FALSE(compare_ff_relative(f2, f3, -1.0f, 1));
36
37         EXPECT_TRUE(compare_ff_relative(f3, f2, -1.0f, 2));
38         EXPECT_TRUE(compare_ff_relative(f2, f3, -1.0f, 2));
39
40
41         EXPECT_FALSE(compare_ff_relative(f4, f2, max_diff, 64));
42         EXPECT_FALSE(compare_ff_relative(f2, f4, max_diff, 64));
43
44         EXPECT_TRUE(compare_ff_relative(f1, f3, max_diff, 64));
45         EXPECT_TRUE(compare_ff_relative(f3, f1, max_diff, 64));
46 }
47
48 TEST(math_base, CompareFFRelativeZero)
49 {
50         float f0 = 0.0f;
51         float f1 = 4.2038954e-045f;  /* *(float *)&(*(int *)&f0 + 3) */
52
53         float fn0 = -0.0f;
54         float fn1 = -2.8025969e-045f;  /* *(float *)&(*(int *)&fn0 - 2) */
55
56         const float max_diff = FLT_EPSILON * 0.1f;
57
58         EXPECT_TRUE(compare_ff_relative(f0, f1, -1.0f, 3));
59         EXPECT_TRUE(compare_ff_relative(f1, f0, -1.0f, 3));
60
61         EXPECT_FALSE(compare_ff_relative(f0, f1, -1.0f, 1));
62         EXPECT_FALSE(compare_ff_relative(f1, f0, -1.0f, 1));
63
64         EXPECT_TRUE(compare_ff_relative(fn0, fn1, -1.0f, 8));
65         EXPECT_TRUE(compare_ff_relative(fn1, fn0, -1.0f, 8));
66
67
68         EXPECT_TRUE(compare_ff_relative(f0, f1, max_diff, 1));
69         EXPECT_TRUE(compare_ff_relative(f1, f0, max_diff, 1));
70
71         EXPECT_TRUE(compare_ff_relative(fn0, f0, max_diff, 1));
72         EXPECT_TRUE(compare_ff_relative(f0, fn0, max_diff, 1));
73
74         EXPECT_TRUE(compare_ff_relative(f0, fn1, max_diff, 1));
75         EXPECT_TRUE(compare_ff_relative(fn1, f0, max_diff, 1));
76
77
78         /* Note: in theory, this should return false, since 0.0f  and -0.0f have 0x80000000 diff,
79          *       but overflow in substraction seems to break something here
80          *       (abs(*(int *)&fn0 - *(int *)&f0) == 0x80000000 == fn0), probably because int32 cannot hold this abs value.
81      *       this is yet another illustration of why one shall never use (near-)zero floats in pure-ULP comparison. */
82 //      EXPECT_FALSE(compare_ff_relative(fn0, f0, -1.0f, 1024));
83 //      EXPECT_FALSE(compare_ff_relative(f0, fn0, -1.0f, 1024));
84
85         EXPECT_FALSE(compare_ff_relative(fn0, f1, -1.0f, 1024));
86         EXPECT_FALSE(compare_ff_relative(f1, fn0, -1.0f, 1024));
87 }