Luna/tests/libluna/TestSharedPtr.cpp
apio 4ed7ec5e93
All checks were successful
continuous-integration/drone/push Build is passing
libluna: Store SharedPtr's ref count in the object itself
2023-07-30 11:32:46 +02:00

62 lines
1.0 KiB
C++

#include <luna/SharedPtr.h>
#include <test.h>
class SampleClass : public Shareable
{
public:
SampleClass(int* ptr, int value) : m_ptr(ptr), m_value(value)
{
}
~SampleClass()
{
if (m_ptr) *m_ptr = m_value;
}
private:
int* m_ptr;
int m_value;
};
TestResult test_shared_ptr_destroyed_on_scope_exit()
{
static int value = 0;
{
SharedPtr<SampleClass> sample = TRY(make_shared<SampleClass>(&value, 1));
}
validate(value == 1);
test_success;
}
TestResult test_shared_ptr_preserved_if_at_least_one_copy_exists()
{
static int value = 0;
static SharedPtr<SampleClass> ptr = {};
{
SharedPtr<SampleClass> sample = TRY(make_shared<SampleClass>(&value, 1));
ptr = sample;
}
validate(value == 0);
ptr = {};
validate(value == 1);
test_success;
}
Result<void> test_main()
{
test_prelude;
run_test(test_shared_ptr_destroyed_on_scope_exit);
run_test(test_shared_ptr_preserved_if_at_least_one_copy_exists);
return {};
}