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