William A. Kennington III | 7a5e232 | 2018-11-02 17:28:35 -0700 | [diff] [blame] | 1 | #include <gtest/gtest.h> |
| 2 | #include <optional> |
| 3 | #include <stdplus/handle/managed.hpp> |
| 4 | #include <string> |
| 5 | #include <tuple> |
| 6 | #include <utility> |
| 7 | #include <vector> |
| 8 | |
| 9 | namespace stdplus |
| 10 | { |
| 11 | namespace |
| 12 | { |
| 13 | |
| 14 | static std::vector<int> dropped; |
| 15 | static int stored = 0; |
| 16 | |
William A. Kennington III | 7910ae0 | 2019-11-11 12:13:25 -0800 | [diff] [blame] | 17 | struct SimpleDrop |
| 18 | { |
| 19 | void operator()(int&& i) noexcept |
| 20 | { |
| 21 | dropped.push_back(std::move(i)); |
| 22 | } |
| 23 | }; |
| 24 | |
William A. Kennington III | 7a5e232 | 2018-11-02 17:28:35 -0700 | [diff] [blame] | 25 | void drop(int&& i) |
| 26 | { |
William A. Kennington III | 7910ae0 | 2019-11-11 12:13:25 -0800 | [diff] [blame] | 27 | SimpleDrop()(std::move(i)); |
William A. Kennington III | 7a5e232 | 2018-11-02 17:28:35 -0700 | [diff] [blame] | 28 | } |
| 29 | |
William A. Kennington III | 7910ae0 | 2019-11-11 12:13:25 -0800 | [diff] [blame] | 30 | struct StoreDrop |
William A. Kennington III | 7a5e232 | 2018-11-02 17:28:35 -0700 | [diff] [blame] | 31 | { |
William A. Kennington III | 7910ae0 | 2019-11-11 12:13:25 -0800 | [diff] [blame] | 32 | void operator()(int&& i, std::string&, int& si) |
| 33 | { |
| 34 | dropped.push_back(std::move(i)); |
| 35 | // Make sure we can update the stored data |
| 36 | stored = si++; |
| 37 | } |
| 38 | }; |
William A. Kennington III | 7a5e232 | 2018-11-02 17:28:35 -0700 | [diff] [blame] | 39 | |
William A. Kennington III | 7910ae0 | 2019-11-11 12:13:25 -0800 | [diff] [blame] | 40 | using SimpleHandleOld = Managed<int>::Handle<drop>; |
| 41 | using SimpleHandle = Managed<int>::HandleF<SimpleDrop>; |
| 42 | using StoreHandle = Managed<int, std::string, int>::HandleF<StoreDrop>; |
William A. Kennington III | 7a5e232 | 2018-11-02 17:28:35 -0700 | [diff] [blame] | 43 | |
William A. Kennington III | 1007be9 | 2019-11-11 12:18:37 -0800 | [diff] [blame^] | 44 | static_assert(std::is_nothrow_move_constructible_v<SimpleHandle>); |
| 45 | static_assert(std::is_nothrow_move_assignable_v<SimpleHandle>); |
| 46 | static_assert(std::is_nothrow_destructible_v<SimpleHandle>); |
| 47 | static_assert(noexcept(std::declval<SimpleHandle>().reset())); |
| 48 | // http://cplusplus.github.io/LWG/lwg-active.html#2116 |
| 49 | // static_assert(std::is_nothrow_move_constructible_v<StoreHandle>); |
| 50 | static_assert(!std::is_nothrow_move_assignable_v<StoreHandle>); |
| 51 | static_assert(!std::is_nothrow_destructible_v<StoreHandle>); |
| 52 | static_assert(!noexcept(std::declval<StoreHandle>().reset())); |
| 53 | |
William A. Kennington III | 7a5e232 | 2018-11-02 17:28:35 -0700 | [diff] [blame] | 54 | class ManagedHandleTest : public ::testing::Test |
| 55 | { |
| 56 | protected: |
| 57 | void SetUp() |
| 58 | { |
| 59 | dropped.clear(); |
| 60 | } |
| 61 | |
| 62 | void TearDown() |
| 63 | { |
| 64 | EXPECT_TRUE(dropped.empty()); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | TEST_F(ManagedHandleTest, EmptyNoStorage) |
| 69 | { |
William A. Kennington III | 7910ae0 | 2019-11-11 12:13:25 -0800 | [diff] [blame] | 70 | SimpleHandleOld h(std::nullopt); |
William A. Kennington III | 7a5e232 | 2018-11-02 17:28:35 -0700 | [diff] [blame] | 71 | EXPECT_FALSE(h); |
| 72 | EXPECT_THROW(h.value(), std::bad_optional_access); |
William A. Kennington III | 079cba7 | 2019-07-01 17:28:32 -0700 | [diff] [blame] | 73 | EXPECT_THROW((void)h.release(), std::bad_optional_access); |
William A. Kennington III | 7a5e232 | 2018-11-02 17:28:35 -0700 | [diff] [blame] | 74 | h.reset(); |
| 75 | EXPECT_FALSE(h.has_value()); |
| 76 | EXPECT_THROW(h.value(), std::bad_optional_access); |
William A. Kennington III | 079cba7 | 2019-07-01 17:28:32 -0700 | [diff] [blame] | 77 | EXPECT_THROW((void)h.release(), std::bad_optional_access); |
William A. Kennington III | 7a5e232 | 2018-11-02 17:28:35 -0700 | [diff] [blame] | 78 | EXPECT_EQ(std::nullopt, h.maybe_value()); |
William A. Kennington III | 079cba7 | 2019-07-01 17:28:32 -0700 | [diff] [blame] | 79 | EXPECT_EQ(std::nullopt, h.maybe_release()); |
William A. Kennington III | 7a5e232 | 2018-11-02 17:28:35 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TEST_F(ManagedHandleTest, EmptyWithStorage) |
| 83 | { |
| 84 | StoreHandle h(std::nullopt, "str", 5); |
| 85 | EXPECT_FALSE(h); |
| 86 | EXPECT_THROW(h.value(), std::bad_optional_access); |
| 87 | h.reset(std::nullopt); |
| 88 | EXPECT_FALSE(h); |
| 89 | EXPECT_THROW(h.value(), std::bad_optional_access); |
| 90 | |
| 91 | StoreHandle h2(std::nullopt, std::make_tuple<std::string, int>("str", 5)); |
| 92 | EXPECT_FALSE(h2); |
| 93 | EXPECT_THROW(h2.value(), std::bad_optional_access); |
| 94 | } |
| 95 | |
| 96 | TEST_F(ManagedHandleTest, SimplePopulated) |
| 97 | { |
| 98 | constexpr int expected = 3; |
| 99 | { |
| 100 | int val = expected; |
| 101 | SimpleHandle h(std::move(val)); |
| 102 | EXPECT_TRUE(h.has_value()); |
| 103 | EXPECT_EQ(expected, *h); |
| 104 | EXPECT_EQ(expected, h.value()); |
| 105 | EXPECT_EQ(expected, h.maybe_value()); |
| 106 | EXPECT_TRUE(dropped.empty()); |
| 107 | } |
| 108 | EXPECT_EQ(std::vector{expected}, dropped); |
| 109 | dropped.clear(); |
| 110 | } |
| 111 | |
| 112 | TEST_F(ManagedHandleTest, OptionalPopulated) |
| 113 | { |
| 114 | constexpr int expected = 3; |
| 115 | { |
| 116 | std::optional<int> maybeVal{expected}; |
| 117 | SimpleHandle h(std::move(maybeVal)); |
| 118 | EXPECT_TRUE(h); |
| 119 | EXPECT_EQ(expected, *h); |
| 120 | EXPECT_EQ(expected, h.value()); |
| 121 | EXPECT_TRUE(dropped.empty()); |
| 122 | } |
| 123 | EXPECT_EQ(std::vector{expected}, dropped); |
| 124 | dropped.clear(); |
| 125 | } |
| 126 | |
| 127 | TEST_F(ManagedHandleTest, SimplePopulatedWithStorage) |
| 128 | { |
| 129 | constexpr int expected = 3; |
| 130 | { |
| 131 | StoreHandle h(int{expected}, std::make_tuple(std::string{"str"}, 5)); |
| 132 | EXPECT_TRUE(h); |
| 133 | EXPECT_EQ(expected, *h); |
| 134 | EXPECT_EQ(expected, h.value()); |
| 135 | EXPECT_TRUE(dropped.empty()); |
| 136 | } |
| 137 | EXPECT_EQ(5, stored); |
| 138 | EXPECT_EQ(std::vector{expected}, dropped); |
| 139 | dropped.clear(); |
| 140 | } |
| 141 | |
| 142 | TEST_F(ManagedHandleTest, ResetPopulatedWithStorage) |
| 143 | { |
| 144 | constexpr int expected = 3; |
| 145 | const std::string s{"str"}; |
| 146 | StoreHandle h(int{expected}, s, 5); |
| 147 | EXPECT_TRUE(dropped.empty()); |
| 148 | h.reset(std::nullopt); |
| 149 | EXPECT_FALSE(h); |
| 150 | EXPECT_THROW(h.value(), std::bad_optional_access); |
| 151 | EXPECT_EQ(5, stored); |
| 152 | EXPECT_EQ(std::vector{expected}, dropped); |
| 153 | dropped.clear(); |
| 154 | } |
| 155 | |
| 156 | TEST_F(ManagedHandleTest, ResetNewPopulated) |
| 157 | { |
| 158 | constexpr int expected = 3, expected2 = 10; |
| 159 | { |
| 160 | SimpleHandle h(int{expected}); |
| 161 | EXPECT_TRUE(dropped.empty()); |
| 162 | h.reset(int{expected2}); |
| 163 | EXPECT_TRUE(h); |
| 164 | EXPECT_EQ(expected2, *h); |
| 165 | EXPECT_EQ(expected2, h.value()); |
| 166 | EXPECT_EQ(std::vector{expected}, dropped); |
| 167 | dropped.clear(); |
| 168 | } |
| 169 | EXPECT_EQ(std::vector{expected2}, dropped); |
| 170 | dropped.clear(); |
| 171 | } |
| 172 | |
| 173 | TEST_F(ManagedHandleTest, ResetNewPopulatedWithStorage) |
| 174 | { |
| 175 | constexpr int expected = 3, expected2 = 10; |
| 176 | { |
| 177 | StoreHandle h(int{expected}, "str", 5); |
| 178 | EXPECT_TRUE(dropped.empty()); |
| 179 | h.reset(int{expected2}); |
| 180 | EXPECT_TRUE(h); |
| 181 | EXPECT_EQ(expected2, *h); |
| 182 | EXPECT_EQ(expected2, h.value()); |
| 183 | EXPECT_EQ(5, stored); |
| 184 | EXPECT_EQ(std::vector{expected}, dropped); |
| 185 | dropped.clear(); |
| 186 | } |
| 187 | EXPECT_EQ(6, stored); |
| 188 | EXPECT_EQ(std::vector{expected2}, dropped); |
| 189 | dropped.clear(); |
| 190 | } |
| 191 | |
William A. Kennington III | 079cba7 | 2019-07-01 17:28:32 -0700 | [diff] [blame] | 192 | TEST_F(ManagedHandleTest, Release) |
| 193 | { |
| 194 | constexpr int expected = 3; |
| 195 | int val = expected; |
| 196 | SimpleHandle h(std::move(val)); |
| 197 | EXPECT_EQ(expected, h.release()); |
| 198 | EXPECT_FALSE(h); |
| 199 | } |
| 200 | |
| 201 | TEST_F(ManagedHandleTest, MaybeRelease) |
| 202 | { |
| 203 | constexpr int expected = 3; |
| 204 | int val = expected; |
| 205 | SimpleHandle h(std::move(val)); |
| 206 | EXPECT_EQ(expected, h.maybe_release()); |
| 207 | EXPECT_FALSE(h); |
| 208 | } |
| 209 | |
William A. Kennington III | 7a5e232 | 2018-11-02 17:28:35 -0700 | [diff] [blame] | 210 | TEST_F(ManagedHandleTest, MoveConstructWithStorage) |
| 211 | { |
| 212 | constexpr int expected = 3; |
| 213 | StoreHandle h1(int{expected}, "str", 5); |
| 214 | { |
| 215 | StoreHandle h2(std::move(h1)); |
| 216 | EXPECT_TRUE(dropped.empty()); |
| 217 | EXPECT_FALSE(h1); |
| 218 | EXPECT_THROW(h1.value(), std::bad_optional_access); |
| 219 | EXPECT_TRUE(h2); |
| 220 | EXPECT_EQ(expected, *h2); |
| 221 | EXPECT_EQ(expected, h2.value()); |
| 222 | } |
| 223 | EXPECT_EQ(5, stored); |
| 224 | EXPECT_EQ(std::vector{expected}, dropped); |
| 225 | dropped.clear(); |
| 226 | } |
| 227 | |
| 228 | TEST_F(ManagedHandleTest, MoveAssignWithStorage) |
| 229 | { |
| 230 | constexpr int expected = 3, expected2 = 10; |
| 231 | { |
| 232 | StoreHandle h1(int{expected}, "str", 5); |
| 233 | StoreHandle h2(int{expected2}, "str", 10); |
| 234 | EXPECT_TRUE(dropped.empty()); |
| 235 | |
| 236 | h2 = std::move(h1); |
| 237 | EXPECT_EQ(10, stored); |
| 238 | EXPECT_EQ(std::vector{expected2}, dropped); |
| 239 | dropped.clear(); |
| 240 | EXPECT_FALSE(h1); |
| 241 | EXPECT_THROW(h1.value(), std::bad_optional_access); |
| 242 | EXPECT_TRUE(h2); |
| 243 | EXPECT_EQ(expected, *h2); |
| 244 | EXPECT_EQ(expected, h2.value()); |
| 245 | } |
| 246 | EXPECT_EQ(5, stored); |
| 247 | EXPECT_EQ(std::vector{expected}, dropped); |
| 248 | dropped.clear(); |
| 249 | } |
| 250 | |
| 251 | } // namespace |
| 252 | } // namespace stdplus |