Blender V4.3
BLI_exception_safety_test_utils.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "BLI_hash.hh"
6#include "BLI_utildefines.h"
7#include "MEM_guardedalloc.h"
8#include "testing/testing.h"
9
10namespace blender::tests {
11
13 private:
14 /* Use some random values that are unlikely to exist at the memory location already. */
15 static constexpr uint32_t is_alive_state = 0x21254634;
16 static constexpr uint32_t is_destructed_state = 0xFA4BC327;
17
18 uint32_t state_;
19
20 /* Make use of leak detector to check if this value has been destructed. */
21 void *my_memory_;
22
23 public:
24 mutable bool throw_during_copy;
25 mutable bool throw_during_move;
26 /* Used for hashing and comparing. */
27 int value;
28
29 ExceptionThrower(int value = 0)
30 : state_(is_alive_state),
31 my_memory_(MEM_mallocN(1, AT)),
34 value(value)
35 {
36 }
37
39 {
40 EXPECT_EQ(other.state_, is_alive_state);
41 if (other.throw_during_copy) {
42 throw std::runtime_error("throwing during copy, as requested");
43 }
44 }
45
47 {
48 EXPECT_EQ(other.state_, is_alive_state);
49 if (other.throw_during_move) {
50 throw std::runtime_error("throwing during move, as requested");
51 }
52 }
53
55 {
56 EXPECT_EQ(other.state_, is_alive_state);
57 if (throw_during_copy || other.throw_during_copy) {
58 throw std::runtime_error("throwing during copy, as requested");
59 }
60 value = other.value;
61 return *this;
62 }
63
65 {
66 EXPECT_EQ(other.state_, is_alive_state);
67 if (throw_during_move || other.throw_during_move) {
68 throw std::runtime_error("throwing during move, as requested");
69 }
70 value = other.value;
71 return *this;
72 }
73
75 {
76 const char *message = "";
77 if (state_ != is_alive_state) {
78 if (state_ == is_destructed_state) {
79 message = "Trying to destruct an already destructed instance.";
80 }
81 else {
82 message = "Trying to destruct an uninitialized instance.";
83 }
84 }
85 EXPECT_EQ(state_, is_alive_state) << message;
86 state_ = is_destructed_state;
87 MEM_freeN(my_memory_);
88 }
89
90 uint64_t hash() const
91 {
92 return uint64_t(value);
93 }
94
95 friend bool operator==(const ExceptionThrower &a, const ExceptionThrower &b)
96 {
97 return a.value == b.value;
98 }
99
100 friend bool operator!=(const ExceptionThrower &a, const ExceptionThrower &b)
101 {
102 return !(a == b);
103 }
104};
105
106} // namespace blender::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
Read Guarded memory(de)allocation.
ExceptionThrower & operator=(const ExceptionThrower &other)
ExceptionThrower & operator=(ExceptionThrower &&other)
friend bool operator==(const ExceptionThrower &a, const ExceptionThrower &b)
friend bool operator!=(const ExceptionThrower &a, const ExceptionThrower &b)
ExceptionThrower(const ExceptionThrower &other)
local_group_size(16, 16) .push_constant(Type b
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
unsigned int uint32_t
Definition stdint.h:80
unsigned __int64 uint64_t
Definition stdint.h:90