blob: 3b261d55c96a7c2cd0839ee18df5a869e3434446 [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);
William A. Kennington III1744c162018-07-17 14:40:14 -070031 EXPECT_TRUE(event);
William A. Kennington III0a816c52018-07-17 14:40:14 -070032 EXPECT_EQ(expected_event, event.get());
33
34 EXPECT_CALL(mock, sd_event_unref(expected_event))
35 .WillOnce(testing::Return(nullptr));
36}
37
38TEST_F(SdRefTest, ConstructNoRef)
39{
40 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
41 &SdEvent::sd_event_unref, std::false_type(), &mock);
William A. Kennington III1744c162018-07-17 14:40:14 -070042 EXPECT_TRUE(event);
William A. Kennington III0a816c52018-07-17 14:40:14 -070043 EXPECT_EQ(expected_event, event.get());
44
45 EXPECT_CALL(mock, sd_event_unref(expected_event))
46 .WillOnce(testing::Return(nullptr));
47}
48
49TEST_F(SdRefTest, CopyConstruct)
50{
51 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
52 &SdEvent::sd_event_unref, std::false_type(), &mock);
William A. Kennington III1744c162018-07-17 14:40:14 -070053 EXPECT_TRUE(event);
William A. Kennington III0a816c52018-07-17 14:40:14 -070054 EXPECT_EQ(expected_event, event.get());
55
56 EXPECT_CALL(mock, sd_event_ref(expected_event))
57 .WillOnce(testing::Return(expected_event));
58 SdRef<sd_event> event2(event);
William A. Kennington III1744c162018-07-17 14:40:14 -070059 EXPECT_TRUE(event);
60 EXPECT_EQ(expected_event, event.get());
61 EXPECT_TRUE(event2);
William A. Kennington III0a816c52018-07-17 14:40:14 -070062 EXPECT_EQ(expected_event, event2.get());
63
64 EXPECT_CALL(mock, sd_event_unref(expected_event))
65 .Times(2)
66 .WillRepeatedly(testing::Return(nullptr));
67}
68
69TEST_F(SdRefTest, MoveConstruct)
70{
71 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
72 &SdEvent::sd_event_unref, std::false_type(), &mock);
William A. Kennington III1744c162018-07-17 14:40:14 -070073 EXPECT_TRUE(event);
William A. Kennington III0a816c52018-07-17 14:40:14 -070074 EXPECT_EQ(expected_event, event.get());
75
76 SdRef<sd_event> event2(std::move(event));
William A. Kennington III1744c162018-07-17 14:40:14 -070077 EXPECT_FALSE(event);
78 EXPECT_EQ(nullptr, event.get());
79 EXPECT_TRUE(event2);
William A. Kennington III0a816c52018-07-17 14:40:14 -070080 EXPECT_EQ(expected_event, event2.get());
81
82 EXPECT_CALL(mock, sd_event_unref(expected_event))
83 .WillOnce(testing::Return(nullptr));
84}
85
86TEST_F(SdRefTest, CopyAssignOverValid)
87{
88 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
89 &SdEvent::sd_event_unref, std::false_type(), &mock);
William A. Kennington III1744c162018-07-17 14:40:14 -070090 EXPECT_TRUE(event);
William A. Kennington III0a816c52018-07-17 14:40:14 -070091 EXPECT_EQ(expected_event, event.get());
92 SdRef<sd_event> event2(expected_event2, &SdEvent::sd_event_ref,
93 &SdEvent::sd_event_unref, std::false_type(), &mock2);
William A. Kennington III1744c162018-07-17 14:40:14 -070094 EXPECT_TRUE(event2);
William A. Kennington III0a816c52018-07-17 14:40:14 -070095 EXPECT_EQ(expected_event2, event2.get());
96
97 EXPECT_CALL(mock2, sd_event_unref(expected_event2))
98 .WillOnce(testing::Return(nullptr));
99 EXPECT_CALL(mock, sd_event_ref(expected_event))
100 .WillOnce(testing::Return(expected_event));
101 event2 = event;
William A. Kennington III1744c162018-07-17 14:40:14 -0700102 EXPECT_TRUE(event);
103 EXPECT_EQ(expected_event, event.get());
104 EXPECT_TRUE(event2);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700105 EXPECT_EQ(expected_event, event2.get());
106
107 EXPECT_CALL(mock, sd_event_unref(expected_event))
108 .Times(2)
109 .WillRepeatedly(testing::Return(nullptr));
110}
111
112TEST_F(SdRefTest, CopyAssignOverMoved)
113{
114 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
115 &SdEvent::sd_event_unref, std::false_type(), &mock);
William A. Kennington III1744c162018-07-17 14:40:14 -0700116 EXPECT_TRUE(event);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700117 EXPECT_EQ(expected_event, event.get());
118 SdRef<sd_event> event2(expected_event2, &SdEvent::sd_event_ref,
119 &SdEvent::sd_event_unref, std::false_type(), &mock2);
William A. Kennington III1744c162018-07-17 14:40:14 -0700120 EXPECT_TRUE(event2);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700121 EXPECT_EQ(expected_event2, event2.get());
122 {
123 SdRef<sd_event> event_mover(std::move(event2));
William A. Kennington III1744c162018-07-17 14:40:14 -0700124 EXPECT_FALSE(event2);
125 EXPECT_EQ(nullptr, event2.get());
126 EXPECT_TRUE(event_mover);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700127 EXPECT_EQ(expected_event2, event_mover.get());
128
129 EXPECT_CALL(mock2, sd_event_unref(expected_event2))
130 .WillOnce(testing::Return(nullptr));
131 }
132
133 EXPECT_CALL(mock, sd_event_ref(expected_event))
134 .WillOnce(testing::Return(expected_event));
135 event2 = event;
William A. Kennington III1744c162018-07-17 14:40:14 -0700136 EXPECT_TRUE(event);
137 EXPECT_EQ(expected_event, event.get());
138 EXPECT_TRUE(event2);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700139 EXPECT_EQ(expected_event, event2.get());
140
141 EXPECT_CALL(mock, sd_event_unref(expected_event))
142 .Times(2)
143 .WillRepeatedly(testing::Return(nullptr));
144}
145
146TEST_F(SdRefTest, CopySelf)
147{
148 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
149 &SdEvent::sd_event_unref, std::false_type(), &mock);
William A. Kennington III1744c162018-07-17 14:40:14 -0700150 EXPECT_TRUE(event);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700151 EXPECT_EQ(expected_event, event.get());
152
153 event = event;
William A. Kennington III1744c162018-07-17 14:40:14 -0700154 EXPECT_TRUE(event);
155 EXPECT_EQ(expected_event, event.get());
William A. Kennington III0a816c52018-07-17 14:40:14 -0700156 EXPECT_CALL(mock, sd_event_unref(expected_event))
157 .WillOnce(testing::Return(nullptr));
158}
159
160TEST_F(SdRefTest, MoveAssignOverValid)
161{
162 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
163 &SdEvent::sd_event_unref, std::false_type(), &mock);
William A. Kennington III1744c162018-07-17 14:40:14 -0700164 EXPECT_TRUE(event);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700165 EXPECT_EQ(expected_event, event.get());
166 SdRef<sd_event> event2(expected_event2, &SdEvent::sd_event_ref,
167 &SdEvent::sd_event_unref, std::false_type(), &mock2);
William A. Kennington III1744c162018-07-17 14:40:14 -0700168 EXPECT_TRUE(event2);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700169 EXPECT_EQ(expected_event2, event2.get());
170
171 EXPECT_CALL(mock2, sd_event_unref(expected_event2))
172 .WillOnce(testing::Return(nullptr));
173 event2 = std::move(event);
William A. Kennington III1744c162018-07-17 14:40:14 -0700174 EXPECT_FALSE(event);
175 EXPECT_EQ(nullptr, event.get());
176 EXPECT_TRUE(event2);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700177 EXPECT_EQ(expected_event, event2.get());
178
179 EXPECT_CALL(mock, sd_event_unref(expected_event))
180 .WillOnce(testing::Return(nullptr));
181}
182
183TEST_F(SdRefTest, MoveAssignOverMoved)
184{
185 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
186 &SdEvent::sd_event_unref, std::false_type(), &mock);
187 EXPECT_EQ(expected_event, event.get());
188 SdRef<sd_event> event2(expected_event2, &SdEvent::sd_event_ref,
189 &SdEvent::sd_event_unref, std::false_type(), &mock2);
190 EXPECT_EQ(expected_event2, event2.get());
191 {
192 SdRef<sd_event> event_mover(std::move(event2));
William A. Kennington III1744c162018-07-17 14:40:14 -0700193 EXPECT_FALSE(event2);
194 EXPECT_EQ(nullptr, event2.get());
195 EXPECT_TRUE(event_mover);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700196 EXPECT_EQ(expected_event2, event_mover.get());
197
198 EXPECT_CALL(mock2, sd_event_unref(expected_event2))
199 .WillOnce(testing::Return(nullptr));
200 }
201
202 event2 = std::move(event);
William A. Kennington III1744c162018-07-17 14:40:14 -0700203 EXPECT_FALSE(event);
204 EXPECT_EQ(nullptr, event.get());
205 EXPECT_TRUE(event2);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700206 EXPECT_EQ(expected_event, event2.get());
207
208 EXPECT_CALL(mock, sd_event_unref(expected_event))
209 .WillOnce(testing::Return(nullptr));
210}
211
212TEST_F(SdRefTest, MoveSelf)
213{
214 SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
215 &SdEvent::sd_event_unref, std::false_type(), &mock);
William A. Kennington III1744c162018-07-17 14:40:14 -0700216 EXPECT_TRUE(event);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700217 EXPECT_EQ(expected_event, event.get());
William A. Kennington III1744c162018-07-17 14:40:14 -0700218
William A. Kennington III0a816c52018-07-17 14:40:14 -0700219 event = std::move(event);
William A. Kennington III1744c162018-07-17 14:40:14 -0700220 EXPECT_TRUE(event);
221 EXPECT_EQ(expected_event, event.get());
William A. Kennington III0a816c52018-07-17 14:40:14 -0700222 EXPECT_CALL(mock, sd_event_unref(expected_event))
223 .WillOnce(testing::Return(nullptr));
224}
225
226} // namespace
227} // namespace internal
228} // namespace sdeventplus