blob: 190d388c88a78dbd27572d601e358c201dbed3e0 [file] [log] [blame]
William A. Kennington III0a816c52018-07-17 14:40:14 -07001#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3#include <sdeventplus/internal/sdref.hpp>
4#include <sdeventplus/test/sdevent.hpp>
5#include <systemd/sd-event.h>
6#include <type_traits>
7#include <utility>
8
9namespace sdeventplus
10{
11namespace internal
12{
13namespace
14{
15
16class SdRefTest : public testing::Test
17{
18 protected:
19 sd_event* const expected_event = reinterpret_cast<sd_event*>(1234);
20 sd_event* const expected_event2 = reinterpret_cast<sd_event*>(2345);
21 testing::StrictMock<test::SdEventMock> mock;
22 testing::StrictMock<test::SdEventMock> mock2;
23};
24
25TEST_F(SdRefTest, ConstructRef)
26{
27 EXPECT_CALL(mock, sd_event_ref(expected_event))
28 .WillOnce(testing::Return(expected_event));
29 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
30 &SdEvent::sd_event_unref, &mock);
31 EXPECT_EQ(expected_event, event.get());
32
33 EXPECT_CALL(mock, sd_event_unref(expected_event))
34 .WillOnce(testing::Return(nullptr));
35}
36
37TEST_F(SdRefTest, ConstructNoRef)
38{
39 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
40 &SdEvent::sd_event_unref, std::false_type(), &mock);
41 EXPECT_EQ(expected_event, event.get());
42
43 EXPECT_CALL(mock, sd_event_unref(expected_event))
44 .WillOnce(testing::Return(nullptr));
45}
46
47TEST_F(SdRefTest, CopyConstruct)
48{
49 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
50 &SdEvent::sd_event_unref, std::false_type(), &mock);
51 EXPECT_EQ(expected_event, event.get());
52
53 EXPECT_CALL(mock, sd_event_ref(expected_event))
54 .WillOnce(testing::Return(expected_event));
55 SdRef<sd_event> event2(event);
56 EXPECT_EQ(expected_event, event2.get());
57
58 EXPECT_CALL(mock, sd_event_unref(expected_event))
59 .Times(2)
60 .WillRepeatedly(testing::Return(nullptr));
61}
62
63TEST_F(SdRefTest, MoveConstruct)
64{
65 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
66 &SdEvent::sd_event_unref, std::false_type(), &mock);
67 EXPECT_EQ(expected_event, event.get());
68
69 SdRef<sd_event> event2(std::move(event));
70 EXPECT_EQ(expected_event, event2.get());
71
72 EXPECT_CALL(mock, sd_event_unref(expected_event))
73 .WillOnce(testing::Return(nullptr));
74}
75
76TEST_F(SdRefTest, CopyAssignOverValid)
77{
78 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
79 &SdEvent::sd_event_unref, std::false_type(), &mock);
80 EXPECT_EQ(expected_event, event.get());
81 SdRef<sd_event> event2(expected_event2, &SdEvent::sd_event_ref,
82 &SdEvent::sd_event_unref, std::false_type(), &mock2);
83 EXPECT_EQ(expected_event2, event2.get());
84
85 EXPECT_CALL(mock2, sd_event_unref(expected_event2))
86 .WillOnce(testing::Return(nullptr));
87 EXPECT_CALL(mock, sd_event_ref(expected_event))
88 .WillOnce(testing::Return(expected_event));
89 event2 = event;
90 EXPECT_EQ(expected_event, event2.get());
91
92 EXPECT_CALL(mock, sd_event_unref(expected_event))
93 .Times(2)
94 .WillRepeatedly(testing::Return(nullptr));
95}
96
97TEST_F(SdRefTest, CopyAssignOverMoved)
98{
99 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
100 &SdEvent::sd_event_unref, std::false_type(), &mock);
101 EXPECT_EQ(expected_event, event.get());
102 SdRef<sd_event> event2(expected_event2, &SdEvent::sd_event_ref,
103 &SdEvent::sd_event_unref, std::false_type(), &mock2);
104 EXPECT_EQ(expected_event2, event2.get());
105 {
106 SdRef<sd_event> event_mover(std::move(event2));
107 EXPECT_EQ(expected_event2, event_mover.get());
108
109 EXPECT_CALL(mock2, sd_event_unref(expected_event2))
110 .WillOnce(testing::Return(nullptr));
111 }
112
113 EXPECT_CALL(mock, sd_event_ref(expected_event))
114 .WillOnce(testing::Return(expected_event));
115 event2 = event;
116 EXPECT_EQ(expected_event, event2.get());
117
118 EXPECT_CALL(mock, sd_event_unref(expected_event))
119 .Times(2)
120 .WillRepeatedly(testing::Return(nullptr));
121}
122
123TEST_F(SdRefTest, CopySelf)
124{
125 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
126 &SdEvent::sd_event_unref, std::false_type(), &mock);
127 EXPECT_EQ(expected_event, event.get());
128
129 event = event;
130 EXPECT_CALL(mock, sd_event_unref(expected_event))
131 .WillOnce(testing::Return(nullptr));
132}
133
134TEST_F(SdRefTest, MoveAssignOverValid)
135{
136 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
137 &SdEvent::sd_event_unref, std::false_type(), &mock);
138 EXPECT_EQ(expected_event, event.get());
139 SdRef<sd_event> event2(expected_event2, &SdEvent::sd_event_ref,
140 &SdEvent::sd_event_unref, std::false_type(), &mock2);
141 EXPECT_EQ(expected_event2, event2.get());
142
143 EXPECT_CALL(mock2, sd_event_unref(expected_event2))
144 .WillOnce(testing::Return(nullptr));
145 event2 = std::move(event);
146 EXPECT_EQ(expected_event, event2.get());
147
148 EXPECT_CALL(mock, sd_event_unref(expected_event))
149 .WillOnce(testing::Return(nullptr));
150}
151
152TEST_F(SdRefTest, MoveAssignOverMoved)
153{
154 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
155 &SdEvent::sd_event_unref, std::false_type(), &mock);
156 EXPECT_EQ(expected_event, event.get());
157 SdRef<sd_event> event2(expected_event2, &SdEvent::sd_event_ref,
158 &SdEvent::sd_event_unref, std::false_type(), &mock2);
159 EXPECT_EQ(expected_event2, event2.get());
160 {
161 SdRef<sd_event> event_mover(std::move(event2));
162 EXPECT_EQ(expected_event2, event_mover.get());
163
164 EXPECT_CALL(mock2, sd_event_unref(expected_event2))
165 .WillOnce(testing::Return(nullptr));
166 }
167
168 event2 = std::move(event);
169 EXPECT_EQ(expected_event, event2.get());
170
171 EXPECT_CALL(mock, sd_event_unref(expected_event))
172 .WillOnce(testing::Return(nullptr));
173}
174
175TEST_F(SdRefTest, MoveSelf)
176{
177 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
178 &SdEvent::sd_event_unref, std::false_type(), &mock);
179 EXPECT_EQ(expected_event, event.get());
180 event = std::move(event);
181 EXPECT_CALL(mock, sd_event_unref(expected_event))
182 .WillOnce(testing::Return(nullptr));
183}
184
185} // namespace
186} // namespace internal
187} // namespace sdeventplus