X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ftest%2FArenaTest.cpp;h=b754f970eec3183ae7716cd7166f57cdd21f4ee7;hb=a62c9841120eb374f8b933d31444a590acee6123;hp=493cd148b9cf1d6a5b2f7917ec41149c2372d558;hpb=244a89966aa1bead86e9c2fb1263bbe8d837be28;p=folly.git diff --git a/folly/test/ArenaTest.cpp b/folly/test/ArenaTest.cpp index 493cd148..b754f970 100644 --- a/folly/test/ArenaTest.cpp +++ b/folly/test/ArenaTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2013 Facebook, Inc. + * Copyright 2016 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,17 +14,19 @@ * limitations under the License. */ -#include "folly/Arena.h" -#include "folly/Memory.h" +#include +#include +#include #include #include #include -#include using namespace folly; +static_assert(IsArenaAllocator::value, ""); + TEST(Arena, SizeSanity) { std::set allocatedItems; @@ -37,7 +39,7 @@ TEST(Arena, SizeSanity) { size_t* ptr = static_cast(arena.allocate(sizeof(long))); allocatedItems.insert(ptr); minimum_size += requestedBlockSize; - maximum_size += goodMallocSize(requestedBlockSize + 1); + maximum_size += goodMallocSize(requestedBlockSize + SysArena::kBlockOverhead); EXPECT_TRUE(arena.totalSize() >= minimum_size); EXPECT_TRUE(arena.totalSize() <= maximum_size); VLOG(4) << minimum_size << " < " << arena.totalSize() << " < " @@ -57,7 +59,8 @@ TEST(Arena, SizeSanity) { allocatedItems.insert(ptr); } minimum_size += 10 * requestedBlockSize; - maximum_size += 10 * goodMallocSize(requestedBlockSize + 1); + maximum_size += 10 * goodMallocSize(requestedBlockSize + + SysArena::kBlockOverhead); EXPECT_TRUE(arena.totalSize() >= minimum_size); EXPECT_TRUE(arena.totalSize() <= maximum_size); VLOG(4) << minimum_size << " < " << arena.totalSize() << " < " @@ -67,7 +70,8 @@ TEST(Arena, SizeSanity) { ptr = static_cast(arena.allocate(10 * requestedBlockSize)); allocatedItems.insert(ptr); minimum_size += 10 * requestedBlockSize; - maximum_size += goodMallocSize(10 * requestedBlockSize + 1); + maximum_size += goodMallocSize(10 * requestedBlockSize + + SysArena::kBlockOverhead); EXPECT_TRUE(arena.totalSize() >= minimum_size); EXPECT_TRUE(arena.totalSize() <= maximum_size); VLOG(4) << minimum_size << " < " << arena.totalSize() << " < " @@ -142,9 +146,31 @@ TEST(Arena, Vector) { } } +TEST(Arena, SizeLimit) { + static const size_t requestedBlockSize = sizeof(size_t); + static const size_t maxSize = 10 * requestedBlockSize; + + SysArena arena(requestedBlockSize, maxSize); + + void* a = arena.allocate(sizeof(size_t)); + EXPECT_TRUE(a != nullptr); + EXPECT_THROW(arena.allocate(maxSize + 1), std::bad_alloc); +} + +TEST(Arena, MoveArena) { + SysArena arena(sizeof(size_t) * 2); + arena.allocate(sizeof(size_t)); + auto totalSize = arena.totalSize(); + auto bytesUsed = arena.bytesUsed(); + + SysArena moved(std::move(arena)); + EXPECT_EQ(totalSize, moved.totalSize()); + EXPECT_EQ(bytesUsed, moved.bytesUsed()); +} + int main(int argc, char *argv[]) { testing::InitGoogleTest(&argc, argv); - google::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); auto ret = RUN_ALL_TESTS(); return ret; }