1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
5 #include "util/arena.h"
7 #include "util/random.h"
8 #include "util/testharness.h"
14 TEST(ArenaTest, Empty) {
18 TEST(ArenaTest, Simple) {
19 std::vector<std::pair<size_t, char*> > allocated;
24 for (int i = 0; i < N; i++) {
26 if (i % (N / 10) == 0) {
29 s = rnd.OneIn(4000) ? rnd.Uniform(6000) :
30 (rnd.OneIn(10) ? rnd.Uniform(100) : rnd.Uniform(20));
33 // Our arena disallows size 0 allocations.
38 r = arena.AllocateAligned(s);
40 r = arena.Allocate(s);
43 for (int b = 0; b < s; b++) {
44 // Fill the "i"th allocation with a known bit pattern
48 allocated.push_back(std::make_pair(s, r));
49 ASSERT_GE(arena.MemoryUsage(), bytes);
51 ASSERT_LE(arena.MemoryUsage(), bytes * 1.10);
54 for (int i = 0; i < allocated.size(); i++) {
55 size_t num_bytes = allocated[i].first;
56 const char* p = allocated[i].second;
57 for (int b = 0; b < num_bytes; b++) {
58 // Check the "i"th allocation for the known bit pattern
59 ASSERT_EQ(int(p[b]) & 0xff, i % 256);
64 } // namespace leveldb
66 int main(int argc, char** argv) {
67 return leveldb::test::RunAllTests();