OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_make_rc.cpp
Go to the documentation of this file.
1#include "test_common.hpp"
2
4
5using namespace openvpn;
6
7// Tests the RcEnable and make_rc functionality.
8
9struct test1
10{
11 int i = 0;
12};
13
15
16// The `f_ptr` function checks that the reference-counted object has the expected value.
17void f_ptr(rc_test1::Ptr rct1, int i)
18{
19 EXPECT_EQ(rct1->i, i);
20}
21
22// Ref to base
23void f_ref(test1 &t1, int i)
24{
25 EXPECT_EQ(t1.i, i);
26}
27
28// Sliced value
29void f_val(test1 t1, int i)
30{
31 EXPECT_EQ(t1.i, i);
32}
33
34// The `direct_enable` test verifies that an RcEnable object can be created directly.
35TEST(make_rc, direct_enable)
36{
37 auto rct1 = RcEnable<test1>::Create();
38 EXPECT_EQ(rct1->i, 0);
39}
40
41// The `simple` test verifies that a reference-counted object can be created using `make_rc`.
42TEST(make_rc, simple)
43{
44 auto rct1 = make_rc<test1>();
45 EXPECT_EQ(rct1->i, 0);
46}
47
48// The `move_init` test verifies that a reference-counted object can be created by moving an existing object.
49TEST(make_rc, move_init)
50{
51 auto t = test1();
52 EXPECT_EQ(t.i, 0);
53 t.i = 42;
54 auto rct1 = make_rc<test1>(std::move(t));
55 EXPECT_EQ(rct1->i, 42);
56}
57
58// The `move_init_call` test verifies that a reference-counted object can be created by moving an existing object
59// and passed to a function.
60TEST(make_rc, move_init_call)
61{
62 auto t = test1();
63 EXPECT_EQ(t.i, 0);
64 t.i = 42;
65 f_ptr(make_rc<test1>(std::move(t)), 42);
66}
67
68// Calls using ref
69TEST(make_rc, call_value)
70{
71 auto rct1 = RcEnable<test1>::Create();
72 f_ref(*rct1, 0);
73}
74
75// Calls using sliced value
76TEST(make_rc, call_slice)
77{
78 auto rct1 = RcEnable<test1>::Create();
79 f_val(*rct1, 0);
80}
81
82// make_rc TS
83TEST(make_rc, simple_ts)
84{
85 auto rct1 = make_rc<test1, RC<thread_safe_refcount>>();
86 EXPECT_EQ(rct1->i, 0);
87}
The smart pointer class.
Definition rc.hpp:119
A class template that enables reference counting for a given type.
Definition make_rc.hpp:29
static Ptr Create(ArgsT &&...args)
Creates a new instance of RcEnable with the given arguments.
Definition make_rc.hpp:43
auto make_rc(ArgsT &&...args)
Helper function to create a reference-counted object with the default thread-unsafe reference countin...
Definition make_rc.hpp:77
TEST(make_rc, direct_enable)
void f_val(test1 t1, int i)
void f_ref(test1 &t1, int i)
void f_ptr(rc_test1::Ptr rct1, int i)