hyprutils/tests/memory.cpp

41 lines
904 B
C++
Raw Permalink Normal View History

2024-06-08 19:37:15 +02:00
#include <hyprutils/memory/WeakPtr.hpp>
#include "shared.hpp"
2024-09-25 23:44:05 +02:00
#include <vector>
2024-06-08 19:37:15 +02:00
using namespace Hyprutils::Memory;
#define SP CSharedPointer
#define WP CWeakPointer
int main(int argc, char** argv, char** envp) {
2024-09-25 23:44:05 +02:00
SP<int> intPtr = makeShared<int>(10);
SP<int> intPtr2 = makeShared<int>(1337);
2024-06-08 19:37:15 +02:00
2024-09-25 23:44:05 +02:00
int ret = 0;
2024-06-08 19:37:15 +02:00
EXPECT(*intPtr, 10);
EXPECT(intPtr.strongRef(), 1);
WP<int> weak = intPtr;
EXPECT(*intPtr, 10);
EXPECT(intPtr.strongRef(), 1);
EXPECT(*weak.get(), 10);
EXPECT(weak.expired(), false);
2024-09-25 23:44:05 +02:00
std::vector<SP<int>> sps;
sps.push_back(intPtr);
sps.emplace_back(intPtr);
sps.push_back(intPtr2);
sps.emplace_back(intPtr2);
std::erase_if(sps, [intPtr](const auto& e) { return e == intPtr; });
intPtr.reset();
EXPECT(weak.impl_->ref(), 0);
EXPECT(intPtr2.strongRef(), 3);
2024-06-08 19:37:15 +02:00
EXPECT(weak.expired(), true);
return ret;
}