hyprutils/tests/memory.cpp

56 lines
1.4 KiB
C++
Raw Normal View History

2024-06-08 19:37:15 +02:00
#include <hyprutils/memory/WeakPtr.hpp>
#include <hyprutils/memory/UniquePtr.hpp>
2024-06-08 19:37:15 +02:00
#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
#define UP CUniquePointer
2024-06-08 19:37:15 +02:00
int main(int argc, char** argv, char** envp) {
SP<int> intPtr = makeShared<int>(10);
SP<int> intPtr2 = makeShared<int>(1337);
UP<int> intUnique = makeUnique<int>(420);
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);
EXPECT(*intUnique, 420);
2024-06-08 19:37:15 +02:00
WP<int> weak = intPtr;
WP<int> weakUnique = intUnique;
2024-06-08 19:37:15 +02:00
EXPECT(*intPtr, 10);
EXPECT(intPtr.strongRef(), 1);
EXPECT(*weak, 10);
2024-06-08 19:37:15 +02:00
EXPECT(weak.expired(), false);
EXPECT(*weakUnique, 420);
EXPECT(weakUnique.expired(), false);
EXPECT(intUnique.impl_->wref(), 1);
SP<int> sharedFromUnique = weakUnique.lock();
EXPECT(sharedFromUnique, nullptr);
2024-06-08 19:37:15 +02:00
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();
intUnique.reset();
2024-09-25 23:44:05 +02:00
EXPECT(weak.impl_->ref(), 0);
EXPECT(weakUnique.impl_->ref(), 0);
EXPECT(weakUnique.impl_->wref(), 1);
2024-09-25 23:44:05 +02:00
EXPECT(intPtr2.strongRef(), 3);
2024-06-08 19:37:15 +02:00
EXPECT(weak.expired(), true);
EXPECT(weakUnique.expired(), true);
2024-06-08 19:37:15 +02:00
return ret;
}