1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <gtest/gtest.h> 18 19 #include "Pointers.h" 20 21 TEST(PointersTest, smoke) { 22 Pointers pointers(1); 23 24 pointers.Add(0x1234, reinterpret_cast<void*>(0xabcd)); 25 void* memory_pointer = pointers.Remove(0x1234); 26 ASSERT_EQ(reinterpret_cast<void*>(0xabcd), memory_pointer); 27 } 28 29 TEST(PointersTest, readd_pointer) { 30 Pointers pointers(1); 31 32 pointers.Add(0x1234, reinterpret_cast<void*>(0xabcd)); 33 void* memory_pointer = pointers.Remove(0x1234); 34 ASSERT_EQ(reinterpret_cast<void*>(0xabcd), memory_pointer); 35 pointers.Add(0x1234, reinterpret_cast<void*>(0x5555)); 36 memory_pointer = pointers.Remove(0x1234); 37 ASSERT_EQ(reinterpret_cast<void*>(0x5555), memory_pointer); 38 } 39 40 41 TEST(PointersTest, expect_collision) { 42 Pointers pointers(2); 43 44 // This assumes the simple hash being used will result in a collision 45 // hitting the same entry. 46 pointers.Add(0x1234, reinterpret_cast<void*>(0xabcd)); 47 pointers.Add(0x11234, reinterpret_cast<void*>(0xabcf)); 48 void* memory_pointer = pointers.Remove(0x11234); 49 ASSERT_EQ(reinterpret_cast<void*>(0xabcf), memory_pointer); 50 memory_pointer = pointers.Remove(0x1234); 51 ASSERT_EQ(reinterpret_cast<void*>(0xabcd), memory_pointer); 52 } 53 54 TEST(PointersTest, multiple_add_removes) { 55 Pointers pointers(4); 56 57 pointers.Add(0x1234, reinterpret_cast<void*>(0xabcd)); 58 pointers.Add(0x1235, reinterpret_cast<void*>(0xabcf)); 59 pointers.Add(0x1236, reinterpret_cast<void*>(0xabc1)); 60 pointers.Add(0x1237, reinterpret_cast<void*>(0xabc2)); 61 62 void* memory_pointer = pointers.Remove(0x1236); 63 ASSERT_EQ(reinterpret_cast<void*>(0xabc1), memory_pointer); 64 65 pointers.Add(0x2349, reinterpret_cast<void*>(0x2abcd)); 66 67 memory_pointer = pointers.Remove(0x1234); 68 ASSERT_EQ(reinterpret_cast<void*>(0xabcd), memory_pointer); 69 memory_pointer = pointers.Remove(0x1237); 70 ASSERT_EQ(reinterpret_cast<void*>(0xabc2), memory_pointer); 71 72 pointers.Add(0x3500, reinterpret_cast<void*>(0x3abcd)); 73 74 memory_pointer = pointers.Remove(0x3500); 75 ASSERT_EQ(reinterpret_cast<void*>(0x3abcd), memory_pointer); 76 memory_pointer = pointers.Remove(0x2349); 77 ASSERT_EQ(reinterpret_cast<void*>(0x2abcd), memory_pointer); 78 } 79 80 static void TestNoEntriesLeft() { 81 Pointers pointers(1); 82 83 // Even though we've requested only one pointer, we get more due 84 // to the way the data is allocated. 85 for (size_t i = 0; i <= pointers.max_pointers(); i++) { 86 pointers.Add(0x1234 + i, reinterpret_cast<void*>(0xabcd + i)); 87 } 88 } 89 90 TEST(PointersTest_DeathTest, no_entries_left) { 91 ASSERT_EXIT(TestNoEntriesLeft(), ::testing::ExitedWithCode(1), ""); 92 } 93 94 static void TestFindNoPointer() { 95 Pointers pointers(1); 96 97 pointers.Remove(0x1234); 98 } 99 100 TEST(PointersTest_DeathTest, find_no_pointer) { 101 ASSERT_EXIT(TestFindNoPointer(), ::testing::ExitedWithCode(1), ""); 102 } 103 104 static void TestRemoveZeroValue() { 105 Pointers pointers(1); 106 107 void* memory = pointers.Remove(0); 108 if (memory) {} 109 } 110 111 TEST(PointersTest_DeathTest, remove_zero_value) { 112 ASSERT_EXIT(TestRemoveZeroValue(), ::testing::ExitedWithCode(1), ""); 113 } 114