Blender V4.3
BLI_fixed_width_int_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
7#include "BLI_rand.hh"
8#include "BLI_timeit.hh"
9#include "BLI_vector.hh"
10
11#include "testing/testing.h"
12
13/* See `BLI_fixed_width_int_str.hh` for why this is necessary. */
14#ifdef WITH_GMP
15
16namespace blender::fixed_width_int::tests {
17
18TEST(fixed_width_int, IsZero)
19{
20 EXPECT_TRUE(is_zero(UInt256(0)));
21 EXPECT_TRUE(is_zero(UInt256(10) - UInt256(10)));
22 EXPECT_TRUE(is_zero(UInt256(10) - UInt256(15) + UInt256(5)));
23 EXPECT_FALSE(is_zero(UInt256(10)));
24
25 EXPECT_TRUE(is_zero(Int256(0)));
26 EXPECT_TRUE(is_zero(Int256(10) - Int256(10)));
27 EXPECT_TRUE(is_zero(Int256(10) - Int256(15) + Int256(5)));
28 EXPECT_FALSE(is_zero(Int256(10)));
29 EXPECT_FALSE(is_zero(Int256(-10)));
30}
31
32TEST(fixed_width_int, ToString)
33{
34 {
35 const std::string str = "4875677549274093345634534";
37 }
38 {
39 const std::string str = "0";
41 }
42 {
43 const std::string str = "4875677549274093345634534";
45 }
46 {
47 const std::string str = "-4875677549274093345634534";
49 }
50 {
51 const std::string str = "0";
53 }
54}
55
56TEST(fixed_width_int, Add256)
57{
58 EXPECT_EQ(UInt256("290213998554153310989149424513459608072") +
59 UInt256("236559186774771353723629567597011581379"),
60 UInt256("526773185328924664712778992110471189451"));
61 EXPECT_EQ(UInt256("211377365172829431692550347604827003294") +
62 UInt256("151035310604094577723885879186052138391"),
63 UInt256("362412675776924009416436226790879141685"));
64 EXPECT_EQ(UInt256("34490924248914309185690728897294455642") +
65 UInt256("151329651396698072567782489740109235288"),
66 UInt256("185820575645612381753473218637403690930"));
67 EXPECT_EQ(UInt256("23020790973174243895398009931650855178") +
68 UInt256("242538071468046767660828531945711005380"),
69 UInt256("265558862441221011556226541877361860558"));
70 EXPECT_EQ(UInt256("220030846719277288761017165278417179519") +
71 UInt256("13817458575896368146281651263001012349"),
72 UInt256("233848305295173656907298816541418191868"));
73 EXPECT_EQ(UInt256("225958958932723616286848406010143428110") +
74 UInt256("309322190961572274983773819144991425669"),
75 UInt256("535281149894295891270622225155134853779"));
76 EXPECT_EQ(UInt256("166851370558999106635673647011389012481") +
77 UInt256("85443075281725354911889976920463997722"),
78 UInt256("252294445840724461547563623931853010203"));
79 EXPECT_EQ(UInt256("274485954517155769304275705148933346392") +
80 UInt256("215279677420695754877443907998549347900"),
81 UInt256("489765631937851524181719613147482694292"));
82 EXPECT_EQ(UInt256("3522191569845770793524407096643088669") +
83 UInt256("100106234023644716469012457480771518776"),
84 UInt256("103628425593490487262536864577414607445"));
85 EXPECT_EQ(UInt256("163994307071630654616433355844082912619") +
86 UInt256("263001956277142014131208604303902541977"),
87 UInt256("426996263348772668747641960147985454596"));
88}
89
90TEST(fixed_width_int, Fuzzy)
91{
92 RandomNumberGenerator rng;
93 for ([[maybe_unused]] const int i : IndexRange(10000)) {
94 {
95 const uint64_t a = rng.get_uint64();
96 const uint64_t b = rng.get_uint64();
100 EXPECT_EQ(a < b, UInt64_8(a) < UInt64_8(b));
101 EXPECT_EQ(a > b, UInt64_8(a) > UInt64_8(b));
102 EXPECT_EQ(a <= b, UInt64_8(a) <= UInt64_8(b));
103 EXPECT_EQ(a >= b, UInt64_8(a) >= UInt64_8(b));
104 EXPECT_EQ(a == b, UInt64_8(a) == UInt64_8(b));
105 EXPECT_EQ(a != b, UInt64_8(a) != UInt64_8(b));
106 EXPECT_FLOAT_EQ(double(a), double(UInt64_8(a)));
107 EXPECT_FLOAT_EQ(float(a), float(UInt64_8(a)));
108 }
109 {
110 const int64_t a = int64_t(rng.get_uint64()) * (rng.get_float() < 0.5f ? -1 : 1);
111 const int64_t b = int64_t(rng.get_uint64()) * (rng.get_float() < 0.5f ? -1 : 1);
112 EXPECT_EQ(a + b, int64_t(Int64_8(a) + Int64_8(b)));
113 EXPECT_EQ(a * b, int64_t(Int64_8(a) * Int64_8(b)));
114 EXPECT_EQ(a - b, int64_t(Int64_8(a) - Int64_8(b)));
115 EXPECT_EQ(a < b, Int64_8(a) < Int64_8(b));
116 EXPECT_EQ(a > b, Int64_8(a) > Int64_8(b));
117 EXPECT_EQ(a <= b, Int64_8(a) <= Int64_8(b));
118 EXPECT_EQ(a >= b, Int64_8(a) >= Int64_8(b));
119 EXPECT_EQ(a == b, Int64_8(a) == Int64_8(b));
120 EXPECT_EQ(a != b, Int64_8(a) != Int64_8(b));
121 EXPECT_EQ(a == 0, is_zero(Int64_8(a)));
122 EXPECT_EQ(b == 0, is_zero(Int64_8(b)));
123 EXPECT_EQ(a < 0, is_negative(Int64_8(a)));
125 EXPECT_FLOAT_EQ(double(a), double(Int64_8(a)));
126 EXPECT_FLOAT_EQ(float(a), float(Int64_8(a)));
127 }
128 }
129}
130
131} // namespace blender::fixed_width_int::tests
132
133#endif /* WITH_GMP */
TEST(array_store, Nop)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
local_group_size(16, 16) .push_constant(Type b
#define str(s)
static const char * to_string(const Interpolation &interp)
Definition gl_shader.cc:82
ccl_device_inline bool is_zero(const float2 a)
bool is_negative(const IntF< T, Size > &a)
__int64 int64_t
Definition stdint.h:89
unsigned __int64 uint64_t
Definition stdint.h:90