William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 1 | #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 | |
| 9 | namespace sdeventplus |
| 10 | { |
| 11 | namespace internal |
| 12 | { |
| 13 | namespace |
| 14 | { |
| 15 | |
| 16 | class 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 | |
| 25 | TEST_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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 31 | EXPECT_TRUE(event); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 32 | EXPECT_EQ(expected_event, event.get()); |
| 33 | |
| 34 | EXPECT_CALL(mock, sd_event_unref(expected_event)) |
| 35 | .WillOnce(testing::Return(nullptr)); |
| 36 | } |
| 37 | |
| 38 | TEST_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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 42 | EXPECT_TRUE(event); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 43 | EXPECT_EQ(expected_event, event.get()); |
| 44 | |
| 45 | EXPECT_CALL(mock, sd_event_unref(expected_event)) |
| 46 | .WillOnce(testing::Return(nullptr)); |
| 47 | } |
| 48 | |
| 49 | TEST_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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 53 | EXPECT_TRUE(event); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 54 | 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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 59 | EXPECT_TRUE(event); |
| 60 | EXPECT_EQ(expected_event, event.get()); |
| 61 | EXPECT_TRUE(event2); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 62 | 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 | |
| 69 | TEST_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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 73 | EXPECT_TRUE(event); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 74 | EXPECT_EQ(expected_event, event.get()); |
| 75 | |
| 76 | SdRef<sd_event> event2(std::move(event)); |
William A. Kennington III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 77 | EXPECT_FALSE(event); |
| 78 | EXPECT_EQ(nullptr, event.get()); |
| 79 | EXPECT_TRUE(event2); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 80 | EXPECT_EQ(expected_event, event2.get()); |
| 81 | |
| 82 | EXPECT_CALL(mock, sd_event_unref(expected_event)) |
| 83 | .WillOnce(testing::Return(nullptr)); |
| 84 | } |
| 85 | |
| 86 | TEST_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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 90 | EXPECT_TRUE(event); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 91 | 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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 94 | EXPECT_TRUE(event2); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 95 | 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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 102 | EXPECT_TRUE(event); |
| 103 | EXPECT_EQ(expected_event, event.get()); |
| 104 | EXPECT_TRUE(event2); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 105 | 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 | |
| 112 | TEST_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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 116 | EXPECT_TRUE(event); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 117 | 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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 120 | EXPECT_TRUE(event2); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 121 | EXPECT_EQ(expected_event2, event2.get()); |
| 122 | { |
| 123 | SdRef<sd_event> event_mover(std::move(event2)); |
William A. Kennington III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 124 | EXPECT_FALSE(event2); |
| 125 | EXPECT_EQ(nullptr, event2.get()); |
| 126 | EXPECT_TRUE(event_mover); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 127 | 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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 136 | EXPECT_TRUE(event); |
| 137 | EXPECT_EQ(expected_event, event.get()); |
| 138 | EXPECT_TRUE(event2); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 139 | 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 | |
| 146 | TEST_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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 150 | EXPECT_TRUE(event); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 151 | EXPECT_EQ(expected_event, event.get()); |
| 152 | |
| 153 | event = event; |
William A. Kennington III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 154 | EXPECT_TRUE(event); |
| 155 | EXPECT_EQ(expected_event, event.get()); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 156 | EXPECT_CALL(mock, sd_event_unref(expected_event)) |
| 157 | .WillOnce(testing::Return(nullptr)); |
| 158 | } |
| 159 | |
| 160 | TEST_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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 164 | EXPECT_TRUE(event); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 165 | 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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 168 | EXPECT_TRUE(event2); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 169 | 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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 174 | EXPECT_FALSE(event); |
| 175 | EXPECT_EQ(nullptr, event.get()); |
| 176 | EXPECT_TRUE(event2); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 177 | EXPECT_EQ(expected_event, event2.get()); |
| 178 | |
| 179 | EXPECT_CALL(mock, sd_event_unref(expected_event)) |
| 180 | .WillOnce(testing::Return(nullptr)); |
| 181 | } |
| 182 | |
| 183 | TEST_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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 193 | EXPECT_FALSE(event2); |
| 194 | EXPECT_EQ(nullptr, event2.get()); |
| 195 | EXPECT_TRUE(event_mover); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 196 | 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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 203 | EXPECT_FALSE(event); |
| 204 | EXPECT_EQ(nullptr, event.get()); |
| 205 | EXPECT_TRUE(event2); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 206 | EXPECT_EQ(expected_event, event2.get()); |
| 207 | |
| 208 | EXPECT_CALL(mock, sd_event_unref(expected_event)) |
| 209 | .WillOnce(testing::Return(nullptr)); |
| 210 | } |
| 211 | |
| 212 | TEST_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 III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 216 | EXPECT_TRUE(event); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 217 | EXPECT_EQ(expected_event, event.get()); |
William A. Kennington III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 218 | |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 219 | event = std::move(event); |
William A. Kennington III | 1744c16 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 220 | EXPECT_TRUE(event); |
| 221 | EXPECT_EQ(expected_event, event.get()); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 222 | EXPECT_CALL(mock, sd_event_unref(expected_event)) |
| 223 | .WillOnce(testing::Return(nullptr)); |
| 224 | } |
| 225 | |
| 226 | } // namespace |
| 227 | } // namespace internal |
| 228 | } // namespace sdeventplus |