diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-05-04 22:57:32 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-05-05 03:31:20 +0200 |
commit | 92d60403f3de094dc2d040a7fce869a5bb60695a (patch) | |
tree | 9f990e482ea22a761af8d2d7a04aae653ccb4204 /test/bptr.cpp | |
parent | b929d0a245219247c81bab7d4d319b5ad9ad58f7 (diff) |
a
Diffstat (limited to 'test/bptr.cpp')
-rw-r--r-- | test/bptr.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/test/bptr.cpp b/test/bptr.cpp index 99d6cd2c..6f823344 100644 --- a/test/bptr.cpp +++ b/test/bptr.cpp @@ -39,8 +39,59 @@ template bptr<Foo> static_pointer_cast(const bptr<Bar>&) noexcept; namespace { +int A_total = 0, A_alive = 0; // NOLINT + +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); } +}; + void test1() { + A_total = 0; A_alive = 0; + + auto p1 = bptr<A>{InPlace, 1}; + fm_assert(p1.use_count() == 1); + fm_assert(p1.get()); + fm_assert(A_total == 1); + fm_assert(A_alive == 1); + + p1 = nullptr; + fm_assert(p1.use_count() == 0); + fm_assert(!p1.get()); + fm_assert(A_total == 1); + fm_assert(A_alive == 0); +} + +void test2() +{ + A_total = 0; A_alive = 0; + + auto p1 = bptr<A>{InPlace, 2}; + auto p2 = p1; + + fm_assert(p1.get()); + fm_assert(p1.get() == p2.get()); + fm_assert(p1->val == 2); + fm_assert(p1->serial == 1); + fm_assert(A_total == 1); + fm_assert(A_alive == 1); + + p1 = nullptr; + fm_assert(!p1.get()); + fm_assert(p2.get()); + fm_assert(p2->val == 2); + fm_assert(p2->serial == 1); + fm_assert(A_total == 1); + fm_assert(A_alive == 1); + + p2 = nullptr; + fm_assert(!p1.get()); + fm_assert(!p2.get()); + fm_assert(A_total == 1); + fm_assert(A_alive == 0); } } // namespace @@ -48,6 +99,7 @@ void test1() void test_app::test_bptr() { test1(); + test2(); } } // namespace floormat |