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.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 }
42 
TEST(PageAllocatorTest,SmallObjects)43 TEST(PageAllocatorTest, SmallObjects) {
44   PageAllocator allocator;
45 
46   for (unsigned i = 1; i < 1024; ++i) {
47     uint8_t *p = reinterpret_cast<uint8_t*>(allocator.Alloc(i));
48     ASSERT_FALSE(p == NULL);
49     memset(p, 0, i);
50   }
51 }
52 
TEST(PageAllocatorTest,LargeObject)53 TEST(PageAllocatorTest, LargeObject) {
54   PageAllocator allocator;
55 
56   uint8_t *p = reinterpret_cast<uint8_t*>(allocator.Alloc(10000));
57   ASSERT_FALSE(p == NULL);
58   for (unsigned i = 1; i < 10; ++i) {
59     uint8_t *p = reinterpret_cast<uint8_t*>(allocator.Alloc(i));
60     ASSERT_FALSE(p == NULL);
61     memset(p, 0, i);
62   }
63 }
64 
65 namespace {
66 typedef testing::Test WastefulVectorTest;
67 }
68 
TEST(WastefulVectorTest,Setup)69 TEST(WastefulVectorTest, Setup) {
70   PageAllocator allocator_;
71   wasteful_vector<int> v(&allocator_);
72   ASSERT_TRUE(v.empty());
73   ASSERT_EQ(v.size(), 0u);
74 }
75 
TEST(WastefulVectorTest,Simple)76 TEST(WastefulVectorTest, Simple) {
77   PageAllocator allocator_;
78   wasteful_vector<unsigned> v(&allocator_);
79 
80   for (unsigned i = 0; i < 256; ++i) {
81     v.push_back(i);
82     ASSERT_EQ(i, v.back());
83     ASSERT_EQ(&v.back(), &v[i]);
84   }
85   ASSERT_FALSE(v.empty());
86   ASSERT_EQ(v.size(), 256u);
87   for (unsigned i = 0; i < 256; ++i)
88     ASSERT_EQ(v[i], i);
89 }
90 
TEST(WastefulVectorTest,UsesPageAllocator)91 TEST(WastefulVectorTest, UsesPageAllocator) {
92   PageAllocator allocator_;
93   wasteful_vector<unsigned> v(&allocator_);
94 
95   v.push_back(1);
96   ASSERT_TRUE(allocator_.OwnsPointer(&v[0]));
97 }
98