1 // Copyright 2009 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h> // Must come first
31 #endif
32
33 #include "breakpad_googletest_includes.h"
34 #include "common/memory_allocator.h"
35
36 using namespace google_breakpad;
37
38 namespace {
39 typedef testing::Test PageAllocatorTest;
40 }
41
TEST(PageAllocatorTest,Setup)42 TEST(PageAllocatorTest, Setup) {
43 PageAllocator allocator;
44 EXPECT_EQ(0U, allocator.pages_allocated());
45 }
46
TEST(PageAllocatorTest,SmallObjects)47 TEST(PageAllocatorTest, SmallObjects) {
48 PageAllocator allocator;
49
50 EXPECT_EQ(0U, allocator.pages_allocated());
51 for (unsigned i = 1; i < 1024; ++i) {
52 uint8_t* p = reinterpret_cast<uint8_t*>(allocator.Alloc(i));
53 ASSERT_FALSE(p == NULL);
54 memset(p, 0, i);
55 }
56 }
57
TEST(PageAllocatorTest,LargeObject)58 TEST(PageAllocatorTest, LargeObject) {
59 PageAllocator allocator;
60
61 EXPECT_EQ(0U, allocator.pages_allocated());
62 uint8_t* p = reinterpret_cast<uint8_t*>(allocator.Alloc(10000));
63 ASSERT_FALSE(p == NULL);
64 EXPECT_EQ(3U, allocator.pages_allocated());
65 for (unsigned i = 1; i < 10; ++i) {
66 uint8_t* p = reinterpret_cast<uint8_t*>(allocator.Alloc(i));
67 ASSERT_FALSE(p == NULL);
68 memset(p, 0, i);
69 }
70 }
71
72 namespace {
73 typedef testing::Test WastefulVectorTest;
74 }
75
TEST(WastefulVectorTest,Setup)76 TEST(WastefulVectorTest, Setup) {
77 PageAllocator allocator_;
78 wasteful_vector<int> v(&allocator_);
79 ASSERT_TRUE(v.empty());
80 ASSERT_EQ(v.size(), 0u);
81 }
82
TEST(WastefulVectorTest,Simple)83 TEST(WastefulVectorTest, Simple) {
84 PageAllocator allocator_;
85 EXPECT_EQ(0U, allocator_.pages_allocated());
86 wasteful_vector<unsigned> v(&allocator_);
87
88 for (unsigned i = 0; i < 256; ++i) {
89 v.push_back(i);
90 ASSERT_EQ(i, v.back());
91 ASSERT_EQ(&v.back(), &v[i]);
92 }
93 ASSERT_FALSE(v.empty());
94 ASSERT_EQ(v.size(), 256u);
95 EXPECT_EQ(1U, allocator_.pages_allocated());
96 for (unsigned i = 0; i < 256; ++i)
97 ASSERT_EQ(v[i], i);
98 }
99
TEST(WastefulVectorTest,UsesPageAllocator)100 TEST(WastefulVectorTest, UsesPageAllocator) {
101 PageAllocator allocator_;
102 wasteful_vector<unsigned> v(&allocator_);
103 EXPECT_EQ(1U, allocator_.pages_allocated());
104
105 v.push_back(1);
106 ASSERT_TRUE(allocator_.OwnsPointer(&v[0]));
107 }
108
TEST(WastefulVectorTest,AutoWastefulVector)109 TEST(WastefulVectorTest, AutoWastefulVector) {
110 PageAllocator allocator_;
111 EXPECT_EQ(0U, allocator_.pages_allocated());
112
113 auto_wasteful_vector<unsigned, 4> v(&allocator_);
114 EXPECT_EQ(0U, allocator_.pages_allocated());
115
116 v.push_back(1);
117 EXPECT_EQ(0U, allocator_.pages_allocated());
118 EXPECT_FALSE(allocator_.OwnsPointer(&v[0]));
119
120 v.resize(4);
121 EXPECT_EQ(0U, allocator_.pages_allocated());
122 EXPECT_FALSE(allocator_.OwnsPointer(&v[0]));
123
124 v.resize(10);
125 EXPECT_EQ(1U, allocator_.pages_allocated());
126 EXPECT_TRUE(allocator_.OwnsPointer(&v[0]));
127 }
128