2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
21 TEST(PointersTest, smoke) {
24 pointers.Add(0x1234, reinterpret_cast<void*>(0xabcd));
25 void* memory_pointer = pointers.Remove(0x1234);
26 ASSERT_EQ(reinterpret_cast<void*>(0xabcd), memory_pointer);
29 TEST(PointersTest, readd_pointer) {
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);
41 TEST(PointersTest, expect_collision) {
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);
54 TEST(PointersTest, multiple_add_removes) {
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));
62 void* memory_pointer = pointers.Remove(0x1236);
63 ASSERT_EQ(reinterpret_cast<void*>(0xabc1), memory_pointer);
65 pointers.Add(0x2349, reinterpret_cast<void*>(0x2abcd));
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);
72 pointers.Add(0x3500, reinterpret_cast<void*>(0x3abcd));
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);
80 static void TestNoEntriesLeft() {
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));
90 TEST(PointersTest_DeathTest, no_entries_left) {
91 ASSERT_EXIT(TestNoEntriesLeft(), ::testing::ExitedWithCode(1), "");
94 static void TestFindNoPointer() {
97 pointers.Remove(0x1234);
100 TEST(PointersTest_DeathTest, find_no_pointer) {
101 ASSERT_EXIT(TestFindNoPointer(), ::testing::ExitedWithCode(1), "");
104 static void TestRemoveZeroValue() {
105 Pointers pointers(1);
107 void* memory = pointers.Remove(0);
111 TEST(PointersTest_DeathTest, remove_zero_value) {
112 ASSERT_EXIT(TestRemoveZeroValue(), ::testing::ExitedWithCode(1), "");