62 lines
1.0 KiB
C++
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 {};
|
||
|
}
|