/* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "gtest/gtest.h" #include "chre/util/system/shared_ptr.h" namespace { class TestBase : public chre::RefBase { public: TestBase() {} TestBase(int value) : value(value) {} ~TestBase() { destructorCount++; } int value = 0; static int destructorCount; }; int TestBase::destructorCount = 0; struct BigArray : public chre::RefBase { int x[2048]; }; class SharedPtrTest : public testing::Test { public: void SetUp() override { TestBase::destructorCount = 0; } }; } // namespace TEST_F(SharedPtrTest, IsNull) { chre::SharedPtr ptr; EXPECT_TRUE(ptr.isNull()); } TEST_F(SharedPtrTest, IsNotNull) { chre::SharedPtr ptr = chre::MakeShared(); EXPECT_FALSE(ptr.isNull()); } TEST_F(SharedPtrTest, MoveConstructor) { chre::SharedPtr ptr = chre::MakeShared(); chre::SharedPtr movedPtr(std::move(ptr)); EXPECT_TRUE(ptr.isNull()); EXPECT_FALSE(movedPtr.isNull()); } TEST_F(SharedPtrTest, CopyConstructor) { chre::SharedPtr ptr = chre::MakeShared(); chre::SharedPtr copiedPtr(ptr); EXPECT_FALSE(ptr.isNull()); EXPECT_FALSE(copiedPtr.isNull()); } TEST_F(SharedPtrTest, MoveAssignment) { chre::SharedPtr ptr = chre::MakeShared(); chre::SharedPtr movedPtr = std::move(ptr); EXPECT_TRUE(ptr.isNull()); EXPECT_FALSE(movedPtr.isNull()); } TEST_F(SharedPtrTest, CopiedAssignment) { chre::SharedPtr ptr = chre::MakeShared(); chre::SharedPtr copiedPtr = ptr; EXPECT_FALSE(ptr.isNull()); EXPECT_FALSE(copiedPtr.isNull()); } TEST_F(SharedPtrTest, Get) { int specialVal = 0xdeadbeef; chre::SharedPtr ptr = chre::MakeShared(specialVal); EXPECT_EQ(specialVal, ptr.get()->value); } TEST_F(SharedPtrTest, Reset) { chre::SharedPtr ptr = chre::MakeShared(); chre::SharedPtr ptr2 = chre::MakeShared(); EXPECT_NE(ptr, ptr2); ptr2.reset(ptr.get()); EXPECT_EQ(ptr, ptr2); ptr.reset(); EXPECT_TRUE(ptr.isNull()); } TEST_F(SharedPtrTest, MemoryReleased) { chre::SharedPtr ptr = chre::MakeShared(); chre::SharedPtr copiedPtr = ptr; ptr.~SharedPtr(); EXPECT_EQ(0, TestBase::destructorCount); copiedPtr.~SharedPtr(); EXPECT_EQ(1, TestBase::destructorCount); } TEST_F(SharedPtrTest, MakeSharedZeroFill) { BigArray baseline = {}; auto myArray = chre::MakeSharedZeroFill(); ASSERT_FALSE(myArray.isNull()); // Note that this doesn't actually test things properly, because we don't // guarantee that malloc is not already giving us zeroed out memory. To // properly do it, we could inject the allocator, but this function is simple // enough that it's not really worth the effort. EXPECT_EQ(std::memcmp(baseline.x, myArray.get()->x, sizeof(baseline.x)), 0); }