Listing the "Blender Foundation" as copyright holder implied the Blender Foundation holds copyright to files which may include work from many developers. While keeping copyright on headers makes sense for isolated libraries, Blender's own code may be refactored or moved between files in a way that makes the per file copyright holders less meaningful. Copyright references to the "Blender Foundation" have been replaced with "Blender Authors", with the exception of `./extern/` since these this contains libraries which are more isolated, any changed to license headers there can be handled on a case-by-case basis. Some directories in `./intern/` have also been excluded: - `./intern/cycles/` it's own `AUTHORS` file is planned. - `./intern/opensubdiv/`. An "AUTHORS" file has been added, using the chromium projects authors file as a template. Design task: #110784 Ref !110783.
107 lines
2.7 KiB
C++
107 lines
2.7 KiB
C++
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
|
|
|
#include "BLI_hash.hh"
|
|
#include "BLI_utildefines.h"
|
|
#include "MEM_guardedalloc.h"
|
|
#include "testing/testing.h"
|
|
|
|
namespace blender::tests {
|
|
|
|
class ExceptionThrower {
|
|
private:
|
|
/* Use some random values that are unlikely to exist at the memory location already. */
|
|
static constexpr uint32_t is_alive_state = 0x21254634;
|
|
static constexpr uint32_t is_destructed_state = 0xFA4BC327;
|
|
|
|
uint32_t state_;
|
|
|
|
/* Make use of leak detector to check if this value has been destructed. */
|
|
void *my_memory_;
|
|
|
|
public:
|
|
mutable bool throw_during_copy;
|
|
mutable bool throw_during_move;
|
|
/* Used for hashing and comparing. */
|
|
int value;
|
|
|
|
ExceptionThrower(int value = 0)
|
|
: state_(is_alive_state),
|
|
my_memory_(MEM_mallocN(1, AT)),
|
|
throw_during_copy(false),
|
|
throw_during_move(false),
|
|
value(value)
|
|
{
|
|
}
|
|
|
|
ExceptionThrower(const ExceptionThrower &other) : ExceptionThrower(other.value)
|
|
{
|
|
EXPECT_EQ(other.state_, is_alive_state);
|
|
if (other.throw_during_copy) {
|
|
throw std::runtime_error("throwing during copy, as requested");
|
|
}
|
|
}
|
|
|
|
ExceptionThrower(ExceptionThrower &&other) : ExceptionThrower(other.value)
|
|
{
|
|
EXPECT_EQ(other.state_, is_alive_state);
|
|
if (other.throw_during_move) {
|
|
throw std::runtime_error("throwing during move, as requested");
|
|
}
|
|
}
|
|
|
|
ExceptionThrower &operator=(const ExceptionThrower &other)
|
|
{
|
|
EXPECT_EQ(other.state_, is_alive_state);
|
|
if (throw_during_copy || other.throw_during_copy) {
|
|
throw std::runtime_error("throwing during copy, as requested");
|
|
}
|
|
value = other.value;
|
|
return *this;
|
|
}
|
|
|
|
ExceptionThrower &operator=(ExceptionThrower &&other)
|
|
{
|
|
EXPECT_EQ(other.state_, is_alive_state);
|
|
if (throw_during_move || other.throw_during_move) {
|
|
throw std::runtime_error("throwing during move, as requested");
|
|
}
|
|
value = other.value;
|
|
return *this;
|
|
}
|
|
|
|
~ExceptionThrower()
|
|
{
|
|
const char *message = "";
|
|
if (state_ != is_alive_state) {
|
|
if (state_ == is_destructed_state) {
|
|
message = "Trying to destruct an already destructed instance.";
|
|
}
|
|
else {
|
|
message = "Trying to destruct an uninitialized instance.";
|
|
}
|
|
}
|
|
EXPECT_EQ(state_, is_alive_state) << message;
|
|
state_ = is_destructed_state;
|
|
MEM_freeN(my_memory_);
|
|
}
|
|
|
|
uint64_t hash() const
|
|
{
|
|
return uint64_t(value);
|
|
}
|
|
|
|
friend bool operator==(const ExceptionThrower &a, const ExceptionThrower &b)
|
|
{
|
|
return a.value == b.value;
|
|
}
|
|
|
|
friend bool operator!=(const ExceptionThrower &a, const ExceptionThrower &b)
|
|
{
|
|
return !(a == b);
|
|
}
|
|
};
|
|
|
|
} // namespace blender::tests
|