Blender  V3.3
BLI_math_base_test.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 
3 #include "testing/testing.h"
4 
5 #include "BLI_math.h"
6 #include "BLI_math_base.hh"
7 #include "BLI_math_vector.hh"
8 
9 namespace blender::tests {
10 
11 /* In tests below, when we are using -1.0f as max_diff value, we actually turn the function into a
12  * pure-ULP one. */
13 
14 /* Put this here, since we cannot use BLI_assert() in inline math files it seems... */
15 TEST(math_base, CompareFFRelativeValid)
16 {
17  EXPECT_TRUE(sizeof(float) == sizeof(int));
18 }
19 
20 TEST(math_base, CompareFFRelativeNormal)
21 {
22  float f1 = 1.99999988f; /* *(float *)&(*(int *)&f2 - 1) */
23  float f2 = 2.00000000f;
24  float f3 = 2.00000048f; /* *(float *)&(*(int *)&f2 + 2) */
25  float f4 = 2.10000000f; /* *(float *)&(*(int *)&f2 + 419430) */
26 
27  const float max_diff = FLT_EPSILON * 0.1f;
28 
29  EXPECT_TRUE(compare_ff_relative(f1, f2, max_diff, 1));
30  EXPECT_TRUE(compare_ff_relative(f2, f1, max_diff, 1));
31 
32  EXPECT_TRUE(compare_ff_relative(f3, f2, max_diff, 2));
33  EXPECT_TRUE(compare_ff_relative(f2, f3, max_diff, 2));
34 
35  EXPECT_FALSE(compare_ff_relative(f3, f2, max_diff, 1));
36  EXPECT_FALSE(compare_ff_relative(f2, f3, max_diff, 1));
37 
38  EXPECT_FALSE(compare_ff_relative(f3, f2, -1.0f, 1));
39  EXPECT_FALSE(compare_ff_relative(f2, f3, -1.0f, 1));
40 
41  EXPECT_TRUE(compare_ff_relative(f3, f2, -1.0f, 2));
42  EXPECT_TRUE(compare_ff_relative(f2, f3, -1.0f, 2));
43 
44  EXPECT_FALSE(compare_ff_relative(f4, f2, max_diff, 64));
45  EXPECT_FALSE(compare_ff_relative(f2, f4, max_diff, 64));
46 
47  EXPECT_TRUE(compare_ff_relative(f1, f3, max_diff, 64));
48  EXPECT_TRUE(compare_ff_relative(f3, f1, max_diff, 64));
49 }
50 
51 TEST(math_base, CompareFFRelativeZero)
52 {
53  float f0 = 0.0f;
54  float f1 = 4.2038954e-045f; /* *(float *)&(*(int *)&f0 + 3) */
55 
56  float fn0 = -0.0f;
57  float fn1 = -2.8025969e-045f; /* *(float *)&(*(int *)&fn0 - 2) */
58 
59  const float max_diff = FLT_EPSILON * 0.1f;
60 
61  EXPECT_TRUE(compare_ff_relative(f0, f1, -1.0f, 3));
62  EXPECT_TRUE(compare_ff_relative(f1, f0, -1.0f, 3));
63 
64  EXPECT_FALSE(compare_ff_relative(f0, f1, -1.0f, 1));
65  EXPECT_FALSE(compare_ff_relative(f1, f0, -1.0f, 1));
66 
67  EXPECT_TRUE(compare_ff_relative(fn0, fn1, -1.0f, 8));
68  EXPECT_TRUE(compare_ff_relative(fn1, fn0, -1.0f, 8));
69 
70  EXPECT_TRUE(compare_ff_relative(f0, f1, max_diff, 1));
71  EXPECT_TRUE(compare_ff_relative(f1, f0, max_diff, 1));
72 
73  EXPECT_TRUE(compare_ff_relative(fn0, f0, max_diff, 1));
74  EXPECT_TRUE(compare_ff_relative(f0, fn0, max_diff, 1));
75 
76  EXPECT_TRUE(compare_ff_relative(f0, fn1, max_diff, 1));
77  EXPECT_TRUE(compare_ff_relative(fn1, f0, max_diff, 1));
78 
79  /* NOTE: in theory, this should return false, since 0.0f and -0.0f have 0x80000000 diff,
80  * but overflow in subtraction seems to break something here
81  * (abs(*(int *)&fn0 - *(int *)&f0) == 0x80000000 == fn0), probably because int32 cannot
82  * hold this abs value. this is yet another illustration of why one shall never use (near-)zero
83  * floats in pure-ULP comparison. */
84  // EXPECT_FALSE(compare_ff_relative(fn0, f0, -1.0f, 1024));
85  // EXPECT_FALSE(compare_ff_relative(f0, fn0, -1.0f, 1024));
86 
87  EXPECT_FALSE(compare_ff_relative(fn0, f1, -1.0f, 1024));
88  EXPECT_FALSE(compare_ff_relative(f1, fn0, -1.0f, 1024));
89 }
90 
91 TEST(math_base, Log2FloorU)
92 {
93  EXPECT_EQ(log2_floor_u(0), 0);
94  EXPECT_EQ(log2_floor_u(1), 0);
95  EXPECT_EQ(log2_floor_u(2), 1);
96  EXPECT_EQ(log2_floor_u(3), 1);
97  EXPECT_EQ(log2_floor_u(4), 2);
98  EXPECT_EQ(log2_floor_u(5), 2);
99  EXPECT_EQ(log2_floor_u(6), 2);
100  EXPECT_EQ(log2_floor_u(7), 2);
101  EXPECT_EQ(log2_floor_u(8), 3);
102  EXPECT_EQ(log2_floor_u(9), 3);
103  EXPECT_EQ(log2_floor_u(123456), 16);
104 }
105 
106 TEST(math_base, Log2CeilU)
107 {
108  EXPECT_EQ(log2_ceil_u(0), 0);
109  EXPECT_EQ(log2_ceil_u(1), 0);
110  EXPECT_EQ(log2_ceil_u(2), 1);
111  EXPECT_EQ(log2_ceil_u(3), 2);
112  EXPECT_EQ(log2_ceil_u(4), 2);
113  EXPECT_EQ(log2_ceil_u(5), 3);
114  EXPECT_EQ(log2_ceil_u(6), 3);
115  EXPECT_EQ(log2_ceil_u(7), 3);
116  EXPECT_EQ(log2_ceil_u(8), 3);
117  EXPECT_EQ(log2_ceil_u(9), 4);
118  EXPECT_EQ(log2_ceil_u(123456), 17);
119 }
120 
121 TEST(math_base, CeilPowerOf10)
122 {
125  EXPECT_EQ(ceil_power_of_10(1e-6f), 1e-6f);
126  EXPECT_NEAR(ceil_power_of_10(100.1f), 1000.0f, 1e-4f);
127  EXPECT_NEAR(ceil_power_of_10(99.9f), 100.0f, 1e-4f);
128 }
129 
130 TEST(math_base, FloorPowerOf10)
131 {
134  EXPECT_EQ(floor_power_of_10(1e-6f), 1e-6f);
135  EXPECT_NEAR(floor_power_of_10(100.1f), 100.0f, 1e-4f);
136  EXPECT_NEAR(floor_power_of_10(99.9f), 10.0f, 1e-4f);
137 }
138 
139 TEST(math_base, MinVectorAndFloat)
140 {
141  EXPECT_EQ(math::min(1.0f, 2.0f), 1.0f);
142 }
143 
144 TEST(math_base, ClampInt)
145 {
146  EXPECT_EQ(math::clamp(111, -50, 101), 101);
147 }
148 
149 TEST(math_base, Midpoint)
150 {
151  EXPECT_NEAR(math::midpoint(100.0f, 200.0f), 150.0f, 1e-4f);
152 }
153 
154 TEST(math_base, InterpolateInt)
155 {
156  EXPECT_EQ(math::interpolate(100, 200, 0.4f), 140);
157 }
158 
159 } // namespace blender::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
MINLINE unsigned int log2_ceil_u(unsigned int x)
float floor_power_of_10(float f)
Definition: math_base.c:64
MINLINE unsigned int log2_floor_u(unsigned int x)
float ceil_power_of_10(float f)
Definition: math_base.c:73
MINLINE int compare_ff_relative(float a, float b, float max_diff, int max_ulps)
T clamp(const T &a, const T &min, const T &max)
T min(const T &a, const T &b)
T midpoint(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
TEST(any, DefaultConstructor)
Definition: BLI_any_test.cc:10