// Copyright (C) 2017 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 "StalePtrRegistry.h" #include namespace gfxstream { namespace { TEST(StalePtrRegistry, Constructor) { StalePtrRegistry reg; } TEST(StalePtrRegistry, Add) { StalePtrRegistry reg; EXPECT_EQ(reg.numCurrEntries(), 0); reg.addPtr(nullptr); EXPECT_EQ(reg.numCurrEntries(), 1); } TEST(StalePtrRegistry, AddRemove) { StalePtrRegistry reg; EXPECT_EQ(reg.numCurrEntries(), 0); reg.addPtr(nullptr); EXPECT_EQ(reg.numCurrEntries(), 1); reg.removePtr(nullptr); EXPECT_EQ(reg.numCurrEntries(), 0); } TEST(StalePtrRegistry, AddMakeStale) { StalePtrRegistry reg; EXPECT_EQ(reg.numCurrEntries(), 0); reg.addPtr(nullptr); EXPECT_EQ(reg.numCurrEntries(), 1); EXPECT_EQ(reg.numStaleEntries(), 0); reg.makeCurrentPtrsStale(); EXPECT_EQ(reg.numCurrEntries(), 0); EXPECT_EQ(reg.numStaleEntries(), 1); } TEST(StalePtrRegistry, Get) { StalePtrRegistry reg; EXPECT_EQ(reg.numCurrEntries(), 0); reg.addPtr(nullptr); EXPECT_EQ(reg.numCurrEntries(), 1); EXPECT_EQ(reg.getPtr(0), nullptr); } TEST(StalePtrRegistry, GetDefaultVal) { StalePtrRegistry reg; EXPECT_EQ(reg.numCurrEntries(), 0); EXPECT_EQ(reg.getPtr(0), nullptr); EXPECT_EQ(reg.getPtr(0, (void*)0x1), (void*)0x1); } TEST(StalePtrRegistry, GetNonExisting) { StalePtrRegistry reg; EXPECT_EQ(reg.numCurrEntries(), 0); EXPECT_EQ(reg.getPtr(0), nullptr); EXPECT_EQ(reg.numCurrEntries(), 0); } TEST(StalePtrRegistry, AddMakeStaleGetStale) { StalePtrRegistry reg; void* ptr = (void*)0xabcdef; uint64_t handle = (uint64_t)(uintptr_t)ptr; EXPECT_EQ(reg.numCurrEntries(), 0); reg.addPtr(ptr); EXPECT_EQ(reg.numCurrEntries(), 1); EXPECT_EQ(reg.numStaleEntries(), 0); reg.makeCurrentPtrsStale(); EXPECT_EQ(reg.numCurrEntries(), 0); EXPECT_EQ(reg.numStaleEntries(), 1); EXPECT_EQ(reg.getPtr(handle), ptr); } TEST(StalePtrRegistry, AddMakeStaleGetStaleWithDelete) { StalePtrRegistry reg; void* ptr = (void*)0xabcdef; uint64_t handle = (uint64_t)(uintptr_t)ptr; EXPECT_EQ(reg.numCurrEntries(), 0); reg.addPtr(ptr); EXPECT_EQ(reg.numCurrEntries(), 1); EXPECT_EQ(reg.numStaleEntries(), 0); reg.makeCurrentPtrsStale(); EXPECT_EQ(reg.numCurrEntries(), 0); EXPECT_EQ(reg.numStaleEntries(), 1); EXPECT_EQ(reg.getPtr(handle, nullptr, true), ptr); EXPECT_EQ(reg.getPtr(handle, nullptr, true), nullptr); } TEST(StalePtrRegistry, AddMakeStaleWithRemap) { StalePtrRegistry reg; void* ptr = (void*)0xabcdef; void* remapped = (void*)0xbcdefa; uint64_t handle = (uint64_t)(uintptr_t)ptr; EXPECT_EQ(reg.numCurrEntries(), 0); reg.addPtr(ptr); EXPECT_EQ(reg.numCurrEntries(), 1); EXPECT_EQ(reg.numStaleEntries(), 0); reg.makeCurrentPtrsStale(); EXPECT_EQ(reg.numCurrEntries(), 0); EXPECT_EQ(reg.numStaleEntries(), 1); reg.remapStalePtr(handle, remapped); EXPECT_EQ(reg.getPtr(handle), remapped); } TEST(StalePtrRegistry, AddMakeStaleWithRemapNameCollision) { StalePtrRegistry reg; void* ptr = (void*)0xabcdef; void* remapped = (void*)0xbcdefa; uint64_t handle = (uint64_t)(uintptr_t)ptr; EXPECT_EQ(reg.numCurrEntries(), 0); reg.addPtr(ptr); EXPECT_EQ(reg.numCurrEntries(), 1); EXPECT_EQ(reg.numStaleEntries(), 0); reg.makeCurrentPtrsStale(); EXPECT_EQ(reg.numCurrEntries(), 0); EXPECT_EQ(reg.numStaleEntries(), 1); reg.remapStalePtr(handle, remapped); EXPECT_EQ(reg.getPtr(handle), remapped); reg.addPtr(ptr); EXPECT_EQ(reg.getPtr(handle), ptr); } TEST(StalePtrRegistry, AddMakeStaleTwice) { StalePtrRegistry reg; void* ptr1 = (void*)0xabcdef; uint64_t handle1 = (uint64_t)(uintptr_t)ptr1; void* ptr2 = (void*)0xbcdefa; uint64_t handle2 = (uint64_t)(uintptr_t)ptr2; reg.addPtr(ptr1); reg.makeCurrentPtrsStale(); EXPECT_EQ(reg.numStaleEntries(), 1); reg.addPtr(ptr2); reg.makeCurrentPtrsStale(); EXPECT_EQ(reg.numCurrEntries(), 0); EXPECT_EQ(reg.numStaleEntries(), 2); EXPECT_EQ(reg.getPtr(handle1), ptr1); EXPECT_EQ(reg.getPtr(handle2), ptr2); } TEST(StalePtrRegistry, AddMakeStaleTwiceWithCollision) { StalePtrRegistry reg; void* ptr1 = (void*)0xabcdef; uint64_t handle1 = (uint64_t)(uintptr_t)ptr1; void* ptr2 = (void*)0xabcdef; uint64_t handle2 = (uint64_t)(uintptr_t)ptr2; reg.addPtr(ptr1); reg.makeCurrentPtrsStale(); EXPECT_EQ(reg.numStaleEntries(), 1); reg.addPtr(ptr2); reg.makeCurrentPtrsStale(); EXPECT_EQ(reg.numCurrEntries(), 0); EXPECT_EQ(reg.numStaleEntries(), 1); EXPECT_EQ(reg.getPtr(handle1), ptr1); EXPECT_EQ(reg.getPtr(handle2), ptr2); } } // namespace } // namespace gfxstream