summaryrefslogtreecommitdiff
path: root/AK/Tests
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-08-02 11:35:05 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-08-02 11:35:05 +0200
commit6db879ee663249041a6a731a8a6f31bd40ca8089 (patch)
treea388a35135da6eab58ccc949f93de411266701d7 /AK/Tests
parentd9cc3e453cb6a7b5e7a3d7b477a497c8dee8f1f4 (diff)
downloadserenity-6db879ee663249041a6a731a8a6f31bd40ca8089.zip
AK: Fix ref leak in NonnullRefPtr::operator=(T&).
We would leak a ref when assigning a T& to a NonnullRefPtr that already contains that same T.
Diffstat (limited to 'AK/Tests')
-rw-r--r--AK/Tests/Makefile5
-rw-r--r--AK/Tests/TestNonnullRefPtr.cpp36
2 files changed, 40 insertions, 1 deletions
diff --git a/AK/Tests/Makefile b/AK/Tests/Makefile
index 3847c4834e..f1a28322a9 100644
--- a/AK/Tests/Makefile
+++ b/AK/Tests/Makefile
@@ -1,4 +1,4 @@
-PROGRAMS = TestString TestQueue TestVector TestHashMap TestJSON TestWeakPtr
+PROGRAMS = TestString TestQueue TestVector TestHashMap TestJSON TestWeakPtr TestNonnullRefPtr
CXXFLAGS = -std=c++17 -Wall -Wextra -ggdb3 -O2 -I../ -I../../
@@ -42,6 +42,9 @@ TestJSON: TestJSON.o $(SHARED_TEST_OBJS)
TestWeakPtr: TestWeakPtr.o $(SHARED_TEST_OBJS)
$(PRE_CXX) $(CXX) $(CXXFLAGS) -o $@ TestWeakPtr.o $(SHARED_TEST_OBJS)
+TestNonnullRefPtr: TestNonnullRefPtr.o $(SHARED_TEST_OBJS)
+ $(PRE_CXX) $(CXX) $(CXXFLAGS) -o $@ TestNonnullRefPtr.o $(SHARED_TEST_OBJS)
+
clean:
rm -f $(SHARED_TEST_OBJS)
rm -f $(PROGRAMS)
diff --git a/AK/Tests/TestNonnullRefPtr.cpp b/AK/Tests/TestNonnullRefPtr.cpp
new file mode 100644
index 0000000000..9012206586
--- /dev/null
+++ b/AK/Tests/TestNonnullRefPtr.cpp
@@ -0,0 +1,36 @@
+#include <AK/TestSuite.h>
+
+#include <AK/NonnullRefPtr.h>
+#include <AK/AKString.h>
+
+struct Object : public RefCounted<Object> {
+ int x;
+};
+
+TEST_CASE(basics)
+{
+ auto object = adopt(*new Object);
+ EXPECT(object.ptr() != nullptr);
+ EXPECT_EQ(object->ref_count(), 1);
+ object->ref();
+ EXPECT_EQ(object->ref_count(), 2);
+ object->deref();
+ EXPECT_EQ(object->ref_count(), 1);
+
+ {
+ NonnullRefPtr another = object;
+ EXPECT_EQ(object->ref_count(), 2);
+ }
+
+ EXPECT_EQ(object->ref_count(), 1);
+}
+
+TEST_CASE(assign_reference)
+{
+ auto object = adopt(*new Object);
+ EXPECT_EQ(object->ref_count(), 1);
+ object = *object;
+ EXPECT_EQ(object->ref_count(), 1);
+}
+
+TEST_MAIN(String)