mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
AK: Add some basic unit tests for WeakPtr.
This commit is contained in:
parent
2fedf36276
commit
4179283562
2 changed files with 62 additions and 2 deletions
|
@ -1,8 +1,8 @@
|
||||||
PROGRAMS = TestString TestQueue TestVector TestHashMap TestJSON
|
PROGRAMS = TestString TestQueue TestVector TestHashMap TestJSON TestWeakPtr
|
||||||
|
|
||||||
all: $(PROGRAMS)
|
all: $(PROGRAMS)
|
||||||
|
|
||||||
CXXFLAGS = -std=c++17 -Wall -Wextra
|
CXXFLAGS = -std=c++17 -Wall -Wextra -ggdb3 -O2
|
||||||
|
|
||||||
TestString: TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../TestSuite.h ../LogStream.cpp
|
TestString: TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../TestSuite.h ../LogStream.cpp
|
||||||
$(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../LogStream.cpp
|
$(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../LogStream.cpp
|
||||||
|
@ -19,5 +19,8 @@ TestHashMap: TestHashMap.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cp
|
||||||
TestJSON: TestJSON.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../TestSuite.h ../LogStream.cpp ../JsonObject.cpp ../JsonValue.cpp ../JsonArray.cpp ../JsonParser.cpp
|
TestJSON: TestJSON.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../TestSuite.h ../LogStream.cpp ../JsonObject.cpp ../JsonValue.cpp ../JsonArray.cpp ../JsonParser.cpp
|
||||||
$(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestJSON.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../LogStream.cpp ../JsonObject.cpp ../JsonValue.cpp ../JsonArray.cpp ../JsonParser.cpp
|
$(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestJSON.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../LogStream.cpp ../JsonObject.cpp ../JsonValue.cpp ../JsonArray.cpp ../JsonParser.cpp
|
||||||
|
|
||||||
|
TestWeakPtr: TestWeakPtr.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../TestSuite.h ../LogStream.cpp ../WeakPtr.h ../Weakable.h
|
||||||
|
$(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestWeakPtr.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../LogStream.cpp
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(PROGRAMS)
|
rm -f $(PROGRAMS)
|
||||||
|
|
57
AK/Tests/TestWeakPtr.cpp
Normal file
57
AK/Tests/TestWeakPtr.cpp
Normal file
|
@ -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)
|
Loading…
Add table
Add a link
Reference in a new issue