summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-05-05 01:38:07 +0200
committerStanislaw Halik <sthalik@misaki.pl>2024-05-05 03:31:21 +0200
commit3f503fd32e456926f271b7d4bc485c91adc7b716 (patch)
treeed7d2f4c501f69fe3535181eb131d69abbeee651
parent965e81a6bfc1d91041d2af32bf459a76d8c42fc8 (diff)
-rw-r--r--compat/borrowed-ptr.inl3
-rw-r--r--test/bptr.cpp59
2 files changed, 60 insertions, 2 deletions
diff --git a/compat/borrowed-ptr.inl b/compat/borrowed-ptr.inl
index 05724fcd..a29e6b1c 100644
--- a/compat/borrowed-ptr.inl
+++ b/compat/borrowed-ptr.inl
@@ -215,6 +215,7 @@ template<typename T> T* bptr<T>::get() const noexcept
else
return nullptr;
}
+
template<typename T> T* bptr<T>::operator->() const noexcept
{
auto* ret = get();
@@ -236,7 +237,7 @@ void bptr<T>::swap(bptr& other) noexcept
template<typename T> uint32_t bptr<T>::use_count() const noexcept
{
- if (blk) [[likely]]
+ if (blk && blk->_ptr) [[likely]]
return blk->_count;
else
return 0;
diff --git a/test/bptr.cpp b/test/bptr.cpp
index 55b10fe0..6050c4f5 100644
--- a/test/bptr.cpp
+++ b/test/bptr.cpp
@@ -1,6 +1,7 @@
#include "app.hpp"
-#include "compat/assert.hpp"
#include "compat/borrowed-ptr.inl"
+#include "compat/assert.hpp"
+#include "compat/defs.hpp"
#include <array>
namespace floormat {
@@ -47,6 +48,8 @@ struct A
int val, serial;
explicit A(int val) : val{val}, serial{++A_total} { ++A_alive; }
~A() noexcept { --A_alive; fm_assert(A_alive >= 0); }
+
+ fm_DECLARE_DELETED_COPY_MOVE_ASSIGNMENTS(A);
};
void test1()
@@ -277,10 +280,62 @@ void test7()
check_empty(p1);
check_empty(p2);
fm_assert(A_total == 1 && A_alive == 0);
+}
+void test8()
+{
A_total = 0; A_alive = 0;
+
+ auto p1 = bptr<A>{InPlace, 81};
+ auto p2 = bptr<A>{InPlace, 82};
+ fm_assert(A_total == 2 && A_alive == 2);
+
+ p1 = p2;
+ fm_assert(A_total == 2 && A_alive == 1);
+
+ p2 = move(p1);
+ fm_assert(A_total == 2 && A_alive == 1);
+
+ p1.reset();
+ p2 = nullptr;
+ (void)p2;
+ fm_assert(A_total == 2 && A_alive == 0);
}
+void test9()
+{
+ A_total = 0; A_alive = 0;
+
+ auto p1 = bptr<A>{InPlace, 9};
+ auto p2 = p1;
+ p1 = p2;
+ fm_assert(p1.use_count() == 2);
+ fm_assert(A_total == 1);
+ fm_assert(A_alive == 1);
+
+ p1.destroy();
+ fm_assert(!p1);
+ fm_assert(!p2);
+ fm_assert(p1.use_count() == 0);
+ fm_assert(p2.use_count() == 0);
+ fm_assert(A_total == 1);
+ fm_assert(A_alive == 0);
+
+ p1.swap(p2);
+ fm_assert(!p1 && !p2);
+ fm_assert(p1.use_count() == 0 && p2.use_count() == 0);
+ fm_assert(A_total == 1);
+ fm_assert(A_alive == 0);
+
+ p1.reset();
+ fm_assert(p1.use_count() == 0);
+ fm_assert(p2.use_count() == 0);
+ p2 = bptr<A>{(A*)nullptr};
+ fm_assert(p1.use_count() == 0);
+ fm_assert(p2.use_count() == 0);
+ fm_assert(A_total == 1 && A_alive == 0);
+};
+
} // namespace
void test_app::test_bptr()
@@ -292,6 +347,8 @@ void test_app::test_bptr()
test5();
test6();
test7();
+ test8();
+ test9();
}
} // namespace floormat