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