#include "StarRefPtr.hpp"
#include "StarCasting.hpp"
#include "gtest/gtest.h"
using namespace Star;
int g_test1Count = 0;
int g_test2Count = 0;
struct Base : RefCounter {};
struct Test1 : Base {
Test1() {
++g_test1Count;
}
~Test1() {
--g_test1Count;
}
};
struct Test2 : Base {
Test2() {
++g_test2Count;
}
~Test2() {
--g_test2Count;
}
};
TEST(IntrusivePtr, All) {
{
RefPtr p1 = make_ref();
RefPtr p2 = make_ref();
EXPECT_TRUE(is(p1));
EXPECT_FALSE(is(p1));
EXPECT_TRUE(is(p2));
EXPECT_FALSE(is(p2));
RefPtr p3 = p1;
RefPtr p4 = p2;
p3 = p3;
swap(p3, p4);
EXPECT_TRUE(is(p4));
EXPECT_FALSE(is(p4));
EXPECT_TRUE(is(p3));
EXPECT_FALSE(is(p3));
EXPECT_EQ(p3, p2);
EXPECT_EQ(p4, p1);
swap(p3, p4);
EXPECT_EQ(p3, p1);
EXPECT_EQ(p4, p2);
RefPtr p5;
EXPECT_FALSE(p5);
EXPECT_NE(p4, p1);
EXPECT_NE(p3, p2);
EXPECT_NE(p3, p5);
p5 = p2;
p2 = std::move(p5);
EXPECT_TRUE(is(p2));
RefPtr p6 = as(p1);
RefPtr p7 = as(p2);
RefPtr p8 = as(p1);
EXPECT_TRUE((bool)p6);
EXPECT_TRUE((bool)p7);
EXPECT_FALSE((bool)p8);
}
EXPECT_EQ(0, g_test1Count);
EXPECT_EQ(0, g_test2Count);
}