| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "SharedObject.hpp" | ||
| 2 | #include "Verbose.hpp" | ||
| 3 | #include "compat/gsl14.hpp" | ||
| 4 | #include <gtest/gtest.h> | ||
| 5 | |||
| 6 | // Derived publicly (but indirectly) from std::enable_shared_from_this | ||
| 7 | class Widget : public Verbose<SharedObject> { | ||
| 8 | public: | ||
| 9 | 36 | ~Widget() noexcept override = default; | |
| 10 | 2 | Widget& operator=(const Widget& other) = default; | |
| 11 | 2 | Widget& operator=(Widget&& other) noexcept = default; | |
| 12 | |||
| 13 | public: // pseudo-protected | ||
| 14 | 4 | explicit Widget(Protected) noexcept : Widget{} {} | |
| 15 | 1 | Widget(Protected, const Widget& other) : Widget{other} {} | |
| 16 | 2 | Widget(Protected, Widget&& other) noexcept : Widget{std::move(other)} {} | |
| 17 |
2/4✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
|
10 | Widget(Protected, const std::string& name) : Verbose<SharedObject>{name} {} |
| 18 | |||
| 19 | protected: | ||
| 20 | 12 | Widget() noexcept : Verbose<SharedObject>{gsl::czstring{__func__}} {} | |
| 21 | 2 | Widget(const Widget& other) = default; | |
| 22 | 2 | Widget(Widget&& other) noexcept = default; | |
| 23 | }; | ||
| 24 | |||
| 25 | // Derived publicly (but indirectly) from std::enable_shared_from_this | ||
| 26 | class Gadget : public Widget { | ||
| 27 | public: | ||
| 28 | 18 | ~Gadget() noexcept override = default; | |
| 29 | 1 | Gadget& operator=(const Gadget& other) = default; | |
| 30 | 1 | Gadget& operator=(Gadget&& other) noexcept = default; | |
| 31 | |||
| 32 | public: // pseudo-protected | ||
| 33 | 4 | explicit Gadget(Protected) noexcept : Gadget{} {} | |
| 34 | 1 | Gadget(Protected, const Gadget& other) : Gadget{other} {} | |
| 35 | 2 | Gadget(Protected, Gadget&& other) noexcept : Gadget{std::move(other)} {} | |
| 36 | 3 | Gadget(Protected tag, const std::string& name) : Widget{tag, name} {} | |
| 37 | |||
| 38 | protected: | ||
| 39 | 12 | Gadget() noexcept : Widget{Protected{}, gsl::czstring{__func__}} {} | |
| 40 | 1 | Gadget(const Gadget& other) = default; | |
| 41 | 1 | Gadget(Gadget&& other) noexcept = default; | |
| 42 | }; | ||
| 43 | |||
| 44 | 2 | void share_const(const Widget& w) { | |
| 45 |
1/2✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | auto p = w.shared_from_this(); |
| 46 |
3/6✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
|
2 | std::cout << gsl::czstring{__func__} << ": use_count=" << p.use_count() |
| 47 |
1/2✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | << std::endl; |
| 48 | 4 | } | |
| 49 | |||
| 50 | 2 | void share(Widget& w) { | |
| 51 |
1/2✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | auto p = w.shared_from_this(); |
| 52 |
3/6✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
|
2 | std::cout << gsl::czstring{__func__} << ": use_count=" << p.use_count() |
| 53 |
1/2✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | << std::endl; |
| 54 | 4 | } | |
| 55 | |||
| 56 | 8 | TEST(SharedFromThis, create) { | |
| 57 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto w1 = create<Widget>(); |
| 58 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto g2 = create<Gadget>(); |
| 59 | 4 | } | |
| 60 | |||
| 61 | 8 | TEST(SharedFromThis, copy) { | |
| 62 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto w1 = create<Widget>(); |
| 63 |
1/2✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
2 | auto w2 = create<Widget>(*w1); // copy construction |
| 64 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto w3 = create<Widget>("Widget three"); |
| 65 |
1/2✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
|
2 | *w3 = *w1; // copy assignment |
| 66 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto g1 = create<Gadget>(); |
| 67 |
1/2✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
2 | auto g2 = create<Gadget>(*g1); // copy construction |
| 68 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto g3 = create<Gadget>("Gadget three"); |
| 69 |
1/2✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
|
2 | *g3 = *g1; // copy assignment |
| 70 | 4 | } | |
| 71 | |||
| 72 | 8 | TEST(SharedFromThis, move) { | |
| 73 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto w1 = create<Widget>(); |
| 74 |
1/2✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
4 | auto w2 = create<Widget>(std::move(*w1)); // move construction |
| 75 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto w3 = create<Widget>("Widget three"); |
| 76 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto w4 = create<Widget>("Widget four"); |
| 77 | 4 | *w4 = std::move(*w3); // move assignment | |
| 78 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto g1 = create<Gadget>(); |
| 79 |
1/2✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
4 | auto g2 = create<Gadget>(std::move(*g1)); // move construction |
| 80 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto g3 = create<Gadget>("Gadget three"); |
| 81 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto g4 = create<Gadget>("Gadget four"); |
| 82 | 4 | *g4 = std::move(*g3); // move assignment | |
| 83 | 4 | } | |
| 84 | |||
| 85 | 8 | TEST(SharedFromThis, share) { | |
| 86 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto w1 = create<Widget>(); |
| 87 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
2 | share(*w1); |
| 88 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
2 | share_const(*w1); |
| 89 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto g1 = create<Gadget>(); |
| 90 |
1/2✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
2 | share(*g1); |
| 91 |
1/2✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
2 | share_const(*g1); |
| 92 | 4 | } | |
| 93 |