blob: fa76ae034183ce6326cb4fe89c0d876a5c2bc3ea [file] [log] [blame]
Colin Crossbcb4ed32016-01-14 15:35:40 -08001/*
2 * Copyright (C) 2016 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 <Allocator.h>
Colin Crossbcb4ed32016-01-14 15:35:40 -080018
19#include <gtest/gtest.h>
20#include <ScopedDisableMalloc.h>
21
22
23std::function<void()> ScopedAlarm::func_;
24
Colin Crossbcb4ed32016-01-14 15:35:40 -080025class AllocatorTest : public testing::Test {
26 protected:
27 AllocatorTest() : heap(), disable_malloc_() {}
28 virtual void SetUp() {
29 heap_count = 0;
30 }
31 virtual void TearDown() {
32 ASSERT_EQ(heap_count, 0);
33 ASSERT_TRUE(heap.empty());
34 ASSERT_FALSE(disable_malloc_.timed_out());
35 }
36 Heap heap;
37 private:
38 ScopedDisableMallocTimeout disable_malloc_;
39};
40
41TEST_F(AllocatorTest, simple) {
42 Allocator<char[100]> allocator(heap);
43 void *ptr = allocator.allocate();
44 ASSERT_TRUE(ptr != NULL);
45 allocator.deallocate(ptr);
46}
47
48TEST_F(AllocatorTest, multiple) {
49 Allocator<char[100]> allocator(heap);
50 void *ptr1 = allocator.allocate();
51 ASSERT_TRUE(ptr1 != NULL);
52 void *ptr2 = allocator.allocate();
53 ASSERT_TRUE(ptr2 != NULL);
54 ASSERT_NE(ptr1, ptr2);
55 allocator.deallocate(ptr1);
56 void *ptr3 = allocator.allocate();
57 ASSERT_EQ(ptr1, ptr3);
58 allocator.deallocate(ptr3);
59 allocator.deallocate(ptr2);
60}
61
62TEST_F(AllocatorTest, many) {
63 const int num = 4096;
64 const int size = 128;
65 Allocator<char[size]> allocator(heap);
66 void *ptr[num];
67 for (int i = 0; i < num; i++) {
68 ptr[i] = allocator.allocate();
69 memset(ptr[i], 0xaa, size);
70 *(reinterpret_cast<unsigned char*>(ptr[i])) = i;
71 }
72
73 for (int i = 0; i < num; i++) {
74 for (int j = 0; j < num; j++) {
75 if (i != j) {
76 ASSERT_NE(ptr[i], ptr[j]);
77 }
78 }
79 }
80
81 for (int i = 0; i < num; i++) {
82 ASSERT_EQ(*(reinterpret_cast<unsigned char*>(ptr[i])), i & 0xFF);
83 allocator.deallocate(ptr[i]);
84 }
85}
86
87TEST_F(AllocatorTest, large) {
88 const size_t size = 1024 * 1024;
89 Allocator<char[size]> allocator(heap);
90 void *ptr = allocator.allocate();
91 memset(ptr, 0xaa, size);
92 allocator.deallocate(ptr);
93}
94
95TEST_F(AllocatorTest, many_large) {
96 const int num = 128;
97 const int size = 1024 * 1024;
98 Allocator<char[size]> allocator(heap);
99 void *ptr[num];
100 for (int i = 0; i < num; i++) {
101 ptr[i] = allocator.allocate();
102 memset(ptr[i], 0xaa, size);
103 *(reinterpret_cast<unsigned char*>(ptr[i])) = i;
104 }
105
106 for (int i = 0; i < num; i++) {
107 ASSERT_EQ(*(reinterpret_cast<unsigned char*>(ptr[i])), i & 0xFF);
108 allocator.deallocate(ptr[i]);
109 }
110}
111
112TEST_F(AllocatorTest, copy) {
113 Allocator<char[100]> a(heap);
114 Allocator<char[200]> b = a;
115 Allocator<char[300]> c(b);
116 Allocator<char[100]> d(a);
117 Allocator<char[100]> e(heap);
118
119 ASSERT_EQ(a, b);
120 ASSERT_EQ(a, c);
121 ASSERT_EQ(a, d);
122 ASSERT_EQ(a, e);
123
124 void* ptr1 = a.allocate();
125 void* ptr2 = b.allocate();
126 void* ptr3 = c.allocate();
127 void* ptr4 = d.allocate();
128
129 b.deallocate(ptr1);
130 d.deallocate(ptr2);
131 a.deallocate(ptr3);
132 c.deallocate(ptr4);
133}
134
135TEST_F(AllocatorTest, stl_vector) {
136 auto v = allocator::vector<int>(Allocator<int>(heap));
137 for (int i = 0; i < 1024; i++) {
138 v.push_back(i);
139 }
140 for (int i = 0; i < 1024; i++) {
141 ASSERT_EQ(v[i], i);
142 }
143 v.clear();
144}
145
146TEST_F(AllocatorTest, stl_list) {
147 auto v = allocator::list<int>(Allocator<int>(heap));
148 for (int i = 0; i < 1024; i++) {
149 v.push_back(i);
150 }
151 int i = 0;
152 for (auto iter = v.begin(); iter != v.end(); iter++, i++) {
153 ASSERT_EQ(*iter, i);
154 }
155 v.clear();
156}
157
158TEST_F(AllocatorTest, shared) {
159 Allocator<int> allocator(heap);
160
161 Allocator<int>::shared_ptr ptr = allocator.make_shared(0);
162 {
163 auto ptr2 = ptr;
164 }
165 ASSERT_NE(ptr, nullptr);
166}
167
168TEST_F(AllocatorTest, unique) {
169 Allocator<int> allocator(heap);
170
171 Allocator<int>::unique_ptr ptr = allocator.make_unique(0);
172
173 ASSERT_NE(ptr, nullptr);
174}