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