summaryrefslogtreecommitdiff
path: root/AK/Tests/TestWeakPtr.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-07-21 11:34:31 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-07-21 11:34:31 +0200
commit41792835627e9cb7c97d92e2d829dfc4bbaea9d8 (patch)
treeef514d7532208184fdb8cc755cae13ab7cfab287 /AK/Tests/TestWeakPtr.cpp
parent2fedf3627649e115ade2d7c803de7a9c7e2fecec (diff)
downloadserenity-41792835627e9cb7c97d92e2d829dfc4bbaea9d8.zip
AK: Add some basic unit tests for WeakPtr.
Diffstat (limited to 'AK/Tests/TestWeakPtr.cpp')
-rw-r--r--AK/Tests/TestWeakPtr.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/AK/Tests/TestWeakPtr.cpp b/AK/Tests/TestWeakPtr.cpp
new file mode 100644
index 0000000000..f3145544ef
--- /dev/null
+++ b/AK/Tests/TestWeakPtr.cpp
@@ -0,0 +1,57 @@
+#include <AK/TestSuite.h>
+#include <AK/AKString.h>
+#include <AK/Weakable.h>
+#include <AK/WeakPtr.h>
+
+namespace AK{
+int g_weaklinks = 0;
+}
+
+class SimpleWeakable : public Weakable<SimpleWeakable> {
+public:
+ SimpleWeakable() {}
+
+private:
+ int m_member { 123 };
+};
+
+TEST_CASE(basic_weak)
+{
+ WeakPtr<SimpleWeakable> weak1;
+ WeakPtr<SimpleWeakable> weak2;
+
+ {
+ SimpleWeakable simple;
+ weak1 = simple.make_weak_ptr();
+ weak2 = simple.make_weak_ptr();
+ EXPECT_EQ(weak1.is_null(), false);
+ EXPECT_EQ(weak2.is_null(), false);
+ EXPECT_EQ(weak1.ptr(), &simple);
+ EXPECT_EQ(weak1.ptr(), weak2.ptr());
+ }
+
+ EXPECT_EQ(weak1.is_null(), true);
+ EXPECT_EQ(weak1.ptr(), nullptr);
+ EXPECT_EQ(weak1.ptr(), weak2.ptr());
+}
+
+TEST_CASE(weakptr_move)
+{
+ WeakPtr<SimpleWeakable> weak1;
+ WeakPtr<SimpleWeakable> weak2;
+
+ {
+ SimpleWeakable simple;
+ weak1 = simple.make_weak_ptr();
+ weak2 = move(weak1);
+ EXPECT_EQ(weak1.is_null(), true);
+ EXPECT_EQ(weak2.is_null(), false);
+ EXPECT_EQ(weak2.ptr(), &simple);
+ }
+
+ EXPECT_EQ(weak2.is_null(), false);
+
+ fprintf(stderr, "ok\n");
+}
+
+TEST_MAIN(WeakPtr)