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