1b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// Copyright (c) 2009, Google Inc.
2bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// All rights reserved.
3bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//
4bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// Redistribution and use in source and binary forms, with or without
5bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// modification, are permitted provided that the following conditions are
6bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// met:
7bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//
8bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//     * Redistributions of source code must retain the above copyright
9bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// notice, this list of conditions and the following disclaimer.
10bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//     * Redistributions in binary form must reproduce the above
11bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// copyright notice, this list of conditions and the following disclaimer
12bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// in the documentation and/or other materials provided with the
13bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// distribution.
14bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//     * Neither the name of Google Inc. nor the names of its
15bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// contributors may be used to endorse or promote products derived from
16bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// this software without specific prior written permission.
17bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//
18bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
30b732342313268422c0c0794dfb1a82cd17ce9e05ivan.penkov@gmail.com#include "breakpad_googletest_includes.h"
314621ee06914b2ebe963c93ea78fabf982cf670dfted.mielczarek#include "common/memory.h"
32bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
33b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidusing namespace google_breakpad;
34bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
35b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidnamespace {
36b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidtypedef testing::Test PageAllocatorTest;
37b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid}
38bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
39b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidTEST(PageAllocatorTest, Setup) {
40b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  PageAllocator allocator;
41b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid}
42bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
43b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidTEST(PageAllocatorTest, SmallObjects) {
44b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  PageAllocator allocator;
45bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
46b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  for (unsigned i = 1; i < 1024; ++i) {
47b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid    uint8_t *p = reinterpret_cast<uint8_t*>(allocator.Alloc(i));
48b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid    ASSERT_FALSE(p == NULL);
49b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid    memset(p, 0, i);
50b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  }
51bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly}
52bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
53b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidTEST(PageAllocatorTest, LargeObject) {
54b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  PageAllocator allocator;
55b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
56b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  uint8_t *p = reinterpret_cast<uint8_t*>(allocator.Alloc(10000));
57b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  ASSERT_FALSE(p == NULL);
58b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  for (unsigned i = 1; i < 10; ++i) {
59b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid    uint8_t *p = reinterpret_cast<uint8_t*>(allocator.Alloc(i));
60b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid    ASSERT_FALSE(p == NULL);
61b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid    memset(p, 0, i);
62b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  }
63bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly}
64bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
65b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidnamespace {
66b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidtypedef testing::Test WastefulVectorTest;
67bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly}
68bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
69b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidTEST(WastefulVectorTest, Setup) {
70b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  PageAllocator allocator_;
71b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  wasteful_vector<int> v(&allocator_);
72e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  ASSERT_TRUE(v.empty());
73b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  ASSERT_EQ(v.size(), 0u);
74bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly}
75bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
76b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidTEST(WastefulVectorTest, Simple) {
77b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  PageAllocator allocator_;
784621ee06914b2ebe963c93ea78fabf982cf670dfted.mielczarek  wasteful_vector<unsigned> v(&allocator_);
79b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
80e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  for (unsigned i = 0; i < 256; ++i) {
81b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid    v.push_back(i);
82e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org    ASSERT_EQ(i, v.back());
83e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org    ASSERT_EQ(&v.back(), &v[i]);
84e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  }
85e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  ASSERT_FALSE(v.empty());
86b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  ASSERT_EQ(v.size(), 256u);
87b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  for (unsigned i = 0; i < 256; ++i)
88b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid    ASSERT_EQ(v[i], i);
89bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly}
90269ee11017cfe0543989d30bb83670b6dd1f1e29ivan.penkov@gmail.com
91269ee11017cfe0543989d30bb83670b6dd1f1e29ivan.penkov@gmail.comTEST(WastefulVectorTest, UsesPageAllocator) {
92269ee11017cfe0543989d30bb83670b6dd1f1e29ivan.penkov@gmail.com  PageAllocator allocator_;
93269ee11017cfe0543989d30bb83670b6dd1f1e29ivan.penkov@gmail.com  wasteful_vector<unsigned> v(&allocator_);
94269ee11017cfe0543989d30bb83670b6dd1f1e29ivan.penkov@gmail.com
95269ee11017cfe0543989d30bb83670b6dd1f1e29ivan.penkov@gmail.com  v.push_back(1);
96269ee11017cfe0543989d30bb83670b6dd1f1e29ivan.penkov@gmail.com  ASSERT_TRUE(allocator_.OwnsPointer(&v[0]));
97269ee11017cfe0543989d30bb83670b6dd1f1e29ivan.penkov@gmail.com}
98