|
25 | 25 |
|
26 | 26 | #include "config.h"
|
27 | 27 |
|
28 |
| -#include "Test.h" |
| 28 | +#include "MoveOnly.h" |
29 | 29 | #include <wtf/Function.h>
|
30 | 30 |
|
31 | 31 | namespace TestWebKitAPI {
|
@@ -137,4 +137,107 @@ TEST(WTF_Function, assignNullReEntersAssignLamda)
|
137 | 137 | EXPECT_EQ(-1, function_for_reentrancy_test());
|
138 | 138 | }
|
139 | 139 |
|
| 140 | +TEST(WTF_Function, Basics) |
| 141 | +{ |
| 142 | + Function<unsigned()> a; |
| 143 | + EXPECT_FALSE(static_cast<bool>(a)); |
| 144 | + EXPECT_EQ(0U, a()); |
| 145 | + |
| 146 | + a = [] { |
| 147 | + return 1U; |
| 148 | + }; |
| 149 | + EXPECT_TRUE(static_cast<bool>(a)); |
| 150 | + EXPECT_EQ(1U, a()); |
| 151 | + |
| 152 | + a = nullptr; |
| 153 | + EXPECT_FALSE(static_cast<bool>(a)); |
| 154 | + EXPECT_EQ(0U, a()); |
| 155 | + |
| 156 | + a = MoveOnly { 2 }; |
| 157 | + EXPECT_TRUE(static_cast<bool>(a)); |
| 158 | + EXPECT_EQ(2U, a()); |
| 159 | + |
| 160 | + Function<unsigned()> b = WTFMove(a); |
| 161 | + EXPECT_TRUE(static_cast<bool>(b)); |
| 162 | + EXPECT_EQ(2U, b()); |
| 163 | + EXPECT_FALSE(static_cast<bool>(a)); |
| 164 | + EXPECT_EQ(0U, a()); |
| 165 | + |
| 166 | + a = MoveOnly { 3 }; |
| 167 | + Function<unsigned()> c = WTFMove(a); |
| 168 | + EXPECT_TRUE(static_cast<bool>(c)); |
| 169 | + EXPECT_EQ(3U, c()); |
| 170 | + EXPECT_FALSE(static_cast<bool>(a)); |
| 171 | + EXPECT_EQ(0U, a()); |
| 172 | + |
| 173 | + b = WTFMove(c); |
| 174 | + EXPECT_TRUE(static_cast<bool>(b)); |
| 175 | + EXPECT_EQ(3U, b()); |
| 176 | + EXPECT_FALSE(static_cast<bool>(c)); |
| 177 | + EXPECT_EQ(0U, c()); |
| 178 | + |
| 179 | + Function<unsigned()> d = nullptr; |
| 180 | + EXPECT_FALSE(static_cast<bool>(d)); |
| 181 | + EXPECT_EQ(0U, d()); |
| 182 | +} |
| 183 | + |
| 184 | +struct FunctionDestructionChecker { |
| 185 | + FunctionDestructionChecker(Function<unsigned()>& function) |
| 186 | + : function { function } |
| 187 | + { |
| 188 | + } |
| 189 | + |
| 190 | + ~FunctionDestructionChecker() |
| 191 | + { |
| 192 | + functionAsBool = static_cast<bool>(function); |
| 193 | + functionResult = function(); |
| 194 | + } |
| 195 | + |
| 196 | + unsigned operator()() const |
| 197 | + { |
| 198 | + return 10; |
| 199 | + } |
| 200 | + |
| 201 | + Function<unsigned()>& function; |
| 202 | + static std::optional<bool> functionAsBool; |
| 203 | + static std::optional<unsigned> functionResult; |
| 204 | +}; |
| 205 | + |
| 206 | +std::optional<bool> FunctionDestructionChecker::functionAsBool; |
| 207 | +std::optional<unsigned> FunctionDestructionChecker::functionResult; |
| 208 | + |
| 209 | +TEST(WTF_Function, AssignBeforeDestroy) |
| 210 | +{ |
| 211 | + Function<unsigned()> a; |
| 212 | + |
| 213 | + a = FunctionDestructionChecker(a); |
| 214 | + a = [] { |
| 215 | + return 1U; |
| 216 | + }; |
| 217 | + EXPECT_TRUE(static_cast<bool>(FunctionDestructionChecker::functionAsBool)); |
| 218 | + EXPECT_TRUE(static_cast<bool>(FunctionDestructionChecker::functionResult)); |
| 219 | + EXPECT_TRUE(FunctionDestructionChecker::functionAsBool.value()); |
| 220 | + EXPECT_EQ(1U, FunctionDestructionChecker::functionResult.value()); |
| 221 | + FunctionDestructionChecker::functionAsBool = std::nullopt; |
| 222 | + FunctionDestructionChecker::functionResult = std::nullopt; |
| 223 | + |
| 224 | + a = FunctionDestructionChecker(a); |
| 225 | + a = nullptr; |
| 226 | + EXPECT_TRUE(static_cast<bool>(FunctionDestructionChecker::functionAsBool)); |
| 227 | + EXPECT_TRUE(static_cast<bool>(FunctionDestructionChecker::functionResult)); |
| 228 | + EXPECT_FALSE(FunctionDestructionChecker::functionAsBool.value()); |
| 229 | + EXPECT_EQ(0U, FunctionDestructionChecker::functionResult.value()); |
| 230 | + FunctionDestructionChecker::functionAsBool = std::nullopt; |
| 231 | + FunctionDestructionChecker::functionResult = std::nullopt; |
| 232 | + |
| 233 | + a = FunctionDestructionChecker(a); |
| 234 | + a = MoveOnly { 2 }; |
| 235 | + EXPECT_TRUE(static_cast<bool>(FunctionDestructionChecker::functionAsBool)); |
| 236 | + EXPECT_TRUE(static_cast<bool>(FunctionDestructionChecker::functionResult)); |
| 237 | + EXPECT_TRUE(FunctionDestructionChecker::functionAsBool.value()); |
| 238 | + EXPECT_EQ(2U, FunctionDestructionChecker::functionResult.value()); |
| 239 | + FunctionDestructionChecker::functionAsBool = std::nullopt; |
| 240 | + FunctionDestructionChecker::functionResult = std::nullopt; |
140 | 241 | }
|
| 242 | + |
| 243 | +} // namespace TestWebKitAPI |
0 commit comments